< prev index next >

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

Print this page




  84      */
  85     private ReadOnlyObjectWrapper<T> focusedItem = new ReadOnlyObjectWrapper<T>(this, "focusedItem");
  86     public final ReadOnlyObjectProperty<T> focusedItemProperty() { return focusedItem.getReadOnlyProperty(); }
  87     public final T getFocusedItem() { return focusedItemProperty().get(); }
  88     final void setFocusedItem(T value) { focusedItem.set(value); }
  89 
  90 
  91 
  92     /***********************************************************************
  93      *                                                                     *
  94      * Public Focus API                                                    *
  95      *                                                                     *
  96      **********************************************************************/
  97 
  98 
  99     /**
 100      * Returns the number of items in the data model that underpins the control.
 101      * An example would be that a ListView focus model would likely return
 102      * <code>listView.getItems().size()</code>. The valid range of focusable
 103      * indices is between 0 and whatever is returned by this method.

 104      */
 105     protected abstract int getItemCount();
 106 
 107     /**
 108      * Returns the item at the given index. An example using ListView would be
 109      * <code>listView.getItems().get(index)</code>.
 110      *
 111      * @param index The index of the item that is requested from the underlying
 112      *      data model.
 113      * @return Returns null if the index is out of bounds, or an element of type
 114      *      T that is related to the given index.
 115      */
 116     protected abstract T getModelItem(int index);
 117 
 118     /**
 119      * <p>Convenience method to inform if the given index is currently focused
 120      * in this SelectionModel. Is functionally equivalent to calling
 121      * <pre><code>getFocusedIndex() == index</code></pre>.
 122      *
 123      * @param index The index to check as to whether it is currently focused
 124      *      or not.
 125      * @return True if the given index is focused, false otherwise.
 126      */
 127     public boolean isFocused(int index) {
 128         if (index < 0 || index >= getItemCount()) return false;
 129 
 130         return getFocusedIndex() == index;
 131     }
 132 
 133     /**
 134      * Causes the item at the given index to receive the focus. This does not
 135      * cause the current selection to change. Updates the focusedItem and
 136      * focusedIndex properties such that <code>focusedIndex = -1</code> unless
 137      * <code>0 <= index < model size</code>.
 138      *
 139      * @param index The index of the item to get focus.
 140      */
 141     public void focus(int index) {
 142         if (index < 0 || index >= getItemCount()) {
 143             setFocusedIndex(-1);
 144         } else {
 145             int oldFocusIndex = getFocusedIndex();
 146             setFocusedIndex(index);
 147 
 148             if (oldFocusIndex == index) {
 149                 // manually update the focus item to ensure consistency
 150                 setFocusedItem(getModelItem(index));
 151             }
 152         }
 153     }
 154 
 155     /**
 156      * Attempts to give focus to the row previous to the currently focused row.
 157      * If the current focus owner is the first row, or is -1 (representing that




  84      */
  85     private ReadOnlyObjectWrapper<T> focusedItem = new ReadOnlyObjectWrapper<T>(this, "focusedItem");
  86     public final ReadOnlyObjectProperty<T> focusedItemProperty() { return focusedItem.getReadOnlyProperty(); }
  87     public final T getFocusedItem() { return focusedItemProperty().get(); }
  88     final void setFocusedItem(T value) { focusedItem.set(value); }
  89 
  90 
  91 
  92     /***********************************************************************
  93      *                                                                     *
  94      * Public Focus API                                                    *
  95      *                                                                     *
  96      **********************************************************************/
  97 
  98 
  99     /**
 100      * Returns the number of items in the data model that underpins the control.
 101      * An example would be that a ListView focus model would likely return
 102      * <code>listView.getItems().size()</code>. The valid range of focusable
 103      * indices is between 0 and whatever is returned by this method.
 104      * @return the number of items in the data model that underpins the control
 105      */
 106     protected abstract int getItemCount();
 107 
 108     /**
 109      * Returns the item at the given index. An example using ListView would be
 110      * <code>listView.getItems().get(index)</code>.
 111      *
 112      * @param index The index of the item that is requested from the underlying
 113      *      data model.
 114      * @return Returns null if the index is out of bounds, or an element of type
 115      *      T that is related to the given index.
 116      */
 117     protected abstract T getModelItem(int index);
 118 
 119     /**
 120      * <p>Convenience method to inform if the given index is currently focused
 121      * in this SelectionModel. Is functionally equivalent to calling
 122      * <pre><code>getFocusedIndex() == index</code></pre>.
 123      *
 124      * @param index The index to check as to whether it is currently focused
 125      *      or not.
 126      * @return True if the given index is focused, false otherwise.
 127      */
 128     public boolean isFocused(int index) {
 129         if (index < 0 || index >= getItemCount()) return false;
 130 
 131         return getFocusedIndex() == index;
 132     }
 133 
 134     /**
 135      * Causes the item at the given index to receive the focus. This does not
 136      * cause the current selection to change. Updates the focusedItem and
 137      * focusedIndex properties such that <code>focusedIndex = -1</code> unless
 138      * <code>0 &lt;= index &lt; model size</code>.
 139      *
 140      * @param index The index of the item to get focus.
 141      */
 142     public void focus(int index) {
 143         if (index < 0 || index >= getItemCount()) {
 144             setFocusedIndex(-1);
 145         } else {
 146             int oldFocusIndex = getFocusedIndex();
 147             setFocusedIndex(index);
 148 
 149             if (oldFocusIndex == index) {
 150                 // manually update the focus item to ensure consistency
 151                 setFocusedItem(getModelItem(index));
 152             }
 153         }
 154     }
 155 
 156     /**
 157      * Attempts to give focus to the row previous to the currently focused row.
 158      * If the current focus owner is the first row, or is -1 (representing that


< prev index next >