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