1 /* 2 * Copyright (c) 2011, 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 class ScaledImageLabel extends JLabel { 171 ScaledImageLabel() { 172 super(null, null, CENTER); 173 } 174 175 void updateIcon() { 176 final Object priorIcon = fFrame.getClientProperty(CACHED_FRAME_ICON_KEY); 177 if (priorIcon instanceof ImageIcon) { 178 setIcon((ImageIcon)priorIcon); 179 return; 180 } 181 182 int width = fFrame.getWidth(); 183 int height = fFrame.getHeight(); 184 185 // Protect us from unsized frames, like in JCK test DefaultDesktopManager2008 186 if (width <= 0 || height <= 0) { 187 width = 128; 188 height = 128; 189 } 190 191 final Image fImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE); 192 final Graphics g = fImage.getGraphics(); 193 fFrame.paint(g); 194 g.dispose(); 195 196 final float scale = (float)fDesktopIcon.getWidth() / (float)Math.max(width, height) * 0.89f; 197 // Sending in -1 for width xor height causes it to maintain aspect ratio 198 final ImageIcon icon = new ImageIcon(fImage.getScaledInstance((int)(width * scale), -1, Image.SCALE_SMOOTH)); 199 fFrame.putClientProperty(CACHED_FRAME_ICON_KEY, icon); 200 setIcon(icon); 201 } 202 203 public void paint(final Graphics g) { 204 if (getIcon() == null) updateIcon(); 205 206 g.translate(0, 2); 207 208 if (!fTrackingIcon) { 209 super.paint(g); 210 return; 211 } 212 213 final ImageIcon prev = (ImageIcon)getIcon(); 214 final ImageIcon pressedIcon = new ImageIcon(AquaUtils.generateSelectedDarkImage(prev.getImage())); 215 setIcon(pressedIcon); 216 super.paint(g); 217 setIcon(prev); 218 } 219 220 boolean mouseInIcon(final MouseEvent e) { 221 return getBounds().contains(e.getX(), e.getY()); 222 } 223 224 public Dimension getPreferredSize() { 225 return new Dimension(64, 64); //$ Dock preferred size 226 } 227 } 228 229 class DockLabel extends JLabel { 230 final static int NUB_HEIGHT = 7; 231 final static int ROUND_ADDITIONAL_HEIGHT = 8; 232 final static int ROUND_ADDITIONAL_WIDTH = 12; 233 234 DockLabel(final String text) { 235 super(text); 236 setBorder(null); 237 setOpaque(false); 238 setFont(AquaFonts.getDockIconFont()); 239 240 final FontMetrics metrics = getFontMetrics(getFont()); 241 setSize(SwingUtilities.computeStringWidth(metrics, getText()) + ROUND_ADDITIONAL_WIDTH * 2, metrics.getAscent() + NUB_HEIGHT + ROUND_ADDITIONAL_HEIGHT); 242 } 243 244 public void paint(final Graphics g) { 245 final int width = getWidth(); 246 final int height = getHeight(); 247 248 final Font font = getFont(); 249 final FontMetrics metrics = getFontMetrics(font); 250 g.setFont(font); 251 252 final String text = getText().trim(); 253 final int ascent = metrics.getAscent(); 254 255 final Rectangle2D stringBounds = metrics.getStringBounds(text, g); 256 final int halfway = width / 2; 257 258 final int x = (halfway - (int)stringBounds.getWidth() / 2); 259 260 final Graphics2D g2d = g instanceof Graphics2D ? (Graphics2D)g : null; 261 if (g2d != null) { 262 g.setColor(UIManager.getColor("DesktopIcon.labelBackground")); 263 final Object origAA = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING); 264 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 265 266 final int roundHeight = height - ROUND_ADDITIONAL_HEIGHT + 1; 267 g.fillRoundRect(0, 0, width, roundHeight, roundHeight, roundHeight); 268 269 final int[] xpts = { halfway, halfway + NUB_HEIGHT, halfway - NUB_HEIGHT }; 270 final int[] ypts = { height, height - NUB_HEIGHT, height - NUB_HEIGHT }; 271 g.fillPolygon(xpts, ypts, 3); 272 273 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, origAA); 274 } 275 276 g.setColor(Color.black); 277 SwingUtilities2.drawString(this, g, text, x, 2 + ascent); 278 g.setColor(Color.white); 279 SwingUtilities2.drawString(this, g, text, x, 1 + ascent); 280 } 281 282 public void show(final Component invoker) { 283 final int desiredLocationX = (invoker.getWidth() - getWidth()) / 2; 284 final int desiredLocationY = -(getHeight() + 6); 285 286 Container parent = invoker.getParent(); 287 288 for (Container p = parent; p != null; p = p.getParent()) { 289 if (p instanceof JRootPane) { 290 if (p.getParent() instanceof JInternalFrame) continue; 291 parent = ((JRootPane)p).getLayeredPane(); 292 for (p = parent.getParent(); p != null && (!(p instanceof java.awt.Window)); p = p.getParent()); 293 break; 294 } 295 } 296 297 final Point p = SwingUtilities.convertPoint(invoker, desiredLocationX, desiredLocationY, parent); 298 setLocation(p.x, p.y); 299 if (parent instanceof JLayeredPane) { 300 ((JLayeredPane)parent).add(this, JLayeredPane.POPUP_LAYER, 0); 301 } 302 } 303 304 public void hide() { 305 final Container parent = getParent(); 306 final Rectangle r = this.getBounds(); 307 if (parent == null) return; 308 parent.remove(this); 309 parent.repaint(r.x, r.y, r.width, r.height); 310 } 311 } 312 }