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