< prev index next >

modules/javafx.controls/src/main/java/javafx/scene/control/TreeTableColumn.java

Print this page




  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,


 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<?,?>>() {


 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 


 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         /**


  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,


 118  * the {@link TableView} class documentation.
 119  *
 120  * @param <S> The type of the TableView generic type (i.e. S == TableView&lt;S&gt;)
 121  * @param <T> The type of the content in all cells in this TableColumn.
 122  * @see TableView
 123  * @see TableCell
 124  * @see TablePosition
 125  * @see javafx.scene.control.cell.TreeItemPropertyValueFactory
 126  * @since JavaFX 8.0
 127  */
 128 public class TreeTableColumn<S,T> extends TableColumnBase<TreeItem<S>,T> implements EventTarget {
 129 
 130     /***************************************************************************
 131      *                                                                         *
 132      * Static properties and methods                                           *
 133      *                                                                         *
 134      **************************************************************************/
 135 
 136     /**
 137      * Parent event for any TreeTableColumn edit event.
 138      * @param <S> the type of the TableView generic type
 139      * @param <T> the type of the content in all cells in this TableColumn
 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 TableView generic type
 155      * @param <T> the type of the content in all cells in this TableColumn
 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 TableView generic type
 169      * @param <T> the type of the content in all cells in this TableColumn
 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 TableView generic type
 184      * @param <T> the type of the content in all cells in this TableColumn
 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<?,?>>() {


 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 


 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         /**
< prev index next >