author | sherman |
Tue, 30 Aug 2011 11:53:11 -0700 | |
changeset 10419 | 12c063b39232 |
parent 5506 | 202f599c92aa |
child 13604 | 31089af1a447 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2002, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package sun.awt.X11; |
|
26 |
||
27 |
import java.awt.*; |
|
28 |
import java.awt.peer.*; |
|
29 |
import java.awt.event.*; |
|
30 |
||
31 |
import java.lang.reflect.Field; |
|
32 |
import java.lang.reflect.Method; |
|
33 |
import java.lang.reflect.InvocationTargetException; |
|
34 |
import sun.awt.SunToolkit; |
|
35 |
||
36 |
public class XMenuItemPeer implements MenuItemPeer { |
|
37 |
||
38 |
/************************************************ |
|
39 |
* |
|
40 |
* Data members |
|
41 |
* |
|
42 |
************************************************/ |
|
43 |
||
44 |
/* |
|
45 |
* Primary members |
|
46 |
*/ |
|
47 |
||
48 |
/** |
|
49 |
* Window that this item belongs to. |
|
50 |
*/ |
|
51 |
private XBaseMenuWindow container; |
|
52 |
||
53 |
/** |
|
54 |
* Target MenuItem. Note that 'target' member |
|
55 |
* in XWindow is required for dispatching events. |
|
56 |
* This member is only used for accessing its fields |
|
57 |
* and firing ActionEvent & ItemEvent |
|
58 |
*/ |
|
59 |
private MenuItem target; |
|
60 |
||
61 |
/* |
|
62 |
* Mapping to window |
|
63 |
*/ |
|
64 |
||
65 |
/** |
|
66 |
* Rectange occupied by menu item in container's |
|
67 |
* coordinates. Filled by map(...) function from |
|
68 |
* XBaseMenuWindow.map() |
|
69 |
*/ |
|
70 |
private Rectangle bounds; |
|
71 |
||
72 |
/** |
|
73 |
* Point in container's coordinate system used as |
|
74 |
* origin by drawText. |
|
75 |
*/ |
|
76 |
private Point textOrigin; |
|
77 |
||
78 |
/* |
|
79 |
* Size constants |
|
80 |
*/ |
|
81 |
private final static int SEPARATOR_WIDTH = 20; |
|
82 |
private final static int SEPARATOR_HEIGHT = 5; |
|
83 |
||
84 |
/* |
|
85 |
* MenuItem's fields & methods |
|
86 |
*/ |
|
87 |
private final static Field f_enabled; |
|
88 |
private final static Field f_label; |
|
89 |
private final static Field f_shortcut; |
|
90 |
private final static Method m_getFont; |
|
91 |
private final static Method m_isItemEnabled; |
|
92 |
private final static Method m_getActionCommand; |
|
93 |
static { |
|
94 |
f_enabled = SunToolkit.getField(MenuItem.class, "enabled"); |
|
95 |
f_label = SunToolkit.getField(MenuItem.class, "label"); |
|
96 |
f_shortcut = SunToolkit.getField(MenuItem.class, "shortcut"); |
|
97 |
||
98 |
m_getFont = SunToolkit.getMethod(MenuComponent.class, "getFont_NoClientCode", null); |
|
99 |
m_getActionCommand = SunToolkit.getMethod(MenuItem.class, "getActionCommandImpl", null); |
|
100 |
m_isItemEnabled = SunToolkit.getMethod(MenuItem.class, "isItemEnabled", null); |
|
101 |
} |
|
102 |
/************************************************ |
|
103 |
* |
|
104 |
* Text Metrics |
|
105 |
* |
|
106 |
************************************************/ |
|
107 |
||
108 |
/** |
|
109 |
* Text metrics are filled in calcTextMetrics function |
|
110 |
* and reset in resetTextMetrics function. Text metrics |
|
111 |
* contain calculated dimensions of various components of |
|
112 |
* menu item. |
|
113 |
*/ |
|
114 |
private TextMetrics textMetrics; |
|
115 |
||
116 |
static class TextMetrics implements Cloneable { |
|
117 |
/* |
|
118 |
* Calculated text size members |
|
119 |
*/ |
|
120 |
private Dimension textDimension; |
|
121 |
private int shortcutWidth; |
|
122 |
private int textBaseline; |
|
123 |
||
124 |
TextMetrics(Dimension textDimension, int shortcutWidth, int textBaseline) { |
|
125 |
this.textDimension = textDimension; |
|
126 |
this.shortcutWidth = shortcutWidth; |
|
127 |
this.textBaseline = textBaseline; |
|
128 |
} |
|
129 |
||
130 |
public Object clone() { |
|
131 |
try { |
|
132 |
return super.clone(); |
|
133 |
} catch (CloneNotSupportedException ex) { |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
5506
diff
changeset
|
134 |
throw new InternalError(ex); |
2 | 135 |
} |
136 |
} |
|
137 |
||
138 |
Dimension getTextDimension() { |
|
139 |
return this.textDimension; |
|
140 |
} |
|
141 |
||
142 |
int getShortcutWidth() { |
|
143 |
return this.shortcutWidth; |
|
144 |
} |
|
145 |
||
146 |
int getTextBaseline() { |
|
147 |
return this.textBaseline; |
|
148 |
} |
|
149 |
} |
|
150 |
||
151 |
/************************************************ |
|
152 |
* |
|
153 |
* Construction |
|
154 |
* |
|
155 |
************************************************/ |
|
156 |
XMenuItemPeer(MenuItem target) { |
|
157 |
this.target = target; |
|
158 |
} |
|
159 |
||
160 |
/************************************************ |
|
161 |
* |
|
162 |
* Implementaion of interface methods |
|
163 |
* |
|
164 |
************************************************/ |
|
165 |
||
166 |
/* |
|
167 |
* From MenuComponentPeer |
|
168 |
*/ |
|
169 |
public void dispose() { |
|
170 |
//Empty function |
|
171 |
} |
|
172 |
||
173 |
public void setFont(Font font) { |
|
174 |
resetTextMetrics(); |
|
175 |
repaintIfShowing(); |
|
176 |
} |
|
177 |
/* |
|
178 |
* From MenuItemPeer |
|
179 |
*/ |
|
180 |
public void setLabel(String label) { |
|
181 |
resetTextMetrics(); |
|
182 |
repaintIfShowing(); |
|
183 |
} |
|
184 |
||
185 |
public void setEnabled(boolean enabled) { |
|
186 |
repaintIfShowing(); |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* DEPRECATED: Replaced by setEnabled(boolean). |
|
191 |
* @see java.awt.peer.MenuItemPeer |
|
192 |
*/ |
|
193 |
public void enable() { |
|
194 |
setEnabled( true ); |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* DEPRECATED: Replaced by setEnabled(boolean). |
|
199 |
* @see java.awt.peer.MenuItemPeer |
|
200 |
*/ |
|
201 |
public void disable() { |
|
202 |
setEnabled( false ); |
|
203 |
} |
|
204 |
||
205 |
/************************************************ |
|
206 |
* |
|
207 |
* Access to target's fields |
|
208 |
* |
|
209 |
************************************************/ |
|
210 |
||
211 |
MenuItem getTarget() { |
|
212 |
return this.target; |
|
213 |
} |
|
214 |
||
215 |
Font getTargetFont() { |
|
216 |
if (target == null) { |
|
116
9c43d9eb1029
6645856: static field XWindowPeer.defaultFont hides XWindow.defaultFont
son
parents:
2
diff
changeset
|
217 |
return XWindow.getDefaultFont(); |
2 | 218 |
} |
219 |
try { |
|
220 |
return (Font)m_getFont.invoke(target, new Object[0]); |
|
221 |
} catch (IllegalAccessException e) { |
|
222 |
e.printStackTrace(); |
|
223 |
} catch (InvocationTargetException e) { |
|
224 |
e.printStackTrace(); |
|
225 |
} |
|
116
9c43d9eb1029
6645856: static field XWindowPeer.defaultFont hides XWindow.defaultFont
son
parents:
2
diff
changeset
|
226 |
return XWindow.getDefaultFont(); |
2 | 227 |
} |
228 |
||
229 |
String getTargetLabel() { |
|
230 |
if (target == null) { |
|
231 |
return ""; |
|
232 |
} |
|
233 |
try { |
|
234 |
String label = (String)f_label.get(target); |
|
235 |
return (label == null) ? "" : label; |
|
236 |
} catch (IllegalAccessException e) { |
|
237 |
e.printStackTrace(); |
|
238 |
} |
|
239 |
return ""; |
|
240 |
} |
|
241 |
||
242 |
boolean isTargetEnabled() { |
|
243 |
if (target == null) { |
|
244 |
return false; |
|
245 |
} |
|
246 |
try { |
|
247 |
return f_enabled.getBoolean(target); |
|
248 |
} catch (IllegalAccessException e) { |
|
249 |
e.printStackTrace(); |
|
250 |
} |
|
251 |
return false; |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Returns true if item and all its parents are enabled |
|
256 |
* This function is used to fix |
|
257 |
* 6184485: Popup menu is not disabled on XToolkit even when calling setEnabled (false) |
|
258 |
*/ |
|
259 |
boolean isTargetItemEnabled() { |
|
260 |
if (target == null) { |
|
261 |
return false; |
|
262 |
} |
|
263 |
try { |
|
264 |
return ((Boolean)m_isItemEnabled.invoke(target, new Object[0])).booleanValue(); |
|
265 |
} catch (IllegalAccessException e) { |
|
266 |
e.printStackTrace(); |
|
267 |
} catch (InvocationTargetException e) { |
|
268 |
e.printStackTrace(); |
|
269 |
} |
|
270 |
return false; |
|
271 |
} |
|
272 |
||
273 |
String getTargetActionCommand() { |
|
274 |
if (target == null) { |
|
275 |
return ""; |
|
276 |
} |
|
277 |
try { |
|
278 |
return (String) m_getActionCommand.invoke(target,(Object[]) null); |
|
279 |
} catch (IllegalAccessException e) { |
|
280 |
e.printStackTrace(); |
|
281 |
} catch (InvocationTargetException e) { |
|
282 |
e.printStackTrace(); |
|
283 |
} |
|
284 |
return ""; |
|
285 |
} |
|
286 |
||
287 |
MenuShortcut getTargetShortcut() { |
|
288 |
if (target == null) { |
|
289 |
return null; |
|
290 |
} |
|
291 |
try { |
|
292 |
return (MenuShortcut)f_shortcut.get(target); |
|
293 |
} catch (IllegalAccessException e) { |
|
294 |
e.printStackTrace(); |
|
295 |
} |
|
296 |
return null; |
|
297 |
} |
|
298 |
||
299 |
String getShortcutText() { |
|
300 |
//Fix for 6180413: shortcuts should not be displayed for any of the menuitems in a popup menu |
|
301 |
if (container == null) { |
|
302 |
return null; |
|
303 |
} |
|
304 |
if (container.getRootMenuWindow() instanceof XPopupMenuPeer) { |
|
305 |
return null; |
|
306 |
} |
|
307 |
MenuShortcut sc = getTargetShortcut(); |
|
308 |
//TODO:This can potentially call user code |
|
309 |
return (sc == null) ? null : sc.toString(); |
|
310 |
} |
|
311 |
||
312 |
||
313 |
/************************************************ |
|
314 |
* |
|
315 |
* Basic manipulations |
|
316 |
* |
|
317 |
************************************************/ |
|
318 |
||
319 |
/** |
|
320 |
* This function is called when filling item vectors |
|
321 |
* in XMenuWindow & XMenuBar. We need it because peers |
|
322 |
* are created earlier than windows. |
|
323 |
* @param container the window that this item belongs to. |
|
324 |
*/ |
|
325 |
void setContainer(XBaseMenuWindow container) { |
|
326 |
synchronized(XBaseMenuWindow.getMenuTreeLock()) { |
|
327 |
this.container = container; |
|
328 |
} |
|
329 |
} |
|
330 |
||
331 |
/** |
|
332 |
* returns the window that this item belongs to |
|
333 |
*/ |
|
334 |
XBaseMenuWindow getContainer() { |
|
335 |
return this.container; |
|
336 |
} |
|
337 |
||
338 |
/************************************************ |
|
339 |
* |
|
340 |
* Overridable behaviour |
|
341 |
* |
|
342 |
************************************************/ |
|
343 |
||
344 |
/** |
|
345 |
* This function should be overriden simply to |
|
346 |
* return false in inherited classes. |
|
347 |
*/ |
|
348 |
boolean isSeparator() { |
|
349 |
boolean r = (getTargetLabel().equals("-")); |
|
350 |
return r; |
|
351 |
} |
|
352 |
||
353 |
/************************************************ |
|
354 |
* |
|
355 |
* Utility functions |
|
356 |
* |
|
357 |
************************************************/ |
|
358 |
||
359 |
/** |
|
360 |
* Returns true if container exists and is showing |
|
361 |
*/ |
|
362 |
boolean isContainerShowing() { |
|
363 |
if (container == null) { |
|
364 |
return false; |
|
365 |
} |
|
366 |
return container.isShowing(); |
|
367 |
} |
|
368 |
||
369 |
/** |
|
370 |
* Repaints item if it is showing |
|
371 |
*/ |
|
372 |
void repaintIfShowing() { |
|
373 |
if (isContainerShowing()) { |
|
374 |
container.postPaintEvent(); |
|
375 |
} |
|
376 |
} |
|
377 |
||
378 |
/** |
|
379 |
* This function is invoked when the user clicks |
|
380 |
* on menu item. |
|
381 |
* @param when the timestamp of action event |
|
382 |
*/ |
|
383 |
void action(long when) { |
|
384 |
if (!isSeparator() && isTargetItemEnabled()) { |
|
385 |
XWindow.postEventStatic(new ActionEvent(target, ActionEvent.ACTION_PERFORMED, |
|
386 |
getTargetActionCommand(), when, |
|
387 |
0)); |
|
388 |
} |
|
389 |
} |
|
390 |
/************************************************ |
|
391 |
* |
|
392 |
* Text metrics |
|
393 |
* |
|
394 |
************************************************/ |
|
395 |
||
396 |
/** |
|
397 |
* Returns text metrics of menu item. |
|
398 |
* This function does not use any locks |
|
399 |
* and is guaranteed to return some value |
|
400 |
* (possibly actual, possibly expired) |
|
401 |
*/ |
|
402 |
TextMetrics getTextMetrics() { |
|
403 |
TextMetrics textMetrics = this.textMetrics; |
|
404 |
if (textMetrics == null) { |
|
405 |
textMetrics = calcTextMetrics(); |
|
406 |
this.textMetrics = textMetrics; |
|
407 |
} |
|
408 |
return textMetrics; |
|
409 |
} |
|
410 |
||
411 |
/** |
|
412 |
* Returns dimensions of item's label. |
|
413 |
* This function does not use any locks |
|
414 |
* Returns actual or expired value |
|
415 |
* or null if error occurs |
|
416 |
*/ |
|
417 |
/*Dimension getTextDimension() { |
|
418 |
TextMetrics textMetrics = this.textMetrics; |
|
419 |
if (textMetrics == null) { |
|
420 |
textMetrics = calcTextMetrics(); |
|
421 |
this.textMetrics = textMetrics; |
|
422 |
} |
|
423 |
return (textMetrics != null) ? textMetrics.textDimension : null; |
|
424 |
}*/ |
|
425 |
||
426 |
/** |
|
427 |
* Returns width of item's shortcut label, |
|
428 |
* 0 if item has no shortcut. |
|
429 |
* The height of shortcut can be deternimed |
|
430 |
* from text dimensions. |
|
431 |
* This function does not use any locks |
|
432 |
* and is guaranteed to return some value |
|
433 |
* (possibly actual, possibly expired) |
|
434 |
*/ |
|
435 |
/*int getShortcutWidth() { |
|
436 |
TextMetrics textMetrics = this.textMetrics; |
|
437 |
if (textMetrics == null) { |
|
438 |
textMetrics = calcTextMetrics(); |
|
439 |
this.textMetrics = textMetrics; |
|
440 |
} |
|
441 |
return (textMetrics != null) ? textMetrics.shortcutWidth : 0; |
|
442 |
} |
|
443 |
||
444 |
int getTextBaseline() { |
|
445 |
TextMetrics textMetrics = this.textMetrics; |
|
446 |
if (textMetrics == null) { |
|
447 |
textMetrics = calcTextMetrics(); |
|
448 |
this.textMetrics = textMetrics; |
|
449 |
} |
|
450 |
return (textMetrics != null) ? textMetrics.textBaseline : 0; |
|
451 |
}*/ |
|
452 |
||
453 |
TextMetrics calcTextMetrics() { |
|
454 |
if (container == null) { |
|
455 |
return null; |
|
456 |
} |
|
457 |
if (isSeparator()) { |
|
458 |
return new TextMetrics(new Dimension(SEPARATOR_WIDTH, SEPARATOR_HEIGHT), 0, 0); |
|
459 |
} |
|
460 |
Graphics g = container.getGraphics(); |
|
461 |
if (g == null) { |
|
462 |
return null; |
|
463 |
} |
|
464 |
try { |
|
465 |
g.setFont(getTargetFont()); |
|
466 |
FontMetrics fm = g.getFontMetrics(); |
|
467 |
String str = getTargetLabel(); |
|
468 |
int width = fm.stringWidth(str); |
|
469 |
int height = fm.getHeight(); |
|
470 |
Dimension textDimension = new Dimension(width, height); |
|
471 |
int textBaseline = fm.getHeight() - fm.getAscent(); |
|
472 |
String sc = getShortcutText(); |
|
473 |
int shortcutWidth = (sc == null) ? 0 : fm.stringWidth(sc); |
|
474 |
return new TextMetrics(textDimension, shortcutWidth, textBaseline); |
|
475 |
} finally { |
|
476 |
g.dispose(); |
|
477 |
} |
|
478 |
} |
|
479 |
||
480 |
void resetTextMetrics() { |
|
481 |
textMetrics = null; |
|
482 |
if (container != null) { |
|
483 |
container.updateSize(); |
|
484 |
} |
|
485 |
} |
|
486 |
||
487 |
/************************************************ |
|
488 |
* |
|
489 |
* Mapping utility functions |
|
490 |
* |
|
491 |
************************************************/ |
|
492 |
||
493 |
/** |
|
494 |
* Sets mapping of item to window. |
|
495 |
* @param bounds bounds of item in container's coordinates |
|
496 |
* @param textOrigin point for drawString in container's coordinates |
|
497 |
* @see XBaseMenuWindow.map() |
|
498 |
*/ |
|
499 |
void map(Rectangle bounds, Point textOrigin) { |
|
500 |
this.bounds = bounds; |
|
501 |
this.textOrigin = textOrigin; |
|
502 |
} |
|
503 |
||
504 |
/** |
|
505 |
* returns bounds of item that were previously set by map() function |
|
506 |
*/ |
|
507 |
Rectangle getBounds() { |
|
508 |
return bounds; |
|
509 |
} |
|
510 |
||
511 |
/** |
|
512 |
* returns origin of item's text that was previously set by map() function |
|
513 |
*/ |
|
514 |
Point getTextOrigin() { |
|
515 |
return textOrigin; |
|
516 |
} |
|
517 |
||
518 |
} |