< prev index next >

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

Print this page




  45  * any one time.
  46  * <p>
  47  * Generally {@code ToggleGroups} are managed automatically simply by specifying
  48  * the name of a {@code ToggleGroup} on the <code>{@link Toggle}</code>, but in
  49  * some situations it is desirable to explicitly manage which
  50  * {@code ToggleGroup} is used by <code>{@link Toggle Toggles}</code>.
  51  * </p>
  52  * @since JavaFX 2.0
  53  */
  54 public class ToggleGroup {
  55 
  56     /**
  57      * Creates a default ToggleGroup instance.
  58      */
  59     public ToggleGroup() {
  60 
  61     }
  62 
  63     /**
  64      * The list of toggles within the ToggleGroup.

  65      */
  66     public final ObservableList<Toggle> getToggles() {
  67         return toggles;
  68     }
  69 
  70     private final ObservableList<Toggle> toggles = new VetoableListDecorator<Toggle>(new TrackableObservableList<Toggle>() {
  71         @Override protected void onChanged(Change<Toggle> c) {
  72             while (c.next()) {
  73                 // Look through the removed toggles, and if any of them was the
  74                 // one and only selected toggle, then we will clear the selected
  75                 // toggle property.
  76                 for (Toggle t : c.getRemoved()) {
  77                     if (t.isSelected()) {
  78                         selectToggle(null);
  79                     }
  80                 }
  81 
  82                 // A Toggle can only be in one group at any one time. If the
  83                 // group is changed, then the toggle is removed from the old group prior to
  84                 // being added to the new group.


 139         }
 140     };
 141 
 142     /**
 143      * Selects the toggle.
 144      *
 145      * @param value The {@code Toggle} that is to be selected.
 146      */
 147     // Note that since selectedToggle is a read-only property, the selectToggle method is some
 148     // other method than setSelectedToggle, even though it is in essence doing the same thing
 149     public final void selectToggle(Toggle value) { selectedToggle.set(value); }
 150 
 151     /**
 152      * Gets the selected {@code Toggle}.
 153      * @return Toggle The selected toggle.
 154      */
 155     public final Toggle getSelectedToggle() { return selectedToggle.get(); }
 156 
 157     /**
 158      * The selected toggle.

 159      */
 160     public final ReadOnlyObjectProperty<Toggle> selectedToggleProperty() { return selectedToggle.getReadOnlyProperty(); }
 161 
 162     private boolean setSelected(Toggle toggle, boolean selected) {
 163         if (toggle != null &&
 164                 toggle.getToggleGroup() == this &&
 165                 !toggle.selectedProperty().isBound()) {
 166             toggle.setSelected(selected);
 167             return true;
 168         }
 169         return false;
 170     }
 171 
 172     // Clear the selected toggle only if there are no other toggles selected.
 173     final void clearSelectedToggle() {
 174         if (!selectedToggle.getValue().isSelected()) {
 175              for (Toggle toggle: getToggles()) {
 176                  if (toggle.isSelected()) {
 177                      return;
 178                  }




  45  * any one time.
  46  * <p>
  47  * Generally {@code ToggleGroups} are managed automatically simply by specifying
  48  * the name of a {@code ToggleGroup} on the <code>{@link Toggle}</code>, but in
  49  * some situations it is desirable to explicitly manage which
  50  * {@code ToggleGroup} is used by <code>{@link Toggle Toggles}</code>.
  51  * </p>
  52  * @since JavaFX 2.0
  53  */
  54 public class ToggleGroup {
  55 
  56     /**
  57      * Creates a default ToggleGroup instance.
  58      */
  59     public ToggleGroup() {
  60 
  61     }
  62 
  63     /**
  64      * The list of toggles within the ToggleGroup.
  65      * @return the list of toggles within the ToggleGroup
  66      */
  67     public final ObservableList<Toggle> getToggles() {
  68         return toggles;
  69     }
  70 
  71     private final ObservableList<Toggle> toggles = new VetoableListDecorator<Toggle>(new TrackableObservableList<Toggle>() {
  72         @Override protected void onChanged(Change<Toggle> c) {
  73             while (c.next()) {
  74                 // Look through the removed toggles, and if any of them was the
  75                 // one and only selected toggle, then we will clear the selected
  76                 // toggle property.
  77                 for (Toggle t : c.getRemoved()) {
  78                     if (t.isSelected()) {
  79                         selectToggle(null);
  80                     }
  81                 }
  82 
  83                 // A Toggle can only be in one group at any one time. If the
  84                 // group is changed, then the toggle is removed from the old group prior to
  85                 // being added to the new group.


 140         }
 141     };
 142 
 143     /**
 144      * Selects the toggle.
 145      *
 146      * @param value The {@code Toggle} that is to be selected.
 147      */
 148     // Note that since selectedToggle is a read-only property, the selectToggle method is some
 149     // other method than setSelectedToggle, even though it is in essence doing the same thing
 150     public final void selectToggle(Toggle value) { selectedToggle.set(value); }
 151 
 152     /**
 153      * Gets the selected {@code Toggle}.
 154      * @return Toggle The selected toggle.
 155      */
 156     public final Toggle getSelectedToggle() { return selectedToggle.get(); }
 157 
 158     /**
 159      * The selected toggle.
 160      * @return the selected toggle
 161      */
 162     public final ReadOnlyObjectProperty<Toggle> selectedToggleProperty() { return selectedToggle.getReadOnlyProperty(); }
 163 
 164     private boolean setSelected(Toggle toggle, boolean selected) {
 165         if (toggle != null &&
 166                 toggle.getToggleGroup() == this &&
 167                 !toggle.selectedProperty().isBound()) {
 168             toggle.setSelected(selected);
 169             return true;
 170         }
 171         return false;
 172     }
 173 
 174     // Clear the selected toggle only if there are no other toggles selected.
 175     final void clearSelectedToggle() {
 176         if (!selectedToggle.getValue().isSelected()) {
 177              for (Toggle toggle: getToggles()) {
 178                  if (toggle.isSelected()) {
 179                      return;
 180                  }


< prev index next >