1 /*
   2  * Copyright (c) 2011, 2014, 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 com.apple.laf;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 import java.awt.geom.Rectangle2D;
  31 import java.awt.image.BufferedImage;
  32 import java.beans.PropertyVetoException;
  33 
  34 import javax.swing.*;
  35 import javax.swing.plaf.*;
  36 
  37 import sun.swing.SwingUtilities2;
  38 
  39 /**
  40  * From MacDockIconUI
  41  *
  42  * A JRSUI L&F implementation of JInternalFrame.JDesktopIcon
  43  * @author
  44  * @version
  45  */
  46 public class AquaInternalFrameDockIconUI extends DesktopIconUI implements MouseListener, MouseMotionListener, ComponentListener {
  47     private static final String CACHED_FRAME_ICON_KEY = "apple.laf.internal.frameIcon";
  48 
  49     protected JInternalFrame.JDesktopIcon fDesktopIcon;
  50     protected JInternalFrame fFrame;
  51     protected ScaledImageLabel fIconPane;
  52     protected DockLabel fDockLabel;
  53     protected boolean fTrackingIcon = false;
  54 
  55     public static ComponentUI createUI(final JComponent c) {
  56         return new AquaInternalFrameDockIconUI();
  57     }
  58 
  59     public void installUI(final JComponent c) {
  60         fDesktopIcon = (JInternalFrame.JDesktopIcon)c;
  61         installComponents();
  62         installListeners();
  63     }
  64 
  65     public void uninstallUI(final JComponent c) {
  66         uninstallComponents();
  67         uninstallListeners();
  68         fDesktopIcon = null;
  69         fFrame = null;
  70     }
  71 
  72     protected void installComponents() {
  73         fFrame = fDesktopIcon.getInternalFrame();
  74         fIconPane = new ScaledImageLabel();
  75         fDesktopIcon.setLayout(new BorderLayout());
  76         fDesktopIcon.add(fIconPane, BorderLayout.CENTER);
  77     }
  78 
  79     protected void uninstallComponents() {
  80         fDesktopIcon.setLayout(null);
  81         fDesktopIcon.remove(fIconPane);
  82     }
  83 
  84     protected void installListeners() {
  85         fDesktopIcon.addMouseListener(this);
  86         fDesktopIcon.addMouseMotionListener(this);
  87         fFrame.addComponentListener(this);
  88     }
  89 
  90     protected void uninstallListeners() {
  91         fFrame.removeComponentListener(this);
  92         fDesktopIcon.removeMouseMotionListener(this);
  93         fDesktopIcon.removeMouseListener(this);
  94     }
  95 
  96     public Dimension getMinimumSize(final JComponent c) {
  97         return new Dimension(32, 32);
  98     }
  99 
 100     public Dimension getMaximumSize(final JComponent c) {
 101         return new Dimension(128, 128);
 102     }
 103 
 104     public Dimension getPreferredSize(final JComponent c) {
 105         return new Dimension(64, 64); //$ Dock preferred size
 106     }
 107 
 108     public Insets getInsets(final JComponent c) {
 109         return new Insets(0, 0, 0, 0);
 110     }
 111 
 112     void updateIcon() {
 113         fIconPane.updateIcon();
 114     }
 115 
 116     public void mousePressed(final MouseEvent e) {
 117         fTrackingIcon = fIconPane.mouseInIcon(e);
 118         if (fTrackingIcon) fIconPane.repaint();
 119     }
 120 
 121     public void mouseReleased(final MouseEvent e) {// only when it's actually in the image
 122         if (fFrame.isIconifiable() && fFrame.isIcon()) {
 123             if (fTrackingIcon) {
 124                 fTrackingIcon = false;
 125                 if (fIconPane.mouseInIcon(e)) {
 126                     if (fDockLabel != null) fDockLabel.hide();
 127                     try {
 128                         fFrame.setIcon(false);
 129                     } catch(final PropertyVetoException e2) {}
 130                 } else {
 131                     fIconPane.repaint();
 132                 }
 133             }
 134         }
 135 
 136         // if the mouse was completely outside fIconPane, hide the label
 137         if (fDockLabel != null && !fIconPane.getBounds().contains(e.getX(), e.getY())) fDockLabel.hide();
 138     }
 139 
 140     public void mouseEntered(final MouseEvent e) {
 141         if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) return;
 142         String title = fFrame.getTitle();
 143         if (title == null || title.equals("")) title = "Untitled";
 144         fDockLabel = new DockLabel(title);
 145         fDockLabel.show(fDesktopIcon);
 146     }
 147 
 148     public void mouseExited(final MouseEvent e) {
 149         if (fDockLabel != null && (e.getModifiers() & InputEvent.BUTTON1_MASK) == 0) fDockLabel.hide();
 150     }
 151 
 152     public void mouseClicked(final MouseEvent e) { }
 153 
 154     public void mouseDragged(final MouseEvent e) { }
 155 
 156     public void mouseMoved(final MouseEvent e) { }
 157 
 158     public void componentHidden(final ComponentEvent e) { }
 159 
 160     public void componentMoved(final ComponentEvent e) { }
 161 
 162     public void componentResized(final ComponentEvent e) {
 163         fFrame.putClientProperty(CACHED_FRAME_ICON_KEY, null);
 164     }
 165 
 166     public void componentShown(final ComponentEvent e) {
 167         fFrame.putClientProperty(CACHED_FRAME_ICON_KEY, null);
 168     }
 169 
 170     @SuppressWarnings("serial") // Superclass is not serializable across versions
 171     class ScaledImageLabel extends JLabel {
 172         ScaledImageLabel() {
 173             super(null, null, CENTER);
 174         }
 175 
 176         void updateIcon() {
 177             final Object priorIcon = fFrame.getClientProperty(CACHED_FRAME_ICON_KEY);
 178             if (priorIcon instanceof ImageIcon) {
 179                 setIcon((ImageIcon)priorIcon);
 180                 return;
 181             }
 182 
 183             int width = fFrame.getWidth();
 184             int height = fFrame.getHeight();
 185 
 186             // Protect us from unsized frames, like in JCK test DefaultDesktopManager2008
 187             if (width <= 0 || height <= 0) {
 188                 width = 128;
 189                 height = 128;
 190             }
 191 
 192             final Image fImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
 193             final Graphics g = fImage.getGraphics();
 194             fFrame.paint(g);
 195             g.dispose();
 196 
 197             final float scale = (float)fDesktopIcon.getWidth() / (float)Math.max(width, height) * 0.89f;
 198             // Sending in -1 for width xor height causes it to maintain aspect ratio
 199             final ImageIcon icon = new ImageIcon(fImage.getScaledInstance((int)(width * scale), -1, Image.SCALE_SMOOTH));
 200             fFrame.putClientProperty(CACHED_FRAME_ICON_KEY, icon);
 201             setIcon(icon);
 202         }
 203 
 204         public void paint(final Graphics g) {
 205             if (getIcon() == null) updateIcon();
 206 
 207             g.translate(0, 2);
 208 
 209             if (!fTrackingIcon) {
 210                 super.paint(g);
 211                 return;
 212             }
 213 
 214             final ImageIcon prev = (ImageIcon)getIcon();
 215             final ImageIcon pressedIcon = new ImageIcon(AquaUtils.generateSelectedDarkImage(prev.getImage()));
 216             setIcon(pressedIcon);
 217             super.paint(g);
 218             setIcon(prev);
 219         }
 220 
 221         boolean mouseInIcon(final MouseEvent e) {
 222             return getBounds().contains(e.getX(), e.getY());
 223         }
 224 
 225         public Dimension getPreferredSize() {
 226             return new Dimension(64, 64); //$ Dock preferred size
 227         }
 228     }
 229 
 230     @SuppressWarnings("serial") // Superclass is not serializable across versions
 231     class DockLabel extends JLabel {
 232         final static int NUB_HEIGHT = 7;
 233         final static int ROUND_ADDITIONAL_HEIGHT = 8;
 234         final static int ROUND_ADDITIONAL_WIDTH = 12;
 235 
 236         DockLabel(final String text) {
 237             super(text);
 238             setBorder(null);
 239             setOpaque(false);
 240             setFont(AquaFonts.getDockIconFont());
 241 
 242             final FontMetrics metrics = getFontMetrics(getFont());
 243             setSize(SwingUtilities.computeStringWidth(metrics, getText()) + ROUND_ADDITIONAL_WIDTH * 2, metrics.getAscent() + NUB_HEIGHT + ROUND_ADDITIONAL_HEIGHT);
 244         }
 245 
 246         public void paint(final Graphics g) {
 247             final int width = getWidth();
 248             final int height = getHeight();
 249 
 250             final Font font = getFont();
 251             final FontMetrics metrics = getFontMetrics(font);
 252             g.setFont(font);
 253 
 254             final String text = getText().trim();
 255             final int ascent = metrics.getAscent();
 256 
 257             final Rectangle2D stringBounds = metrics.getStringBounds(text, g);
 258             final int halfway = width / 2;
 259 
 260             final int x = (halfway - (int)stringBounds.getWidth() / 2);
 261 
 262             final Graphics2D g2d = g instanceof Graphics2D ? (Graphics2D)g : null;
 263             if (g2d != null) {
 264                 g.setColor(UIManager.getColor("DesktopIcon.labelBackground"));
 265                 final Object origAA = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
 266                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 267 
 268                 final int roundHeight = height - ROUND_ADDITIONAL_HEIGHT + 1;
 269                 g.fillRoundRect(0, 0, width, roundHeight, roundHeight, roundHeight);
 270 
 271                 final int[] xpts = { halfway, halfway + NUB_HEIGHT, halfway - NUB_HEIGHT };
 272                 final int[] ypts = { height, height - NUB_HEIGHT, height - NUB_HEIGHT };
 273                 g.fillPolygon(xpts, ypts, 3);
 274 
 275                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, origAA);
 276             }
 277 
 278             g.setColor(Color.black);
 279             SwingUtilities2.drawString(this, g, text, x, 2 + ascent);
 280             g.setColor(Color.white);
 281             SwingUtilities2.drawString(this, g, text, x, 1 + ascent);
 282         }
 283 
 284         public void show(final Component invoker) {
 285             final int desiredLocationX = (invoker.getWidth() - getWidth()) / 2;
 286             final int desiredLocationY = -(getHeight() + 6);
 287 
 288             Container parent = invoker.getParent();
 289 
 290             for (Container p = parent; p != null; p = p.getParent()) {
 291                 if (p instanceof JRootPane) {
 292                     if (p.getParent() instanceof JInternalFrame) continue;
 293                     parent = ((JRootPane)p).getLayeredPane();
 294                     for (p = parent.getParent(); p != null && (!(p instanceof java.awt.Window)); p = p.getParent());
 295                     break;
 296                 }
 297             }
 298 
 299             final Point p = SwingUtilities.convertPoint(invoker, desiredLocationX, desiredLocationY, parent);
 300             setLocation(p.x, p.y);
 301             if (parent instanceof JLayeredPane) {
 302                 ((JLayeredPane)parent).add(this, JLayeredPane.POPUP_LAYER, 0);
 303             }
 304         }
 305 
 306         public void hide() {
 307             final Container parent = getParent();
 308             final Rectangle r = this.getBounds();
 309             if (parent == null) return;
 310             parent.remove(this);
 311             parent.repaint(r.x, r.y, r.width, r.height);
 312         }
 313     }
 314 }