01 /*
02 * Copyright 2014-2016 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package griffon.plugins.glazedlists.javafx.models;
17
18 import griffon.plugins.glazedlists.javafx.gui.FXTableFormat;
19 import griffon.plugins.glazedlists.javafx.gui.FXWritableTableFormat;
20 import javafx.beans.value.ObservableValue;
21 import javafx.collections.FXCollections;
22 import javafx.collections.ObservableList;
23 import javafx.scene.control.TableColumn;
24 import javafx.scene.control.TableView;
25
26 import javax.annotation.Nonnull;
27 import java.util.ArrayList;
28 import java.util.Collection;
29
30 import static java.util.Objects.requireNonNull;
31
32 /**
33 * @author Andres Almiray
34 */
35 public class DefaultFXTableViewModel<E> implements FXTableViewModel<E> {
36 private static final String ERROR_TABLE_VIEW_NULL = "Argument 'tableView' must not be null";
37 private final ObservableList<E> source;
38 private final FXTableFormat<? super E> format;
39 private final Collection<TableColumn<E, ?>> columns = new ArrayList<>();
40
41 public DefaultFXTableViewModel(@Nonnull ObservableList<E> source, @Nonnull FXTableFormat<? super E> format) {
42 this.source = requireNonNull(source, "Argument 'source' must not be null");
43 this.format = requireNonNull(format, "Argument 'format' must not be null");
44 computeColumns();
45 }
46
47 private void computeColumns() {
48 for (int i = 0; i < format.getColumnCount(); i++) {
49 String columnName = format.getColumnName(i);
50 TableColumn<E, ?> column = new TableColumn<>(columnName);
51
52 processTableFormat(column, columnName, i);
53 if (format instanceof FXWritableTableFormat) {
54 processWritableTableFormat(column, columnName, i);
55 }
56
57 columns.add(column);
58 }
59 }
60
61 @SuppressWarnings({"unchecked", "ConstantConditions"})
62 protected <T> void processWritableTableFormat(@Nonnull TableColumn<E, T> column, String columnName, int columnIndex) {
63 // empty
64 }
65
66 @SuppressWarnings("unchecked")
67 protected <T> void processTableFormat(@Nonnull TableColumn<E, T> column, String columnName, int columnIndex) {
68 column.setCellValueFactory(cell -> (ObservableValue<T>) format.getColumnObservableValue(cell.getValue(), columnIndex));
69 column.setCellFactory(param -> format.getTableCellFactory(columnIndex).createTableCell(param));
70 }
71
72 @Nonnull
73 public ObservableList<E> getSource() {
74 return source;
75 }
76
77 @Nonnull
78 public FXTableFormat<? super E> getFormat() {
79 return format;
80 }
81
82 @Override
83 public void attachTo(@Nonnull TableView<E> tableView) {
84 requireNonNull(tableView, ERROR_TABLE_VIEW_NULL);
85 tableView.setItems(source);
86 tableView.getColumns().addAll(columns);
87 }
88
89 @Override
90 public void detachFrom(@Nonnull TableView<E> tableView) {
91 requireNonNull(tableView, ERROR_TABLE_VIEW_NULL);
92 tableView.setItems(FXCollections.<E>emptyObservableList());
93 tableView.getColumns().removeAll(columns);
94 }
95 }
|