KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > motif > MotifDesktopIconUI


1 /*
2  * @(#)MotifDesktopIconUI.java 1.24 04/04/15
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.motif;
9
10 import com.sun.java.swing.SwingUtilities2;
11 import java.awt.*;
12 import java.awt.event.*;
13 import javax.swing.*;
14 import javax.swing.border.*;
15 import javax.swing.plaf.*;
16 import javax.swing.plaf.basic.*;
17 import java.beans.*;
18 import java.util.EventListener JavaDoc;
19 import java.io.Serializable JavaDoc;
20
21
22 /**
23  * Motif rendition of the component.
24  *
25  * @version 1.24 04/15/04
26  * @author Thomas Ball
27  * @author Rich Schiavi
28  */

29 public class MotifDesktopIconUI extends BasicDesktopIconUI
30 {
31     protected DesktopIconActionListener desktopIconActionListener;
32     protected DesktopIconMouseListener desktopIconMouseListener;
33
34     protected Icon defaultIcon;
35     protected IconButton iconButton;
36     protected IconLabel iconLabel;
37
38     // This is only used for its system menu, but we need a reference to it so
39
// we can remove its listeners.
40
private MotifInternalFrameTitlePane sysMenuTitlePane;
41
42     JPopupMenu systemMenu;
43     EventListener JavaDoc mml;
44
45     final static int LABEL_HEIGHT = 18;
46     final static int LABEL_DIVIDER = 4; // padding between icon and label
47

48     final static Font defaultTitleFont = new Font("SansSerif", Font.PLAIN, 12);
49
50     public static ComponentUI createUI(JComponent c) {
51         return new MotifDesktopIconUI();
52     }
53
54     public MotifDesktopIconUI() {
55     }
56
57     protected void installDefaults(){
58     super.installDefaults();
59     setDefaultIcon(UIManager.getIcon("DesktopIcon.icon"));
60     iconButton = createIconButton(defaultIcon);
61         // An underhanded way of creating a system popup menu.
62
sysMenuTitlePane = new MotifInternalFrameTitlePane(frame);
63         systemMenu = sysMenuTitlePane.getSystemMenu();
64
65         MotifBorders.FrameBorder border = new MotifBorders.FrameBorder(desktopIcon);
66     desktopIcon.setLayout(new BorderLayout());
67         iconButton.setBorder(border);
68     desktopIcon.add(iconButton, BorderLayout.CENTER);
69         iconLabel = createIconLabel(frame);
70         iconLabel.setBorder(border);
71         desktopIcon.add(iconLabel, BorderLayout.SOUTH);
72         desktopIcon.setSize(desktopIcon.getPreferredSize());
73         desktopIcon.validate();
74     JLayeredPane.putLayer(desktopIcon, JLayeredPane.getLayer(frame));
75     }
76
77     protected void installComponents(){
78     }
79
80     protected void uninstallComponents(){
81     }
82     
83     protected void installListeners(){
84     super.installListeners();
85     desktopIconActionListener = createDesktopIconActionListener();
86     desktopIconMouseListener = createDesktopIconMouseListener();
87     iconButton.addActionListener(desktopIconActionListener);
88     iconButton.addMouseListener(desktopIconMouseListener);
89     }
90
91     JInternalFrame.JDesktopIcon getDesktopIcon(){
92       return desktopIcon;
93     }
94
95     void setDesktopIcon(JInternalFrame.JDesktopIcon d){
96       desktopIcon = d;
97     }
98
99     JInternalFrame getFrame(){
100       return frame;
101     }
102   
103     void setFrame(JInternalFrame f){
104       frame = f ;
105     }
106
107     protected void showSystemMenu(){
108       systemMenu.show(iconButton, 0, getDesktopIcon().getHeight());
109     }
110     
111     protected void hideSystemMenu(){
112       systemMenu.setVisible(false);
113     }
114
115     protected IconLabel createIconLabel(JInternalFrame frame){
116     return new IconLabel(frame);
117     }
118     
119     protected IconButton createIconButton(Icon i){
120     return new IconButton(i);
121     }
122     
123     protected DesktopIconActionListener createDesktopIconActionListener(){
124     return new DesktopIconActionListener();
125     }
126
127     protected DesktopIconMouseListener createDesktopIconMouseListener(){
128     return new DesktopIconMouseListener();
129     }
130     
131     protected void uninstallDefaults(){
132     super.uninstallDefaults();
133     desktopIcon.setLayout(null);
134     desktopIcon.remove(iconButton);
135     desktopIcon.remove(iconLabel);
136     }
137     
138     protected void uninstallListeners(){
139         super.uninstallListeners();
140         iconButton.removeActionListener(desktopIconActionListener);
141         iconButton.removeMouseListener(desktopIconMouseListener);
142         sysMenuTitlePane.uninstallListeners();
143     }
144
145     public Dimension getMinimumSize(JComponent c) {
146         JInternalFrame iframe = desktopIcon.getInternalFrame();
147     
148         int w = defaultIcon.getIconWidth();
149         int h = defaultIcon.getIconHeight() + LABEL_HEIGHT + LABEL_DIVIDER;
150
151     Border border = iframe.getBorder();
152     if(border != null) {
153         w += border.getBorderInsets(iframe).left +
154                 border.getBorderInsets(iframe).right;
155         h += border.getBorderInsets(iframe).bottom +
156                 border.getBorderInsets(iframe).top;
157         }
158
159     return new Dimension(w, h);
160     }
161
162     public Dimension getPreferredSize(JComponent c) {
163         return getMinimumSize(c);
164     }
165
166     public Dimension getMaximumSize(JComponent c){
167         return getMinimumSize(c);
168     }
169
170     /**
171       * Returns the default desktop icon.
172       */

173     public Icon getDefaultIcon() {
174     return defaultIcon;
175     }
176
177     /**
178       * Sets the icon used as the default desktop icon.
179       */

180     public void setDefaultIcon(Icon newIcon) {
181     defaultIcon = newIcon;
182     }
183
184     protected class IconLabel extends JPanel {
185     JInternalFrame frame;
186
187         IconLabel(JInternalFrame f) {
188             super();
189         this.frame = f;
190             setFont(defaultTitleFont);
191
192             // Forward mouse events to titlebar for moves.
193
addMouseMotionListener(new MouseMotionListener() {
194                 public void mouseDragged(MouseEvent e) {
195                     forwardEventToParent(e);
196                 }
197                 public void mouseMoved(MouseEvent e) {
198                     forwardEventToParent(e);
199                 }
200             });
201             addMouseListener(new MouseListener() {
202                 public void mouseClicked(MouseEvent e) {
203                     forwardEventToParent(e);
204                 }
205                 public void mousePressed(MouseEvent e) {
206                     forwardEventToParent(e);
207                 }
208                 public void mouseReleased(MouseEvent e) {
209                     forwardEventToParent(e);
210                 }
211                 public void mouseEntered(MouseEvent e) {
212                     forwardEventToParent(e);
213                 }
214                 public void mouseExited(MouseEvent e) {
215                     forwardEventToParent(e);
216                 }
217             });
218         }
219
220         void forwardEventToParent(MouseEvent e) {
221             getParent().dispatchEvent(new MouseEvent(
222                 getParent(), e.getID(), e.getWhen(), e.getModifiers(),
223                 e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
224         }
225
226         public boolean isFocusTraversable() {
227             return false;
228         }
229
230         public Dimension getMinimumSize() {
231             return new Dimension(defaultIcon.getIconWidth() + 1,
232                                  LABEL_HEIGHT + LABEL_DIVIDER);
233         }
234
235         public Dimension getPreferredSize() {
236             String JavaDoc title = frame.getTitle();
237             FontMetrics fm = frame.getFontMetrics(defaultTitleFont);
238             int w = 4;
239             if (title != null) {
240                 w += SwingUtilities2.stringWidth(frame, fm, title);
241             }
242             return new Dimension(w, LABEL_HEIGHT + LABEL_DIVIDER);
243         }
244
245         public void paint(Graphics g) {
246             super.paint(g);
247
248             // touch-up frame
249
int maxX = getWidth() - 1;
250             Color shadow =
251                 UIManager.getColor("inactiveCaptionBorder").darker().darker();
252             g.setColor(shadow);
253             g.setClip(0, 0, getWidth(), getHeight());
254             g.drawLine(maxX - 1, 1, maxX - 1, 1);
255             g.drawLine(maxX, 0, maxX, 0);
256
257             // fill background
258
g.setColor(UIManager.getColor("inactiveCaption"));
259             g.fillRect(2, 1, maxX - 3, LABEL_HEIGHT + 1);
260
261             // draw text -- clipping to truncate text like CDE/Motif
262
g.setClip(2, 1, maxX - 4, LABEL_HEIGHT);
263             int y = LABEL_HEIGHT - SwingUtilities2.getFontMetrics(frame, g).
264                                                    getDescent();
265             g.setColor(UIManager.getColor("inactiveCaptionText"));
266             String JavaDoc title = frame.getTitle();
267             if (title != null) {
268                 SwingUtilities2.drawString(frame, g, title, 4, y);
269             }
270         }
271     }
272
273     protected class IconButton extends JButton {
274         Icon icon;
275
276         IconButton(Icon icon) {
277             super(icon);
278             this.icon = icon;
279             // Forward mouse events to titlebar for moves.
280
addMouseMotionListener(new MouseMotionListener() {
281                 public void mouseDragged(MouseEvent e) {
282                     forwardEventToParent(e);
283                 }
284                 public void mouseMoved(MouseEvent e) {
285                     forwardEventToParent(e);
286                 }
287             });
288             addMouseListener(new MouseListener() {
289                 public void mouseClicked(MouseEvent e) {
290                     forwardEventToParent(e);
291                 }
292                 public void mousePressed(MouseEvent e) {
293                     forwardEventToParent(e);
294                 }
295                 public void mouseReleased(MouseEvent e) {
296                     if (!systemMenu.isShowing()) {
297                         forwardEventToParent(e);
298                     }
299                 }
300                 public void mouseEntered(MouseEvent e) {
301                     forwardEventToParent(e);
302                 }
303                 public void mouseExited(MouseEvent e) {
304                     forwardEventToParent(e);
305                 }
306             });
307         }
308
309         void forwardEventToParent(MouseEvent e) {
310             getParent().dispatchEvent(new MouseEvent(
311                 getParent(), e.getID(), e.getWhen(), e.getModifiers(),
312                 e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
313         }
314
315         public boolean isFocusTraversable() {
316             return false;
317         }
318     }
319
320
321     protected class DesktopIconActionListener implements ActionListener {
322         public void actionPerformed(ActionEvent e){
323         systemMenu.show(iconButton, 0, getDesktopIcon().getHeight());
324     }
325     }
326
327     protected class DesktopIconMouseListener extends MouseAdapter {
328     // if we drag or move we should deengage the popup
329
public void mousePressed(MouseEvent e){
330         if (e.getClickCount() == 2){
331         try {
332             getFrame().setIcon(false);
333         } catch (PropertyVetoException e2){ }
334         systemMenu.setVisible(false);
335         /* the mouse release will not get reported correctly,
336            because the icon will no longer be in the hierarchy;
337            maybe that should be fixed, but until it is, we need
338            to do the required cleanup here. */

339             getFrame().getDesktopPane().getDesktopManager().endDraggingFrame((JComponent)e.getSource());
340         }
341     }
342     }
343 }
344
Popular Tags