001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2012 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.client.tide.collections.javafx;
022    
023    import javafx.beans.property.ReadOnlyProperty;
024    import javafx.beans.value.ObservableValue;
025    import javafx.scene.control.TableColumn;
026    import javafx.scene.control.TableColumn.SortType;
027    import javafx.scene.control.TableView;
028    
029    /**
030     * @author William DRAI
031     */
032    public class TableViewSort<S> implements Sort {
033            
034            private TableView<S> tableView;
035            private S exampleData;
036            
037            private String[] order = new String[0];
038            private boolean[] desc = new boolean[0];
039    
040            public TableViewSort(final TableView<S> tableView, final Class<S> exampleDataClass) {
041                    this.tableView = tableView;
042                    try {
043                            this.exampleData = exampleDataClass.newInstance();
044                    }
045                    catch (Exception e) {
046                            throw new RuntimeException("Could not instantiate example data class " + exampleDataClass, e);
047                    }
048            }
049            
050            public void build() {
051                    int i = 0;
052                    order = new String[tableView.getSortOrder().size()];
053                    desc = new boolean[tableView.getSortOrder().size()];
054                    for (TableColumn<S, ?> column : tableView.getSortOrder()) {
055                            ObservableValue<?> property = column.getCellObservableValue(exampleData);
056                            if (property instanceof ReadOnlyProperty<?>) {
057                                    order[i] = ((ReadOnlyProperty<?>)property).getName();
058                                    desc[i] = column.getSortType() == SortType.ASCENDING;
059                                    i++;
060                            }
061                            else
062                                    throw new IllegalArgumentException("Call values must implement Property to apply TableViewSort adapter");
063                    }
064            }
065            
066            public String[] getOrder() {
067                    return order;
068            }
069            
070            public boolean[] getDesc() {
071                    return desc;
072            }
073    }