Home » openjdk-7 » javax » swing » plaf » basic » [javadoc | source]

    1   /*
    2    * Copyright (c) 1997, 2009, Oracle and/or its affiliates. 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.  Oracle designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   22    * or visit www.oracle.com if you need additional information or have any
   23    * questions.
   24    */
   25   
   26   package javax.swing.plaf.basic;
   27   
   28   import java.awt;
   29   import java.awt.event;
   30   import javax.swing;
   31   import javax.swing.event;
   32   import javax.swing.border;
   33   import javax.swing.plaf;
   34   import java.beans;
   35   
   36   /**
   37    * Basic L&F for a minimized window on a desktop.
   38    *
   39    * @author David Kloba
   40    * @author Steve Wilson
   41    * @author Rich Schiavi
   42    */
   43   public class BasicDesktopIconUI extends DesktopIconUI {
   44   
   45       protected JInternalFrame.JDesktopIcon desktopIcon;
   46       protected JInternalFrame frame;
   47   
   48       /**
   49        * The title pane component used in the desktop icon.
   50        *
   51        * @since 1.5
   52        */
   53       protected JComponent iconPane;
   54       MouseInputListener mouseInputListener;
   55   
   56   
   57   
   58       public static ComponentUI createUI(JComponent c)    {
   59           return new BasicDesktopIconUI();
   60       }
   61   
   62       public BasicDesktopIconUI() {
   63       }
   64   
   65       public void installUI(JComponent c)   {
   66           desktopIcon = (JInternalFrame.JDesktopIcon)c;
   67           frame = desktopIcon.getInternalFrame();
   68           installDefaults();
   69           installComponents();
   70   
   71           // Update icon layout if frame is already iconified
   72           JInternalFrame f = desktopIcon.getInternalFrame();
   73           if (f.isIcon() && f.getParent() == null) {
   74               JDesktopPane desktop = desktopIcon.getDesktopPane();
   75               if (desktop != null) {
   76                   DesktopManager desktopManager = desktop.getDesktopManager();
   77                   if (desktopManager instanceof DefaultDesktopManager) {
   78                       desktopManager.iconifyFrame(f);
   79                   }
   80               }
   81           }
   82   
   83           installListeners();
   84           JLayeredPane.putLayer(desktopIcon, JLayeredPane.getLayer(frame));
   85       }
   86   
   87       public void uninstallUI(JComponent c) {
   88           uninstallDefaults();
   89           uninstallComponents();
   90   
   91           // Force future UI to relayout icon
   92           JInternalFrame f = desktopIcon.getInternalFrame();
   93           if (f.isIcon()) {
   94               JDesktopPane desktop = desktopIcon.getDesktopPane();
   95               if (desktop != null) {
   96                   DesktopManager desktopManager = desktop.getDesktopManager();
   97                   if (desktopManager instanceof DefaultDesktopManager) {
   98                       // This will cause DefaultDesktopManager to layout the icon
   99                       f.putClientProperty("wasIconOnce", null);
  100                       // Move aside to allow fresh layout of all icons
  101                       desktopIcon.setLocation(Integer.MIN_VALUE, 0);
  102                   }
  103               }
  104           }
  105   
  106           uninstallListeners();
  107           frame = null;
  108           desktopIcon = null;
  109       }
  110   
  111       protected void installComponents() {
  112           iconPane = new BasicInternalFrameTitlePane(frame);
  113           desktopIcon.setLayout(new BorderLayout());
  114           desktopIcon.add(iconPane, BorderLayout.CENTER);
  115       }
  116   
  117       protected void uninstallComponents() {
  118           desktopIcon.remove(iconPane);
  119           desktopIcon.setLayout(null);
  120           iconPane = null;
  121       }
  122   
  123       protected void installListeners() {
  124           mouseInputListener = createMouseInputListener();
  125           desktopIcon.addMouseMotionListener(mouseInputListener);
  126           desktopIcon.addMouseListener(mouseInputListener);
  127       }
  128   
  129       protected void uninstallListeners() {
  130           desktopIcon.removeMouseMotionListener(mouseInputListener);
  131           desktopIcon.removeMouseListener(mouseInputListener);
  132           mouseInputListener = null;
  133       }
  134   
  135       protected void installDefaults() {
  136           LookAndFeel.installBorder(desktopIcon, "DesktopIcon.border");
  137           LookAndFeel.installProperty(desktopIcon, "opaque", Boolean.TRUE);
  138       }
  139   
  140       protected void uninstallDefaults() {
  141           LookAndFeel.uninstallBorder(desktopIcon);
  142       }
  143   
  144       protected MouseInputListener createMouseInputListener() {
  145           return new MouseInputHandler();
  146       }
  147   
  148       public Dimension getPreferredSize(JComponent c) {
  149           return desktopIcon.getLayout().preferredLayoutSize(desktopIcon);
  150       }
  151   
  152       public Dimension getMinimumSize(JComponent c) {
  153           Dimension dim = new Dimension(iconPane.getMinimumSize());
  154           Border border = frame.getBorder();
  155   
  156           if (border != null) {
  157               dim.height += border.getBorderInsets(frame).bottom +
  158                             border.getBorderInsets(frame).top;
  159           }
  160           return dim;
  161       }
  162   
  163       /**
  164        * Desktop icons can not be resized.  Therefore, we should always
  165        * return the minimum size of the desktop icon.
  166        *
  167        * @see #getMinimumSize
  168        */
  169       public Dimension getMaximumSize(JComponent c){
  170           return iconPane.getMaximumSize();
  171       }
  172   
  173       public Insets getInsets(JComponent c) {
  174           JInternalFrame iframe = desktopIcon.getInternalFrame();
  175           Border border = iframe.getBorder();
  176           if(border != null)
  177               return border.getBorderInsets(iframe);
  178   
  179           return new Insets(0,0,0,0);
  180       }
  181   
  182       public void deiconize() {
  183           try { frame.setIcon(false); } catch (PropertyVetoException e2) { }
  184       }
  185   
  186       /**
  187        * Listens for mouse movements and acts on them.
  188        *
  189        * This class should be treated as a "protected" inner class.
  190        * Instantiate it only within subclasses of {@code BasicDesktopIconUI}.
  191        */
  192       public class MouseInputHandler extends MouseInputAdapter
  193       {
  194           // _x & _y are the mousePressed location in absolute coordinate system
  195           int _x, _y;
  196           // __x & __y are the mousePressed location in source view's coordinate system
  197           int __x, __y;
  198           Rectangle startingBounds;
  199   
  200           public void mouseReleased(MouseEvent e) {
  201               _x = 0;
  202               _y = 0;
  203               __x = 0;
  204               __y = 0;
  205               startingBounds = null;
  206   
  207               JDesktopPane d;
  208               if((d = desktopIcon.getDesktopPane()) != null) {
  209                   DesktopManager dm = d.getDesktopManager();
  210                   dm.endDraggingFrame(desktopIcon);
  211               }
  212   
  213           }
  214   
  215           public void mousePressed(MouseEvent e) {
  216               Point p = SwingUtilities.convertPoint((Component)e.getSource(),
  217                           e.getX(), e.getY(), null);
  218               __x = e.getX();
  219               __y = e.getY();
  220               _x = p.x;
  221               _y = p.y;
  222               startingBounds = desktopIcon.getBounds();
  223   
  224               JDesktopPane d;
  225               if((d = desktopIcon.getDesktopPane()) != null) {
  226                   DesktopManager dm = d.getDesktopManager();
  227                   dm.beginDraggingFrame(desktopIcon);
  228               }
  229   
  230               try { frame.setSelected(true); } catch (PropertyVetoException e1) { }
  231               if(desktopIcon.getParent() instanceof JLayeredPane) {
  232                   ((JLayeredPane)desktopIcon.getParent()).moveToFront(desktopIcon);
  233               }
  234   
  235               if(e.getClickCount() > 1) {
  236                   if(frame.isIconifiable() && frame.isIcon()) {
  237                       deiconize();
  238                   }
  239               }
  240   
  241           }
  242   
  243            public void mouseMoved(MouseEvent e) {}
  244   
  245            public void mouseDragged(MouseEvent e) {
  246               Point p;
  247               int newX, newY, newW, newH;
  248               int deltaX;
  249               int deltaY;
  250               Dimension min;
  251               Dimension max;
  252               p = SwingUtilities.convertPoint((Component)e.getSource(),
  253                                           e.getX(), e.getY(), null);
  254   
  255                   Insets i = desktopIcon.getInsets();
  256                   int pWidth, pHeight;
  257                   pWidth = ((JComponent)desktopIcon.getParent()).getWidth();
  258                   pHeight = ((JComponent)desktopIcon.getParent()).getHeight();
  259   
  260                   if (startingBounds == null) {
  261                     // (STEVE) Yucky work around for bug ID 4106552
  262                       return;
  263                   }
  264                   newX = startingBounds.x - (_x - p.x);
  265                   newY = startingBounds.y - (_y - p.y);
  266                   // Make sure we stay in-bounds
  267                   if(newX + i.left <= -__x)
  268                       newX = -__x - i.left;
  269                   if(newY + i.top <= -__y)
  270                       newY = -__y - i.top;
  271                   if(newX + __x + i.right > pWidth)
  272                       newX = pWidth - __x - i.right;
  273                   if(newY + __y + i.bottom > pHeight)
  274                       newY =  pHeight - __y - i.bottom;
  275   
  276                   JDesktopPane d;
  277                   if((d = desktopIcon.getDesktopPane()) != null) {
  278                       DesktopManager dm = d.getDesktopManager();
  279                       dm.dragFrame(desktopIcon, newX, newY);
  280                   } else {
  281                       moveAndRepaint(desktopIcon, newX, newY,
  282                                   desktopIcon.getWidth(), desktopIcon.getHeight());
  283                   }
  284                   return;
  285           }
  286   
  287           public void moveAndRepaint(JComponent f, int newX, int newY,
  288                                           int newWidth, int newHeight) {
  289               Rectangle r = f.getBounds();
  290               f.setBounds(newX, newY, newWidth, newHeight);
  291               SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r);
  292               f.getParent().repaint(r.x, r.y, r.width, r.height);
  293           }
  294       }; /// End MotionListener
  295   
  296   }

Home » openjdk-7 » javax » swing » plaf » basic » [javadoc | source]