0N/A/*
2362N/A * Copyright (c) 1997, 2009, 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.*;
0N/Aimport java.awt.event.*;
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.event.*;
0N/Aimport javax.swing.border.*;
0N/Aimport javax.swing.plaf.*;
0N/Aimport java.beans.*;
0N/A
0N/A/**
0N/A * Basic L&F for a minimized window on a desktop.
0N/A *
0N/A * @author David Kloba
0N/A * @author Steve Wilson
0N/A * @author Rich Schiavi
0N/A */
0N/Apublic class BasicDesktopIconUI extends DesktopIconUI {
0N/A
0N/A protected JInternalFrame.JDesktopIcon desktopIcon;
0N/A protected JInternalFrame frame;
0N/A
0N/A /**
0N/A * The title pane component used in the desktop icon.
0N/A *
0N/A * @since 1.5
0N/A */
0N/A protected JComponent iconPane;
0N/A MouseInputListener mouseInputListener;
0N/A
0N/A
0N/A
0N/A public static ComponentUI createUI(JComponent c) {
0N/A return new BasicDesktopIconUI();
0N/A }
0N/A
0N/A public BasicDesktopIconUI() {
0N/A }
0N/A
0N/A public void installUI(JComponent c) {
0N/A desktopIcon = (JInternalFrame.JDesktopIcon)c;
0N/A frame = desktopIcon.getInternalFrame();
0N/A installDefaults();
0N/A installComponents();
0N/A
0N/A // Update icon layout if frame is already iconified
0N/A JInternalFrame f = desktopIcon.getInternalFrame();
0N/A if (f.isIcon() && f.getParent() == null) {
0N/A JDesktopPane desktop = desktopIcon.getDesktopPane();
0N/A if (desktop != null) {
0N/A DesktopManager desktopManager = desktop.getDesktopManager();
0N/A if (desktopManager instanceof DefaultDesktopManager) {
0N/A desktopManager.iconifyFrame(f);
0N/A }
0N/A }
0N/A }
0N/A
0N/A installListeners();
0N/A JLayeredPane.putLayer(desktopIcon, JLayeredPane.getLayer(frame));
0N/A }
0N/A
0N/A public void uninstallUI(JComponent c) {
0N/A uninstallDefaults();
0N/A uninstallComponents();
0N/A
0N/A // Force future UI to relayout icon
0N/A JInternalFrame f = desktopIcon.getInternalFrame();
0N/A if (f.isIcon()) {
0N/A JDesktopPane desktop = desktopIcon.getDesktopPane();
0N/A if (desktop != null) {
0N/A DesktopManager desktopManager = desktop.getDesktopManager();
0N/A if (desktopManager instanceof DefaultDesktopManager) {
0N/A // This will cause DefaultDesktopManager to layout the icon
0N/A f.putClientProperty("wasIconOnce", null);
0N/A // Move aside to allow fresh layout of all icons
0N/A desktopIcon.setLocation(Integer.MIN_VALUE, 0);
0N/A }
0N/A }
0N/A }
0N/A
0N/A uninstallListeners();
0N/A frame = null;
0N/A desktopIcon = null;
0N/A }
0N/A
0N/A protected void installComponents() {
0N/A iconPane = new BasicInternalFrameTitlePane(frame);
0N/A desktopIcon.setLayout(new BorderLayout());
0N/A desktopIcon.add(iconPane, BorderLayout.CENTER);
0N/A }
0N/A
0N/A protected void uninstallComponents() {
0N/A desktopIcon.remove(iconPane);
0N/A desktopIcon.setLayout(null);
0N/A iconPane = null;
0N/A }
0N/A
0N/A protected void installListeners() {
0N/A mouseInputListener = createMouseInputListener();
0N/A desktopIcon.addMouseMotionListener(mouseInputListener);
0N/A desktopIcon.addMouseListener(mouseInputListener);
0N/A }
0N/A
0N/A protected void uninstallListeners() {
0N/A desktopIcon.removeMouseMotionListener(mouseInputListener);
0N/A desktopIcon.removeMouseListener(mouseInputListener);
0N/A mouseInputListener = null;
0N/A }
0N/A
0N/A protected void installDefaults() {
0N/A LookAndFeel.installBorder(desktopIcon, "DesktopIcon.border");
0N/A LookAndFeel.installProperty(desktopIcon, "opaque", Boolean.TRUE);
0N/A }
0N/A
0N/A protected void uninstallDefaults() {
0N/A LookAndFeel.uninstallBorder(desktopIcon);
0N/A }
0N/A
0N/A protected MouseInputListener createMouseInputListener() {
0N/A return new MouseInputHandler();
0N/A }
0N/A
0N/A public Dimension getPreferredSize(JComponent c) {
0N/A return desktopIcon.getLayout().preferredLayoutSize(desktopIcon);
0N/A }
0N/A
0N/A public Dimension getMinimumSize(JComponent c) {
0N/A Dimension dim = new Dimension(iconPane.getMinimumSize());
0N/A Border border = frame.getBorder();
0N/A
0N/A if (border != null) {
0N/A dim.height += border.getBorderInsets(frame).bottom +
0N/A border.getBorderInsets(frame).top;
0N/A }
0N/A return dim;
0N/A }
0N/A
0N/A /**
0N/A * Desktop icons can not be resized. Therefore, we should always
0N/A * return the minimum size of the desktop icon.
0N/A *
0N/A * @see #getMinimumSize
0N/A */
0N/A public Dimension getMaximumSize(JComponent c){
0N/A return iconPane.getMaximumSize();
0N/A }
0N/A
0N/A public Insets getInsets(JComponent c) {
0N/A JInternalFrame iframe = desktopIcon.getInternalFrame();
0N/A Border border = iframe.getBorder();
0N/A if(border != null)
0N/A return border.getBorderInsets(iframe);
0N/A
0N/A return new Insets(0,0,0,0);
0N/A }
0N/A
0N/A public void deiconize() {
0N/A try { frame.setIcon(false); } catch (PropertyVetoException e2) { }
0N/A }
0N/A
0N/A /**
0N/A * Listens for mouse movements and acts on them.
0N/A *
0N/A * This class should be treated as a "protected" inner class.
3972N/A * Instantiate it only within subclasses of {@code BasicDesktopIconUI}.
0N/A */
0N/A public class MouseInputHandler extends MouseInputAdapter
0N/A {
0N/A // _x & _y are the mousePressed location in absolute coordinate system
0N/A int _x, _y;
0N/A // __x & __y are the mousePressed location in source view's coordinate system
0N/A int __x, __y;
0N/A Rectangle startingBounds;
0N/A
0N/A public void mouseReleased(MouseEvent e) {
0N/A _x = 0;
0N/A _y = 0;
0N/A __x = 0;
0N/A __y = 0;
0N/A startingBounds = null;
0N/A
0N/A JDesktopPane d;
0N/A if((d = desktopIcon.getDesktopPane()) != null) {
0N/A DesktopManager dm = d.getDesktopManager();
0N/A dm.endDraggingFrame(desktopIcon);
0N/A }
0N/A
0N/A }
0N/A
0N/A public void mousePressed(MouseEvent e) {
0N/A Point p = SwingUtilities.convertPoint((Component)e.getSource(),
0N/A e.getX(), e.getY(), null);
0N/A __x = e.getX();
0N/A __y = e.getY();
0N/A _x = p.x;
0N/A _y = p.y;
0N/A startingBounds = desktopIcon.getBounds();
0N/A
0N/A JDesktopPane d;
0N/A if((d = desktopIcon.getDesktopPane()) != null) {
0N/A DesktopManager dm = d.getDesktopManager();
0N/A dm.beginDraggingFrame(desktopIcon);
0N/A }
0N/A
0N/A try { frame.setSelected(true); } catch (PropertyVetoException e1) { }
0N/A if(desktopIcon.getParent() instanceof JLayeredPane) {
0N/A ((JLayeredPane)desktopIcon.getParent()).moveToFront(desktopIcon);
0N/A }
0N/A
0N/A if(e.getClickCount() > 1) {
0N/A if(frame.isIconifiable() && frame.isIcon()) {
0N/A deiconize();
0N/A }
0N/A }
0N/A
0N/A }
0N/A
0N/A public void mouseMoved(MouseEvent e) {}
0N/A
0N/A public void mouseDragged(MouseEvent e) {
0N/A Point p;
0N/A int newX, newY, newW, newH;
0N/A int deltaX;
0N/A int deltaY;
0N/A Dimension min;
0N/A Dimension max;
0N/A p = SwingUtilities.convertPoint((Component)e.getSource(),
0N/A e.getX(), e.getY(), null);
0N/A
0N/A Insets i = desktopIcon.getInsets();
0N/A int pWidth, pHeight;
0N/A pWidth = ((JComponent)desktopIcon.getParent()).getWidth();
0N/A pHeight = ((JComponent)desktopIcon.getParent()).getHeight();
0N/A
0N/A if (startingBounds == null) {
0N/A // (STEVE) Yucky work around for bug ID 4106552
0N/A return;
0N/A }
0N/A newX = startingBounds.x - (_x - p.x);
0N/A newY = startingBounds.y - (_y - p.y);
0N/A // Make sure we stay in-bounds
0N/A if(newX + i.left <= -__x)
0N/A newX = -__x - i.left;
0N/A if(newY + i.top <= -__y)
0N/A newY = -__y - i.top;
0N/A if(newX + __x + i.right > pWidth)
0N/A newX = pWidth - __x - i.right;
0N/A if(newY + __y + i.bottom > pHeight)
0N/A newY = pHeight - __y - i.bottom;
0N/A
0N/A JDesktopPane d;
0N/A if((d = desktopIcon.getDesktopPane()) != null) {
0N/A DesktopManager dm = d.getDesktopManager();
0N/A dm.dragFrame(desktopIcon, newX, newY);
0N/A } else {
0N/A moveAndRepaint(desktopIcon, newX, newY,
0N/A desktopIcon.getWidth(), desktopIcon.getHeight());
0N/A }
0N/A return;
0N/A }
0N/A
0N/A public void moveAndRepaint(JComponent f, int newX, int newY,
0N/A int newWidth, int newHeight) {
0N/A Rectangle r = f.getBounds();
0N/A f.setBounds(newX, newY, newWidth, newHeight);
0N/A SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r);
0N/A f.getParent().repaint(r.x, r.y, r.width, r.height);
0N/A }
0N/A }; /// End MotionListener
0N/A
0N/A}