1 /*
2  * Copyright (c) 1998, 2003, 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.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     /**
52      * Constructs a new instance of {@code MetalDesktopIconUI}.
53      *
54      * @param c a component
55      * @return a new instance of {@code MetalDesktopIconUI}
56      */
createUI(JComponent c)57     public static ComponentUI createUI(JComponent c)    {
58         return new MetalDesktopIconUI();
59     }
60 
61     /**
62      * Constructs a new instance of {@code MetalDesktopIconUI}.
63      */
MetalDesktopIconUI()64     public MetalDesktopIconUI() {
65     }
66 
installDefaults()67     protected void installDefaults() {
68         super.installDefaults();
69         LookAndFeel.installColorsAndFont(desktopIcon, "DesktopIcon.background", "DesktopIcon.foreground", "DesktopIcon.font");
70         width = UIManager.getInt("DesktopIcon.width");
71     }
72 
installComponents()73     protected void installComponents() {
74         frame = desktopIcon.getInternalFrame();
75         Icon icon = frame.getFrameIcon();
76         String title = frame.getTitle();
77 
78         button = new JButton (title, icon);
79         button.addActionListener( new ActionListener() {
80                                   public void actionPerformed(ActionEvent e) {
81              deiconize(); }} );
82         button.setFont(desktopIcon.getFont());
83         button.setBackground(desktopIcon.getBackground());
84         button.setForeground(desktopIcon.getForeground());
85 
86         int buttonH = button.getPreferredSize().height;
87 
88         Icon drag = new MetalBumps((buttonH/3), buttonH,
89                                    MetalLookAndFeel.getControlHighlight(),
90                                    MetalLookAndFeel.getControlDarkShadow(),
91                                    MetalLookAndFeel.getControl());
92         label = new JLabel(drag);
93 
94         label.setBorder( new MatteBorder( 0, 2, 0, 1, desktopIcon.getBackground()) );
95         desktopIcon.setLayout(new BorderLayout(2, 0));
96         desktopIcon.add(button, BorderLayout.CENTER);
97         desktopIcon.add(label, BorderLayout.WEST);
98     }
99 
uninstallComponents()100     protected void uninstallComponents() {
101         desktopIcon.setLayout(null);
102         desktopIcon.remove(label);
103         desktopIcon.remove(button);
104         button = null;
105         frame = null;
106     }
107 
installListeners()108     protected void installListeners() {
109         super.installListeners();
110         desktopIcon.getInternalFrame().addPropertyChangeListener(
111                 titleListener = new TitleListener());
112     }
113 
uninstallListeners()114     protected void uninstallListeners() {
115         desktopIcon.getInternalFrame().removePropertyChangeListener(
116                 titleListener);
117         titleListener = null;
118         super.uninstallListeners();
119     }
120 
121 
getPreferredSize(JComponent c)122     public Dimension getPreferredSize(JComponent c) {
123         // Metal desktop icons can not be resized.  Their dimensions should
124         // always be the minimum size.  See getMinimumSize(JComponent c).
125         return getMinimumSize(c);
126     }
127 
getMinimumSize(JComponent c)128     public Dimension getMinimumSize(JComponent c) {
129         // For the metal desktop icon we will use the layout maanger to
130         // determine the correct height of the component, but we want to keep
131         // the width consistent according to the jlf spec.
132         return new Dimension(width,
133                 desktopIcon.getLayout().minimumLayoutSize(desktopIcon).height);
134     }
135 
getMaximumSize(JComponent c)136     public Dimension getMaximumSize(JComponent c) {
137         // Metal desktop icons can not be resized.  Their dimensions should
138         // always be the minimum size.  See getMinimumSize(JComponent c).
139         return getMinimumSize(c);
140     }
141 
142     class TitleListener implements PropertyChangeListener {
propertyChange(PropertyChangeEvent e)143         public void propertyChange (PropertyChangeEvent e) {
144           if (e.getPropertyName().equals("title")) {
145             button.setText((String)e.getNewValue());
146           }
147 
148           if (e.getPropertyName().equals("frameIcon")) {
149             button.setIcon((Icon)e.getNewValue());
150           }
151         }
152     }
153 }
154