DefaultTableViewModel.java
001 /*
002  * Copyright 2014-2016 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.plugins.glazedlists.javafx.models;
017 
018 import ca.odell.glazedlists.gui.TableFormat;
019 import javafx.beans.property.ReadOnlyObjectPropertyBase;
020 import javafx.beans.value.ObservableValue;
021 import javafx.collections.FXCollections;
022 import javafx.collections.ObservableList;
023 import javafx.scene.control.TableColumn;
024 import javafx.scene.control.TableView;
025 import javafx.util.Callback;
026 
027 import javax.annotation.Nonnull;
028 import java.util.ArrayList;
029 import java.util.Collection;
030 
031 import static java.util.Objects.requireNonNull;
032 
033 /**
034  @author Andres Almiray
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     private final ObservableList<E> source;
039     private final TableFormat<E> format;
040     private final Collection<TableColumn<E, Object>> columns = new ArrayList<>();
041 
042     public DefaultTableViewModel(@Nonnull ObservableList<E> source, @Nonnull TableFormat<E> format) {
043         this.source = requireNonNull(source, "Argument 'source' must not be null");
044         this.format = requireNonNull(format, "Argument 'format' must not be null");
045         computeColumns();
046     }
047 
048     private void computeColumns() {
049         for (int i = 0; i < format.getColumnCount(); i++) {
050             final String columnName = format.getColumnName(i);
051             TableColumn<E, Object> column = new TableColumn<>(columnName);
052 
053             final int columnIndex = i;
054             column.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<E, Object>, ObservableValue<Object>>() {
055                 @Override
056                 @SuppressWarnings("unchecked")
057                 public ObservableValue<Object> call(final TableColumn.CellDataFeatures<E, Object> cell) {
058                     return new ReadOnlyObjectPropertyBase<Object>() {
059                         @Override
060                         public Object get() {
061                             return format.getColumnValue(cell.getValue(), columnIndex);
062                         }
063 
064                         @Override
065                         public Object getBean() {
066                             return cell.getValue();
067                         }
068 
069                         @Override
070                         public String getName() {
071                             return columnName;
072                         }
073                     };
074                 }
075             });
076 
077             columns.add(column);
078         }
079     }
080 
081     @Nonnull
082     public ObservableList<E> getSource() {
083         return source;
084     }
085 
086     @Nonnull
087     public TableFormat<E> getFormat() {
088         return format;
089     }
090 
091     @Override
092     public void attachTo(@Nonnull TableView<E> tableView) {
093         requireNonNull(tableView, ERROR_TABLE_VIEW_NULL);
094         tableView.setItems(source);
095         tableView.getColumns().addAll(columns);
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 }