Merge from mainline.
[official-gcc.git] / libjava / classpath / javax / swing / plaf / basic / BasicRootPaneUI.java
blob933db4c6bc21bb65737ead6747c575dfae78ad06
1 /* BasicRootPaneUI.java --
2 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.plaf.basic;
41 import java.awt.event.ActionEvent;
42 import java.beans.PropertyChangeEvent;
43 import java.beans.PropertyChangeListener;
45 import javax.swing.AbstractAction;
46 import javax.swing.ButtonModel;
47 import javax.swing.InputMap;
48 import javax.swing.JButton;
49 import javax.swing.JComponent;
50 import javax.swing.JRootPane;
51 import javax.swing.LookAndFeel;
52 import javax.swing.SwingUtilities;
53 import javax.swing.UIManager;
54 import javax.swing.plaf.ActionMapUIResource;
55 import javax.swing.plaf.ComponentInputMapUIResource;
56 import javax.swing.plaf.ComponentUI;
57 import javax.swing.plaf.RootPaneUI;
59 public class BasicRootPaneUI extends RootPaneUI
60 implements PropertyChangeListener
63 /**
64 * Performed when the user activates the default button inside the JRootPane,
65 * usually by pressing 'ENTER'.
67 private class DefaultPressAction
68 extends AbstractAction
70 /**
71 * The JRootPane for which this action should be installed.
73 private JRootPane rootPane;
75 /**
76 * Creates a new DefaultPressAction for the specified JRootPane.
78 DefaultPressAction(JRootPane rp)
80 rootPane = rp;
83 /**
84 * Performes the action.
86 public void actionPerformed(ActionEvent ev)
88 JButton b = rootPane.getDefaultButton();
89 if (b != null)
91 ButtonModel m = b.getModel();
92 m.setArmed(true);
93 m.setPressed(true);
98 /**
99 * Performed when the user activates the default button inside the JRootPane,
100 * usually by releasing 'ENTER'.
102 private class DefaultReleaseAction
103 extends AbstractAction
106 * The JRootPane for which this action should be installed.
108 private JRootPane rootPane;
111 * Creates a new DefaultReleaseAction for the specified JRootPane.
113 DefaultReleaseAction(JRootPane rp)
115 rootPane = rp;
119 * Performes the action.
121 public void actionPerformed(ActionEvent ev)
123 JButton b = rootPane.getDefaultButton();
124 if (b != null)
126 ButtonModel m = b.getModel();
127 m.setPressed(false);
128 m.setArmed(false);
133 public static ComponentUI createUI(JComponent x)
135 return new BasicRootPaneUI();
138 public void installUI(JComponent c)
140 super.installUI(c);
141 if (c instanceof JRootPane)
143 JRootPane rp = (JRootPane) c;
144 installDefaults(rp);
145 installComponents(rp);
146 installListeners(rp);
147 installKeyboardActions(rp);
152 * Installs the look and feel defaults for JRootPane.
154 * @param rp the root pane to install the defaults to
156 protected void installDefaults(JRootPane rp)
158 // TODO: What to do here, if anything? (might be a hook method)
162 * Installs additional look and feel components to the root pane.
164 * @param rp the root pane to install the components to
166 protected void installComponents(JRootPane rp)
168 // All components are initialized in the JRootPane constructor, and since
169 // the createXXXPane methods are protected, I see no reasonable way,
170 // and no need to initialize them here. This method is here anyway
171 // for compatibility and to provide the necessary hooks to subclasses.
175 * Installs any look and feel specific listeners on the root pane.
177 * @param rp the root pane to install the listeners to
179 protected void installListeners(JRootPane rp)
181 rp.addPropertyChangeListener(this);
185 * Installs look and feel keyboard actions on the root pane.
187 * @param rp the root pane to install the keyboard actions to
189 protected void installKeyboardActions(JRootPane rp)
191 // Install the keyboard actions.
192 ActionMapUIResource am = new ActionMapUIResource();
193 am.put("press", new DefaultPressAction(rp));
194 am.put("release", new DefaultReleaseAction(rp));
195 SwingUtilities.replaceUIActionMap(rp, am);
197 // Install the input map from the UIManager. It seems like the actual
198 // bindings are installed in the JRootPane only when the defaultButton
199 // property receives a value. So we also only install an empty
200 // input map here, and fill it in propertyChange.
201 ComponentInputMapUIResource im = new ComponentInputMapUIResource(rp);
202 SwingUtilities.replaceUIInputMap(rp, JComponent.WHEN_IN_FOCUSED_WINDOW,
203 im);
206 public void propertyChange(PropertyChangeEvent event)
208 JRootPane source = (JRootPane) event.getSource();
209 String propertyName = event.getPropertyName();
210 if (propertyName.equals("defaultButton"))
212 Object newValue = event.getNewValue();
213 InputMap im =
214 SwingUtilities.getUIInputMap(source,
215 JComponent.WHEN_IN_FOCUSED_WINDOW);
216 if (newValue != null)
218 Object[] keybindings =
219 (Object[]) UIManager.get
220 ("RootPane.defaultButtonWindowKeyBindings");
221 LookAndFeel.loadKeyBindings(im, keybindings);
223 else
225 im.clear();
231 * Uninstalls this UI from the root pane. This calls
232 * {@link #uninstallDefaults}, {@link #uninstallComponents},
233 * {@link #uninstallListeners}, {@link #uninstallKeyboardActions}
234 * in this order.
236 * @param c the root pane to uninstall the UI from
238 public void uninstallUI(JComponent c)
240 super.uninstallUI(c);
241 if (c instanceof JRootPane)
243 JRootPane rp = (JRootPane) c;
244 uninstallDefaults(rp);
245 uninstallComponents(rp);
246 uninstallListeners(rp);
247 uninstallKeyboardActions(rp);
252 * Uninstalls the look and feel defaults that have been installed in
253 * {@link #installDefaults}.
255 * @param rp the root pane to uninstall the defaults from
257 protected void uninstallDefaults(JRootPane rp)
259 // We do nothing here.
263 * Uninstalls look and feel components from the root pane.
265 * @param rp the root pane to uninstall the components from
267 protected void uninstallComponents(JRootPane rp)
269 // We do nothing here.
273 * Uninstalls any look and feel specific listeners from the root pane.
275 * @param rp the root pane to uninstall the listeners from
277 protected void uninstallListeners(JRootPane rp)
279 rp.removePropertyChangeListener(this);
283 * Uninstalls look and feel keyboard actions from the root pane.
285 * @param rp the root pane to uninstall the keyboard actions from
287 protected void uninstallKeyboardActions(JRootPane rp)
289 SwingUtilities.replaceUIActionMap(rp, null);
290 SwingUtilities.replaceUIInputMap(rp, JComponent.WHEN_IN_FOCUSED_WINDOW,
291 null);