< prev index next >

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

Print this page




 131 
 132 
 133 
 134     /***************************************************************************
 135      *                                                                         *
 136      * Constructors                                                            *
 137      *                                                                         *
 138      **************************************************************************/
 139 
 140     /**
 141      * Create a new ContextMenu
 142      */
 143     public ContextMenu() {
 144         getStyleClass().setAll(DEFAULT_STYLE_CLASS);
 145         setAutoHide(true);
 146         setConsumeAutoHidingEvents(false);
 147     }
 148 
 149     /**
 150      * Create a new ContextMenu initialized with the given items

 151      */
 152     public ContextMenu(MenuItem... items) {
 153         this();
 154         this.items.addAll(items);
 155     }
 156 
 157 
 158 
 159     /***************************************************************************
 160      *                                                                         *
 161      * Properties                                                              *
 162      *                                                                         *
 163      **************************************************************************/
 164 
 165     /**
 166      * Callback function to be informed when an item contained within this
 167      * {@code ContextMenu} has been activated. The current implementation informs
 168      * all parent menus as well, so that it is not necessary to listen to all
 169      * sub menus for events.
 170      */


 200                         // instances
 201                         item.getParentPopup().getItems().remove(item);
 202                     }
 203                     item.setParentPopup(ContextMenu.this);
 204                 }
 205             }
 206         }
 207     };
 208 
 209 
 210 
 211     /***************************************************************************
 212      *                                                                         *
 213      * Public API                                                              *
 214      *                                                                         *
 215      **************************************************************************/
 216 
 217     /**
 218      * The menu items on the context menu. If this ObservableList is modified at
 219      * runtime, the ContextMenu will update as expected.

 220      * @see MenuItem
 221      */
 222     public final ObservableList<MenuItem> getItems() { return items; }
 223 
 224     /**
 225      * Shows the {@code ContextMenu} relative to the given anchor node, on the side
 226      * specified by the {@code hpos} and {@code vpos} parameters, and offset
 227      * by the given {@code dx} and {@code dy} values for the x-axis and y-axis, respectively.
 228      * If there is not enough room, the menu is moved to the opposite side and
 229      * the offset is not applied.
 230      * <p>
 231      * To clarify the purpose of the {@code hpos} and {@code vpos} parameters,
 232      * consider that they are relative to the anchor node. As such, a {@code hpos}
 233      * and {@code vpos} of {@code CENTER} would mean that the ContextMenu appears
 234      * on top of the anchor, with the (0,0) position of the {@code ContextMenu}
 235      * positioned at (0,0) of the anchor. A {@code hpos} of right would then shift
 236      * the {@code ContextMenu} such that its top-left (0,0) position would be attached
 237      * to the top-right position of the anchor.
 238      * <p>
 239      * This function is useful for finely tuning the position of a menu,
 240      * relative to the parent node to ensure close alignment.




 241      */
 242     // TODO provide more detail
 243      public void show(Node anchor, Side side, double dx, double dy) {
 244         if (anchor == null) return;
 245         if (getItems().size() == 0) return;
 246 
 247         getScene().setNodeOrientation(anchor.getEffectiveNodeOrientation());
 248         // FIXME because Side is not yet in javafx.geometry, we have to convert
 249         // to the old HPos/VPos API here, as Utils can not refer to Side in the
 250         // charting API.
 251         HPos hpos = side == Side.LEFT ? HPos.LEFT : side == Side.RIGHT ? HPos.RIGHT : HPos.CENTER;
 252         VPos vpos = side == Side.TOP ? VPos.TOP : side == Side.BOTTOM ? VPos.BOTTOM : VPos.CENTER;
 253 
 254         // translate from anchor/hpos/vpos/dx/dy into screenX/screenY
 255         Point2D point = Utils.pointRelativeTo(anchor,
 256                 prefWidth(-1), prefHeight(-1),
 257                 hpos, vpos, dx, dy, true);
 258         doShow(anchor, point.getX(), point.getY());
 259     }
 260 
 261      /**
 262      * Shows the {@code ContextMenu} at the specified screen coordinates. If there
 263      * is not enough room at the specified location to show the {@code ContextMenu}
 264      * given its size requirements, the necessary adjustments are made to bring
 265      * the {@code ContextMenu} back back on screen. This also means that the
 266      * {@code ContextMenu} will not span multiple monitors.



 267      */

 268     public void show(Node anchor, double screenX, double screenY) {
 269         if (anchor == null) return;
 270         if (getItems().size() == 0) return;
 271         getScene().setNodeOrientation(anchor.getEffectiveNodeOrientation());
 272         doShow(anchor, screenX, screenY);
 273     }
 274 
 275     /**
 276      * Hides this {@code ContextMenu} and any visible submenus, assuming that when this function
 277      * is called that the {@code ContextMenu} was showing.
 278      * <p>
 279      * If this {@code ContextMenu} is not showing, then nothing happens.
 280      */
 281     @Override public void hide() {
 282         if (!isShowing()) return;
 283         Event.fireEvent(this, new Event(Menu.ON_HIDING));
 284         super.hide();
 285         Event.fireEvent(this, new Event(Menu.ON_HIDDEN));
 286     }
 287 




 131 
 132 
 133 
 134     /***************************************************************************
 135      *                                                                         *
 136      * Constructors                                                            *
 137      *                                                                         *
 138      **************************************************************************/
 139 
 140     /**
 141      * Create a new ContextMenu
 142      */
 143     public ContextMenu() {
 144         getStyleClass().setAll(DEFAULT_STYLE_CLASS);
 145         setAutoHide(true);
 146         setConsumeAutoHidingEvents(false);
 147     }
 148 
 149     /**
 150      * Create a new ContextMenu initialized with the given items
 151      * @param items the list of menu items
 152      */
 153     public ContextMenu(MenuItem... items) {
 154         this();
 155         this.items.addAll(items);
 156     }
 157 
 158 
 159 
 160     /***************************************************************************
 161      *                                                                         *
 162      * Properties                                                              *
 163      *                                                                         *
 164      **************************************************************************/
 165 
 166     /**
 167      * Callback function to be informed when an item contained within this
 168      * {@code ContextMenu} has been activated. The current implementation informs
 169      * all parent menus as well, so that it is not necessary to listen to all
 170      * sub menus for events.
 171      */


 201                         // instances
 202                         item.getParentPopup().getItems().remove(item);
 203                     }
 204                     item.setParentPopup(ContextMenu.this);
 205                 }
 206             }
 207         }
 208     };
 209 
 210 
 211 
 212     /***************************************************************************
 213      *                                                                         *
 214      * Public API                                                              *
 215      *                                                                         *
 216      **************************************************************************/
 217 
 218     /**
 219      * The menu items on the context menu. If this ObservableList is modified at
 220      * runtime, the ContextMenu will update as expected.
 221      * @return the menu items on this context menu
 222      * @see MenuItem
 223      */
 224     public final ObservableList<MenuItem> getItems() { return items; }
 225 
 226     /**
 227      * Shows the {@code ContextMenu} relative to the given anchor node, on the side
 228      * specified by the {@code hpos} and {@code vpos} parameters, and offset
 229      * by the given {@code dx} and {@code dy} values for the x-axis and y-axis, respectively.
 230      * If there is not enough room, the menu is moved to the opposite side and
 231      * the offset is not applied.
 232      * <p>
 233      * To clarify the purpose of the {@code hpos} and {@code vpos} parameters,
 234      * consider that they are relative to the anchor node. As such, a {@code hpos}
 235      * and {@code vpos} of {@code CENTER} would mean that the ContextMenu appears
 236      * on top of the anchor, with the (0,0) position of the {@code ContextMenu}
 237      * positioned at (0,0) of the anchor. A {@code hpos} of right would then shift
 238      * the {@code ContextMenu} such that its top-left (0,0) position would be attached
 239      * to the top-right position of the anchor.
 240      * <p>
 241      * This function is useful for finely tuning the position of a menu,
 242      * relative to the parent node to ensure close alignment.
 243      * @param anchor the anchor node
 244      * @param side the side
 245      * @param dx the dx value for the x-axis
 246      * @param dy the dy value for the y-axis
 247      */
 248     // TODO provide more detail
 249      public void show(Node anchor, Side side, double dx, double dy) {
 250         if (anchor == null) return;
 251         if (getItems().size() == 0) return;
 252 
 253         getScene().setNodeOrientation(anchor.getEffectiveNodeOrientation());
 254         // FIXME because Side is not yet in javafx.geometry, we have to convert
 255         // to the old HPos/VPos API here, as Utils can not refer to Side in the
 256         // charting API.
 257         HPos hpos = side == Side.LEFT ? HPos.LEFT : side == Side.RIGHT ? HPos.RIGHT : HPos.CENTER;
 258         VPos vpos = side == Side.TOP ? VPos.TOP : side == Side.BOTTOM ? VPos.BOTTOM : VPos.CENTER;
 259 
 260         // translate from anchor/hpos/vpos/dx/dy into screenX/screenY
 261         Point2D point = Utils.pointRelativeTo(anchor,
 262                 prefWidth(-1), prefHeight(-1),
 263                 hpos, vpos, dx, dy, true);
 264         doShow(anchor, point.getX(), point.getY());
 265     }
 266 
 267      /**
 268      * Shows the {@code ContextMenu} at the specified screen coordinates. If there
 269      * is not enough room at the specified location to show the {@code ContextMenu}
 270      * given its size requirements, the necessary adjustments are made to bring
 271      * the {@code ContextMenu} back back on screen. This also means that the
 272      * {@code ContextMenu} will not span multiple monitors.
 273      * @param anchor the anchor node
 274      * @param screenX the x position of the anchor in screen coordinates
 275      * @param screenY the y position of the anchor in screen coordinates
 276      */
 277     @Override
 278     public void show(Node anchor, double screenX, double screenY) {
 279         if (anchor == null) return;
 280         if (getItems().size() == 0) return;
 281         getScene().setNodeOrientation(anchor.getEffectiveNodeOrientation());
 282         doShow(anchor, screenX, screenY);
 283     }
 284 
 285     /**
 286      * Hides this {@code ContextMenu} and any visible submenus, assuming that when this function
 287      * is called that the {@code ContextMenu} was showing.
 288      * <p>
 289      * If this {@code ContextMenu} is not showing, then nothing happens.
 290      */
 291     @Override public void hide() {
 292         if (!isShowing()) return;
 293         Event.fireEvent(this, new Event(Menu.ON_HIDING));
 294         super.hide();
 295         Event.fireEvent(this, new Event(Menu.ON_HIDDEN));
 296     }
 297 


< prev index next >