KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicRootPaneUI


1 /*
2  * @(#)BasicRootPaneUI.java 1.15 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.basic;
9
10 import java.awt.event.ActionEvent JavaDoc;
11 import java.awt.KeyboardFocusManager JavaDoc;
12 import java.awt.Component JavaDoc;
13 import java.awt.Point JavaDoc;
14 import java.awt.Rectangle JavaDoc;
15 import java.beans.PropertyChangeEvent JavaDoc;
16 import java.beans.PropertyChangeListener JavaDoc;
17 import javax.swing.*;
18 import javax.swing.plaf.*;
19 import sun.swing.DefaultLookup;
20 import sun.swing.UIAction;
21
22 /**
23  * Basic implementation of RootPaneUI, there is one shared between all
24  * JRootPane instances.
25  *
26  * @version 1.15 12/19/03
27  * @author Scott Violet
28  */

29 public class BasicRootPaneUI extends RootPaneUI implements
30                   PropertyChangeListener JavaDoc {
31     private static RootPaneUI rootPaneUI = new BasicRootPaneUI JavaDoc();
32
33     public static ComponentUI createUI(JComponent c) {
34         return rootPaneUI;
35     }
36
37     public void installUI(JComponent c) {
38         installDefaults((JRootPane)c);
39         installComponents((JRootPane)c);
40         installListeners((JRootPane)c);
41         installKeyboardActions((JRootPane)c);
42     }
43
44     
45     public void uninstallUI(JComponent c) {
46         uninstallDefaults((JRootPane)c);
47         uninstallComponents((JRootPane)c);
48         uninstallListeners((JRootPane)c);
49         uninstallKeyboardActions((JRootPane)c);
50     }
51
52     protected void installDefaults(JRootPane c){
53         LookAndFeel.installProperty(c, "opaque", Boolean.FALSE);
54     }
55
56     protected void installComponents(JRootPane root) {
57     }
58
59     protected void installListeners(JRootPane root) {
60     root.addPropertyChangeListener(this);
61     }
62
63     protected void installKeyboardActions(JRootPane root) {
64     InputMap km = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, root);
65     SwingUtilities.replaceUIInputMap(root,
66                 JComponent.WHEN_IN_FOCUSED_WINDOW, km);
67         km = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
68                 root);
69         SwingUtilities.replaceUIInputMap(root,
70                 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, km);
71
72         LazyActionMap.installLazyActionMap(root, BasicRootPaneUI JavaDoc.class,
73                 "RootPane.actionMap");
74     updateDefaultButtonBindings(root);
75     }
76
77     protected void uninstallDefaults(JRootPane root) {
78     }
79
80     protected void uninstallComponents(JRootPane root) {
81     }
82
83     protected void uninstallListeners(JRootPane root) {
84     root.removePropertyChangeListener(this);
85     }
86
87     protected void uninstallKeyboardActions(JRootPane root) {
88     SwingUtilities.replaceUIInputMap(root, JComponent.
89                        WHEN_IN_FOCUSED_WINDOW, null);
90     SwingUtilities.replaceUIActionMap(root, null);
91     }
92
93     InputMap getInputMap(int condition, JComponent c) {
94         if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
95             return (InputMap)DefaultLookup.get(c, this,
96                                        "RootPane.ancestorInputMap");
97         }
98
99     if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
100         return createInputMap(condition, c);
101     }
102     return null;
103     }
104
105     ComponentInputMap createInputMap(int condition, JComponent c) {
106     return new RootPaneInputMap(c);
107     }
108
109     static void loadActionMap(LazyActionMap JavaDoc map) {
110     map.put(new Actions(Actions.PRESS));
111     map.put(new Actions(Actions.RELEASE));
112     map.put(new Actions(Actions.POST_POPUP));
113     }
114
115     /**
116      * Invoked when the default button property has changed. This reloads
117      * the bindings from the defaults table with name
118      * <code>RootPane.defaultButtonWindowKeyBindings</code>.
119      */

