jdk/src/share/classes/javax/swing/plaf/basic/DesktopIconMover.java
changeset 3499 dd9363188303
parent 3498 dbd1b49d341b
parent 3384 bca2225b66d7
child 3500 44c8157997f5
equal deleted inserted replaced
3498:dbd1b49d341b 3499:dd9363188303
     1 /*
       
     2  * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package javax.swing.plaf.basic;
       
    27 
       
    28 import javax.swing.*;
       
    29 import java.awt.*;
       
    30 import java.awt.event.*;
       
    31 import java.beans.*;
       
    32 
       
    33 /**
       
    34  * DesktopIconMover is intended to move desktop icon
       
    35  * when parent window is resized.
       
    36  */
       
    37 class DesktopIconMover implements ComponentListener, PropertyChangeListener {
       
    38     private Component parent;
       
    39     private JInternalFrame frame; // if not null, DesktopIconMover(frame)
       
    40                                   // constructor was used
       
    41     private JInternalFrame.JDesktopIcon icon;
       
    42     private Rectangle parentBounds;
       
    43     private boolean componentListenerAdded = false;
       
    44 
       
    45     public DesktopIconMover(JInternalFrame frame) {
       
    46         if (frame == null) {
       
    47             throw new NullPointerException("Frame cannot be null");
       
    48         }
       
    49         this.frame = frame;
       
    50         this.icon = frame.getDesktopIcon();
       
    51         if (icon == null) {
       
    52             throw new NullPointerException(
       
    53                     "frame.getDesktopIcon() cannot be null");
       
    54         }
       
    55         this.parent = frame.getParent();
       
    56         if (this.parent != null) {
       
    57             parentBounds = this.parent.getBounds();
       
    58         }
       
    59     }
       
    60 
       
    61     public DesktopIconMover(JInternalFrame.JDesktopIcon icon) {
       
    62         if (icon == null) {
       
    63             throw new NullPointerException("Icon cannot be null");
       
    64         }
       
    65         this.icon = icon;
       
    66         this.parent = icon.getParent();
       
    67         if (this.parent != null) {
       
    68             parentBounds = this.parent.getBounds();
       
    69         }
       
    70     }
       
    71 
       
    72     public void installListeners() {
       
    73         if (frame != null) {
       
    74             frame.addPropertyChangeListener(this);
       
    75         } else {
       
    76             icon.addPropertyChangeListener(this);
       
    77         }
       
    78         addComponentListener();
       
    79     }
       
    80 
       
    81     public void uninstallListeners() {
       
    82         if (frame != null) {
       
    83             frame.removePropertyChangeListener(this);
       
    84         } else {
       
    85             icon.removePropertyChangeListener(this);
       
    86         }
       
    87         removeComponentListener();
       
    88     }
       
    89 
       
    90     public void propertyChange(PropertyChangeEvent evt) {
       
    91         String propName = evt.getPropertyName();
       
    92         if ("ancestor".equals(propName)) {
       
    93             Component newAncestor = (Component) evt.getNewValue();
       
    94 
       
    95             // Remove component listener if parent is changing
       
    96             Component probablyNewParent = getCurrentParent();
       
    97             if ((probablyNewParent != null) &&
       
    98                     (!probablyNewParent.equals(parent))) {
       
    99                 removeComponentListener();
       
   100                 parent = probablyNewParent;
       
   101             }
       
   102 
       
   103             if (newAncestor == null) {
       
   104                 removeComponentListener();
       
   105             } else {
       
   106                 addComponentListener();
       
   107             }
       
   108 
       
   109             // Update parentBounds
       
   110             if (parent != null) {
       
   111                 parentBounds = parent.getBounds();
       
   112             } else {
       
   113                 parentBounds = null;
       
   114             }
       
   115         } else if (JInternalFrame.IS_CLOSED_PROPERTY.equals(propName)) {
       
   116             removeComponentListener();
       
   117         }
       
   118     }
       
   119 
       
   120     private void addComponentListener() {
       
   121         if (!componentListenerAdded && (parent != null)) {
       
   122             parent.addComponentListener(this);
       
   123             componentListenerAdded = true;
       
   124         }
       
   125     }
       
   126 
       
   127     private void removeComponentListener() {
       
   128         if ((parent != null) && componentListenerAdded) {
       
   129             parent.removeComponentListener(this);
       
   130             componentListenerAdded = false;
       
   131         }
       
   132     }
       
   133 
       
   134     private Component getCurrentParent() {
       
   135         if (frame != null) {
       
   136             return frame.getParent();
       
   137         } else {
       
   138             return icon.getParent();
       
   139         }
       
   140     }
       
   141 
       
   142     public void componentResized(ComponentEvent e) {
       
   143         if ((parent == null) || (parentBounds == null)) {
       
   144             return;
       
   145         }
       
   146 
       
   147         Rectangle parentNewBounds = parent.getBounds();
       
   148         if ((parentNewBounds == null) || parentNewBounds.equals(parentBounds)) {
       
   149             return;
       
   150         }
       
   151 
       
   152         // Move desktop icon only in up-down direction
       
   153         int newIconY = icon.getLocation().y +
       
   154                 (parentNewBounds.height - parentBounds.height);
       
   155         icon.setLocation(icon.getLocation().x, newIconY);
       
   156 
       
   157         parentBounds = parentNewBounds;
       
   158     }
       
   159 
       
   160     public void componentMoved(ComponentEvent e) {
       
   161     }
       
   162 
       
   163     public void componentShown(ComponentEvent e) {
       
   164     }
       
   165 
       
   166     public void componentHidden(ComponentEvent e) {
       
   167     }
       
   168 }