193 //System.err.println("GetBestScreenBounds p: "+ p.x + ", " + p.y); |
195 //System.err.println("GetBestScreenBounds p: "+ p.x + ", " + p.y); |
194 final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
196 final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
195 final GraphicsDevice[] gs = ge.getScreenDevices(); |
197 final GraphicsDevice[] gs = ge.getScreenDevices(); |
196 //System.err.println(" gs.length = " + gs.length); |
198 //System.err.println(" gs.length = " + gs.length); |
197 final Rectangle comboBoxBounds = comboBox.getBounds(); |
199 final Rectangle comboBoxBounds = comboBox.getBounds(); |
198 if (gs.length == 1) { |
|
199 final Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize(); |
|
200 |
|
201 //System.err.println(" scrSize: "+ scrSize); |
|
202 |
|
203 // If the combo box is totally off screen, don't show a popup |
|
204 if ((p.x + comboBoxBounds.width < 0) || (p.y + comboBoxBounds.height < 0) || (p.x > scrSize.width) || (p.y > scrSize.height)) { |
|
205 return null; |
|
206 } |
|
207 Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(comboBox.getGraphicsConfiguration()); |
|
208 return new Rectangle(0, insets.top, scrSize.width, scrSize.height - insets.top - insets.bottom); |
|
209 } |
|
210 |
200 |
211 for (final GraphicsDevice gd : gs) { |
201 for (final GraphicsDevice gd : gs) { |
212 final GraphicsConfiguration[] gc = gd.getConfigurations(); |
202 final GraphicsConfiguration[] gc = gd.getConfigurations(); |
213 for (final GraphicsConfiguration element0 : gc) { |
203 for (final GraphicsConfiguration element0 : gc) { |
214 final Rectangle gcBounds = element0.getBounds(); |
204 final Rectangle gcBounds = element0.getBounds(); |
215 if (gcBounds.contains(p)) return gcBounds; |
205 if (gcBounds.contains(p)) { |
|
206 return getAvailableScreenArea(gcBounds, element0); |
|
207 } |
216 } |
208 } |
217 } |
209 } |
218 |
210 |
219 // Hmm. Origin's off screen, but is any part on? |
211 // Hmm. Origin's off screen, but is any part on? |
220 comboBoxBounds.setLocation(p); |
212 comboBoxBounds.setLocation(p); |
221 for (final GraphicsDevice gd : gs) { |
213 for (final GraphicsDevice gd : gs) { |
222 final GraphicsConfiguration[] gc = gd.getConfigurations(); |
214 final GraphicsConfiguration[] gc = gd.getConfigurations(); |
223 for (final GraphicsConfiguration element0 : gc) { |
215 for (final GraphicsConfiguration element0 : gc) { |
224 final Rectangle gcBounds = element0.getBounds(); |
216 final Rectangle gcBounds = element0.getBounds(); |
225 if (gcBounds.intersects(comboBoxBounds)) return gcBounds; |
217 if (gcBounds.intersects(comboBoxBounds)) { |
|
218 if (gcBounds.contains(p)) { |
|
219 return getAvailableScreenArea(gcBounds, element0); |
|
220 } |
|
221 } |
226 } |
222 } |
227 } |
223 } |
228 |
224 |
229 return null; |
225 return null; |
|
226 } |
|
227 |
|
228 private Rectangle getAvailableScreenArea(Rectangle bounds, |
|
229 GraphicsConfiguration gc) { |
|
230 Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc); |
|
231 return new Rectangle(0, insets.top, bounds.width, |
|
232 bounds.height - insets.top); |
230 } |
233 } |
231 |
234 |
232 @Override |
235 @Override |
233 protected Rectangle computePopupBounds(int px, int py, int pw, int ph) { |
236 protected Rectangle computePopupBounds(int px, int py, int pw, int ph) { |
234 final int itemCount = comboBox.getModel().getSize(); |
237 final int itemCount = comboBox.getModel().getSize(); |