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