1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene.control;
  27 
  28 import com.sun.javafx.scene.control.Properties;
  29 import javafx.css.CssMetaData;
  30 import java.util.Collections;
  31 import java.util.List;
  32 import java.util.Map;
  33 import javafx.beans.property.ObjectProperty;
  34 import javafx.beans.property.ReadOnlyObjectProperty;
  35 import javafx.beans.property.ReadOnlyObjectWrapper;
  36 import javafx.beans.property.SimpleObjectProperty;
  37 import javafx.beans.value.ObservableValue;
  38 import javafx.beans.value.WritableValue;
  39 import javafx.collections.FXCollections;
  40 import javafx.collections.ListChangeListener;
  41 import javafx.collections.ObservableList;
  42 import javafx.collections.WeakListChangeListener;
  43 import javafx.event.Event;
  44 import javafx.event.EventHandler;
  45 import javafx.event.EventTarget;
  46 import javafx.event.EventType;
  47 import javafx.scene.Node;
  48 import javafx.util.Callback;
  49 import javafx.css.Styleable;
  50 import javafx.scene.control.skin.TableViewSkinBase;
  51 /**
  52  * A {@link TreeTableView} is made up of a number of TreeTableColumn instances. Each
  53  * TreeTableColumn in a {@link TreeTableView} is responsible for displaying
  54  * (and editing) the contents of that column. As well as being responsible for
  55  * displaying and editing data for a single column, a TreeTableColumn also
  56  * contains the necessary properties to:
  57  * <ul>
  58  *    <li>Be resized (using {@link #minWidthProperty() minWidth}/
  59  *    {@link #prefWidthProperty() prefWidth}/
  60  *    {@link #maxWidthProperty() maxWidth} and {@link #widthProperty() width} properties)
  61  *    <li>Have its {@link #visibleProperty() visibility} toggled
  62  *    <li>Display {@link #textProperty() header text}
  63  *    <li>Display any {@link #getColumns() nested columns} it may contain
  64  *    <li>Have a {@link #contextMenuProperty() context menu} when the user 
  65  *      right-clicks the column header area
  66  *    <li>Have the contents of the table be sorted (using 
  67  *      {@link #comparatorProperty() comparator}, {@link #sortable sortable} and
  68  *      {@link #sortTypeProperty() sortType})
  69  * </ul>
  70  * </p>
  71  * 
  72  * When creating a TreeTableColumn instance, perhaps the two most important properties
  73  * to set are the column {@link #textProperty() text} (what to show in the column
  74  * header area), and the column {@link #cellValueFactory cell value factory}
  75  * (which is used to populate individual cells in the column). This can be 
  76  * achieved using some variation on the following code:
  77  * 
  78  * <pre>{@code
  79  * firstNameCol.setCellValueFactory(new Callback<CellDataFeatures<Person, String>, ObservableValue<String>>() {
  80  *     public ObservableValue<String> call(CellDataFeatures<Person, String> p) {
  81  *         // p.getValue() returns the TreeItem<Person> instance for a particular TreeTableView row,
  82  *         // p.getValue().getValue() returns the Person instance inside the TreeItem<Person>
  83  *         return p.getValue().getValue().firstNameProperty();
  84  *     }
  85  *  });
  86  * }}</pre>
  87  * 
  88  * This approach assumes that the object returned from <code>p.getValue().getValue()</code>
  89  * has a JavaFX {@link ObservableValue} that can simply be returned. The benefit of this
  90  * is that the TableView will internally create bindings to ensure that,
  91  * should the returned {@link ObservableValue} change, the cell contents will be
  92  * automatically refreshed. 
  93  * 
  94  * <p>In situations where a TableColumn must interact with classes created before
  95  * JavaFX, or that generally do not wish to use JavaFX APIs for properties, it is
  96  * possible to wrap the returned value in a {@link ReadOnlyObjectWrapper} instance. For
  97  * example:
  98  * 
  99  *<pre>{@code
 100  * firstNameCol.setCellValueFactory(new Callback<CellDataFeatures<Person, String>, ObservableValue<String>>() {
 101  *     public ObservableValue<String> call(CellDataFeatures<Person, String> p) {
 102  *         // p.getValue() returns the TreeItem<Person> instance for a particular TreeTableView row,
 103  *         // p.getValue().getValue() returns the Person instance inside the TreeItem<Person>
 104  *         return new ReadOnlyObjectWrapper(p.getValue().getValue().getFirstName());
 105  *     }
 106  *  });
 107  * }}</pre>
 108  * 
 109  * It is hoped that over time there will be convenience cell value factories 
 110  * developed and made available to developers. As of the JavaFX 2.0 release, 
 111  * there is one such convenience class: {@link javafx.scene.control.cell.TreeItemPropertyValueFactory}.
 112  * This class removes the need to write the code above, instead relying on reflection to
 113  * look up a given property from a String. Refer to the 
 114  * <code>TreeItemPropertyValueFactory</code> class documentation for more information
 115  * on how to use this with a TableColumn.
 116  * 
 117  * Finally, for more detail on how to use TableColumn, there is further documentation in
 118  * the {@link TableView} class documentation.
 119  * 
 120  * @param <S> The type of the TableView generic type (i.e. S == TableView&lt;S&gt;)
 121  * @param <T> The type of the content in all cells in this TableColumn.
 122  * @see TableView
 123  * @see TableCell
 124  * @see TablePosition
 125  * @see javafx.scene.control.cell.TreeItemPropertyValueFactory
 126  * @since JavaFX 8.0
 127  */
 128 public class TreeTableColumn<S,T> extends TableColumnBase<TreeItem<S>,T> implements EventTarget {
 129     
 130     /***************************************************************************
 131      *                                                                         *
 132      * Static properties and methods                                           *
 133      *                                                                         *
 134      **************************************************************************/
 135     
 136     /**
 137      * Parent event for any TreeTableColumn edit event.
 138      */
 139     @SuppressWarnings("unchecked")
 140     public static <S,T> EventType<TreeTableColumn.CellEditEvent<S,T>> editAnyEvent() {
 141         return (EventType<TreeTableColumn.CellEditEvent<S,T>>) EDIT_ANY_EVENT;
 142     }
 143     private static final EventType<?> EDIT_ANY_EVENT =
 144             new EventType<>(Event.ANY, "TREE_TABLE_COLUMN_EDIT");
 145 
 146     /**
 147      * Indicates that the user has performed some interaction to start an edit
 148      * event, or alternatively the 
 149      * {@link TreeTableView#edit(int, javafx.scene.control.TreeTableColumn)}
 150      * method has been called.
 151      */
 152     @SuppressWarnings("unchecked")
 153     public static <S,T> EventType<TreeTableColumn.CellEditEvent<S,T>> editStartEvent() {
 154         return (EventType<TreeTableColumn.CellEditEvent<S,T>>) EDIT_START_EVENT;
 155     }
 156     private static final EventType<?> EDIT_START_EVENT =
 157             new EventType<>(editAnyEvent(), "EDIT_START");
 158 
 159     /**
 160      * Indicates that the editing has been canceled, meaning that no change should
 161      * be made to the backing data source.
 162      */
 163     @SuppressWarnings("unchecked")
 164     public static <S,T> EventType<TreeTableColumn.CellEditEvent<S,T>> editCancelEvent() {
 165         return (EventType<TreeTableColumn.CellEditEvent<S,T>>) EDIT_CANCEL_EVENT;
 166     }
 167     private static final EventType<?> EDIT_CANCEL_EVENT =
 168             new EventType<>(editAnyEvent(), "EDIT_CANCEL");
 169 
 170     /**
 171      * Indicates that the editing has been committed by the user, meaning that
 172      * a change should be made to the backing data source to reflect the new
 173      * data.
 174      */
 175     @SuppressWarnings("unchecked")
 176     public static <S,T> EventType<TreeTableColumn.CellEditEvent<S,T>> editCommitEvent() {
 177         return (EventType<TreeTableColumn.CellEditEvent<S,T>>) EDIT_COMMIT_EVENT;
 178     }
 179     private static final EventType<?> EDIT_COMMIT_EVENT =
 180             new EventType<>(editAnyEvent(), "EDIT_COMMIT");
 181     
 182     
 183     
 184     /**
 185      * If no cellFactory is specified on a TreeTableColumn instance, then this one
 186      * will be used by default. At present it simply renders the TableCell item 
 187      * property within the {@link TableCell#graphicProperty() graphic} property
 188      * if the {@link Cell#item item} is a Node, or it simply calls 
 189      * <code>toString()</code> if it is not null, setting the resulting string
 190      * inside the {@link Cell#textProperty() text} property.
 191      */
 192     public static final Callback<TreeTableColumn<?,?>, TreeTableCell<?,?>> DEFAULT_CELL_FACTORY = 
 193             new Callback<TreeTableColumn<?,?>, TreeTableCell<?,?>>() {
 194         @Override public TreeTableCell<?,?> call(TreeTableColumn<?,?> param) {
 195             return new TreeTableCell() {
 196                 @Override protected void updateItem(Object item, boolean empty) {
 197                     if (item == getItem()) return;
 198 
 199                     super.updateItem(item, empty);
 200 
 201                     if (item == null) {
 202                         super.setText(null);
 203                         super.setGraphic(null);
 204                     } else if (item instanceof Node) {
 205                         super.setText(null);
 206                         super.setGraphic((Node)item);
 207                     } else {
 208                         super.setText(item.toString());
 209                         super.setGraphic(null);
 210                     }
 211                 }
 212             };
 213         }
 214     };
 215 
 216     
 217     
 218     /***************************************************************************
 219      *                                                                         *
 220      * Constructors                                                            *
 221      *                                                                         *
 222      **************************************************************************/    
 223 
 224     /**
 225      * Creates a default TreeTableColumn with default cell factory, comparator, and
 226      * onEditCommit implementation.
 227      */
 228     public TreeTableColumn() {
 229         getStyleClass().add(DEFAULT_STYLE_CLASS);
 230         
 231         setOnEditCommit(DEFAULT_EDIT_COMMIT_HANDLER);
 232 
 233         // we listen to the columns list here to ensure that widths are
 234         // maintained properly, and to also set the column hierarchy such that
 235         // all children columns know that this TreeTableColumn is their parent.
 236         getColumns().addListener(weakColumnsListener);
 237 
 238         treeTableViewProperty().addListener(observable -> {
 239             // set all children of this tableView to have the same TableView
 240             // as this column
 241             for (TreeTableColumn<S, ?> tc : getColumns()) {
 242                 tc.setTreeTableView(getTreeTableView());
 243             }
 244 
 245             // This code was commented out due to RT-22391, with this enabled
 246             // the parent column will be null, which is not desired
 247 //                // set the parent of this column to also have this tableView
 248 //                if (getParentColumn() != null) {
 249 //                    getParentColumn().setTableView(getTableView());
 250 //                }
 251         });
 252     }
 253 
 254     /**
 255      * Creates a TreeTableColumn with the text set to the provided string, with
 256      * default cell factory, comparator, and onEditCommit implementation.
 257      * 
 258      * @param text The string to show when the TreeTableColumn is placed within
 259      *      the TreeTableView.
 260      */
 261     public TreeTableColumn(String text) {
 262         this();
 263         setText(text);
 264     }
 265     
 266     
 267     
 268     /***************************************************************************
 269      *                                                                         *
 270      * Listeners                                                               *
 271      *                                                                         *
 272      **************************************************************************/
 273     
 274     private EventHandler<TreeTableColumn.CellEditEvent<S,T>> DEFAULT_EDIT_COMMIT_HANDLER =
 275             t -> {
 276                 ObservableValue<T> ov = getCellObservableValue(t.getRowValue());
 277                 if (ov instanceof WritableValue) {
 278                     ((WritableValue)ov).setValue(t.getNewValue());
 279                 }
 280             };
 281     
 282     private ListChangeListener<TreeTableColumn<S, ?>> columnsListener = new ListChangeListener<TreeTableColumn<S,?>>() {
 283         @Override public void onChanged(ListChangeListener.Change<? extends TreeTableColumn<S,?>> c) {
 284             while (c.next()) {
 285                 // update the TreeTableColumn.treeTableView property
 286                 for (TreeTableColumn<S,?> tc : c.getRemoved()) {
 287                     // Fix for RT-16978. In TableColumnHeader we add before we
 288                     // remove when moving a TreeTableColumn. This means that for
 289                     // a very brief moment the tc is duplicated, and we can prevent
 290                     // nulling out the tableview and parent column. Without this
 291                     // here, in a very special circumstance it is possible to null
 292                     // out the entire content of a column by reordering and then
 293                     // sorting another column.
 294                     if (getColumns().contains(tc)) continue;
 295                     
 296                     tc.setTreeTableView(null);
 297                     tc.setParentColumn(null);
 298                 }
 299                 for (TreeTableColumn<S,?> tc : c.getAddedSubList()) {
 300                     tc.setTreeTableView(getTreeTableView());
 301                 }
 302 
 303                 updateColumnWidths();
 304             }
 305         }
 306     };
 307     
 308     private WeakListChangeListener<TreeTableColumn<S, ?>> weakColumnsListener = 
 309             new WeakListChangeListener<>(columnsListener);
 310     
 311     
 312     /***************************************************************************
 313      *                                                                         *
 314      * Instance Variables                                                      *
 315      *                                                                         *
 316      **************************************************************************/
 317     
 318     // Contains any children columns that should be nested within this column
 319     private final ObservableList<TreeTableColumn<S,?>> columns = FXCollections.<TreeTableColumn<S,?>>observableArrayList();
 320     
 321     
 322     
 323     /***************************************************************************
 324      *                                                                         *
 325      * Properties                                                              *
 326      *                                                                         *
 327      **************************************************************************/
 328     
 329     
 330     // --- TreeTableView
 331     /**
 332      * The TreeTableView that this TreeTableColumn belongs to.
 333      */
 334     private ReadOnlyObjectWrapper<TreeTableView<S>> treeTableView = 
 335             new ReadOnlyObjectWrapper<TreeTableView<S>>(this, "treeTableView");
 336     public final ReadOnlyObjectProperty<TreeTableView<S>> treeTableViewProperty() {
 337         return treeTableView.getReadOnlyProperty();
 338     }
 339     final void setTreeTableView(TreeTableView<S> value) { treeTableView.set(value); }
 340     public final TreeTableView<S> getTreeTableView() { return treeTableView.get(); }
 341 
 342     
 343 
 344     // --- Cell value factory
 345     /**
 346      * The cell value factory needs to be set to specify how to populate all
 347      * cells within a single TreeTableColumn. A cell value factory is a {@link Callback}
 348      * that provides a {@link CellDataFeatures} instance, and expects an
 349      * {@link ObservableValue} to be returned. The returned ObservableValue instance
 350      * will be observed internally to allow for updates to the value to be 
 351      * immediately reflected on screen.
 352      * 
 353      * <p>An example of how to set a cell value factory is:
 354      * 
 355      * <pre>{@code
 356      * firstNameCol.setCellValueFactory(new Callback<CellDataFeatures<Person, String>, ObservableValue<String>>() {
 357      *     public ObservableValue<String> call(CellDataFeatures<Person, String> p) {
 358      *         // p.getValue() returns the TreeItem<Person> instance for a particular TreeTableView row,
 359      *         // p.getValue().getValue() returns the Person instance inside the TreeItem<Person>
 360      *         return p.getValue().getValue().firstNameProperty();
 361      *     }
 362      *  });
 363      * }}</pre>
 364      * 
 365      * A common approach is to want to populate cells in a TreeTableColumn using
 366      * a single value from a Java bean. To support this common scenario, there
 367      * is the {@link javafx.scene.control.cell.TreeItemPropertyValueFactory} class.
 368      * Refer to this class for more information on how to use it, but briefly
 369      * here is how the above use case could be simplified using the TreeItemPropertyValueFactory class:
 370      * 
 371      * <pre><code>
 372      * firstNameCol.setCellValueFactory(new TreeItemPropertyValueFactory&lt;Person,String&gt;("firstName"));
 373      * </code></pre>
 374      * 
 375      * @see javafx.scene.control.cell.TreeItemPropertyValueFactory
 376      */
 377     private ObjectProperty<Callback<TreeTableColumn.CellDataFeatures<S,T>, ObservableValue<T>>> cellValueFactory;
 378     public final void setCellValueFactory(Callback<TreeTableColumn.CellDataFeatures<S,T>, ObservableValue<T>> value) {
 379         cellValueFactoryProperty().set(value);
 380     }
 381     public final Callback<TreeTableColumn.CellDataFeatures<S,T>, ObservableValue<T>> getCellValueFactory() {
 382         return cellValueFactory == null ? null : cellValueFactory.get();
 383     }
 384     public final ObjectProperty<Callback<TreeTableColumn.CellDataFeatures<S,T>, ObservableValue<T>>> cellValueFactoryProperty() {
 385         if (cellValueFactory == null) {
 386             cellValueFactory = new SimpleObjectProperty<Callback<TreeTableColumn.CellDataFeatures<S,T>, ObservableValue<T>>>(this, "cellValueFactory");
 387         }
 388         return cellValueFactory;
 389     }
 390     
 391     
 392     // --- Cell Factory
 393     /**
 394      * The cell factory for all cells in this column. The cell factory
 395      * is responsible for rendering the data contained within each TreeTableCell 
 396      * for a single TreeTableColumn.
 397      * 
 398      * <p>By default TreeTableColumn uses a {@link #DEFAULT_CELL_FACTORY default cell
 399      * factory}, but this can be replaced with a custom implementation, for 
 400      * example to show data in a different way or to support editing. There is a 
 401      * lot of documentation on creating custom cell factories
 402      * elsewhere (see {@link Cell} and {@link TreeTableView} for example).</p>
 403      * 
 404      * <p>Finally, there are a number of pre-built cell factories available in the
 405      * {@link javafx.scene.control.cell} package.
 406      *
 407      */
 408     private final ObjectProperty<Callback<TreeTableColumn<S,T>, TreeTableCell<S,T>>> cellFactory =
 409         new SimpleObjectProperty<Callback<TreeTableColumn<S,T>, TreeTableCell<S,T>>>(
 410             this, "cellFactory", (Callback<TreeTableColumn<S,T>, TreeTableCell<S,T>>) ((Callback) DEFAULT_CELL_FACTORY)) {
 411                 @Override protected void invalidated() {
 412                     TreeTableView<S> table = getTreeTableView();
 413                     if (table == null) return;
 414                     Map<Object,Object> properties = table.getProperties();
 415                     if (properties.containsKey(Properties.RECREATE)) {
 416                         properties.remove(Properties.RECREATE);
 417                     }
 418                     properties.put(Properties.RECREATE, Boolean.TRUE);
 419                 }
 420             };
 421     public final void setCellFactory(Callback<TreeTableColumn<S,T>, TreeTableCell<S,T>> value) {
 422         cellFactory.set(value);
 423     }
 424     public final Callback<TreeTableColumn<S,T>, TreeTableCell<S,T>> getCellFactory() {
 425         return cellFactory.get();
 426     }
 427     public final ObjectProperty<Callback<TreeTableColumn<S,T>, TreeTableCell<S,T>>> cellFactoryProperty() {
 428         return cellFactory;
 429     }
 430     
 431     
 432     // --- Sort Type
 433     /**
 434      * Used to state whether this column, if it is part of a sort order (see
 435      * {@link TreeTableView#getSortOrder()} for more details), should be sorted 
 436      * in ascending or descending order. 
 437      * Simply toggling this property will result in the sort order changing in 
 438      * the TreeTableView, assuming of course that this column is in the 
 439      * sortOrder ObservableList to begin with.
 440      */
 441     private ObjectProperty<SortType> sortType;
 442     public final ObjectProperty<SortType> sortTypeProperty() {
 443         if (sortType == null) {
 444             sortType = new SimpleObjectProperty<SortType>(this, "sortType", SortType.ASCENDING);
 445         }
 446         return sortType;
 447     }
 448     public final void setSortType(SortType value) {
 449         sortTypeProperty().set(value);
 450     }
 451     public final SortType getSortType() {
 452         return sortType == null ? SortType.ASCENDING : sortType.get();
 453     }
 454     
 455     
 456     // --- On Edit Start
 457     /**
 458      * This event handler will be fired when the user successfully initiates
 459      * editing.
 460      */
 461     private ObjectProperty<EventHandler<TreeTableColumn.CellEditEvent<S,T>>> onEditStart;
 462     public final void setOnEditStart(EventHandler<TreeTableColumn.CellEditEvent<S,T>> value) {
 463         onEditStartProperty().set(value);
 464     }
 465     public final EventHandler<TreeTableColumn.CellEditEvent<S,T>> getOnEditStart() {
 466         return onEditStart == null ? null : onEditStart.get();
 467     }
 468     public final ObjectProperty<EventHandler<TreeTableColumn.CellEditEvent<S,T>>> onEditStartProperty() {
 469         if (onEditStart == null) {
 470             onEditStart = new SimpleObjectProperty<EventHandler<TreeTableColumn.CellEditEvent<S,T>>>(this, "onEditStart") {
 471                 @Override protected void invalidated() {
 472                     eventHandlerManager.setEventHandler(TreeTableColumn.<S,T>editStartEvent(), get());
 473                 }
 474             };
 475         }
 476         return onEditStart;
 477     }
 478 
 479 
 480     // --- On Edit Commit
 481     /**
 482      * This event handler will be fired when the user successfully commits their
 483      * editing.
 484      */
 485     private ObjectProperty<EventHandler<TreeTableColumn.CellEditEvent<S,T>>> onEditCommit;
 486     public final void setOnEditCommit(EventHandler<TreeTableColumn.CellEditEvent<S,T>> value) {
 487         onEditCommitProperty().set(value);
 488     }
 489     public final EventHandler<TreeTableColumn.CellEditEvent<S,T>> getOnEditCommit() {
 490         return onEditCommit == null ? null : onEditCommit.get();
 491     }
 492     public final ObjectProperty<EventHandler<TreeTableColumn.CellEditEvent<S,T>>> onEditCommitProperty() {
 493         if (onEditCommit == null) {
 494             onEditCommit = new SimpleObjectProperty<EventHandler<TreeTableColumn.CellEditEvent<S,T>>>(this, "onEditCommit") {
 495                 @Override protected void invalidated() {
 496                     eventHandlerManager.setEventHandler(TreeTableColumn.<S,T>editCommitEvent(), get());
 497                 }
 498             };
 499         }
 500         return onEditCommit;
 501     }
 502 
 503 
 504     // --- On Edit Cancel
 505     /**
 506      * This event handler will be fired when the user cancels editing a cell.
 507      */
 508     private ObjectProperty<EventHandler<TreeTableColumn.CellEditEvent<S,T>>> onEditCancel;
 509     public final void setOnEditCancel(EventHandler<TreeTableColumn.CellEditEvent<S,T>> value) {
 510         onEditCancelProperty().set(value);
 511     }
 512     public final EventHandler<TreeTableColumn.CellEditEvent<S,T>> getOnEditCancel() {
 513         return onEditCancel == null ? null : onEditCancel.get();
 514     }
 515     public final ObjectProperty<EventHandler<TreeTableColumn.CellEditEvent<S,T>>> onEditCancelProperty() {
 516         if (onEditCancel == null) {
 517             onEditCancel = new SimpleObjectProperty<EventHandler<TreeTableColumn.CellEditEvent<S,T>>>(this, "onEditCancel") {
 518                 @Override protected void invalidated() {
 519                     eventHandlerManager.setEventHandler(TreeTableColumn.<S,T>editCancelEvent(), get());
 520                 }
 521             };
 522         }
 523         return onEditCancel;
 524     }
 525     
 526     
 527 
 528     /***************************************************************************
 529      *                                                                         *
 530      * Public API                                                              *
 531      *                                                                         *
 532      **************************************************************************/    
 533     
 534     /** {@inheritDoc} */
 535     @Override public final ObservableList<TreeTableColumn<S,?>> getColumns() {
 536         return columns;
 537     }
 538     
 539     /** {@inheritDoc} */
 540     @Override public final ObservableValue<T> getCellObservableValue(int index) {
 541         if (index < 0) return null;
 542         
 543         // Get the table
 544         final TreeTableView<S> table = getTreeTableView();
 545         if (table == null || index >= table.getExpandedItemCount()) return null;
 546         
 547         // Get the rowData
 548         TreeItem<S> item = table.getTreeItem(index);
 549         return getCellObservableValue(item);
 550     }
 551     
 552     /** {@inheritDoc} */
 553     @Override public final ObservableValue<T> getCellObservableValue(TreeItem<S> item) {
 554         // Get the factory
 555         final Callback<TreeTableColumn.CellDataFeatures<S,T>, ObservableValue<T>> factory = getCellValueFactory();
 556         if (factory == null) return null;
 557         
 558         // Get the table
 559         final TreeTableView<S> table = getTreeTableView();
 560         if (table == null) return null;
 561         
 562         // Call the factory
 563         final TreeTableColumn.CellDataFeatures<S,T> cdf = new TreeTableColumn.CellDataFeatures<S,T>(table, this, item);
 564         return factory.call(cdf);
 565     }
 566 
 567     
 568     
 569     /***************************************************************************
 570      *                                                                         *
 571      * Private Implementation                                                  *
 572      *                                                                         *
 573      **************************************************************************/        
 574 
 575 
 576     
 577     /***************************************************************************
 578      *                                                                         *
 579      * Stylesheet Handling                                                     *
 580      *                                                                         *
 581      **************************************************************************/
 582 
 583     private static final String DEFAULT_STYLE_CLASS = "table-column";
 584     
 585     /**
 586      * {@inheritDoc}
 587      * @return "TreeTableColumn"
 588      */
 589     @Override public String getTypeSelector() {
 590         return "TreeTableColumn";
 591     }
 592 
 593     /**
 594      * {@inheritDoc}
 595      * @return {@code getTreeTableView()}
 596      */
 597     @Override public Styleable getStyleableParent() {
 598         return getTreeTableView();
 599     }
 600 
 601     /**
 602      * {@inheritDoc}
 603     */
 604     @Override public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
 605         return getClassCssMetaData();
 606     }
 607     
 608     public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
 609         return Collections.emptyList();
 610     }                
 611 
 612     /**
 613      * @treatAsPrivate implementation detail
 614      * @deprecated This is an internal API that is not intended for use and will be removed in the next version
 615      */
 616     @Deprecated    
 617     // TODO implement
 618     // SB-dependency: RT-21094 has been filed to track this
 619     public Node impl_styleableGetNode() {
 620 //        if (! (getTableView().getSkin() instanceof TableViewSkin)) return null;
 621 //        TableViewSkin skin = (TableViewSkin) getTableView().getSkin();
 622 //
 623 //        TableHeaderRow tableHeader = skin.getTableHeaderRow();
 624 //        NestedTreeTableColumnHeader rootHeader = tableHeader.getRootHeader();
 625 //
 626 //        // we now need to do a search for the header. We'll go depth-first.
 627 //        return scan(rootHeader);
 628             return null;
 629         }
 630 
 631 //    private TreeTableColumnHeader scan(TreeTableColumnHeader header) {
 632 //        // firstly test that the parent isn't what we are looking for
 633 //        if (TreeTableColumn.this.equals(header.getTreeTableColumn())) {
 634 //            return header;
 635 //        }
 636 //
 637 //        if (header instanceof NestedTreeTableColumnHeader) {
 638 //            NestedTreeTableColumnHeader parent = (NestedTreeTableColumnHeader) header;
 639 //            for (int i = 0; i < parent.getColumnHeaders().size(); i++) {
 640 //                TreeTableColumnHeader result = scan(parent.getColumnHeaders().get(i));
 641 //                if (result != null) {
 642 //                    return result;
 643 //                }
 644 //            }
 645 //        }
 646 //
 647 //        return null;
 648 //    }   
 649     
 650     /***************************************************************************
 651      *                                                                         *
 652      * Support Interfaces                                                      *
 653      *                                                                         *
 654      **************************************************************************/
 655 
 656     /**
 657      * A support class used in TreeTableColumn as a wrapper class 
 658      * to provide all necessary information for a particular {@link Cell}. Once
 659      * instantiated, this class is immutable.
 660      * 
 661      * @param <S> The TableView type
 662      * @param <T> The TreeTableColumn type
 663      * @since JavaFX 8.0
 664      */
 665     public static class CellDataFeatures<S,T> {
 666         private final TreeTableView<S> treeTableView;
 667         private final TreeTableColumn<S,T> tableColumn;
 668         private final TreeItem<S> value;
 669 
 670         /**
 671          * Instantiates a CellDataFeatures instance with the given properties
 672          * set as read-only values of this instance.
 673          * 
 674          * @param treeTableView The TableView that this instance refers to.
 675          * @param tableColumn The TreeTableColumn that this instance refers to.
 676          * @param value The value for a row in the TableView.
 677          */
 678         public CellDataFeatures(TreeTableView<S> treeTableView,
 679                 TreeTableColumn<S,T> tableColumn, TreeItem<S> value) {
 680             this.treeTableView = treeTableView;
 681             this.tableColumn = tableColumn;
 682             this.value = value;
 683         }
 684 
 685         /**
 686          * Returns the value passed in to the constructor.
 687          */
 688         public TreeItem<S> getValue() {
 689             return value;
 690         }
 691 
 692         /**
 693          * Returns the {@link TreeTableColumn} passed in to the constructor.
 694          */
 695         public TreeTableColumn<S,T> getTreeTableColumn() {
 696             return tableColumn;
 697         }
 698 
 699         /**
 700          * Returns the {@link TableView} passed in to the constructor.
 701          */
 702         public TreeTableView<S> getTreeTableView() {
 703             return treeTableView;
 704         }
 705     }
 706     
 707     
 708     
 709     /**
 710      * An event that is fired when a user performs an edit on a table cell.
 711      * @since JavaFX 8.0
 712      */
 713     public static class CellEditEvent<S,T> extends Event {
 714         private static final long serialVersionUID = -609964441682677579L;
 715 
 716         /**
 717          * Common supertype for all cell edit event types.
 718          */
 719         public static final EventType<?> ANY = EDIT_ANY_EVENT;
 720 
 721         // represents the new value input by the end user. This is NOT the value
 722         // to go back into the TableView.items list - this new value represents
 723         // just the input for a single cell, so it is likely that it needs to go
 724         // back into a property within an item in the TableView.items list.
 725         private final T newValue;
 726 
 727         // The location of the edit event
 728         private transient final TreeTablePosition<S,T> pos;
 729 
 730         /**
 731          * Creates a new event that can be subsequently fired to the relevant listeners.
 732          *
 733          * @param table The TableView on which this event occurred.
 734          * @param pos The position upon which this event occurred.
 735          * @param eventType The type of event that occurred.
 736          * @param newValue The value input by the end user.
 737          */
 738         public CellEditEvent(TreeTableView<S> table, TreeTablePosition<S,T> pos,
 739                 EventType<TreeTableColumn.CellEditEvent<S,T>> eventType, T newValue) {
 740             super(table, Event.NULL_SOURCE_TARGET, eventType);
 741 
 742             if (table == null) {
 743                 throw new NullPointerException("TableView can not be null");
 744             }
 745             this.pos = pos;
 746             this.newValue = newValue;
 747         }
 748 
 749         /**
 750          * Returns the TableView upon which this event occurred.
 751          * @return The TableView control upon which this event occurred.
 752          */
 753         public TreeTableView<S> getTreeTableView() {
 754             return pos.getTreeTableView();
 755         }
 756 
 757         /**
 758          * Returns the TreeTableColumn upon which this event occurred.
 759          *
 760          * @return The TreeTableColumn that the edit occurred in.
 761          */
 762         public TreeTableColumn<S,T> getTableColumn() {
 763             return pos.getTableColumn();
 764         }
 765 
 766         /**
 767          * Returns the position upon which this event occurred.
 768          * @return The position upon which this event occurred.
 769          */
 770         public TreeTablePosition<S,T> getTreeTablePosition() {
 771             return pos;
 772         }
 773 
 774         /**
 775          * Returns the new value input by the end user. This is <b>not</b> the value
 776          * to go back into the TableView.items list - this new value represents
 777          * just the input for a single cell, so it is likely that it needs to go
 778          * back into a property within an item in the TableView.items list.
 779          *
 780          * @return An Object representing the new value input by the user.
 781          */
 782         public T getNewValue() {
 783             return newValue;
 784         }
 785 
 786         /**
 787          * Attempts to return the old value at the position referred to in the
 788          * TablePosition returned by {@link #getTreeTablePosition()}. This may return
 789          * null for a number of reasons.
 790          *
 791          * @return Returns the value stored in the position being edited, or null
 792          *     if it can not be retrieved.
 793          */
 794         public T getOldValue() {
 795             TreeItem<S> rowData = getRowValue();
 796             if (rowData == null || pos.getTableColumn() == null) {
 797                 return null;
 798             }
 799 
 800             // if we are here, we now need to get the data for the specific column
 801             return (T) pos.getTableColumn().getCellData(rowData);
 802         }
 803         
 804         /**
 805          * Convenience method that returns the value for the row (that is, from
 806          * the TableView {@link TableView#itemsProperty() items} list), for the
 807          * row contained within the {@link TablePosition} returned in
 808          * {@link #getTreeTablePosition()}.
 809          */
 810         public TreeItem<S> getRowValue() {
 811 //            List<S> items = getTreeTableView().getItems();
 812 //            if (items == null) return null;
 813 
 814             TreeTableView<S> treeTable = getTreeTableView();
 815             int row = pos.getRow();
 816             if (row < 0 || row >= treeTable.getExpandedItemCount()) return null;
 817 
 818             return treeTable.getTreeItem(row);
 819         }
 820     }
 821     
 822     /**
 823      * Enumeration that specifies the type of sorting being applied to a specific
 824      * column.
 825      * @since JavaFX 8.0
 826      */
 827     public static enum SortType {
 828         /**
 829          * Column will be sorted in an ascending order.
 830          */
 831         ASCENDING,
 832         
 833         /**
 834          * Column will be sorted in a descending order.
 835          */
 836         DESCENDING;
 837         
 838         // UNSORTED
 839     }
 840 }