J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998-2003 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.metal; |
| 27 | |
| 28 | import java.awt.*; |
| 29 | import java.awt.event.*; |
| 30 | import javax.swing.*; |
| 31 | import javax.swing.border.*; |
| 32 | import javax.swing.plaf.*; |
| 33 | import java.beans.*; |
| 34 | import java.util.EventListener; |
| 35 | import java.io.Serializable; |
| 36 | import javax.swing.plaf.basic.BasicDesktopIconUI; |
| 37 | |
| 38 | /** |
| 39 | * Metal desktop icon. |
| 40 | * |
| 41 | * @author Steve Wilson |
| 42 | */ |
| 43 | public class MetalDesktopIconUI extends BasicDesktopIconUI |
| 44 | { |
| 45 | |
| 46 | JButton button; |
| 47 | JLabel label; |
| 48 | TitleListener titleListener; |
| 49 | private int width; |
| 50 | |
| 51 | public static ComponentUI createUI(JComponent c) { |
| 52 | return new MetalDesktopIconUI(); |
| 53 | } |
| 54 | |
| 55 | public MetalDesktopIconUI() { |
| 56 | } |
| 57 | |
| 58 | protected void installDefaults() { |
| 59 | super.installDefaults(); |
| 60 | LookAndFeel.installColorsAndFont(desktopIcon, "DesktopIcon.background", "DesktopIcon.foreground", "DesktopIcon.font"); |
| 61 | width = UIManager.getInt("DesktopIcon.width"); |
| 62 | } |
| 63 | |
| 64 | protected void installComponents() { |
| 65 | frame = desktopIcon.getInternalFrame(); |
| 66 | Icon icon = frame.getFrameIcon(); |
| 67 | String title = frame.getTitle(); |
| 68 | |
| 69 | button = new JButton (title, icon); |
| 70 | button.addActionListener( new ActionListener() { |
| 71 | public void actionPerformed(ActionEvent e) { |
| 72 | deiconize(); }} ); |
| 73 | button.setFont(desktopIcon.getFont()); |
| 74 | button.setBackground(desktopIcon.getBackground()); |
| 75 | button.setForeground(desktopIcon.getForeground()); |
| 76 | |
| 77 | int buttonH = button.getPreferredSize().height; |
| 78 | |
| 79 | Icon drag = new MetalBumps((buttonH/3), buttonH, |
| 80 | MetalLookAndFeel.getControlHighlight(), |
| 81 | MetalLookAndFeel.getControlDarkShadow(), |
| 82 | MetalLookAndFeel.getControl()); |
| 83 | label = new JLabel(drag); |
| 84 | |
| 85 | label.setBorder( new MatteBorder( 0, 2, 0, 1, desktopIcon.getBackground()) ); |
| 86 | desktopIcon.setLayout(new BorderLayout(2, 0)); |
| 87 | desktopIcon.add(button, BorderLayout.CENTER); |
| 88 | desktopIcon.add(label, BorderLayout.WEST); |
| 89 | } |
| 90 | |
| 91 | protected void uninstallComponents() { |
| 92 | desktopIcon.setLayout(null); |
| 93 | desktopIcon.remove(label); |
| 94 | desktopIcon.remove(button); |
| 95 | button = null; |
| 96 | frame = null; |
| 97 | } |
| 98 | |
| 99 | protected void installListeners() { |
| 100 | super.installListeners(); |
| 101 | desktopIcon.getInternalFrame().addPropertyChangeListener( |
| 102 | titleListener = new TitleListener()); |
| 103 | } |
| 104 | |
| 105 | protected void uninstallListeners() { |
| 106 | desktopIcon.getInternalFrame().removePropertyChangeListener( |
| 107 | titleListener); |
| 108 | titleListener = null; |
| 109 | super.uninstallListeners(); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | public Dimension getPreferredSize(JComponent c) { |
| 114 | // Metal desktop icons can not be resized. Their dimensions should |
| 115 | // always be the minimum size. See getMinimumSize(JComponent c). |
| 116 | return getMinimumSize(c); |
| 117 | } |
| 118 | |
| 119 | public Dimension getMinimumSize(JComponent c) { |
| 120 | // For the metal desktop icon we will use the layout maanger to |
| 121 | // determine the correct height of the component, but we want to keep |
| 122 | // the width consistent according to the jlf spec. |
| 123 | return new Dimension(width, |
| 124 | desktopIcon.getLayout().minimumLayoutSize(desktopIcon).height); |
| 125 | } |
| 126 | |
| 127 | public Dimension getMaximumSize(JComponent c) { |
| 128 | // Metal desktop icons can not be resized. Their dimensions should |
| 129 | // always be the minimum size. See getMinimumSize(JComponent c). |
| 130 | return getMinimumSize(c); |
| 131 | } |
| 132 | |
| 133 | class TitleListener implements PropertyChangeListener { |
| 134 | public void propertyChange (PropertyChangeEvent e) { |
| 135 | if (e.getPropertyName().equals("title")) { |
| 136 | button.setText((String)e.getNewValue()); |
| 137 | } |
| 138 | |
| 139 | if (e.getPropertyName().equals("frameIcon")) { |
| 140 | button.setIcon((Icon)e.getNewValue()); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |