0N/A/*
2362N/A * Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing.plaf.basic;
0N/A
0N/Aimport java.awt.event.ActionEvent;
0N/Aimport java.awt.KeyboardFocusManager;
0N/Aimport java.awt.Component;
0N/Aimport java.awt.Point;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.beans.PropertyChangeEvent;
0N/Aimport java.beans.PropertyChangeListener;
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.plaf.*;
0N/Aimport sun.swing.DefaultLookup;
0N/Aimport sun.swing.UIAction;
0N/A
0N/A/**
0N/A * Basic implementation of RootPaneUI, there is one shared between all
0N/A * JRootPane instances.
0N/A *
0N/A * @author Scott Violet
0N/A * @since 1.3
0N/A */
0N/Apublic class BasicRootPaneUI extends RootPaneUI implements
0N/A PropertyChangeListener {
0N/A private static RootPaneUI rootPaneUI = new BasicRootPaneUI();
0N/A
0N/A public static ComponentUI createUI(JComponent c) {
0N/A return rootPaneUI;
0N/A }
0N/A
0N/A public void installUI(JComponent c) {
0N/A installDefaults((JRootPane)c);
0N/A installComponents((JRootPane)c);
0N/A installListeners((JRootPane)c);
0N/A installKeyboardActions((JRootPane)c);
0N/A }
0N/A
0N/A
0N/A public void uninstallUI(JComponent c) {
0N/A uninstallDefaults((JRootPane)c);
0N/A uninstallComponents((JRootPane)c);
0N/A uninstallListeners((JRootPane)c);
0N/A uninstallKeyboardActions((JRootPane)c);
0N/A }
0N/A
0N/A protected void installDefaults(JRootPane c){
0N/A LookAndFeel.installProperty(c, "opaque", Boolean.FALSE);
0N/A }
0N/A
0N/A protected void installComponents(JRootPane root) {
0N/A }
0N/A
0N/A protected void installListeners(JRootPane root) {
0N/A root.addPropertyChangeListener(this);
0N/A }
0N/A
0N/A protected void installKeyboardActions(JRootPane root) {
0N/A InputMap km = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, root);
0N/A SwingUtilities.replaceUIInputMap(root,
0N/A JComponent.WHEN_IN_FOCUSED_WINDOW, km);
0N/A km = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
0N/A root);
0N/A SwingUtilities.replaceUIInputMap(root,
0N/A JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, km);
0N/A
0N/A LazyActionMap.installLazyActionMap(root, BasicRootPaneUI.class,
0N/A "RootPane.actionMap");
0N/A updateDefaultButtonBindings(root);
0N/A }
0N/A
0N/A protected void uninstallDefaults(JRootPane root) {
0N/A }
0N/A
0N/A protected void uninstallComponents(JRootPane root) {
0N/A }
0N/A
0N/A protected void uninstallListeners(JRootPane root) {
0N/A root.removePropertyChangeListener(this);
0N/A }
0N/A
0N/A protected void uninstallKeyboardActions(JRootPane root) {
0N/A SwingUtilities.replaceUIInputMap(root, JComponent.
0N/A WHEN_IN_FOCUSED_WINDOW, null);
0N/A SwingUtilities.replaceUIActionMap(root, null);
0N/A }
0N/A
0N/A InputMap getInputMap(int condition, JComponent c) {
0N/A if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
0N/A return (InputMap)DefaultLookup.get(c, this,
0N/A "RootPane.ancestorInputMap");
0N/A }
0N/A
0N/A if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
0N/A return createInputMap(condition, c);
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A ComponentInputMap createInputMap(int condition, JComponent c) {
0N/A return new RootPaneInputMap(c);
0N/A }
0N/A
0N/A static void loadActionMap(LazyActionMap map) {
0N/A map.put(new Actions(Actions.PRESS));
0N/A map.put(new Actions(Actions.RELEASE));
0N/A map.put(new Actions(Actions.POST_POPUP));
0N/A }
0N/A
0N/A /**
0N/A * Invoked when the default button property has changed. This reloads
0N/A * the bindings from the defaults table with name
0N/A * <code>RootPane.defaultButtonWindowKeyBindings</code>.
0N/A */
0N/A void updateDefaultButtonBindings(JRootPane root) {
0N/A InputMap km = SwingUtilities.getUIInputMap(root, JComponent.
0N/A WHEN_IN_FOCUSED_WINDOW);
0N/A while (km != null && !(km instanceof RootPaneInputMap)) {
0N/A km = km.getParent();
0N/A }
0N/A if (km != null) {
0N/A km.clear();
0N/A if (root.getDefaultButton() != null) {
0N/A Object[] bindings = (Object[])DefaultLookup.get(root, this,
0N/A "RootPane.defaultButtonWindowKeyBindings");
0N/A if (bindings != null) {
0N/A LookAndFeel.loadKeyBindings(km, bindings);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Invoked when a property changes on the root pane. If the event
0N/A * indicates the <code>defaultButton</code> has changed, this will
0N/A * reinstall the keyboard actions.
0N/A */
0N/A public void propertyChange(PropertyChangeEvent e) {
0N/A if(e.getPropertyName().equals("defaultButton")) {
0N/A JRootPane rootpane = (JRootPane)e.getSource();
0N/A updateDefaultButtonBindings(rootpane);
0N/A if (rootpane.getClientProperty("temporaryDefaultButton") == null) {
0N/A rootpane.putClientProperty("initialDefaultButton", e.getNewValue());
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A static class Actions extends UIAction {
0N/A public static final String PRESS = "press";
0N/A public static final String RELEASE = "release";
0N/A public static final String POST_POPUP = "postPopup";
0N/A
0N/A Actions(String name) {
0N/A super(name);
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent evt) {
0N/A JRootPane root = (JRootPane)evt.getSource();
0N/A JButton owner = root.getDefaultButton();
0N/A String key = getName();
0N/A
0N/A if (key == POST_POPUP) { // Action to post popup
0N/A Component c = KeyboardFocusManager
0N/A .getCurrentKeyboardFocusManager()
0N/A .getFocusOwner();
0N/A
0N/A if(c instanceof JComponent) {
0N/A JComponent src = (JComponent) c;
0N/A JPopupMenu jpm = src.getComponentPopupMenu();
0N/A if(jpm != null) {
0N/A Point pt = src.getPopupLocation(null);
0N/A if(pt == null) {
0N/A Rectangle vis = src.getVisibleRect();
0N/A pt = new Point(vis.x+vis.width/2,
0N/A vis.y+vis.height/2);
0N/A }
0N/A jpm.show(c, pt.x, pt.y);
0N/A }
0N/A }
0N/A }
0N/A else if (owner != null
0N/A && SwingUtilities.getRootPane(owner) == root) {
0N/A if (key == PRESS) {
0N/A owner.doClick(20);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public boolean isEnabled(Object sender) {
0N/A String key = getName();
0N/A if(key == POST_POPUP) {
0N/A MenuElement[] elems = MenuSelectionManager
0N/A .defaultManager()
0N/A .getSelectedPath();
0N/A if(elems != null && elems.length != 0) {
0N/A return false;
0N/A // We shall not interfere with already opened menu
0N/A }
0N/A
0N/A Component c = KeyboardFocusManager
0N/A .getCurrentKeyboardFocusManager()
0N/A .getFocusOwner();
0N/A if(c instanceof JComponent) {
0N/A JComponent src = (JComponent) c;
0N/A return src.getComponentPopupMenu() != null;
0N/A }
0N/A
0N/A return false;
0N/A }
0N/A
0N/A if (sender != null && sender instanceof JRootPane) {
0N/A JButton owner = ((JRootPane)sender).getDefaultButton();
0N/A return (owner != null && owner.getModel().isEnabled());
0N/A }
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A private static class RootPaneInputMap extends ComponentInputMapUIResource {
0N/A public RootPaneInputMap(JComponent c) {
0N/A super(c);
0N/A }
0N/A }
0N/A}