26
26
package javafx .scene .control ;
27
27
28
28
import com .sun .javafx .beans .IDProperty ;
29
+ import com .sun .javafx .collections .TrackableObservableList ;
30
+ import com .sun .javafx .util .Utils ;
31
+
29
32
import javafx .beans .property .ObjectProperty ;
30
33
import javafx .beans .property .ObjectPropertyBase ;
31
34
import javafx .collections .ListChangeListener .Change ;
32
35
import javafx .collections .ObservableList ;
33
36
import javafx .event .ActionEvent ;
34
37
import javafx .event .Event ;
35
38
import javafx .event .EventHandler ;
36
- import javafx .geometry .Bounds ;
37
- import javafx .geometry .NodeOrientation ;
39
+ import javafx .geometry .HPos ;
40
+ import javafx .geometry .Point2D ;
38
41
import javafx .geometry .Side ;
42
+ import javafx .geometry .VPos ;
39
43
import javafx .scene .Node ;
40
44
import javafx .scene .Scene ;
41
- import javafx .stage .Window ;
42
-
43
- import com .sun .javafx .collections .TrackableObservableList ;
44
45
import javafx .scene .control .skin .ContextMenuSkin ;
46
+ import javafx .stage .Window ;
45
47
46
48
/**
47
49
* <p>
@@ -244,55 +246,19 @@ public void show(Node anchor, Side side, double dx, double dy) {
244
246
if (getItems ().size () == 0 ) return ;
245
247
246
248
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
+ }
253
252
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
+ }
296
262
297
263
/**
298
264
* Shows the {@code ContextMenu} at the specified screen coordinates. If there
@@ -309,7 +275,6 @@ public void show(Node anchor, double screenX, double screenY) {
309
275
if (anchor == null ) return ;
310
276
if (getItems ().size () == 0 ) return ;
311
277
getScene ().setNodeOrientation (anchor .getEffectiveNodeOrientation ());
312
- setAnchorLocation (AnchorLocation .CONTENT_TOP_LEFT );
313
278
doShow (anchor , screenX , screenY );
314
279
}
315
280
0 commit comments