Debian Codegraph @

src/com/jidesoft/plaf/eclipse/EclipseJideSplitButtonUI.java

raw
1 /*
2 * @(#)EclipseJideSplitButtonUI.java
3 *
4 * Copyright 2002 - 2004 JIDE Software Inc. All rights reserved.
5 */
6
7 package com.jidesoft.plaf.eclipse;
8
9 import com.jidesoft.icons.IconsFactory;
10 import com.jidesoft.plaf.UIDefaultsLookup;
11 import com.jidesoft.plaf.basic.LazyActionMap;
12 import com.jidesoft.plaf.basic.ThemePainter;
13 import com.jidesoft.plaf.basic.UIAction;
14 import com.jidesoft.swing.*;
15 import com.jidesoft.utils.SecurityUtils;
16
17 import javax.swing.*;
18 import javax.swing.event.MouseInputListener;
19 import javax.swing.plaf.ComponentUI;
20 import javax.swing.plaf.UIResource;
21 import javax.swing.plaf.basic.BasicGraphicsUtils;
22 import javax.swing.plaf.basic.BasicHTML;
23 import javax.swing.text.View;
24 import java.awt.*;
25 import java.awt.event.*;
26 import java.util.ArrayList;
27
28 /**
29 * EclipseJideSplitButtonUI implementation.
30 */
31 public class EclipseJideSplitButtonUI extends EclipseMenuUI {
32
33 protected ThemePainter _painter;
34
35 protected Color _shadowColor;
36 protected Color _darkShadowColor;
37 protected Color _highlight;
38 protected Color _lightHighlightColor;
39
40 protected int _splitButtonMargin = 12;
41 protected int _splitButtonMarginOnMenu = 20;
42
43 private FocusListener _focusListener;
44
45 private static final String propertyPrefix = "JideSplitButton";
46
47 @Override
48 protected String getPropertyPrefix() {
49 return propertyPrefix;
50 }
51
52 @SuppressWarnings({"UnusedDeclaration"})
53 public static ComponentUI createUI(JComponent c) {
54 return new EclipseJideSplitButtonUI();
55 }
56
57 @Override
58 protected void installDefaults() {
59 _painter = (ThemePainter) UIDefaultsLookup.get("Theme.painter");
60 _shadowColor = UIDefaultsLookup.getColor("controlShadow");
61 _darkShadowColor = UIDefaultsLookup.getColor("controlDkShadow");
62 _highlight = UIDefaultsLookup.getColor("controlHighlight");
63 _lightHighlightColor = UIDefaultsLookup.getColor("controlLtHighlight");
64 menuItem.setRolloverEnabled(true);
65
66 super.installDefaults();
67 }
68
69 @Override
70 protected void uninstallDefaults() {
71 _painter = null;
72 _shadowColor = null;
73 _highlight = null;
74 _lightHighlightColor = null;
75 _darkShadowColor = null;
76
77 super.uninstallDefaults();
78 }
79
80 @Override
81 protected void installListeners() {
82 super.installListeners();
83 if (_focusListener == null) {
84 _focusListener = new FocusListener() {
85 public void focusGained(FocusEvent e) {
86 menuItem.repaint();
87 }
88
89 public void focusLost(FocusEvent e) {
90 menuItem.repaint();
91 }
92 };
93 }
94 menuItem.addFocusListener(_focusListener);
95 }
96
97 @Override
98 protected void uninstallListeners() {
99 super.uninstallListeners();
100 if (_focusListener != null) {
101 menuItem.removeFocusListener(_focusListener);
102 }
103 }
104
105 /*
106 * Returns the ui that is of type <code>klass</code>, or null if one can not be found.
107 */
108 static Object getUIOfType(ComponentUI ui, Class klass) {
109 if (klass.isInstance(ui)) {
110 return ui;
111 }
112 return null;
113 }
114
115 /**
116 * Returns the InputMap for condition <code>condition</code>. Called as part of
117 * <code>installKeyboardActions</code>.
118 *
119 * @param condition the condition
120 * @param c the component
121 * @return the input map.
122 */
123 public InputMap getInputMap(int condition, JComponent c) {
124 if (condition == JComponent.WHEN_FOCUSED) {
125 EclipseJideSplitButtonUI ui = (EclipseJideSplitButtonUI) getUIOfType(
126 ((JideSplitButton) c).getUI(), EclipseJideSplitButtonUI.class);
127 if (ui != null) {
128 return (InputMap) UIDefaultsLookup.get(ui.getPropertyPrefix() + ".focusInputMap");
129 }
130 }
131 return null;
132 }
133
134 @Override
135 protected void installKeyboardActions() {
136 super.installKeyboardActions();
137 AbstractButton b = menuItem;
138
139 LazyActionMap.installLazyActionMap(b, EclipseJideSplitButtonUI.class,
140 "JideSplitButton.actionMap");
141
142 InputMap km = getInputMap(JComponent.WHEN_FOCUSED, b);
143
144 SwingUtilities.replaceUIInputMap(b, JComponent.WHEN_FOCUSED, km);
145 }
146
147 @Override
148 protected void uninstallKeyboardActions() {
149 AbstractButton b = menuItem;
150 SwingUtilities.replaceUIInputMap(b, JComponent.
151 WHEN_IN_FOCUSED_WINDOW, null);
152 SwingUtilities.replaceUIInputMap(b, JComponent.WHEN_FOCUSED, null);
153 SwingUtilities.replaceUIActionMap(b, null);
154 super.uninstallKeyboardActions();
155 }
156
157 @Override
158 protected MouseInputListener createMouseInputListener(JComponent c) {
159 return new MouseInputHandler();
160 }
161
162 /**
163 * Gets the bounds for the button part of the <code>JideSplitButton</code>.
164 *
165 * @param c the component. In this case, it is the <code>JideSplitButton</code>.
166 * @param orientation the orientation.
167 * @param width the width of the <code>JideSplitButton</code>
168 * @param height the height of the <code>JideSplitButton</code>.
169 * @return the bounds for the button part of the <code>JideSplitButton</code>.
170 */
171 protected Rectangle getButtonRect(JComponent c, int orientation, int width, int height) {
172 Rectangle rect;
173 if (orientation == SwingConstants.HORIZONTAL && c.getComponentOrientation().isLeftToRight()) {
174 rect = new Rectangle(0, 0, width - _splitButtonMargin, height);
175 }
176 else {
177 rect = new Rectangle(_splitButtonMargin - 1, 0, width - _splitButtonMargin, height);
178 }
179 return rect;
180 }
181
182 /**
183 * Gets the bounds for the drop down part of the <code>JideSplitButton</code>.
184 *
185 * @param c the component. In this case, it is the <code>JideSplitButton</code>.
186 * @param orientation the orientation.
187 * @param width the width of the <code>JideSplitButton</code>
188 * @param height the height of the <code>JideSplitButton</code>.
189 * @return the bounds for the drop down part of the <code>JideSplitButton</code>.
190 */
191 protected Rectangle getDropDownRect(JComponent c, int orientation, int width, int height) {
192 Object position = c.getClientProperty(JideButton.CLIENT_PROPERTY_SEGMENT_POSITION);
193 Rectangle rect;
194 if (c.getComponentOrientation().isLeftToRight()) {
195 rect = new Rectangle(width - _splitButtonMargin - 1 + getOffset(), 0, _splitButtonMargin - getOffset(), height);
196 }
197 else {
198 rect = new Rectangle(0, 0, _splitButtonMargin - getOffset(), height);
199 }
200 if (position == null || JideButton.SEGMENT_POSITION_ONLY.equals(position)) {
201 }
202 else if (JideButton.SEGMENT_POSITION_FIRST.equals(position)) {
203 if (orientation == SwingConstants.HORIZONTAL) {
204 rect.width++;
205 }
206 else {
207 rect.height++;
208 }
209 }
210 else if (JideButton.SEGMENT_POSITION_MIDDLE.equals(position)) {
211 if (orientation == SwingConstants.HORIZONTAL) {
212 rect.width++;
213 }
214 else {
215 rect.height++;
216 }
217 }
218 else if (JideButton.SEGMENT_POSITION_LAST.equals(position)) {
219 }
220 return rect;
221 }
222
223 protected void paintSunkenBorder(Graphics g, Rectangle b) {
224 Color old = g.getColor();
225 g.setColor(_shadowColor); // inner 3D border
226 g.drawLine(b.x, b.y, b.x + b.width - 1, b.y);
227 g.drawLine(b.x, b.y, b.x, b.y + b.height - 1);
228
229 g.setColor(_lightHighlightColor); // black drop shadow __|
230 g.drawLine(b.x, b.y + b.height - 1, b.x + b.width - 1, b.y + b.height - 1);
231 g.drawLine(b.x + b.width - 1, b.y, b.x + b.width - 1, b.y + b.height - 1);
232 g.setColor(old);
233 }
234
235 protected void paintSunken2Border(Graphics g, Rectangle b) {
236 Color old = g.getColor();
237 g.setColor(_darkShadowColor); // inner 3D border
238 g.drawLine(b.x, b.y, b.x + b.width - 2, b.y);
239 g.drawLine(b.x, b.y, b.x, b.y + b.height - 2);
240
241 g.setColor(_shadowColor); // inner 3D border
242 g.drawLine(b.x + 1, b.y + 1, b.x + b.width - 3, b.y + 1);
243 g.drawLine(b.x + 1, b.y + 1, b.x + 1, b.y + b.height - 3);
244
245 g.setColor(_lightHighlightColor); // black drop shadow __|
246 g.drawLine(b.x, b.y + b.height - 1, b.x + b.width - 1, b.y + b.height - 1);
247 g.drawLine(b.x + b.width - 1, b.x, b.x + b.width - 1, b.y + b.height - 1);
248 g.setColor(old);
249 }
250
251 protected void paintRaisedBorder(Graphics g, Rectangle b) {
252 Color old = g.getColor();
253 g.setColor(_lightHighlightColor); // inner 3D border
254 g.drawLine(b.x, b.y, b.x + b.width - 1, b.y);
255 g.drawLine(b.x, b.y, b.x, b.y + b.height - 1);
256
257 g.setColor(_shadowColor); // black drop shadow __|
258 g.drawLine(b.x, b.y + b.height - 1, b.x + b.width - 1, b.y + b.height - 1);
259 g.drawLine(b.x + b.width - 1, b.y, b.x + b.width - 1, b.y + b.height - 1);
260 g.setColor(old);
261 }
262
263 protected void paintRaised2Border(Graphics g, Rectangle b) {
264 Color old = g.getColor();
265 g.setColor(_lightHighlightColor); // inner 3D border
266 g.drawLine(b.x, b.y, b.x + b.width - 1, b.y);
267 g.drawLine(b.x, b.y, b.x, b.y + b.height - 1);
268
269 g.setColor(_shadowColor); // gray drop shadow __|
270 g.drawLine(b.x + 1, b.y + b.height - 2, b.x + b.width - 2, b.y + b.height - 2);
271 g.drawLine(b.x + b.width - 2, 1, b.x + b.width - 2, b.y + b.height - 2);
272
273 g.setColor(_darkShadowColor); // black drop shadow __|
274 g.drawLine(b.x, b.y + b.height - 1, b.x + b.width - 1, b.y + b.height - 1);
275 g.drawLine(b.x + b.width - 1, b.y, b.x + b.width - 1, b.y + b.height - 1);
276 g.setColor(old);
277 }
278
279 private Color getForegroundOfState(JMenuItem menuItem) {
280 int state = JideSwingUtilities.getButtonState(menuItem);
281 Color foreground = null;
282 if (menuItem instanceof ComponentStateSupport) {
283 foreground = ((ComponentStateSupport) menuItem).getForegroundOfState(state);
284 }
285 if (foreground == null || foreground instanceof UIResource) {
286 foreground = menuItem.getForeground();
287 }
288 return foreground;
289 }
290
291 @Override
292 protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) {
293 ButtonModel model = menuItem.getModel();
294 int menuWidth;
295 int menuHeight;
296 int orientation = JideSwingUtilities.getOrientationOf(menuItem);
297 if (orientation == SwingConstants.HORIZONTAL) {
298 menuWidth = menuItem.getWidth();
299 menuHeight = menuItem.getHeight();
300 }
301 else {
302 menuWidth = menuItem.getHeight();
303 menuHeight = menuItem.getWidth();
304 }
305 // have to change to HORIZONTAL because we rotate the Graphics already
306 orientation = SwingConstants.HORIZONTAL;
307
308 boolean paintBackground;
309 Object o = menuItem.getClientProperty("JideSplitButton.alwaysPaintBackground");
310 if (o instanceof Boolean) {
311 paintBackground = (Boolean) o;
312 }
313 else {
314 paintBackground = menuItem.isOpaque();
315 }
316
317 if (!((JMenu) menuItem).isTopLevelMenu()) {
318 super.paintBackground(g, menuItem, bgColor);
319 if (menuItem.isEnabled()) {
320 if (model.isArmed() || model.isPressed() || isMouseOver()) {
321 g.setColor(selectionForeground);
322 g.drawLine(menuWidth - _splitButtonMarginOnMenu, 0, menuWidth - _splitButtonMarginOnMenu, menuHeight - 2);
323 JideSwingUtilities.paintArrow(g, selectionForeground, menuWidth - _splitButtonMarginOnMenu / 2 - 2, menuHeight / 2 - 3, 7, SwingConstants.VERTICAL);
324 }
325 else {
326 g.setColor(getForegroundOfState(menuItem));
327 g.drawLine(menuWidth - _splitButtonMarginOnMenu, 0, menuWidth - _splitButtonMarginOnMenu, menuHeight - 2);
328 JideSwingUtilities.paintArrow(g, getForegroundOfState(menuItem), menuWidth - _splitButtonMarginOnMenu / 2 - 2, menuHeight / 2 - 3, 7, SwingConstants.VERTICAL);
329 }
330 }
331 else {
332 g.setColor(UIDefaultsLookup.getColor("controlDkShadow"));
333 g.drawLine(menuWidth - _splitButtonMarginOnMenu, 0, menuWidth - _splitButtonMarginOnMenu, menuHeight - 2);
334 JideSwingUtilities.paintArrow(g, UIDefaultsLookup.getColor("controlDkShadow"), menuWidth - _splitButtonMarginOnMenu / 2 - 2, menuHeight / 2 - 3, 7, SwingConstants.VERTICAL);
335 }
336 return;
337 }
338
339 if (paintBackground) {
340 if (menuItem.getParent() != null) {
341 g.setColor(menuItem.getParent().getBackground());
342 }
343 else {
344 g.setColor(menuItem.getBackground());
345 }
346 g.fillRect(0, 0, menuWidth, menuHeight);
347 }
348
349 JideSplitButton b = (JideSplitButton) menuItem;
350 if (b.getButtonStyle() == ButtonStyle.TOOLBAR_STYLE) {
351 if ((model.isSelected())) {
352 if (b.isAlwaysDropdown()) {
353 Rectangle rect = new Rectangle(0, 0, menuWidth, menuHeight);
354 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
355 }
356 else if (b.getClientProperty(JideButton.CLIENT_PROPERTY_SEGMENT_POSITION) != null) {
357 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
358 if (b.isButtonEnabled()) {
359 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
360 }
361 else if (paintBackground) {
362 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DISABLE_ROLLOVER);
363 }
364 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
365 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_PRESSED);
366 }
367 else {
368 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
369 if (b.isButtonEnabled()) {
370 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_SELECTED);
371 }
372 else if (paintBackground) {
373 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DISABLE_SELECTED);
374 }
375 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
376 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_SELECTED);
377 getPainter().paintSelectedMenu(b, g, new Rectangle(0, 0, menuWidth, menuHeight), orientation, ThemePainter.STATE_SELECTED);
378 }
379 }
380 else if (model.isArmed() || model.isPressed()) {
381 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
382 if (b.isButtonEnabled()) {
383 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_PRESSED);
384 }
385 else if (paintBackground) {
386 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DISABLE);
387 }
388 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
389 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
390 }
391 else if (model instanceof SplitButtonModel && ((DefaultSplitButtonModel) model).isButtonSelected()) {
392 if ((isMouseOver() || b.hasFocus()) && model.isEnabled()) {
393 Rectangle rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
394 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
395 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
396 if (b.isButtonEnabled()) {
397 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_PRESSED);
398 }
399 else if (paintBackground) {
400 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DISABLE);
401 }
402 }
403 else {
404 Rectangle rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
405 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DEFAULT);
406 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
407 if (b.isButtonEnabled()) {
408 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_SELECTED);
409 }
410 else if (paintBackground) {
411 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DISABLE_SELECTED);
412 }
413 }
414 }
415 else if (((b.isRolloverEnabled() && isMouseOver()) || b.hasFocus()) && model.isEnabled()) {
416 if (b.isAlwaysDropdown()) {
417 Rectangle rect = new Rectangle(0, 0, menuWidth, menuHeight);
418 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
419 }
420 else {
421 // Draw a line border with background
422 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
423 if (b.isButtonEnabled()) {
424 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
425 }
426 else if (paintBackground) {
427 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DISABLE_ROLLOVER);
428 }
429 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
430 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
431 }
432 }
433 else {
434 if (paintBackground) {
435 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
436 if (b.isEnabled() && b.isButtonEnabled()) {
437 getPainter().paintButtonBackground(b, g, rect, 0, ThemePainter.STATE_DEFAULT);
438 }
439 else {
440 getPainter().paintButtonBackground(b, g, rect, 0, ThemePainter.STATE_DISABLE);
441 }
442 if ("true".equals(SecurityUtils.getProperty("shadingtheme", "false"))) {
443 JideSwingUtilities.fillGradient(g, rect, SwingConstants.HORIZONTAL);
444 }
445 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
446 if (b.isEnabled()) {
447 getPainter().paintButtonBackground(b, g, rect, 0, ThemePainter.STATE_DEFAULT);
448 }
449 else {
450 getPainter().paintButtonBackground(b, g, rect, 0, ThemePainter.STATE_DISABLE);
451 }
452 if ("true".equals(SecurityUtils.getProperty("shadingtheme", "false"))) {
453 JideSwingUtilities.fillGradient(g, rect, SwingConstants.HORIZONTAL);
454 }
455 }
456 }
457 }
458 else if (b.getButtonStyle() == ButtonStyle.FLAT_STYLE) {
459 if ((model.isSelected())) {
460 // Draw a dark shadow border without bottom
461 getPainter().paintSelectedMenu(b, g, new Rectangle(0, 0, menuWidth, menuHeight), orientation, ThemePainter.STATE_SELECTED);
462 }
463 else if (model.isArmed() || model.isPressed()) {
464 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
465 if (b.isButtonEnabled()) {
466 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
467 }
468 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
469 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
470
471 if (!b.isOpaque()) {
472 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
473 paintSunkenBorder(g, rect);
474 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
475 paintRaisedBorder(g, rect);
476 }
477 }
478 else if (model instanceof SplitButtonModel && ((DefaultSplitButtonModel) model).isButtonSelected()) {
479 if ((isMouseOver() || b.hasFocus()) && model.isEnabled()) {
480 Rectangle rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
481 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
482 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
483 if (b.isButtonEnabled()) {
484 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
485 }
486 if (!b.isOpaque()) {
487 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
488 paintSunkenBorder(g, rect);
489 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
490 paintRaisedBorder(g, rect);
491 }
492 }
493 else {
494 Rectangle rect;
495 if (b.isOpaque()) {
496 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
497 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
498 }
499 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
500 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
501
502 if (!b.isOpaque()) {
503 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
504 paintSunkenBorder(g, rect);
505 // rect = new Rectangle(menuWidth - _splitButtonMargin + getOffset(), 0, _splitButtonMargin - getOffset(), menuHeight);
506 // paintRaisedBorder(g, rect);
507 }
508 }
509
510 }
511 else {
512 if (((b.isRolloverEnabled() && isMouseOver()) || b.hasFocus()) && model.isEnabled()) {
513 // Draw a line border with background
514 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
515 if (b.isButtonEnabled()) {
516 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
517 }
518 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
519 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
520
521 if (b.isAlwaysDropdown()) {
522 rect = new Rectangle(0, 0, menuWidth, menuHeight);
523 paintRaisedBorder(g, rect);
524 }
525 else {
526 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
527 paintRaisedBorder(g, rect);
528 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
529 paintRaisedBorder(g, rect);
530 }
531 }
532 else {
533 if (b.isOpaque()) {
534 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
535 if (b.isButtonEnabled()) {
536 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DEFAULT);
537 }
538 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
539 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DEFAULT);
540 }
541 }
542 }
543 }
544 else if (b.getButtonStyle() == ButtonStyle.TOOLBOX_STYLE) {
545 if ((model.isSelected())) {
546 // Draw a dark shadow border without bottom
547 getPainter().paintSelectedMenu(b, g, new Rectangle(0, 0, menuWidth, menuHeight), orientation, ThemePainter.STATE_SELECTED);
548 }
549 else if (model.isArmed() || model.isPressed()) {
550 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
551 if (b.isButtonEnabled()) {
552 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_PRESSED);
553 }
554 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
555 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
556
557 if (!b.isOpaque()) {
558 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
559 paintSunken2Border(g, rect);
560 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
561 paintRaisedBorder(g, rect);
562 }
563 }
564 else if (model instanceof SplitButtonModel && ((DefaultSplitButtonModel) model).isButtonSelected()) {
565 if (isMouseOver() && model.isEnabled()) {
566 Rectangle rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
567 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
568 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
569 if (b.isButtonEnabled()) {
570 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_PRESSED);
571 }
572 if (!b.isOpaque()) {
573 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
574 paintSunken2Border(g, rect);
575 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
576 paintRaisedBorder(g, rect);
577 }
578 }
579 else {
580 Rectangle rect;
581 if (b.isOpaque()) {
582 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
583 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DEFAULT);
584 }
585 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
586 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_SELECTED);
587
588 if (!b.isOpaque()) {
589 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
590 paintSunken2Border(g, rect);
591 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
592 paintRaisedBorder(g, rect);
593 }
594 }
595
596 }
597 else {
598 if (b.isRolloverEnabled() && isMouseOver() && model.isEnabled()) {
599 // Draw a line border with background
600 if (b.isAlwaysDropdown()) {
601 Rectangle rect = new Rectangle(0, 0, menuWidth, menuHeight);
602 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
603 paintRaised2Border(g, rect);
604 }
605 else {
606 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
607 if (b.isButtonEnabled()) {
608 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
609 }
610 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
611 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
612 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
613 paintRaised2Border(g, rect);
614 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
615 paintRaised2Border(g, rect);
616 }
617 }
618 else {
619 if (b.isOpaque()) {
620 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
621 if (b.isButtonEnabled()) {
622 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DEFAULT);
623 }
624 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
625 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DEFAULT);
626 }
627 else {
628 if (b.isAlwaysDropdown()) {
629 Rectangle rect = new Rectangle(0, 0, menuWidth, menuHeight);
630 paintRaisedBorder(g, rect);
631 }
632 else {
633 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
634 paintRaisedBorder(g, rect);
635 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
636 paintRaisedBorder(g, rect);
637 }
638 }
639 }
640 }
641 }
642
643 paintArrow(menuItem, g);
644 }
645
646 protected void paintArrow(JMenuItem menuItem, Graphics g) {
647 int menuWidth;
648 int menuHeight;
649 int orientation = JideSwingUtilities.getOrientationOf(menuItem);
650 if (orientation == SwingConstants.HORIZONTAL) {
651 menuWidth = menuItem.getWidth();
652 menuHeight = menuItem.getHeight();
653 }
654 else {
655 menuWidth = menuItem.getHeight();
656 menuHeight = menuItem.getWidth();
657 }
658 int startX;
659 if (menuItem.getComponentOrientation().isLeftToRight()) {
660 startX = menuWidth - 9;
661 }
662 else {
663 startX = 4;
664 }
665 if (menuItem.isEnabled()) {
666 JideSwingUtilities.paintArrow(g, getForegroundOfState(menuItem), startX, menuHeight / 2 - 1, 5, SwingConstants.HORIZONTAL);
667 }
668 else {
669 JideSwingUtilities.paintArrow(g, UIDefaultsLookup.getColor("controlShadow"), startX, menuHeight / 2 - 1, 5, SwingConstants.HORIZONTAL);
670 }
671 }
672
673 protected class MouseInputHandler implements MouseInputListener {
674 public void mouseClicked(MouseEvent e) {
675 cancelMenuIfNecessary(e);
676 }
677
678 /**
679 * Invoked when the mouse has been clicked on the menu. This method clears or sets the selection path of the
680 * MenuSelectionManager.
681 *
682 * @param e the mouse event
683 */
684 public void mousePressed(MouseEvent e) {
685 JMenu menu = (JMenu) menuItem;
686 if (!menu.isEnabled())
687 return;
688
689 setMouseOver(true);
690
691 if (!SwingUtilities.isLeftMouseButton(e)) {
692 return;
693 }
694 if (isClickOnButton(e, menu)) {
695 if (((JideSplitButton) menuItem).isButtonEnabled()) {
696 // click button
697 menu.getModel().setArmed(true);
698 menu.getModel().setPressed(true);
699 }
700 if (!menu.hasFocus() && menu.isRequestFocusEnabled()) {
701 menu.requestFocus();
702 }
703 }
704 else {
705 downButtonPressed(menu);
706 }
707 }
708
709 private boolean isClickOnButton(MouseEvent e, JMenu menu) {
710 if (((JideSplitButton) menu).isAlwaysDropdown()) {
711 return false;
712 }
713
714 boolean clickOnDropDown = false;
715 int size = ((JMenu) menuItem).isTopLevelMenu() ? _splitButtonMargin : _splitButtonMarginOnMenu;
716 if (JideSwingUtilities.getOrientationOf(menuItem) == SwingConstants.HORIZONTAL) {
717 if (e.getPoint().getX() < menu.getWidth() - size) {
718 clickOnDropDown = true;
719 }
720 }
721 else {
722 if (e.getPoint().getY() < menu.getHeight() - size) {
723 clickOnDropDown = true;
724 }
725 }
726 return clickOnDropDown;
727 }
728
729 /**
730 * Invoked when the mouse has been released on the menu. Delegates the mouse event to the MenuSelectionManager.
731 *
732 * @param e the mouse event
733 */
734 public void mouseReleased(MouseEvent e) {
735 JMenu menu = (JMenu) menuItem;
736 if (!menu.isEnabled()) {
737 return;
738 }
739 if (!isClickOnButton(e, menu)) {
740 // these two lines order matters. In this order, it would not trigger actionPerformed.
741 menuItem.getModel().setArmed(false);
742 menuItem.getModel().setPressed(false);
743 }
744 cancelMenuIfNecessary(e);
745 }
746
747 private void cancelMenuIfNecessary(MouseEvent e) {
748 JMenu menu = (JMenu) menuItem;
749 if (!menu.isEnabled())
750 return;
751 if (isClickOnButton(e, menu)) {
752 if (((JideSplitButton) menuItem).isButtonEnabled()) {
753 // click button
754 // these two lines order matters. In this order, it would trigger actionPerformed.
755 if (SwingUtilities.isLeftMouseButton(e)) {
756 menu.getModel().setPressed(false);
757 menu.getModel().setArmed(false);
758 }
759 else {
760 menu.getModel().setArmed(false);
761 menu.getModel().setPressed(false);
762 }
763
764 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
765 MenuElement[] menuElements = manager.getSelectedPath();
766 for (int i = menuElements.length - 1; i >= 0; i--) {
767 MenuElement menuElement = menuElements[i];
768 if (menuElement instanceof JPopupMenu && ((JPopupMenu) menuElement).isAncestorOf(menu)) {
769 menu.getModel().setRollover(false);
770 setMouseOver(false);
771 manager.clearSelectedPath();
772 }
773 }
774 }
775 }
776 else {
777 // MenuSelectionManager manager =
778 // MenuSelectionManager.defaultManager();
779 // manager.processMouseEvent(e);
780 // if (!e.isConsumed())
781 // manager.clearSelectedPath();
782 }
783 }
784
785 /**
786 * Invoked when the cursor enters the menu. This method sets the selected path for the MenuSelectionManager and
787 * handles the case in which a menu item is used to pop up an additional menu, as in a hierarchical menu
788 * system.
789 *
790 * @param e the mouse event; not used
791 */
792 public void mouseEntered(MouseEvent e) {
793 JMenu menu = (JMenu) menuItem;
794 if (!menu.isEnabled())
795 return;
796
797 MenuSelectionManager manager =
798 MenuSelectionManager.defaultManager();
799 MenuElement selectedPath[] = manager.getSelectedPath();
800 if (!menu.isTopLevelMenu()) {
801 if (!(selectedPath.length > 0 &&
802 selectedPath[selectedPath.length - 1] ==
803 menu.getPopupMenu())) {
804 if (menu.getDelay() == 0) {
805 appendPath(getPath(), menu.getPopupMenu());
806 }
807 else {
808 manager.setSelectedPath(getPath());
809 setupPostTimer(menu);
810 }
811 }
812 }
813 else {
814 if (selectedPath.length > 0 &&
815 selectedPath[0] == menu.getParent()) {
816 MenuElement newPath[] = new MenuElement[3];
817 // A top level menu's parent is by definition
818 // a JMenuBar
819 newPath[0] = (MenuElement) menu.getParent();
820 newPath[1] = menu;
821 newPath[2] = menu.getPopupMenu();
822 manager.setSelectedPath(newPath);
823 }
824 }
825
826 if (!SwingUtilities.isLeftMouseButton(e)) {
827 setMouseOver(true);
828 }
829 menuItem.repaint();
830 }
831
832 public void mouseExited(MouseEvent e) {
833 setMouseOver(false);
834 menuItem.repaint();
835 }
836
837 /**
838 * Invoked when a mouse button is pressed on the menu and then dragged. Delegates the mouse event to the
839 * MenuSelectionManager.
840 *
841 * @param e the mouse event
842 * @see java.awt.event.MouseMotionListener#mouseDragged
843 */
844 public void mouseDragged(MouseEvent e) {
845 JMenu menu = (JMenu) menuItem;
846 if (!menu.isEnabled())
847 return;
848 MenuSelectionManager.defaultManager().processMouseEvent(e);
849 }
850
851 public void mouseMoved(MouseEvent e) {
852 }
853 }
854
855 @Override
856 public Dimension getMinimumSize(JComponent c) {
857 if (!(c instanceof JMenu) || !((JMenu) c).isTopLevelMenu()) {
858 return super.getMinimumSize(c);
859 }
860
861 Dimension d = getPreferredSize(c);
862 View v = (View) c.getClientProperty(BasicHTML.propertyKey);
863 if (v != null) {
864 if (JideSwingUtilities.getOrientationOf(c) == SwingConstants.HORIZONTAL)
865 d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
866 else // TODO: not sure if this is correct
867 d.height -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
868 }
869
870 return d;
871 }
872
873 @Override
874 public Dimension getMaximumSize(JComponent c) {
875 if (!(c instanceof JMenu) || !((JMenu) c).isTopLevelMenu()) {
876 return super.getMaximumSize(c);
877 }
878
879 Dimension d = getPreferredSize(c);
880 View v = (View) c.getClientProperty(BasicHTML.propertyKey);
881 if (v != null) {
882 if (JideSwingUtilities.getOrientationOf(c) == SwingConstants.HORIZONTAL)
883 d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
884 else // TODO: not sure if this is correct
885 d.height += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
886 }
887
888 return d;
889 }
890
891 @Override
892 public Dimension getPreferredSize(JComponent c) {
893 if (!(c instanceof JMenu) || !((JMenu) c).isTopLevelMenu()) {
894 return super.getPreferredSize(c);
895 }
896
897 AbstractButton b = (AbstractButton) c;
898
899 boolean isHorizontal = true;
900 if (JideSwingUtilities.getOrientationOf(c) == SwingConstants.VERTICAL) {
901 isHorizontal = false;
902 }
903
904 // JDK PORTING HINT
905 // JDK1.3: No getIconTextGap, use defaultTextIconGap
906 Dimension d = BasicGraphicsUtils.getPreferredButtonSize(b, defaultTextIconGap);
907 // d.width += b.getMargin().left + b.getMargin().right;
908 // d.height += b.getMargin().bottom + b.getMargin().top;
909
910 int size = ((JMenu) menuItem).isTopLevelMenu() ? _splitButtonMargin : _splitButtonMarginOnMenu;
911 d.width += size;
912
913 if (isHorizontal)
914 return d;
915 else
916 return new Dimension(d.height, d.width); // swap width and height
917 }
918
919 protected void paintIcon(JMenuItem b, Graphics g) {
920 ButtonModel model = b.getModel();
921
922 // Paint the Icon
923 if (b.getIcon() != null) {
924 // rotate back since we don't want to paint icon in a rotated way.
925 if (JideSwingUtilities.getOrientationOf(b) == SwingConstants.VERTICAL) {
926 g.translate(0, b.getWidth() - 1);
927 ((Graphics2D) g).rotate(-Math.PI / 2);
928 }
929 Icon icon;
930 if (!model.isEnabled()) {
931 icon = b.getDisabledIcon();
932 if (icon == null) {
933 icon = b.getIcon();
934 if (icon instanceof ImageIcon) {
935 icon = IconsFactory.createGrayImage(((ImageIcon) icon).getImage());
936 }
937 else {
938 icon = IconsFactory.createGrayImage(b, icon);
939 }
940 }
941 }
942 else if (model.isPressed() && model.isArmed()) {
943 icon = b.getPressedIcon();
944 if (icon == null) {
945 // Use default icon
946 icon = b.getIcon();
947 }
948 }
949 else {
950 icon = b.getIcon();
951 }
952
953 if (icon != null) {
954 icon.paintIcon(b, g, iconRect.x, iconRect.y);
955 // if (model.isRollover() && !model.isPressed() && !model.isSelected()) {
956 // icon.paintIcon(b, g, iconRect.x, iconRect.y);
957 // }
958 // else {
959 // icon.paintIcon(b, g, iconRect.x, iconRect.y);
960 // }
961 }
962
963 if (JideSwingUtilities.getOrientationOf(b) == SwingConstants.VERTICAL) {
964 ((Graphics2D) g).rotate(Math.PI / 2);
965 g.translate(0, -b.getHeight() + 1);
966 }
967 }
968 }
969
970 /**
971 * Actions for Buttons. Two type of action are supported: pressed: Moves the button to a pressed state released:
972 * Disarms the button.
973 */
974 private static class Actions extends UIAction {
975 private static final String PRESS = "pressed";
976 private static final String RELEASE = "released";
977 private static final String DOWN_PRESS = "downPressed";
978 private static final String DOWN_RELEASE = "downReleased";
979
980 Actions(String name) {
981 super(name);
982 }
983
984 public void actionPerformed(ActionEvent e) {
985 AbstractButton b = (AbstractButton) e.getSource();
986 String key = getName();
987
988 // if isAlwaysDropDown it true, treat PRESS as DOWN_PRESS
989 if (PRESS.equals(key) && ((JideSplitButton) b).isAlwaysDropdown()) {
990 key = DOWN_PRESS;
991 }
992
993 if (PRESS.equals(key)) {
994 ButtonModel model = b.getModel();
995 model.setArmed(true);
996 model.setPressed(true);
997 if (!b.hasFocus()) {
998 b.requestFocus();
999 }
1000 }
1001 else if (RELEASE.equals(key)) {
1002 ButtonModel model = b.getModel();
1003 model.setPressed(false);
1004 model.setArmed(false);
1005 }
1006 else if (DOWN_PRESS.equals(key)) {
1007 downButtonPressed((JMenu) b);
1008 }
1009 else if (DOWN_RELEASE.equals(key)) {
1010 }
1011 }
1012
1013 @Override
1014 public boolean isEnabled(Object sender) {
1015 return !(sender != null && (sender instanceof AbstractButton) &&
1016 !((AbstractButton) sender).getModel().isEnabled());
1017 }
1018 }
1019
1020 protected int getOffset() {
1021 return 1;
1022 }
1023
1024 /**
1025 * Populates Buttons actions.
1026 *
1027 * @param map the action map to load
1028 */
1029 public static void loadActionMap(LazyActionMap map) {
1030 map.put(new Actions(Actions.PRESS));
1031 map.put(new Actions(Actions.RELEASE));
1032 map.put(new Actions(Actions.DOWN_PRESS));
1033 map.put(new Actions(Actions.DOWN_RELEASE));
1034 }
1035
1036 @Override
1037 protected void updateMnemonicBinding() {
1038 super.updateMnemonicBinding();
1039 int mnemonic = menuItem.getModel().getMnemonic();
1040 if (mnemonic != 0 && windowInputMap != null) {
1041 int[] shortcutKeys = (int[]) UIDefaultsLookup.get("Menu.shortcutKeys");
1042 if (shortcutKeys == null) {
1043 shortcutKeys = new int[]{KeyEvent.ALT_MASK};
1044 }
1045 for (int shortcutKey : shortcutKeys) {
1046 windowInputMap.put(KeyStroke.getKeyStroke(mnemonic,
1047 shortcutKey, false),
1048 "pressed");
1049 windowInputMap.put(KeyStroke.getKeyStroke(mnemonic,
1050 shortcutKey, true),
1051 "released");
1052 }
1053 }
1054 }
1055
1056 protected static void downButtonPressed(JMenu menu) {
1057 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
1058 if (menu.isTopLevelMenu()) {
1059 if (menu.isSelected()) {
1060 manager.clearSelectedPath();
1061 }
1062 else {
1063 //Container cnt = menu.getParent();
1064 Container cnt = getFirstParentMenuElement(menu);
1065
1066 if (cnt != null && cnt instanceof MenuElement) {
1067 ArrayList<Component> parents = new ArrayList<Component>();
1068 while (cnt instanceof MenuElement) {
1069 parents.add(0, cnt);
1070 if (cnt instanceof JPopupMenu) {
1071 cnt = (Container) ((JPopupMenu) cnt).getInvoker();
1072 }
1073 else {
1074 //cnt = cnt.getParent();
1075 cnt = getFirstParentMenuElement(cnt);
1076 }
1077 }
1078
1079 MenuElement me[] = new MenuElement[parents.size() + 1];
1080 for (int i = 0; i < parents.size(); i++) {
1081 Container container = (Container) parents.get(i);
1082 me[i] = (MenuElement) container;
1083 }
1084 me[parents.size()] = menu;
1085 manager.setSelectedPath(me);
1086 }
1087 else {
1088 MenuElement me[] = new MenuElement[1];
1089 me[0] = menu;
1090 manager.setSelectedPath(me);
1091 }
1092 }
1093 }
1094
1095 MenuElement selectedPath[] = manager.getSelectedPath();
1096 if (selectedPath.length > 0 &&
1097 selectedPath[selectedPath.length - 1] != menu.getPopupMenu()) {
1098 if (menu.isTopLevelMenu() ||
1099 menu.getDelay() == 0) {
1100 appendPath(selectedPath, menu.getPopupMenu());
1101 }
1102 else {
1103 setupPostTimer(menu);
1104 }
1105 }
1106 }
1107
1108 protected static Container getFirstParentMenuElement(Component comp) {
1109 Container parent = comp.getParent();
1110
1111 while (parent != null) {
1112 if (parent instanceof MenuElement)
1113 return parent;
1114
1115 parent = parent.getParent();
1116 }
1117
1118 return null;
1119 }
1120 }