KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > metal > MetalDesktopIconUI


1 /*
2  * @(#)MetalDesktopIconUI.java 1.21 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.metal;
9
10 import java.awt.*;
11 import java.awt.event.*;
12 import javax.swing.*;
13 import javax.swing.border.*;
14 import javax.swing.plaf.*;
15 import java.beans.*;
16 import java.util.EventListener JavaDoc;
17 import java.io.Serializable JavaDoc;
18 import javax.swing.plaf.basic.BasicDesktopIconUI JavaDoc;
19
20 /**
21  * Metal desktop icon.
22  *
23  * @version 1.21 12/19/03
24  * @author Steve Wilson
25  */

26 public class MetalDesktopIconUI extends BasicDesktopIconUI JavaDoc
27 {
28
29     JButton button;
30     JLabel label;
31     TitleListener titleListener;
32     private int width;
33
34     public static ComponentUI createUI(JComponent c) {
35         return new MetalDesktopIconUI JavaDoc();
36     }
37
38     public MetalDesktopIconUI() {
39     }
40
41     protected void installDefaults() {
42         super.installDefaults();
43         LookAndFeel.installColorsAndFont(desktopIcon, "DesktopIcon.background", "DesktopIcon.foreground", "DesktopIcon.font");
44         width = UIManager.getInt("DesktopIcon.width");
45     }
46    
47     protected void installComponents() {
48     frame = desktopIcon.getInternalFrame();
49     Icon icon = frame.getFrameIcon();
50     String JavaDoc title = frame.getTitle();
51
52     button = new JButton (title, icon);
53     button.addActionListener( new ActionListener() {
54                               public void actionPerformed(ActionEvent e) {
55              deiconize(); }} );
56     button.setFont(desktopIcon.getFont());
57     button.setBackground(desktopIcon.getBackground());
58     button.setForeground(desktopIcon.getForeground());
59
60     int buttonH = button.getPreferredSize().height;
61
62     Icon drag = new MetalBumps JavaDoc((buttonH/3), buttonH,
63                    MetalLookAndFeel.getControlHighlight(),
64                    MetalLookAndFeel.getControlDarkShadow(),
65                    MetalLookAndFeel.getControl());
66     label = new JLabel(drag);
67
68     label.setBorder( new MatteBorder( 0, 2, 0, 1, desktopIcon.getBackground()) );
69     desktopIcon.setLayout(new BorderLayout(2, 0));
70     desktopIcon.add(button, BorderLayout.CENTER);
71     desktopIcon.add(label, BorderLayout.WEST);
72     }
73
74     protected void uninstallComponents() {
75     desktopIcon.setLayout(null);
76     desktopIcon.remove(label);
77     desktopIcon.remove(button);
78         button = null;
79         frame = null;
80     }
81  
82     protected void installListeners() {
83         super.installListeners();
84         desktopIcon.getInternalFrame().addPropertyChangeListener(
85                 titleListener = new TitleListener());
86     }
87
88     protected void uninstallListeners() {
89         desktopIcon.getInternalFrame().removePropertyChangeListener(
90                 titleListener);
91         titleListener = null;
92         super.uninstallListeners();
93     }
94         
95
96     public Dimension getPreferredSize(JComponent c) {
97         // Metal desktop icons can not be resized. Their dimensions should
98
// always be the minimum size. See getMinimumSize(JComponent c).
99
return getMinimumSize(c);
100     }
101   
102     public Dimension getMinimumSize(JComponent c) {
103         // For the metal desktop icon we will use the layout maanger to
104
// determine the correct height of the component, but we want to keep
105
// the width consistent according to the jlf spec.
106
return new Dimension(width,
107                 desktopIcon.getLayout().minimumLayoutSize(desktopIcon).height);
108     }
109
110     public Dimension getMaximumSize(JComponent c) {
111         // Metal desktop icons can not be resized. Their dimensions should
112
// always be the minimum size. See getMinimumSize(JComponent c).
113
return getMinimumSize(c);
114     }
115
116     class TitleListener implements PropertyChangeListener {
117         public void propertyChange (PropertyChangeEvent e) {
118       if (e.getPropertyName().equals("title")) {
119         button.setText((String JavaDoc)e.getNewValue());
120       }
121
122       if (e.getPropertyName().equals("frameIcon")) {
123         button.setIcon((Icon)e.getNewValue());
124       }
125     }
126     }
127 }
128
Popular Tags