KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > windows > WindowsPopupMenuUI


1 /*
2  * @(#)WindowsPopupMenuUI.java 1.22 07/01/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.windows;
9
10 import java.awt.Component JavaDoc;
11 import java.awt.Graphics JavaDoc;
12 import java.awt.Insets JavaDoc;
13 import java.awt.KeyEventPostProcessor JavaDoc;
14 import java.awt.KeyboardFocusManager JavaDoc;
15 import java.awt.Window JavaDoc;
16 import java.awt.event.KeyEvent JavaDoc;
17 import javax.swing.*;
18 import javax.swing.event.*;
19 import javax.swing.plaf.*;
20 import javax.swing.plaf.basic.*;
21
22 import com.sun.java.swing.plaf.windows.TMSchema.Part;
23 import com.sun.java.swing.plaf.windows.TMSchema.State;
24 import com.sun.java.swing.plaf.windows.XPStyle.Skin;
25
26 /**
27  * Windows rendition of the component.
28  * <p>
29  * <strong>Warning:</strong>
30  * Serialized objects of this class will not be compatible with
31  * future Swing releases. The current serialization support is appropriate
32  * for short term storage or RMI between applications running the same
33  * version of Swing. A future release of Swing will provide support for
34  * long term persistence.
35  *
36  * @version 1.22 01/18/07
37  * @author Igor Kushnirskiy
38  */

39 public class WindowsPopupMenuUI extends BasicPopupMenuUI {
40
41     static MnemonicListener mnemonicListener = null;
42     static final Object JavaDoc GUTTER_OFFSET_KEY =
43         new StringBuilder JavaDoc("GUTTER_OFFSET_KEY");
44
45     public static ComponentUI createUI(JComponent c) {
46     return new WindowsPopupMenuUI();
47     }
48
49     public void installListeners() {
50         super.installListeners();
51     if (! UIManager.getBoolean("Button.showMnemonics") &&
52             mnemonicListener == null) {
53
54             mnemonicListener = new MnemonicListener();
55             MenuSelectionManager.defaultManager().
56                 addChangeListener(mnemonicListener);
57         }
58     }
59
60     /**
61      * Returns the <code>Popup</code> that will be responsible for
62      * displaying the <code>JPopupMenu</code>.
63      *
64      * @param popupMenu JPopupMenu requesting Popup
65      * @param x Screen x location Popup is to be shown at
66      * @param y Screen y location Popup is to be shown at.
67      * @return Popup that will show the JPopupMenu
68      * @since 1.4
69      */

70     public Popup getPopup(JPopupMenu popupMenu, int x, int y) {
71         PopupFactory popupFactory = PopupFactory.getSharedInstance();
72         return popupFactory.getPopup(popupMenu.getInvoker(), popupMenu, x, y);
73     }
74
75     static class MnemonicListener implements ChangeListener {
76         JRootPane repaintRoot = null;
77
78         public void stateChanged(ChangeEvent ev) {
79         MenuSelectionManager msm = (MenuSelectionManager)ev.getSource();
80             MenuElement[] path = msm.getSelectedPath();
81             if (path.length == 0) {
82                 if(!WindowsLookAndFeel.isMnemonicHidden()) {
83                     // menu was canceled -- hide mnemonics
84
WindowsLookAndFeel.setMnemonicHidden(true);
85                     if (repaintRoot != null) {
86                         Window JavaDoc win =
87                             SwingUtilities.getWindowAncestor(repaintRoot);
88                         WindowsUtils.repaintMnemonicsInWindow(win);
89                     }
90                 }
91             } else {
92                 Component JavaDoc c = (Component JavaDoc)path[0];
93                 if (c instanceof JPopupMenu) c = ((JPopupMenu)c).getInvoker();
94                 repaintRoot = SwingUtilities.getRootPane(c);
95             }
96         }
97     }
98     
99     /**
100      * Returns offset for the text.
101      * BasicMenuItemUI sets max text offset on the JPopupMenuUI.
102      * Note: for 1.5 version it always returns {@code -1}
103      * @param c PopupMenu to return text offset for.
104      * @return text offset for the component
105      */

106     static int getTextOffset(JComponent c) {
107         int rv = -1;
108         return rv;
109     }
110     
111     /**
112      * Returns span before gutter.
113      * used only on Vista.
114      * @return span before gutter
115      */

116     static int getSpanBeforeGutter() {
117         return 3;
118     }
119     
120     /**
121      * Returns span after gutter.
122      * used only on Vista.
123      * @return span after gutter
124      */

125     static int getSpanAfterGutter() {
126         return 3;
127     }
128     
129     /**
130      * Returns gutter width.
131      * used only on Vista.
132      * @return width of the gutter
133      */

134     static int getGutterWidth() {
135         int rv = 2;
136         XPStyle xp = XPStyle.getXP();
137         if (xp != null) {
138             Skin skin = xp.getSkin(null, Part.MP_POPUPGUTTER);
139             rv = skin.getWidth();
140         }
141         return rv;
142     }
143
144     /**
145      * Checks if PopupMenu is leftToRight
146      * The orientation is derived from the children of the component.
147      * It is leftToRight if all the children are leftToRight
148      *
149      * @param c component to return orientation for
150      * @return true if all the children are leftToRight
151      */

152     private static boolean isLeftToRight(JComponent c) {
153         boolean leftToRight = true;
154         for (int i = c.getComponentCount() - 1; i >=0 && leftToRight; i-- ) {
155             leftToRight =
156                 c.getComponent(i).getComponentOrientation().isLeftToRight();
157         }
158         return leftToRight;
159     }
160     
161     @Override JavaDoc
162     public void paint(Graphics JavaDoc g, JComponent c) {
163         /*
164          * Note: 1.5 backport does not paint gutter.
165          * See comment for getTextOffset()
166          */

167         if (WindowsMenuItemUI.isVistaPainting()) {
168             XPStyle xp = XPStyle.getXP();
169             Skin skin = xp.getSkin(c, Part.MP_POPUPBACKGROUND);
170             skin.paintSkin(g, 0, 0, c.getWidth(),c.getHeight(), State.NORMAL);
171             int textOffset = getTextOffset(c);
172             if (textOffset >= 0
173                     /* paint gutter only for leftToRight case */
174                     && isLeftToRight(c)) {
175                 skin = xp.getSkin(c, Part.MP_POPUPGUTTER);
176                 int gutterWidth = getGutterWidth();
177                 int gutterOffset =
178                     textOffset - getSpanAfterGutter() - gutterWidth;
179                 c.putClientProperty(GUTTER_OFFSET_KEY,
180                     Integer.valueOf(gutterOffset));
181                 Insets JavaDoc insets = c.getInsets();
182                 skin.paintSkin(g, gutterOffset, insets.top,
183                     gutterWidth, c.getHeight() - insets.bottom - insets.top,
184                     State.NORMAL);
185             } else {
186                 if (c.getClientProperty(GUTTER_OFFSET_KEY) != null) {
187                     c.putClientProperty(GUTTER_OFFSET_KEY, null);
188                 }
189             }
190         } else {
191             super.paint(g, c);
192         }
193     }
194 }
195
Popular Tags