Method largely borrowed from Swing's JMenu. Computes the origin for the JMenu 's popup menu. This method uses Look and Feel properties named Menu.menuPopupOffsetX , Menu.menuPopupOffsetY , Menu.submenuPopupOffsetX , and Menu.submenuPopupOffsetY<
()
| 95 | * <code>JMenu</code>'s popup menu |
| 96 | */ |
| 97 | protected Point getPopupMenuOrigin() { |
| 98 | int x = 0; |
| 99 | int y = 0; |
| 100 | // Figure out the sizes needed to caclulate the menu position |
| 101 | Dimension s = getSize(); |
| 102 | Dimension pmSize = popup.getSize(); |
| 103 | // For the first time the menu is popped up, |
| 104 | // the size has not yet been initiated |
| 105 | if(pmSize.width == 0) { |
| 106 | pmSize = popup.getPreferredSize(); |
| 107 | } |
| 108 | Point position = getLocationOnScreen(); |
| 109 | Toolkit toolkit = Toolkit.getDefaultToolkit(); |
| 110 | GraphicsConfiguration gc = getGraphicsConfiguration(); |
| 111 | Rectangle screenBounds = new Rectangle(toolkit.getScreenSize()); |
| 112 | GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
| 113 | GraphicsDevice[] gd = ge.getScreenDevices(); |
| 114 | for(int i = 0; i < gd.length; i++) { |
| 115 | if(gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) { |
| 116 | GraphicsConfiguration dgc = gd[i].getDefaultConfiguration(); |
| 117 | if(dgc.getBounds().contains(position)) { |
| 118 | gc = dgc; |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if(gc != null) { |
| 125 | screenBounds = gc.getBounds(); |
| 126 | // take screen insets (e.g. taskbar) into account |
| 127 | Insets screenInsets = toolkit.getScreenInsets(gc); |
| 128 | |
| 129 | screenBounds.width -= Math.abs(screenInsets.left + screenInsets.right); |
| 130 | screenBounds.height -= Math.abs(screenInsets.top + screenInsets.bottom); |
| 131 | position.x -= Math.abs(screenInsets.left); |
| 132 | position.y -= Math.abs(screenInsets.top); |
| 133 | } |
| 134 | |
| 135 | // We are a toplevel menu (pull-down) |
| 136 | int xOffset = UIManager.getInt("Menu.menuPopupOffsetX"); |
| 137 | int yOffset = UIManager.getInt("Menu.menuPopupOffsetY"); |
| 138 | |
| 139 | if(getComponentOrientation().isLeftToRight()) { |
| 140 | // First determine the x: |
| 141 | x = xOffset; // Extend to the right |
| 142 | if(position.x + x + pmSize.width >= screenBounds.width + screenBounds.x && |
| 143 | // popup doesn't fit - place it wherever there's more |
| 144 | // room |
| 145 | screenBounds.width - s.width < 2 * (position.x - screenBounds.x)) { |
| 146 | |
| 147 | x = s.width - xOffset - pmSize.width; |
| 148 | } |
| 149 | } |
| 150 | else { |
| 151 | // First determine the x: |
| 152 | x = s.width - xOffset - pmSize.width; // Extend to the left |
| 153 | if(position.x + x < screenBounds.x && |
| 154 | // popup doesn't fit - place it wherever there's more room |
no test coverage detected