1457 lines · 53.3 KB · Java
RawDownload
/* * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.swing.plaf.basic; import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.plaf.*;import javax.swing.event.*;import java.beans.*;import sun.swing.DefaultLookup;import sun.swing.UIAction; /** * A basic L&F implementation of JInternalFrame. * * @author David Kloba * @author Rich Schiavi */public class BasicInternalFrameUI extends InternalFrameUI{     protected JInternalFrame frame;     private Handler handler;    protected MouseInputAdapter          borderListener;    protected PropertyChangeListener     propertyChangeListener;    protected LayoutManager              internalFrameLayout;    protected ComponentListener          componentListener;    protected MouseInputListener         glassPaneDispatcher;    private InternalFrameListener        internalFrameListener;     protected JComponent northPane;    protected JComponent southPane;    protected JComponent westPane;    protected JComponent eastPane;     protected BasicInternalFrameTitlePane titlePane; // access needs this     private static DesktopManager sharedDesktopManager;    private boolean componentListenerAdded = false;     private Rectangle parentBounds;     private boolean dragging = false;    private boolean resizing = false;     /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected KeyStroke openMenuKey;     private boolean keyBindingRegistered = false;    private boolean keyBindingActive = false; /////////////////////////////////////////////////////////////////////////////// ComponentUI Interface Implementation methods/////////////////////////////////////////////////////////////////////////////    public static ComponentUI createUI(JComponent b)    {        return new BasicInternalFrameUI((JInternalFrame)b);    }     public BasicInternalFrameUI(JInternalFrame b)   {        LookAndFeel laf = UIManager.getLookAndFeel();        if (laf instanceof BasicLookAndFeel) {            ((BasicLookAndFeel)laf).installAWTEventListener();        }    }     public void installUI(JComponent c)   {         frame = (JInternalFrame)c;         installDefaults();        installListeners();        installComponents();        installKeyboardActions();         LookAndFeel.installProperty(frame, "opaque", Boolean.TRUE);    }     public void uninstallUI(JComponent c) {        if(c != frame)            throw new IllegalComponentStateException(                this + " was asked to deinstall() "                + c + " when it only knows about "                + frame + ".");         uninstallKeyboardActions();        uninstallComponents();        uninstallListeners();        uninstallDefaults();        updateFrameCursor();        handler = null;        frame = null;    }     protected void installDefaults(){        Icon frameIcon = frame.getFrameIcon();        if (frameIcon == null || frameIcon instanceof UIResource) {            frame.setFrameIcon(UIManager.getIcon("InternalFrame.icon"));        }         // Enable the content pane to inherit background color from its        // parent by setting its background color to null.        Container contentPane = frame.getContentPane();        if (contentPane != null) {          Color bg = contentPane.getBackground();          if (bg instanceof UIResource)            contentPane.setBackground(null);        }        frame.setLayout(internalFrameLayout = createLayoutManager());        frame.setBackground(UIManager.getLookAndFeelDefaults().getColor("control"));         LookAndFeel.installBorder(frame, "InternalFrame.border");     }    protected void installKeyboardActions(){        createInternalFrameListener();        if (internalFrameListener != null) {            frame.addInternalFrameListener(internalFrameListener);        }         LazyActionMap.installLazyActionMap(frame, BasicInternalFrameUI.class,            "InternalFrame.actionMap");    }     static void loadActionMap(LazyActionMap map) {        map.put(new UIAction("showSystemMenu") {            public void actionPerformed(ActionEvent evt) {                JInternalFrame iFrame = (JInternalFrame)evt.getSource();                if (iFrame.getUI() instanceof BasicInternalFrameUI) {                    JComponent comp = ((BasicInternalFrameUI)                        iFrame.getUI()).getNorthPane();                    if (comp instanceof BasicInternalFrameTitlePane) {                        ((BasicInternalFrameTitlePane)comp).                            showSystemMenu();                    }                }            }             public boolean isEnabled(Object sender){                if (sender instanceof JInternalFrame) {                    JInternalFrame iFrame = (JInternalFrame)sender;                    if (iFrame.getUI() instanceof BasicInternalFrameUI) {                        return ((BasicInternalFrameUI)iFrame.getUI()).                            isKeyBindingActive();                    }                }                return false;            }        });         // Set the ActionMap's parent to the Auditory Feedback Action Map        BasicLookAndFeel.installAudioActionMap(map);    }     protected void installComponents(){        setNorthPane(createNorthPane(frame));        setSouthPane(createSouthPane(frame));        setEastPane(createEastPane(frame));        setWestPane(createWestPane(frame));    }     /**     * @since 1.3     */    protected void installListeners() {        borderListener = createBorderListener(frame);        propertyChangeListener = createPropertyChangeListener();        frame.addPropertyChangeListener(propertyChangeListener);        installMouseHandlers(frame);        glassPaneDispatcher = createGlassPaneDispatcher();        if (glassPaneDispatcher != null) {            frame.getGlassPane().addMouseListener(glassPaneDispatcher);            frame.getGlassPane().addMouseMotionListener(glassPaneDispatcher);        }        componentListener =  createComponentListener();        if (frame.getParent() != null) {          parentBounds = frame.getParent().getBounds();        }        if ((frame.getParent() != null) && !componentListenerAdded) {            frame.getParent().addComponentListener(componentListener);            componentListenerAdded = true;        }    }     // Provide a FocusListener to listen for a WINDOW_LOST_FOCUS event,    // so that a resize can be cancelled if the focus is lost while resizing    // when an Alt-Tab, modal dialog popup, iconify, dispose, or remove    // of the internal frame occurs.    private WindowFocusListener getWindowFocusListener(){        return getHandler();    }     // Cancel a resize in progress by calling finishMouseReleased().    private void cancelResize() {        if (resizing) {            if (borderListener instanceof BorderListener) {                ((BorderListener)borderListener).finishMouseReleased();            }        }    }     private Handler getHandler() {        if (handler == null) {            handler = new Handler();        }        return handler;    }     InputMap getInputMap(int condition) {        if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {            return createInputMap(condition);        }        return null;    }     InputMap createInputMap(int condition) {        if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {            Object[] bindings = (Object[])DefaultLookup.get(                    frame, this, "InternalFrame.windowBindings");             if (bindings != null) {                return LookAndFeel.makeComponentInputMap(frame, bindings);            }        }        return null;    }     protected void uninstallDefaults() {        Icon frameIcon = frame.getFrameIcon();        if (frameIcon instanceof UIResource) {            frame.setFrameIcon(null);        }        internalFrameLayout = null;        frame.setLayout(null);        LookAndFeel.uninstallBorder(frame);    }     protected void uninstallComponents(){        setNorthPane(null);        setSouthPane(null);        setEastPane(null);        setWestPane(null);        if(titlePane != null) {            titlePane.uninstallDefaults();        }        titlePane = null;    }     /**     * @since 1.3     */    protected void uninstallListeners() {        if ((frame.getParent() != null) && componentListenerAdded) {            frame.getParent().removeComponentListener(componentListener);            componentListenerAdded = false;        }        componentListener = null;      if (glassPaneDispatcher != null) {          frame.getGlassPane().removeMouseListener(glassPaneDispatcher);          frame.getGlassPane().removeMouseMotionListener(glassPaneDispatcher);          glassPaneDispatcher = null;      }      deinstallMouseHandlers(frame);      frame.removePropertyChangeListener(propertyChangeListener);      propertyChangeListener = null;      borderListener = null;    }     protected void uninstallKeyboardActions(){        if (internalFrameListener != null) {            frame.removeInternalFrameListener(internalFrameListener);        }        internalFrameListener = null;         SwingUtilities.replaceUIInputMap(frame, JComponent.                                         WHEN_IN_FOCUSED_WINDOW, null);        SwingUtilities.replaceUIActionMap(frame, null);     }     void updateFrameCursor() {        if (resizing) {            return;        }        Cursor s = frame.getLastCursor();        if (s == null) {            s = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);        }        frame.setCursor(s);    }     protected LayoutManager createLayoutManager(){        return getHandler();    }     protected PropertyChangeListener createPropertyChangeListener(){        return getHandler();    }       public Dimension getPreferredSize(JComponent x)    {        if(frame == x)            return frame.getLayout().preferredLayoutSize(x);        return new Dimension(100, 100);    }     public Dimension getMinimumSize(JComponent x)  {        if(frame == x) {            return frame.getLayout().minimumLayoutSize(x);        }        return new Dimension(0, 0);    }     public Dimension getMaximumSize(JComponent x) {        return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);    }       /**      * Installs necessary mouse handlers on <code>newPane</code>      * and adds it to the frame.      * Reverse process for the <code>currentPane</code>.      */    protected void replacePane(JComponent currentPane, JComponent newPane) {        if(currentPane != null) {            deinstallMouseHandlers(currentPane);            frame.remove(currentPane);        }        if(newPane != null) {           frame.add(newPane);           installMouseHandlers(newPane);        }    }     protected void deinstallMouseHandlers(JComponent c) {      c.removeMouseListener(borderListener);      c.removeMouseMotionListener(borderListener);    }     protected void installMouseHandlers(JComponent c) {      c.addMouseListener(borderListener);      c.addMouseMotionListener(borderListener);    }     protected JComponent createNorthPane(JInternalFrame w) {      titlePane = new BasicInternalFrameTitlePane(w);      return titlePane;    }      protected JComponent createSouthPane(JInternalFrame w) {        return null;    }     protected JComponent createWestPane(JInternalFrame w) {        return null;    }     protected JComponent createEastPane(JInternalFrame w) {        return null;    }      protected MouseInputAdapter createBorderListener(JInternalFrame w) {        return new BorderListener();    }     protected void createInternalFrameListener(){        internalFrameListener = getHandler();    }     protected final boolean isKeyBindingRegistered(){      return keyBindingRegistered;    }     protected final void setKeyBindingRegistered(boolean b){      keyBindingRegistered = b;    }     public final boolean isKeyBindingActive(){      return keyBindingActive;    }     protected final void setKeyBindingActive(boolean b){      keyBindingActive = b;    }      protected void setupMenuOpenKey(){        // PENDING(hania): Why are these WHEN_IN_FOCUSED_WINDOWs? Shouldn't        // they be WHEN_ANCESTOR_OF_FOCUSED_COMPONENT?        // Also, no longer registering on the desktopicon, the previous        // action did nothing.        InputMap map = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);        SwingUtilities.replaceUIInputMap(frame,                                      JComponent.WHEN_IN_FOCUSED_WINDOW, map);        //ActionMap actionMap = getActionMap();        //SwingUtilities.replaceUIActionMap(frame, actionMap);    }     protected void setupMenuCloseKey(){    }     public JComponent getNorthPane() {        return northPane;    }     public void setNorthPane(JComponent c) {        if (northPane != null &&                northPane instanceof BasicInternalFrameTitlePane) {            ((BasicInternalFrameTitlePane)northPane).uninstallListeners();        }        replacePane(northPane, c);        northPane = c;        if (c instanceof BasicInternalFrameTitlePane) {          titlePane = (BasicInternalFrameTitlePane)c;        }    }     public JComponent getSouthPane() {        return southPane;    }     public void setSouthPane(JComponent c) {        southPane = c;    }     public JComponent getWestPane() {        return westPane;    }     public void setWestPane(JComponent c) {        westPane = c;    }     public JComponent getEastPane() {        return eastPane;    }     public void setEastPane(JComponent c) {        eastPane = c;    }     public class InternalFramePropertyChangeListener implements        PropertyChangeListener {        // NOTE: This class exists only for backward compatability. All        // its functionality has been moved into Handler. If you need to add        // new functionality add it to the Handler, but make sure this        // class calls into the Handler.        /**         * Detects changes in state from the JInternalFrame and handles         * actions.         */        public void propertyChange(PropertyChangeEvent evt) {            getHandler().propertyChange(evt);        }    }   public class InternalFrameLayout implements LayoutManager {    // NOTE: This class exists only for backward compatability. All    // its functionality has been moved into Handler. If you need to add    // new functionality add it to the Handler, but make sure this    // class calls into the Handler.    public void addLayoutComponent(String name, Component c) {        getHandler().addLayoutComponent(name, c);    }

Lines 1–500 of 1457.