2626package javafx .scene .control ;
2727
2828import com .sun .javafx .beans .IDProperty ;
29+ import com .sun .javafx .collections .TrackableObservableList ;
30+ import com .sun .javafx .util .Utils ;
31+
2932import javafx .beans .property .ObjectProperty ;
3033import javafx .beans .property .ObjectPropertyBase ;
3134import javafx .collections .ListChangeListener .Change ;
3235import javafx .collections .ObservableList ;
3336import javafx .event .ActionEvent ;
3437import javafx .event .Event ;
3538import javafx .event .EventHandler ;
36- import javafx .geometry .Bounds ;
37- import javafx .geometry .NodeOrientation ;
39+ import javafx .geometry .HPos ;
40+ import javafx .geometry .Point2D ;
3841import javafx .geometry .Side ;
42+ import javafx .geometry .VPos ;
3943import javafx .scene .Node ;
4044import javafx .scene .Scene ;
41- import javafx .stage .Window ;
42-
43- import com .sun .javafx .collections .TrackableObservableList ;
4445import javafx .scene .control .skin .ContextMenuSkin ;
46+ import javafx .stage .Window ;
4547
4648/**
4749 * <p>
@@ -244,55 +246,19 @@ public void show(Node anchor, Side side, double dx, double dy) {
244246 if (getItems ().size () == 0 ) return ;
245247
246248 getScene ().setNodeOrientation (anchor .getEffectiveNodeOrientation ());
247- setAnchorLocation (getAnchorLocation (side , anchor .getEffectiveNodeOrientation ()));
248- Bounds anchorBounds = anchor .localToScreen (anchor .getLayoutBounds ());
249- double x = getXBySide (anchorBounds , side , anchor .getEffectiveNodeOrientation ()) + dx ;
250- double y = getYBySide (anchorBounds , side ) + dy ;
251- doShow (anchor , x , y );
252- }
249+ if (getScene ().getStylesheets ().isEmpty ()) {
250+ getScene ().getStylesheets ().setAll (anchor .getScene ().getStylesheets ());
251+ }
253252
254- private AnchorLocation getAnchorLocation (Side side , NodeOrientation orientation ) {
255- if (orientation == NodeOrientation .RIGHT_TO_LEFT ) {
256- switch (side ) {
257- case TOP : return AnchorLocation .CONTENT_BOTTOM_RIGHT ;
258- case RIGHT : return AnchorLocation .CONTENT_TOP_RIGHT ;
259- case BOTTOM : return AnchorLocation .CONTENT_TOP_RIGHT ;
260- case LEFT : return AnchorLocation .CONTENT_TOP_LEFT ;
261- }
262- } else {
263- switch (side ) {
264- case TOP : return AnchorLocation .CONTENT_BOTTOM_LEFT ;
265- case RIGHT : return AnchorLocation .CONTENT_TOP_LEFT ;
266- case BOTTOM : return AnchorLocation .CONTENT_TOP_LEFT ;
267- case LEFT : return AnchorLocation .CONTENT_TOP_RIGHT ;
268- }
269- }
270- return AnchorLocation .CONTENT_TOP_LEFT ; // never reached
271- }
272-
273- private double getXBySide (Bounds anchorBounds , Side side , NodeOrientation orientation ) {
274- if (orientation == NodeOrientation .RIGHT_TO_LEFT ) {
275- if (side == Side .RIGHT ) {
276- return anchorBounds .getMinX ();
277- } else {
278- return anchorBounds .getMaxX ();
279- }
280- } else {
281- if (side == Side .RIGHT ) {
282- return anchorBounds .getMaxX ();
283- } else {
284- return anchorBounds .getMinX ();
285- }
286- }
287- }
288-
289- private double getYBySide (Bounds anchorBounds , Side side ) {
290- if (side == Side .BOTTOM ) {
291- return anchorBounds .getMaxY ();
292- } else {
293- return anchorBounds .getMinY ();
294- }
295- }
253+ HPos hpos = side == Side .LEFT ? HPos .LEFT : side == Side .RIGHT ? HPos .RIGHT : HPos .CENTER ;
254+ VPos vpos = side == Side .TOP ? VPos .TOP : side == Side .BOTTOM ? VPos .BOTTOM : VPos .CENTER ;
255+
256+ // translate from anchor/hpos/vpos/dx/dy into screenX/screenY
257+ Point2D point = Utils .pointRelativeTo (anchor ,
258+ prefWidth (-1 ), prefHeight (-1 ),
259+ hpos , vpos , dx , dy , true );
260+ doShow (anchor , point .getX (), point .getY ());
261+ }
296262
297263 /**
298264 * Shows the {@code ContextMenu} at the specified screen coordinates. If there
@@ -309,7 +275,6 @@ public void show(Node anchor, double screenX, double screenY) {
309275 if (anchor == null ) return ;
310276 if (getItems ().size () == 0 ) return ;
311277 getScene ().setNodeOrientation (anchor .getEffectiveNodeOrientation ());
312- setAnchorLocation (AnchorLocation .CONTENT_TOP_LEFT );
313278 doShow (anchor , screenX , screenY );
314279 }
315280
0 commit comments