Debian Codegraph @

src/com/jidesoft/plaf/basic/BasicJideSplitButtonUI.java

raw
1 /*
2 * @(#)VsnetMenuUI.java
3 *
4 * Copyright 2002 JIDE Software Inc. All rights reserved.
5 */
6
7 package com.jidesoft.plaf.basic;
8
9 import com.jidesoft.icons.IconsFactory;
10 import com.jidesoft.plaf.LookAndFeelFactory;
11 import com.jidesoft.plaf.UIDefaultsLookup;
12 import com.jidesoft.plaf.vsnet.VsnetMenuUI;
13 import com.jidesoft.swing.*;
14 import com.jidesoft.utils.SecurityUtils;
15
16 import javax.swing.*;
17 import javax.swing.event.MouseInputListener;
18 import javax.swing.plaf.ComponentUI;
19 import javax.swing.plaf.UIResource;
20 import javax.swing.plaf.basic.BasicHTML;
21 import javax.swing.text.View;
22 import java.awt.*;
23 import java.awt.event.*;
24 import java.awt.geom.Rectangle2D;
25 import java.util.ArrayList;
26
27
28 /**
29 * SplitButtonUI implementation
30 */
31 public class BasicJideSplitButtonUI extends VsnetMenuUI {
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 protected boolean _isFloatingIcon = false;
44
45 private FocusListener _focusListener;
46
47 private static final String propertyPrefix = "JideSplitButton";
48
49 @SuppressWarnings({"UnusedDeclaration"})
50 public static ComponentUI createUI(JComponent x) {
51 return new BasicJideSplitButtonUI();
52 }
53
54 @Override
55 protected String getPropertyPrefix() {
56 return propertyPrefix;
57 }
58
59 @Override
60 protected void installDefaults() {
61 _painter = (ThemePainter) UIDefaultsLookup.get("Theme.painter");
62 _isFloatingIcon = UIDefaultsLookup.getBoolean("Icon.floating");
63
64 _shadowColor = UIDefaultsLookup.getColor("JideButton.shadow");
65 _darkShadowColor = UIDefaultsLookup.getColor("JideButton.darkShadow");
66 _highlight = UIDefaultsLookup.getColor("JideButton.highlight");
67 _lightHighlightColor = UIDefaultsLookup.getColor("JideButton.light");
68
69 menuItem.setRolloverEnabled(true);
70
71 super.installDefaults();
72 }
73
74 @Override
75 protected void uninstallDefaults() {
76 _painter = null;
77
78 _shadowColor = null;
79 _highlight = null;
80 _lightHighlightColor = null;
81 _darkShadowColor = null;
82
83 super.uninstallDefaults();
84 }
85
86 @Override
87 protected void installListeners() {
88 super.installListeners();
89 if (_focusListener == null) {
90 _focusListener = new FocusListener() {
91 public void focusGained(FocusEvent e) {
92 menuItem.repaint();
93 }
94
95 public void focusLost(FocusEvent e) {
96 menuItem.repaint();
97 }
98 };
99 }
100 menuItem.addFocusListener(_focusListener);
101 }
102
103 @Override
104 protected void uninstallListeners() {
105 super.uninstallListeners();
106 if (_focusListener != null) {
107 menuItem.removeFocusListener(_focusListener);
108 }
109 }
110
111 /**
112 * Returns the ui that is of type <code>clazz</code>, or null if one can not be found.
113 *
114 * @param ui the ui
115 * @param clazz the class
116 * @return the actual ui for the class.
117 */
118 static Object getUIOfType(ComponentUI ui, Class clazz) {
119 if (clazz.isInstance(ui)) {
120 return ui;
121 }
122 return null;
123 }
124
125 /**
126 * Returns the InputMap for condition <code>condition</code>. Called as part of
127 * <code>installKeyboardActions</code>.
128 *
129 * @param condition the condition
130 * @param c the component
131 * @return the input map.
132 */
133 public InputMap getInputMap(int condition, JComponent c) {
134 if (condition == JComponent.WHEN_FOCUSED) {
135 BasicJideSplitButtonUI ui = (BasicJideSplitButtonUI) getUIOfType(
136 ((JideSplitButton) c).getUI(), BasicJideSplitButtonUI.class);
137 if (ui != null) {
138 return (InputMap) UIDefaultsLookup.get(ui.getPropertyPrefix() + ".focusInputMap");
139 }
140 }
141 return null;
142 }
143
144 @Override
145 protected void installKeyboardActions() {
146 super.installKeyboardActions();
147 AbstractButton b = menuItem;
148
149 LazyActionMap.installLazyActionMap(b, BasicJideSplitButtonUI.class,
150 "JideSplitButton.actionMap");
151
152 InputMap km = getInputMap(JComponent.WHEN_FOCUSED, b);
153
154 SwingUtilities.replaceUIInputMap(b, JComponent.WHEN_FOCUSED, km);
155 }
156
157 @Override
158 protected void uninstallKeyboardActions() {
159 AbstractButton b = menuItem;
160 SwingUtilities.replaceUIInputMap(b, JComponent.
161 WHEN_IN_FOCUSED_WINDOW, null);
162 SwingUtilities.replaceUIInputMap(b, JComponent.WHEN_FOCUSED, null);
163 SwingUtilities.replaceUIActionMap(b, null);
164 super.uninstallKeyboardActions();
165 }
166
167 @Override
168 protected MouseInputListener createMouseInputListener(JComponent c) {
169 return new MouseInputHandler();
170 }
171
172 @Override
173 protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) {
174 ButtonModel model = menuItem.getModel();
175 int menuWidth;
176 int menuHeight;
177 int orientation = JideSwingUtilities.getOrientationOf(menuItem);
178 if (orientation == SwingConstants.HORIZONTAL) {
179 menuWidth = menuItem.getWidth();
180 menuHeight = menuItem.getHeight();
181 }
182 else {
183 menuWidth = menuItem.getHeight();
184 menuHeight = menuItem.getWidth();
185 }
186 // have to change to HORIZONTAL because we rotate the Graphics already
187 orientation = SwingConstants.HORIZONTAL;
188
189 boolean paintBackground;
190 Object o = menuItem.getClientProperty("JideSplitButton.alwaysPaintBackground");
191 if (o instanceof Boolean) {
192 paintBackground = (Boolean) o;
193 }
194 else {
195 paintBackground = menuItem.isOpaque();
196 }
197
198 if (!((JMenu) menuItem).isTopLevelMenu()) {
199 super.paintBackground(g, menuItem, bgColor);
200 if (menuItem.isEnabled()) {
201 if (model.isArmed() || model.isPressed() || isMouseOver()) {
202 g.setColor(selectionForeground);
203 g.drawLine(menuWidth - _splitButtonMarginOnMenu, 0, menuWidth - _splitButtonMarginOnMenu, menuHeight - 2);
204 JideSwingUtilities.paintArrow(g, selectionForeground, menuWidth - _splitButtonMarginOnMenu / 2 - 2, menuHeight / 2 - 3, 7, SwingConstants.VERTICAL);
205 }
206 else {
207 g.setColor(getForegroundOfState(menuItem));
208 g.drawLine(menuWidth - _splitButtonMarginOnMenu, 0, menuWidth - _splitButtonMarginOnMenu, menuHeight - 2);
209 JideSwingUtilities.paintArrow(g, getForegroundOfState(menuItem), menuWidth - _splitButtonMarginOnMenu / 2 - 2, menuHeight / 2 - 3, 7, SwingConstants.VERTICAL);
210 }
211 }
212 else {
213 g.setColor(UIDefaultsLookup.getColor("controlDkShadow"));
214 g.drawLine(menuWidth - _splitButtonMarginOnMenu, 0, menuWidth - _splitButtonMarginOnMenu, menuHeight - 2);
215 JideSwingUtilities.paintArrow(g, UIDefaultsLookup.getColor("controlDkShadow"), menuWidth - _splitButtonMarginOnMenu / 2 - 2, menuHeight / 2 - 3, 7, SwingConstants.VERTICAL);
216 }
217 return;
218 }
219
220 if (paintBackground) {
221 if (menuItem.getParent() != null) {
222 g.setColor(menuItem.getParent().getBackground());
223 }
224 else {
225 g.setColor(menuItem.getBackground());
226 }
227 g.fillRect(0, 0, menuWidth, menuHeight);
228 }
229
230 JideSplitButton b = (JideSplitButton) menuItem;
231 if (b.getButtonStyle() == ButtonStyle.TOOLBAR_STYLE) {
232 if ((model.isSelected())) {
233 if (isAlwaysDropdown(b)) {
234 Rectangle rect = new Rectangle(0, 0, menuWidth, menuHeight);
235 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_SELECTED);
236 }
237 else if (b.getClientProperty(JideButton.CLIENT_PROPERTY_SEGMENT_POSITION) != null) {
238 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
239 if (b.isButtonEnabled()) {
240 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
241 }
242 else if (paintBackground) {
243 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DISABLE_ROLLOVER);
244 }
245 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
246 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_PRESSED);
247 }
248 else {
249 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
250 if (b.isButtonEnabled()) {
251 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_SELECTED);
252 }
253 else if (paintBackground) {
254 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DISABLE_SELECTED);
255 }
256 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
257 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_SELECTED);
258 getPainter().paintSelectedMenu(b, g, new Rectangle(0, 0, menuWidth, menuHeight), orientation, ThemePainter.STATE_SELECTED);
259 }
260 }
261 else if (model.isArmed() || model.isPressed()) {
262 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
263 if (b.isButtonEnabled()) {
264 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_PRESSED);
265 }
266 else if (paintBackground) {
267 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DISABLE);
268 }
269 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
270 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
271 }
272 else if (model instanceof SplitButtonModel && ((DefaultSplitButtonModel) model).isButtonSelected()) {
273 if ((isMouseOver() || b.hasFocus()) && model.isEnabled()) {
274 Rectangle rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
275 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
276 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
277 if (b.isButtonEnabled()) {
278 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_PRESSED);
279 }
280 else if (paintBackground) {
281 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DISABLE);
282 }
283 }
284 else {
285 Rectangle rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
286 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DEFAULT);
287 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
288 if (b.isButtonEnabled()) {
289 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_SELECTED);
290 }
291 else if (paintBackground) {
292 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DISABLE_SELECTED);
293 }
294 }
295 }
296 else if (((b.isRolloverEnabled() && isMouseOver()) || b.hasFocus()) && model.isEnabled()) {
297 if (isAlwaysDropdown(b)) {
298 Rectangle rect = new Rectangle(0, 0, menuWidth, menuHeight);
299 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
300 }
301 else {
302 // Draw a line border with background
303 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
304 if (b.isButtonEnabled()) {
305 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
306 }
307 else if (paintBackground) {
308 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DISABLE_ROLLOVER);
309 }
310 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
311 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
312 }
313 }
314 else {
315 if (paintBackground) {
316 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
317 if (b.isEnabled() && b.isButtonEnabled()) {
318 getPainter().paintButtonBackground(b, g, rect, 0, ThemePainter.STATE_DEFAULT);
319 }
320 else {
321 getPainter().paintButtonBackground(b, g, rect, 0, ThemePainter.STATE_DISABLE);
322 }
323 if ("true".equals(SecurityUtils.getProperty("shadingtheme", "false"))) {
324 JideSwingUtilities.fillGradient(g, rect, SwingConstants.HORIZONTAL);
325 }
326 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
327 if (b.isEnabled()) {
328 getPainter().paintButtonBackground(b, g, rect, 0, ThemePainter.STATE_DEFAULT);
329 }
330 else {
331 getPainter().paintButtonBackground(b, g, rect, 0, ThemePainter.STATE_DISABLE);
332 }
333 if ("true".equals(SecurityUtils.getProperty("shadingtheme", "false"))) {
334 JideSwingUtilities.fillGradient(g, rect, SwingConstants.HORIZONTAL);
335 }
336 }
337 }
338 }
339 else if (b.getButtonStyle() == ButtonStyle.FLAT_STYLE) {
340 if ((model.isSelected())) {
341 // Draw a dark shadow border without bottom
342 getPainter().paintSelectedMenu(b, g, new Rectangle(0, 0, menuWidth, menuHeight), orientation, ThemePainter.STATE_SELECTED);
343 }
344 else if (model.isArmed() || model.isPressed()) {
345 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
346 if (b.isButtonEnabled()) {
347 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
348 }
349 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
350 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
351
352 if (!b.isOpaque()) {
353 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
354 paintSunkenBorder(g, rect);
355 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
356 paintRaisedBorder(g, rect);
357 }
358 }
359 else if (model instanceof SplitButtonModel && ((DefaultSplitButtonModel) model).isButtonSelected()) {
360 if ((isMouseOver() || b.hasFocus()) && model.isEnabled()) {
361 Rectangle rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
362 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
363 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
364 if (b.isButtonEnabled()) {
365 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
366 }
367 if (!b.isOpaque()) {
368 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
369 paintSunkenBorder(g, rect);
370 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
371 paintRaisedBorder(g, rect);
372 }
373 }
374 else {
375 Rectangle rect;
376 if (b.isOpaque()) {
377 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
378 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
379 }
380 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
381 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
382
383 if (!b.isOpaque()) {
384 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
385 paintSunkenBorder(g, rect);
386 // rect = new Rectangle(menuWidth - _splitButtonMargin + getOffset(), 0, _splitButtonMargin - getOffset(), menuHeight);
387 // paintRaisedBorder(g, rect);
388 }
389 }
390
391 }
392 else {
393 if (((b.isRolloverEnabled() && isMouseOver()) || b.hasFocus()) && model.isEnabled()) {
394 // Draw a line border with background
395 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
396 if (b.isButtonEnabled()) {
397 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
398 }
399 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
400 JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight);
401
402 if (isAlwaysDropdown(b)) {
403 rect = new Rectangle(0, 0, menuWidth, menuHeight);
404 paintRaisedBorder(g, rect);
405 }
406 else {
407 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
408 paintRaisedBorder(g, rect);
409 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
410 paintRaisedBorder(g, rect);
411 }
412 }
413 else {
414 if (b.isOpaque()) {
415 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
416 if (b.isButtonEnabled()) {
417 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DEFAULT);
418 }
419 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
420 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DEFAULT);
421 }
422 }
423 }
424 }
425 else if (b.getButtonStyle() == ButtonStyle.TOOLBOX_STYLE) {
426 if ((model.isSelected())) {
427 // Draw a dark shadow border without bottom
428 getPainter().paintSelectedMenu(b, g, new Rectangle(0, 0, menuWidth, menuHeight), orientation, ThemePainter.STATE_SELECTED);
429 }
430 else if (model.isArmed() || model.isPressed()) {
431 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
432 if (b.isButtonEnabled()) {
433 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_PRESSED);
434 }
435 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
436 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
437
438 if (!b.isOpaque()) {
439 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
440 paintSunken2Border(g, rect);
441 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
442 paintRaisedBorder(g, rect);
443 }
444 }
445 else if (model instanceof SplitButtonModel && ((DefaultSplitButtonModel) model).isButtonSelected()) {
446 if (isMouseOver() && model.isEnabled()) {
447 Rectangle rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
448 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
449 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
450 if (b.isButtonEnabled()) {
451 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_PRESSED);
452 }
453 if (!b.isOpaque()) {
454 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
455 paintSunken2Border(g, rect);
456 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
457 paintRaisedBorder(g, rect);
458 }
459 }
460 else {
461 Rectangle rect;
462 if (b.isOpaque()) {
463 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
464 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DEFAULT);
465 }
466 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
467 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_SELECTED);
468
469 if (!b.isOpaque()) {
470 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
471 paintSunken2Border(g, rect);
472 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
473 paintRaisedBorder(g, rect);
474 }
475 }
476
477 }
478 else {
479 if (b.isRolloverEnabled() && isMouseOver() && model.isEnabled()) {
480 // Draw a line border with background
481 if (isAlwaysDropdown(b)) {
482 Rectangle rect = new Rectangle(0, 0, menuWidth, menuHeight);
483 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
484 paintRaised2Border(g, rect);
485 }
486 else {
487 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
488 if (b.isButtonEnabled()) {
489 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
490 }
491 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
492 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_ROLLOVER);
493 rect = getButtonRect(b, orientation, menuWidth, menuHeight);
494 paintRaised2Border(g, rect);
495 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
496 paintRaised2Border(g, rect);
497 }
498 }
499 else {
500 if (b.isOpaque()) {
501 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
502 if (b.isButtonEnabled()) {
503 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DEFAULT);
504 }
505 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
506 getPainter().paintButtonBackground(b, g, rect, orientation, ThemePainter.STATE_DEFAULT);
507 }
508 else {
509 if (isAlwaysDropdown(b)) {
510 Rectangle rect = new Rectangle(0, 0, menuWidth, menuHeight);
511 paintRaisedBorder(g, rect);
512 }
513 else {
514 Rectangle rect = getButtonRect(b, orientation, menuWidth, menuHeight);
515 paintRaisedBorder(g, rect);
516 rect = getDropDownRect(b, orientation, menuWidth, menuHeight);
517 paintRaisedBorder(g, rect);
518 }
519 }
520 }
521 }
522 }
523
524 paintArrow(menuItem, g);
525 }
526
527 protected void paintArrow(JMenuItem menuItem, Graphics g) {
528 int menuWidth;
529 int menuHeight;
530 int orientation = JideSwingUtilities.getOrientationOf(menuItem);
531 if (orientation == SwingConstants.HORIZONTAL) {
532 menuWidth = menuItem.getWidth();
533 menuHeight = menuItem.getHeight();
534 }
535 else {
536 menuWidth = menuItem.getHeight();
537 menuHeight = menuItem.getWidth();
538 }
539 int startX;
540 if (menuItem.getComponentOrientation().isLeftToRight()) {
541 startX = menuWidth - 9;
542 }
543 else {
544 startX = 4;
545 }
546 if (menuItem.isEnabled()) {
547 JideSwingUtilities.paintArrow(g, getForegroundOfState(menuItem), startX, menuHeight / 2 - 1, 5, SwingConstants.HORIZONTAL);
548 }
549 else {
550 JideSwingUtilities.paintArrow(g, UIDefaultsLookup.getColor("controlShadow"), startX, menuHeight / 2 - 1, 5, SwingConstants.HORIZONTAL);
551 }
552 }
553
554 /**
555 * Gets the bounds for the drop down part of the <code>JideSplitButton</code>.
556 *
557 * @param c the component. In this case, it is the <code>JideSplitButton</code>.
558 * @param orientation the orientation.
559 * @param width the width of the <code>JideSplitButton</code>
560 * @param height the height of the <code>JideSplitButton</code>.
561 * @return the bounds for the drop down part of the <code>JideSplitButton</code>.
562 */
563 protected Rectangle getDropDownRect(JComponent c, int orientation, int width, int height) {
564 Object position = c.getClientProperty(JideButton.CLIENT_PROPERTY_SEGMENT_POSITION);
565 Rectangle rect;
566 if (c.getComponentOrientation().isLeftToRight()) {
567 rect = new Rectangle(width - _splitButtonMargin - 1 + getOffset(), 0, _splitButtonMargin - getOffset(), height);
568 }
569 else {
570 rect = new Rectangle(0, 0, _splitButtonMargin - getOffset(), height);
571 }
572 if (position == null || JideButton.SEGMENT_POSITION_ONLY.equals(position)) {
573 }
574 else if (JideButton.SEGMENT_POSITION_FIRST.equals(position)) {
575 if (orientation == SwingConstants.HORIZONTAL) {
576 rect.width++;
577 }
578 else {
579 rect.height++;
580 }
581 }
582 else if (JideButton.SEGMENT_POSITION_MIDDLE.equals(position)) {
583 if (orientation == SwingConstants.HORIZONTAL) {
584 rect.width++;
585 }
586 else {
587 rect.height++;
588 }
589 }
590 else if (JideButton.SEGMENT_POSITION_LAST.equals(position)) {
591 }
592 return rect;
593 }
594
595 /**
596 * Gets the bounds for the button part of the <code>JideSplitButton</code>.
597 *
598 * @param c the component. In this case, it is the <code>JideSplitButton</code>.
599 * @param orientation the orientation.
600 * @param width the width of the <code>JideSplitButton</code>
601 * @param height the height of the <code>JideSplitButton</code>.
602 * @return the bounds for the button part of the <code>JideSplitButton</code>.
603 */
604 protected Rectangle getButtonRect(JComponent c, int orientation, int width, int height) {
605 Rectangle rect;
606 if (orientation == SwingConstants.HORIZONTAL && c.getComponentOrientation().isLeftToRight()) {
607 rect = new Rectangle(0, 0, width - _splitButtonMargin, height);
608 }
609 else {
610 rect = new Rectangle(_splitButtonMargin - 1, 0, width - _splitButtonMargin, height);
611 }
612 return rect;
613 }
614
615 protected void paintSunkenBorder(Graphics g, Rectangle b) {
616 Color old = g.getColor();
617 g.setColor(_shadowColor); // inner 3D border
618 g.drawLine(b.x, b.y, b.x + b.width - 1, b.y);
619 g.drawLine(b.x, b.y, b.x, b.y + b.height - 1);
620
621 g.setColor(_lightHighlightColor); // black drop shadow __|
622 g.drawLine(b.x, b.y + b.height - 1, b.x + b.width - 1, b.y + b.height - 1);
623 g.drawLine(b.x + b.width - 1, b.y, b.x + b.width - 1, b.y + b.height - 1);
624 g.setColor(old);
625 }
626
627 protected void paintSunken2Border(Graphics g, Rectangle b) {
628 Color old = g.getColor();
629 g.setColor(_darkShadowColor); // inner 3D border
630 g.drawLine(b.x, b.y, b.x + b.width - 2, b.y);
631 g.drawLine(b.x, b.y, b.x, b.y + b.height - 2);
632
633 g.setColor(_shadowColor); // inner 3D border
634 g.drawLine(b.x + 1, b.y + 1, b.x + b.width - 3, b.y + 1);
635 g.drawLine(b.x + 1, b.y + 1, b.x + 1, b.y + b.height - 3);
636
637 g.setColor(_lightHighlightColor); // black drop shadow __|
638 g.drawLine(b.x, b.y + b.height - 1, b.x + b.width - 1, b.y + b.height - 1);
639 g.drawLine(b.x + b.width - 1, b.x, b.x + b.width - 1, b.y + b.height - 1);
640 g.setColor(old);
641 }
642
643 protected void paintRaised2Border(Graphics g, Rectangle b) {
644 Color old = g.getColor();
645 g.setColor(_lightHighlightColor); // inner 3D border
646 g.drawLine(b.x, b.y, b.x + b.width - 1, b.y);
647 g.drawLine(b.x, b.y, b.x, b.y + b.height - 1);
648
649 g.setColor(_shadowColor); // gray drop shadow __|
650 g.drawLine(b.x + 1, b.y + b.height - 2, b.x + b.width - 2, b.y + b.height - 2);
651 g.drawLine(b.x + b.width - 2, 1, b.x + b.width - 2, b.y + b.height - 2);
652
653 g.setColor(_darkShadowColor); // black drop shadow __|
654 g.drawLine(b.x, b.y + b.height - 1, b.x + b.width - 1, b.y + b.height - 1);
655 g.drawLine(b.x + b.width - 1, b.y, b.x + b.width - 1, b.y + b.height - 1);
656 g.setColor(old);
657 }
658
659 protected void paintRaisedBorder(Graphics g, Rectangle b) {
660 Color old = g.getColor();
661 g.setColor(_lightHighlightColor); // inner 3D border
662 g.drawLine(b.x, b.y, b.x + b.width - 1, b.y);
663 g.drawLine(b.x, b.y, b.x, b.y + b.height - 1);
664
665 g.setColor(_shadowColor); // black drop shadow __|
666 g.drawLine(b.x, b.y + b.height - 1, b.x + b.width - 1, b.y + b.height - 1);
667 g.drawLine(b.x + b.width - 1, b.y, b.x + b.width - 1, b.y + b.height - 1);
668 g.setColor(old);
669 }
670
671 protected class MouseInputHandler implements MouseInputListener {
672 public void mouseClicked(MouseEvent e) {
673 cancelMenuIfNecessary(e);
674 }
675
676 /**
677 * Invoked when the mouse has been clicked on the menu. This method clears or sets the selection path of the
678 * MenuSelectionManager.
679 *
680 * @param e the mouse event
681 */
682 public void mousePressed(MouseEvent e) {
683 JMenu menu = (JMenu) menuItem;
684 if (!menu.isEnabled())
685 return;
686
687 setMouseOver(true);
688
689 if (!SwingUtilities.isLeftMouseButton(e)) {
690 return;
691 }
692 if (isClickOnButton(e, menu)) {
693 if (((JideSplitButton) menuItem).isButtonEnabled()) {
694 // click button
695 menu.getModel().setArmed(true);
696 menu.getModel().setPressed(true);
697 }
698 if (!menu.hasFocus() && menu.isRequestFocusEnabled()) {
699 menu.requestFocus();
700 }
701 }
702 else {
703 downButtonPressed(menu);
704 }
705 }
706
707 private boolean isClickOnButton(MouseEvent e, JMenu menu) {
708 if (((JideSplitButton) menu).isAlwaysDropdown()) {
709 return false;
710 }
711
712 boolean clickOnDropDown = false;
713 if (BasicJideButtonUI.shouldWrapText(menuItem)) {
714 int size = 27;
715 if (JideSwingUtilities.getOrientationOf(menuItem) == SwingConstants.HORIZONTAL) {
716 if (e.getPoint().getY() < menu.getHeight() - size) {
717 clickOnDropDown = true;
718 }
719 }
720 else {
721 if (e.getPoint().getY() < menu.getHeight() - size) {
722 clickOnDropDown = true;
723 }
724 }
725 }
726 else {
727 int size = ((JMenu) menuItem).isTopLevelMenu() ? _splitButtonMargin : _splitButtonMarginOnMenu;
728 if (JideSwingUtilities.getOrientationOf(menuItem) == SwingConstants.HORIZONTAL) {
729 if (menu.getComponentOrientation().isLeftToRight()) {
730 if (e.getPoint().getX() < menu.getWidth() - size) {
731 clickOnDropDown = true;
732 }
733 }
734 else {
735 if (e.getPoint().getX() >= size) {
736 clickOnDropDown = true;
737 }
738 }
739 }
740 else {
741 if (e.getPoint().getY() < menu.getHeight() - size) {
742 clickOnDropDown = true;
743 }
744 }
745 }
746 return clickOnDropDown;
747 }
748
749 /**
750 * Invoked when the mouse has been released on the menu. Delegates the mouse event to the MenuSelectionManager.
751 *
752 * @param e the mouse event
753 */
754 public void mouseReleased(MouseEvent e) {
755 JMenu menu = (JMenu) menuItem;
756 if (!menu.isEnabled()) {
757 return;
758 }
759 if (!isClickOnButton(e, menu)) {
760 // these two lines order matters. In this order, it would not trigger actionPerformed.
761 menuItem.getModel().setArmed(false);
762 menuItem.getModel().setPressed(false);
763 }
764 cancelMenuIfNecessary(e);
765 }
766
767 private void cancelMenuIfNecessary(MouseEvent e) {
768 JMenu menu = (JMenu) menuItem;
769 if (!menu.isEnabled())
770 return;
771 if (isClickOnButton(e, menu)) {
772 if (((JideSplitButton) menuItem).isButtonEnabled()) {
773 // click button
774 // these two lines order matters. In this order, it would trigger actionPerformed.
775 if (SwingUtilities.isLeftMouseButton(e)) {
776 menu.getModel().setPressed(false);
777 menu.getModel().setArmed(false);
778 }
779 else {
780 menu.getModel().setArmed(false);
781 menu.getModel().setPressed(false);
782 }
783
784 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
785 MenuElement[] menuElements = manager.getSelectedPath();
786 for (int i = menuElements.length - 1; i >= 0; i--) {
787 MenuElement menuElement = menuElements[i];
788 if (menuElement instanceof JPopupMenu && ((JPopupMenu) menuElement).isAncestorOf(menu)) {
789 menu.getModel().setRollover(false);
790 setMouseOver(false);
791 manager.clearSelectedPath();
792 }
793 }
794 }
795 }
796 else {
797 // MenuSelectionManager manager =
798 // MenuSelectionManager.defaultManager();
799 // manager.processMouseEvent(e);
800 // if (!e.isConsumed())
801 // manager.clearSelectedPath();
802 }
803 }
804
805 /**
806 * Invoked when the cursor enters the menu. This method sets the selected path for the MenuSelectionManager and
807 * handles the case in which a menu item is used to pop up an additional menu, as in a hierarchical menu
808 * system.
809 *
810 * @param e the mouse event; not used
811 */
812 public void mouseEntered(MouseEvent e) {
813 JMenu menu = (JMenu) menuItem;
814 if (!menu.isEnabled())
815 return;
816
817 MenuSelectionManager manager =
818 MenuSelectionManager.defaultManager();
819 MenuElement selectedPath[] = manager.getSelectedPath();
820 if (!menu.isTopLevelMenu()) {
821 if (!(selectedPath.length > 0 &&
822 selectedPath[selectedPath.length - 1] ==
823 menu.getPopupMenu())) {
824 if (menu.getDelay() == 0) {
825 appendPath(getPath(), menu.getPopupMenu());
826 }
827 else {
828 manager.setSelectedPath(getPath());
829 setupPostTimer(menu);
830 }
831 }
832 }
833 else {
834 if (selectedPath.length > 0 &&
835 selectedPath[0] == menu.getParent()) {
836 MenuElement newPath[] = new MenuElement[3];
837 // A top level menu's parent is by definition
838 // a JMenuBar
839 newPath[0] = (MenuElement) menu.getParent();
840 newPath[1] = menu;
841 newPath[2] = menu.getPopupMenu();
842 manager.setSelectedPath(newPath);
843 }
844 }
845
846 if (!SwingUtilities.isLeftMouseButton(e)) {
847 setMouseOver(true);
848 }
849 menuItem.repaint();
850 }
851
852 public void mouseExited(MouseEvent e) {
853 setMouseOver(false);
854 menuItem.repaint();
855 }
856
857 /**
858 * Invoked when a mouse button is pressed on the menu and then dragged. Delegates the mouse event to the
859 * MenuSelectionManager.
860 *
861 * @param e the mouse event
862 * @see java.awt.event.MouseMotionListener#mouseDragged
863 */
864 public void mouseDragged(MouseEvent e) {
865 JMenu menu = (JMenu) menuItem;
866 if (!menu.isEnabled())
867 return;
868 MenuSelectionManager.defaultManager().processMouseEvent(e);
869 }
870
871 public void mouseMoved(MouseEvent e) {
872 JMenu menu = (JMenu) menuItem;
873 if (!menu.isEnabled())
874 return;
875
876 if (menuItem instanceof JideSplitButton) {
877 if (isClickOnButton(e, ((JMenu) menuItem))) {
878 ((SplitButtonModel) ((JideSplitButton) menuItem).getModel()).setButtonRollover(true);
879 }
880 else {
881 ((SplitButtonModel) ((JideSplitButton) menuItem).getModel()).setButtonRollover(false);
882 }
883 }
884 }
885 }
886
887 @Override
888 public Dimension getPreferredSize(JComponent c) {
889 if (!(c instanceof JMenu) || !((JMenu) c).isTopLevelMenu()) {
890 return super.getPreferredSize(c);
891 }
892
893 AbstractButton b = (AbstractButton) c;
894
895 boolean isHorizontal = JideSwingUtilities.getOrientationOf(c) == SwingConstants.HORIZONTAL;
896
897 Dimension d = JideSwingUtilities.getPreferredButtonSize(b, defaultTextIconGap, true); // TODO: we should use isHorizontal when JideSwingUtilities.getPreferredButtonSize supports it
898
899 if (BasicJideButtonUI.shouldWrapText(c)) {
900 if (c instanceof JideSplitButton) {
901 d.width += getAdjustExtraWidth(b, b.getText(), 8);
902 }
903 }
904 else {
905 d.width += getRightMargin();
906
907 if (isDownArrowVisible(b.getParent())) {
908 d.width += 1;
909 }
910 }
911
912 if (isHorizontal)
913 return d;
914 else
915 //noinspection SuspiciousNameCombination
916 return new Dimension(d.height, d.width); // swap width and height
917 }
918
919 @Override
920 public Dimension getMinimumSize(JComponent c) {
921 if (!(c instanceof JMenu) || !((JMenu) c).isTopLevelMenu()) {
922 return super.getMinimumSize(c);
923 }
924
925 Dimension d = getPreferredSize(c);
926 View v = (View) c.getClientProperty(BasicHTML.propertyKey);
927 if (v != null) {
928 if (JideSwingUtilities.getOrientationOf(c) == SwingConstants.HORIZONTAL)
929 d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
930 else // TODO: not sure if this is correct
931 d.height -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
932 }
933
934 return d;
935 }
936
937 @Override
938 public Dimension getMaximumSize(JComponent c) {
939 if (!(c instanceof JMenu) || !((JMenu) c).isTopLevelMenu()) {
940 return super.getMaximumSize(c);
941 }
942
943 Dimension d = getPreferredSize(c);
944 View v = (View) c.getClientProperty(BasicHTML.propertyKey);
945 if (v != null) {
946 if (JideSwingUtilities.getOrientationOf(c) == SwingConstants.HORIZONTAL)
947 d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
948 else // TODO: not sure if this is correct
949 d.height += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
950 }
951
952 return d;
953 }
954
955 @Override
956 protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect, String text) {
957 // Note: This method is almost identical to the same method in WindowsMenuItemUI
958 ButtonModel model = menuItem.getModel();
959
960 FontMetrics fm = menuItem.getFontMetrics(menuItem.getFont());
961 if (!(menuItem instanceof JMenu) || !((JMenu) menuItem).isTopLevelMenu()) {
962 int defaultTextIconGap = UIDefaultsLookup.getInt("MenuItem.textIconGap");
963 int defaultShadowWidth = UIDefaultsLookup.getInt("MenuItem.shadowWidth");
964 if (menuItem.getComponentOrientation().isLeftToRight()) {
965 textRect.x = defaultShadowWidth + defaultTextIconGap;
966 }
967 else {
968 // isLeftToRight is false
969 Rectangle2D rectText = fm.getStringBounds(text, g);
970 textRect.x = (int) (menuItem.getWidth() - defaultShadowWidth - defaultTextIconGap + rectText.getWidth() + (4 + menuItem.getHeight() / 2 - 1));
971 }
972 }
973 else if (!menuItem.getComponentOrientation().isLeftToRight()) {
974 if (menuItem.getComponentOrientation().isHorizontal()) {
975 Rectangle2D rectText = fm.getStringBounds(text, g);
976 textRect.x = (int) (menuItem.getWidth() - textRect.x - rectText.getWidth() + (4 + menuItem.getHeight() / 2 - 1));
977 }
978 }
979
980 int mnemonicIndex = menuItem.getDisplayedMnemonicIndex();
981 // W2K Feature: Check to see if the Underscore should be rendered.
982 if (LookAndFeelFactory.isMnemonicHidden()) {
983 mnemonicIndex = -1;
984 }
985
986 Color oldColor = g.getColor();
987
988 if (!model.isEnabled() || (menuItem instanceof JideSplitButton && !((JideSplitButton) menuItem).isButtonEnabled())) {
989 if (menuItem.getParent() != null) {
990 // *** paint the text disabled
991 g.setColor(menuItem.getParent().getBackground().brighter());
992
993 // JDK PORTING HINT
994 // JDK1.3: No drawStringUnderlineCharAt, draw the string then draw the underline
995 drawStringUnderlineCharAt(menuItem, g, text, mnemonicIndex,
996 textRect.x, textRect.y + fm.getAscent());
997 g.setColor(menuItem.getParent().getBackground().darker());
998 }
999
1000 // JDK PORTING HINT
1001 // JDK1.3: No drawStringUnderlineCharAt, draw the string then draw the underline
1002 drawStringUnderlineCharAt(menuItem, g, text, mnemonicIndex,
1003 textRect.x - 1, textRect.y + fm.getAscent() - 1);
1004 }
1005 else {
1006 // For Win95, the selected text color is the selection foreground color
1007 Color color = getForegroundOfState(menuItem);
1008 if(color == null || color instanceof UIResource) {
1009 if (model.isSelected()) {
1010 g.setColor(selectionForeground);
1011 }
1012 else {
1013 g.setColor(color);
1014 }
1015 }
1016 else {
1017 g.setColor(color);
1018 }
1019 drawStringUnderlineCharAt(menuItem, g, text,
1020 mnemonicIndex,
1021 textRect.x,
1022 textRect.y + fm.getAscent());
1023 }
1024 g.setColor(oldColor);
1025 }
1026
1027 private Color getForegroundOfState(JMenuItem menuItem) {
1028 int state = JideSwingUtilities.getButtonState(menuItem);
1029 Color foreground = null;
1030 if (menuItem instanceof ComponentStateSupport) {
1031 foreground = ((ComponentStateSupport) menuItem).getForegroundOfState(state);
1032 }
1033 if (foreground == null || foreground instanceof UIResource) {
1034 foreground = menuItem.getForeground();
1035 }
1036 return foreground;
1037 }
1038
1039 protected void drawStringUnderlineCharAt(JComponent c, Graphics g, String text,
1040 int underlinedIndex, int x, int y) {
1041 JideSwingUtilities.drawStringUnderlineCharAt(c, g, text, underlinedIndex, x, y);
1042 }
1043
1044 @Override
1045 protected void paintIcon(JMenuItem b, Graphics g) {
1046 ButtonModel model = b.getModel();
1047
1048 // Paint the Icon
1049 if (b.getIcon() != null) {
1050 // if (JideSwingUtilities.getOrientationOf(b) == SwingConstants.VERTICAL) {
1051 // ((Graphics2D) g).translate(0, b.getWidth() - 1);
1052 // ((Graphics2D) g).rotate(-Math.PI / 2);
1053 // }
1054
1055 Icon icon = getIcon(b);
1056
1057 if (icon != null) {
1058 if (!b.getComponentOrientation().isLeftToRight()) {
1059 if (b.getComponentOrientation().isHorizontal()) {
1060 iconRect.x = b.getWidth() - iconRect.x - icon.getIconWidth() + ( 4 + b.getHeight() / 2 - 1);
1061 }
1062 }
1063
1064 boolean enabled = model.isEnabled() && (!(model instanceof SplitButtonModel) || ((SplitButtonModel) model).isButtonEnabled());
1065 if (isFloatingIcon() && enabled) {
1066 if (model.isRollover() && !model.isPressed() && !model.isSelected()) {
1067 if (!"true".equals(SecurityUtils.getProperty("shadingtheme", "false"))) {
1068 if (icon instanceof ImageIcon) {
1069 ImageIcon shadow = IconsFactory.createGrayImage(((ImageIcon) icon).getImage());
1070 shadow.paintIcon(b, g, iconRect.x + 1, iconRect.y + 1);
1071 }
1072 else {
1073 ImageIcon shadow = IconsFactory.createGrayImage(b, icon);
1074 shadow.paintIcon(b, g, iconRect.x + 1, iconRect.y + 1);
1075 }
1076 icon.paintIcon(b, g, iconRect.x - 1, iconRect.y - 1);
1077 }
1078 else {
1079 icon.paintIcon(b, g, iconRect.x, iconRect.y);
1080 }
1081 }
1082 else {
1083 icon.paintIcon(b, g, iconRect.x, iconRect.y);
1084 }
1085 }
1086 else {
1087 icon.paintIcon(b, g, iconRect.x, iconRect.y);
1088 }
1089 }
1090
1091 // if (JideSwingUtilities.getOrientationOf(b) == SwingConstants.VERTICAL) {
1092 // ((Graphics2D) g).rotate(Math.PI / 2);
1093 // ((Graphics2D) g).translate(0, -b.getHeight() + 1);
1094 // }
1095 }
1096 }
1097
1098 @Override
1099 protected boolean isFloatingIcon() {
1100 return _isFloatingIcon;
1101 }
1102
1103 @Override
1104 protected Icon getIcon(AbstractButton b) {
1105 ButtonModel model = b.getModel();
1106 Icon icon = b.getIcon();
1107 Icon tmpIcon = null;
1108 if (!model.isEnabled() || !((JideSplitButton) menuItem).isButtonEnabled()) {
1109 if (model.isSelected()) {
1110 tmpIcon = b.getDisabledSelectedIcon();
1111 }
1112 else {
1113 tmpIcon = b.getDisabledIcon();
1114 }
1115
1116 // create default disabled icon
1117 if (tmpIcon == null) {
1118 if (icon instanceof ImageIcon) {
1119 icon = IconsFactory.createGrayImage(((ImageIcon) icon).getImage());
1120 }
1121 else {
1122 icon = IconsFactory.createGrayImage(b, icon);
1123 }
1124 }
1125 }
1126 else if (model.isPressed() && model.isArmed()) {
1127 tmpIcon = b.getPressedIcon();
1128 if (tmpIcon != null) {
1129 // revert back to 0 offset
1130 // clearTextShiftOffset();
1131 }
1132 }
1133 else if (b.isRolloverEnabled() && model.isRollover()) {
1134 if (model.isSelected()) {
1135 tmpIcon = b.getRolloverSelectedIcon();
1136 }
1137 else {
1138 tmpIcon = b.getRolloverIcon();
1139 }
1140 }
1141 else if (model.isSelected()) {
1142 tmpIcon = b.getSelectedIcon();
1143 }
1144
1145 if (tmpIcon != null) {
1146 icon = tmpIcon;
1147 }
1148 return icon;
1149 }
1150
1151 /**
1152 * The gap between the button part and the drop down menu part.
1153 *
1154 * @return the gap.
1155 */
1156 protected int getOffset() {
1157 return 0;
1158 }
1159
1160 protected boolean isAlwaysDropdown(JMenuItem menuItem) {
1161 return menuItem instanceof JideSplitButton && ((JideSplitButton) menuItem).isAlwaysDropdown();
1162 }
1163
1164 @Override
1165 protected int getRightMargin() {
1166 return _splitButtonMargin - 1;
1167 }
1168
1169 /**
1170 * Actions for Buttons. Two type of action are supported: pressed: Moves the button to a pressed state released:
1171 * Disarms the button.
1172 */
1173 private static class Actions extends UIAction {
1174 private static final String PRESS = "pressed";
1175 private static final String RELEASE = "released";
1176 private static final String DOWN_PRESS = "downPressed";
1177 private static final String DOWN_RELEASE = "downReleased";
1178
1179 Actions(String name) {
1180 super(name);
1181 }
1182
1183 public void actionPerformed(ActionEvent e) {
1184 AbstractButton b = (AbstractButton) e.getSource();
1185 String key = getName();
1186
1187 // if isAlwaysDropDown it true, treat PRESS as DOWN_PRESS
1188 if (PRESS.equals(key) && ((JideSplitButton) b).isAlwaysDropdown()) {
1189 key = DOWN_PRESS;
1190 }
1191
1192 if (PRESS.equals(key)) {
1193 ButtonModel model = b.getModel();
1194 model.setArmed(true);
1195 model.setPressed(true);
1196 if (!b.hasFocus()) {
1197 b.requestFocus();
1198 }
1199 }
1200 else if (RELEASE.equals(key)) {
1201 ButtonModel model = b.getModel();
1202 model.setPressed(false);
1203 model.setArmed(false);
1204 }
1205 else if (DOWN_PRESS.equals(key)) {
1206 downButtonPressed((JMenu) b);
1207 }
1208 else if (DOWN_RELEASE.equals(key)) {
1209 }
1210 }
1211
1212 @Override
1213 public boolean isEnabled(Object sender) {
1214 return !(sender != null && (sender instanceof AbstractButton) &&
1215 !((AbstractButton) sender).getModel().isEnabled());
1216 }
1217 }
1218
1219 /**
1220 * Populates Buttons actions.
1221 *
1222 * @param map the map
1223 */
1224 public static void loadActionMap(LazyActionMap map) {
1225 map.put(new Actions(Actions.PRESS));
1226 map.put(new Actions(Actions.RELEASE));
1227 map.put(new Actions(Actions.DOWN_PRESS));
1228 map.put(new Actions(Actions.DOWN_RELEASE));
1229 }
1230
1231 @Override
1232 protected void updateMnemonicBinding() {
1233 super.updateMnemonicBinding();
1234 int mnemonic = menuItem.getModel().getMnemonic();
1235 if (mnemonic != 0 && windowInputMap != null) {
1236 int[] shortcutKeys = (int[]) UIDefaultsLookup.get("Menu.shortcutKeys");
1237 if (shortcutKeys == null) {
1238 shortcutKeys = new int[]{KeyEvent.ALT_MASK};
1239 }
1240 for (int shortcutKey : shortcutKeys) {
1241 windowInputMap.put(KeyStroke.getKeyStroke(mnemonic,
1242 shortcutKey, false),
1243 "pressed");
1244 windowInputMap.put(KeyStroke.getKeyStroke(mnemonic,
1245 shortcutKey, true),
1246 "released");
1247 }
1248 }
1249 }
1250
1251 protected static void downButtonPressed(JMenu menu) {
1252 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
1253 if (menu.isTopLevelMenu()) {
1254 if (menu.isSelected()) {
1255 manager.clearSelectedPath();
1256 }
1257 else {
1258 //Container cnt = menu.getParent();
1259 Container cnt = getFirstParentMenuElement(menu);
1260
1261 if (cnt != null && cnt instanceof MenuElement) {
1262 ArrayList<Component> parents = new ArrayList<Component>();
1263 while (cnt instanceof MenuElement) {
1264 parents.add(0, cnt);
1265 if (cnt instanceof JPopupMenu) {
1266 cnt = (Container) ((JPopupMenu) cnt).getInvoker();
1267 }
1268 else {
1269 //cnt = cnt.getParent();
1270 cnt = getFirstParentMenuElement(cnt);
1271 }
1272 }
1273
1274 MenuElement me[] = new MenuElement[parents.size() + 1];
1275 for (int i = 0; i < parents.size(); i++) {
1276 Container container = (Container) parents.get(i);
1277 me[i] = (MenuElement) container;
1278 }
1279 me[parents.size()] = menu;
1280 manager.setSelectedPath(me);
1281 }
1282 else {
1283 MenuElement me[] = new MenuElement[1];
1284 me[0] = menu;
1285 manager.setSelectedPath(me);
1286 }
1287 }
1288 }
1289
1290 MenuElement selectedPath[] = manager.getSelectedPath();
1291 if (selectedPath.length > 0 &&
1292 selectedPath[selectedPath.length - 1] != menu.getPopupMenu()) {
1293 if (menu.isTopLevelMenu() ||
1294 menu.getDelay() == 0) {
1295 appendPath(selectedPath, menu.getPopupMenu());
1296 }
1297 else {
1298 setupPostTimer(menu);
1299 }
1300 }
1301 }
1302
1303 protected static Container getFirstParentMenuElement(Component comp) {
1304 Container parent = comp.getParent();
1305
1306 while (parent != null) {
1307 if (parent instanceof MenuElement)
1308 return parent;
1309
1310 parent = parent.getParent();
1311 }
1312
1313 return null;
1314 }
1315
1316
1317 /**
1318 * @param c the component
1319 * @param text the text
1320 * @param extraWidth the extra width
1321 * @return the adjusted width.
1322 */
1323 public static int getAdjustExtraWidth(Component c, String text, int extraWidth) {
1324 String[] lines = getWrappedText(text);
1325 Font font = c.getFont();
1326 FontMetrics fm = c.getFontMetrics(font);
1327 int line1Width = fm.stringWidth(lines[0]);
1328 int line2Width = lines.length <= 1 ? 0 : fm.stringWidth(lines[1]);
1329 int oldMaxWidth = Math.max(line1Width, line2Width);
1330 line2Width += extraWidth;
1331 int maxWidth = Math.max(line1Width, line2Width);
1332 return maxWidth - oldMaxWidth;
1333 }
1334
1335 public static String getMaxLengthWord(String text) {
1336 if (text.indexOf(' ') == -1) {
1337 return text;
1338 }
1339 else {
1340 int minDiff = text.length();
1341 int minPos = -1;
1342 int mid = text.length() / 2;
1343
1344 int pos = -1;
1345 while (true) {
1346 pos = text.indexOf(' ', pos + 1);
1347 if (pos == -1) {
1348 break;
1349 }
1350 int diff = Math.abs(pos - mid);
1351 if (diff < minDiff) {
1352 minDiff = diff;
1353 minPos = pos;
1354 }
1355 }
1356 return minPos >= mid ? text.substring(0, minPos) : text.substring(minPos + 1);
1357 }
1358 }
1359
1360 /**
1361 * Gets the text after wrapping. Please note, it will only wrap text into two lines thus it is not designed for
1362 * general usage.
1363 *
1364 * @param text the text
1365 * @return the two lines.
1366 */
1367 public static String[] getWrappedText(String text) {
1368 String[] words = text.split(" ");
1369 if (words.length <= 2) {
1370 return words; // no line break
1371 }
1372 else if (words.length >= 3) {
1373 int minDiff = text.length();
1374 int minPos = -1;
1375 int pos = -1;
1376 int mid = text.length() / 2;
1377 while (true) {
1378 pos = text.indexOf(' ', pos + 1);
1379 if (pos == -1) {
1380 break;
1381 }
1382 int diff = Math.abs(pos - mid);
1383 if (diff < minDiff) {
1384 minDiff = diff;
1385 minPos = pos;
1386 }
1387 }
1388 return new String[]{text.substring(0, minPos), text.substring(minPos + 1)};
1389 }
1390
1391 return words;
1392 }
1393 }
1394
1395
1396