120     void updateDefaultButtonBindings(JRootPane root) {
121     InputMap km = SwingUtilities.getUIInputMap(root, JComponent.
122                            WHEN_IN_FOCUSED_WINDOW);
123     while (km != null && !(km instanceof RootPaneInputMap)) {
124         km = km.getParent();
125     }
126     if (km != null) {
127         km.clear();
128         if (root.getDefaultButton() != null) {
129         Object JavaDoc[] bindings = (Object JavaDoc[])DefaultLookup.get(root, this,
130                            "RootPane.defaultButtonWindowKeyBindings");
131         if (bindings != null) {
132             LookAndFeel.loadKeyBindings(km, bindings);
133         }
134         }
135     }
136     }
137
138     /**
139      * Invoked when a property changes on the root pane. If the event
140      * indicates the <code>defaultButton</code> has changed, this will
141      * reinstall the keyboard actions.
142      */

143     public void propertyChange(PropertyChangeEvent JavaDoc e) {
144     if(e.getPropertyName().equals("defaultButton")) {
145         JRootPane rootpane = (JRootPane)e.getSource();
146         updateDefaultButtonBindings(rootpane);
147         if (rootpane.getClientProperty("temporaryDefaultButton") == null) {
148         rootpane.putClientProperty("initialDefaultButton", e.getNewValue());
149         }
150     }
151     }
152
153
154     static class Actions extends UIAction {
155         public static final String JavaDoc PRESS = "press";
156         public static final String JavaDoc RELEASE = "release";
157         public static final String JavaDoc POST_POPUP = "postPopup";
158
159         Actions(String JavaDoc name) {
160             super(name);
161         }
162
163         public void actionPerformed(ActionEvent JavaDoc evt) {
164             JRootPane root = (JRootPane)evt.getSource();
165         JButton owner = root.getDefaultButton();
166             String JavaDoc key = getName();
167
168             if (key == POST_POPUP) { // Action to post popup
169
Component JavaDoc c = KeyboardFocusManager
170                         .getCurrentKeyboardFocusManager()
171                          .getFocusOwner();
172
173                 if(c instanceof JComponent) {
174                     JComponent src = (JComponent) c;
175                     JPopupMenu jpm = src.getComponentPopupMenu();
176                     if(jpm != null) {
177                         Point JavaDoc pt = src.getPopupLocation(null);
178                         if(pt == null) {
179                             Rectangle JavaDoc vis = src.getVisibleRect();
180                             pt = new Point JavaDoc(vis.x+vis.width/2,
181                                            vis.y+vis.height/2);
182                         }
183                         jpm.show(c, pt.x, pt.y);
184                     }
185                 }
186             }
187             else if (owner != null
188                      && SwingUtilities.getRootPane(owner) == root) {
189                 if (key == PRESS) {
190                     owner.doClick(20);
191                 }
192             }
193         }
194
195         public boolean isEnabled(Object JavaDoc sender) {
196             String JavaDoc key = getName();
197             if(key == POST_POPUP) {
198                 MenuElement[] elems = MenuSelectionManager
199                         .defaultManager()
200                         .getSelectedPath();
201                 if(elems != null && elems.length != 0) {
202                     return false;
203                     // We shall not interfere with already opened menu
204
}
205
206                 Component JavaDoc c = KeyboardFocusManager
207                        .getCurrentKeyboardFocusManager()
208                         .getFocusOwner();
209                 if(c instanceof JComponent) {
210                     JComponent src = (JComponent) c;
211                     return src.getComponentPopupMenu() != null;
212                 }
213
214                 return false;
215             }
216                 
217             if (sender != null && sender instanceof JRootPane) {
218             JButton owner = ((JRootPane)sender).getDefaultButton();
219                 return (owner != null && owner.getModel().isEnabled());
220             }
221             return true;
222         }
223     }
224
225     private static class RootPaneInputMap extends ComponentInputMapUIResource {
226     public RootPaneInputMap(JComponent c) {
227         super(c);
228     }
229     }
230 }
231
Popular Tags