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