DefaultTableViewModel.java
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2008-2017 the original author or authors.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package griffon.javafx.scene.control;
019 
020 import javafx.collections.FXCollections;
021 import javafx.collections.ObservableList;
022 import javafx.scene.control.TableColumn;
023 import javafx.scene.control.TableView;
024 
025 import javax.annotation.Nonnull;
026 import java.util.ArrayList;
027 import java.util.List;
028 
029 import static griffon.util.GriffonClassUtils.requireState;
030 import static java.util.Objects.requireNonNull;
031 
032 /**
033  @author Andres Almiray
034  @since 2.11.0
035  */
036 public class DefaultTableViewModel<E> implements TableViewModel<E> {
037     private static final String ERROR_TABLE_VIEW_NULL = "Argument 'tableView' must not be null";
038 
039     private final ObservableList<E> source;
040     private final TableViewFormat<E> format;
041     private final List<TableColumn<E, Object>> columns = new ArrayList<>();
042 
043     public DefaultTableViewModel(@Nonnull ObservableList<E> source, @Nonnull TableViewFormat<E> format) {
044         this.source = requireNonNull(source, "Argument 'source' must not be null");
045         this.format = requireNonNull(format, "Argument 'format' must not be null");
046         computeColumns();
047     }
048 
049     @SuppressWarnings("unchecked")
050     private void computeColumns() {
051         for (int i = 0; i < format.getColumnCount(); i++) {
052             final String columnName = format.getColumnName(i);
053             TableColumn column = new TableColumn(columnName);
054 
055             final int columnIndex = i;
056             final TableCellFactory tableCellFactory = format.getTableCellFactory(i);
057             if (tableCellFactory != null) {
058                 column.setCellFactory(cell -> tableCellFactory.createTableCell((TableColumncell));
059             }
060             column.setCellValueFactory(cell -> format.getObservableValue((E) ((TableColumn.CellDataFeaturescell).getValue(), columnIndex));
061 
062             columns.add(column);
063         }
064     }
065 
066     @Nonnull
067     public ObservableList<E> getSource() {
068         return source;
069     }
070 
071     @Nonnull
072     public TableViewFormat<E> getFormat() {
073         return format;
074     }
075 
076     @Nonnull
077     @Override
078     public TableColumn<E, ?> getColumnAt(int index) {
079         requireState(index >= 0"Argument 'index' must be greater or equal to zero");
080         requireState(index < columns.size()"Argument 'index' must be less than " + columns.size());
081         return columns.get(index);
082     }
083 
084     @Override
085     public void attachTo(@Nonnull TableView<E> tableView) {
086         requireNonNull(tableView, ERROR_TABLE_VIEW_NULL);
087         tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
088         tableView.setItems(source);
089         tableView.getColumns().addAll(columns);
090 
091         int proportionalSize = 100 / columns.size();
092         for (int i = 0; i < columns.size(); i++) {
093             Double size = format.getColumnSize(i);
094             int columnSize = size != null (int) (100 * size: proportionalSize;
095             TableColumn column = columns.get(i);
096             column.setMaxWidth(1f * Integer.MAX_VALUE * columnSize);
097         }
098     }
099 
100     @Override
101     public void detachFrom(@Nonnull TableView<E> tableView) {
102         requireNonNull(tableView, ERROR_TABLE_VIEW_NULL);
103         tableView.setItems(FXCollections.<E>emptyObservableList());
104         tableView.getColumns().removeAll(columns);
105     }
106 }