Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/gtk/java/swing/plaf/gtk/GtkDesktopIconUI.java


1   /*
2    * @(#)GtkDesktopIconUI.java  1.2 00/01/12
3    *
4    * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
5    * 
6    * This software is the proprietary information of Sun Microsystems, Inc.  
7    * Use is subject to license terms.
8    * 
9    */
10  
11  package org.gtk.java.swing.plaf.gtk;
12  
13  import java.awt.*;
14  import java.awt.event.*;
15  import javax.swing.*;
16  import javax.swing.border.*;
17  import javax.swing.plaf.*;
18  import javax.swing.plaf.basic.*;
19  import java.beans.*;
20  import java.util.EventListener;
21  import java.io.Serializable;
22  
23  
24  /**
25   * Gtk rendition of the component.
26   *
27   * @version 1.15 08/26/98
28   * @author Thomas Ball
29   * @author Rich Schiavi
30   */
31  public class GtkDesktopIconUI extends BasicDesktopIconUI
32  {
33      protected DesktopIconActionListener desktopIconActionListener;
34      protected DesktopIconMouseListener  desktopIconMouseListener;
35  
36      protected Icon       defaultIcon;
37      protected IconButton iconButton;
38      protected IconLabel  iconLabel;
39      
40      JPopupMenu systemMenu;
41      EventListener mml;
42  
43      final static int LABEL_HEIGHT = 18;
44      final static int LABEL_DIVIDER = 4;    // padding between icon and label
45  
46      final static Font defaultTitleFont = new Font("SansSerif", Font.PLAIN, 12);
47  
48      public static ComponentUI createUI(JComponent c)    {
49          return new GtkDesktopIconUI();
50      }
51  
52      public GtkDesktopIconUI() {
53      }
54  
55      protected void installDefaults(){
56    super.installDefaults();
57    setDefaultIcon(UIManager.getIcon("DesktopIcon.icon"));
58    iconButton = createIconButton(defaultIcon);
59          // An unhanded way of creating a system popup menu.
60    GtkInternalFrameTitlePane titlePane = 
61              new GtkInternalFrameTitlePane(frame);
62          systemMenu = titlePane.getSystemMenu();
63          GtkBorders.FrameBorder border = new GtkBorders.FrameBorder(desktopIcon);
64    desktopIcon.setLayout(new BorderLayout());
65          iconButton.setBorder(border);
66    desktopIcon.add(iconButton, BorderLayout.CENTER);
67          iconLabel = createIconLabel(frame);
68          iconLabel.setBorder(border);
69          desktopIcon.add(iconLabel, BorderLayout.SOUTH);
70          desktopIcon.setSize(desktopIcon.getPreferredSize());
71          desktopIcon.validate();
72    JLayeredPane.putLayer(desktopIcon, JLayeredPane.getLayer(frame));
73      }
74  
75      protected void installComponents(){
76      }
77  
78      protected void uninstallComponents(){
79      }
80      
81      protected void installListeners(){
82    super.installListeners();
83    desktopIconActionListener = createDesktopIconActionListener();
84    desktopIconMouseListener = createDesktopIconMouseListener();
85    iconButton.addActionListener(desktopIconActionListener);
86    iconButton.addMouseListener(desktopIconMouseListener);
87      }
88  
89      JInternalFrame.JDesktopIcon getDesktopIcon(){
90        return desktopIcon;
91      }
92  
93      void setDesktopIcon(JInternalFrame.JDesktopIcon d){
94        desktopIcon = d;
95      }
96  
97      JInternalFrame getFrame(){
98        return frame;
99      }
100   
101     void setFrame(JInternalFrame f){
102       frame = f ;
103     }
104 
105     protected void showSystemMenu(){
106       systemMenu.show(iconButton, 0, getDesktopIcon().getHeight());
107     }
108     
109     protected void hideSystemMenu(){
110       systemMenu.setVisible(false);
111     }
112 
113     protected IconLabel createIconLabel(JInternalFrame frame){
114   return new IconLabel(frame);
115     }
116     
117     protected IconButton createIconButton(Icon i){
118   return new IconButton(i);
119     }
120     
121     protected DesktopIconActionListener createDesktopIconActionListener(){
122   return new DesktopIconActionListener();
123     }
124 
125     protected DesktopIconMouseListener createDesktopIconMouseListener(){
126   return new DesktopIconMouseListener();
127     }
128     
129     protected void uninstallDefaults(){
130   super.uninstallDefaults();
131   desktopIcon.setLayout(null);
132   desktopIcon.remove(iconButton);
133   desktopIcon.remove(iconLabel);
134     }
135   
136     protected void uninstallListeners(){
137   super.uninstallListeners();
138   iconButton.removeActionListener(desktopIconActionListener);
139   iconButton.removeMouseListener(desktopIconMouseListener);
140     }
141 
142     public Dimension getMinimumSize(JComponent c) {
143         JInternalFrame iframe = desktopIcon.getInternalFrame();
144   
145         int w = defaultIcon.getIconWidth();
146         int h = defaultIcon.getIconHeight() + LABEL_HEIGHT + LABEL_DIVIDER;
147 
148   Border border = iframe.getBorder();
149   if(border != null) {
150       w += border.getBorderInsets(iframe).left + 
151                 border.getBorderInsets(iframe).right;
152       h += border.getBorderInsets(iframe).bottom + 
153                 border.getBorderInsets(iframe).top;
154         }
155 
156   return new Dimension(w, h);
157     }
158 
159     public Dimension getPreferredSize(JComponent c) {
160         return getMinimumSize(c);
161     }
162 
163     public Dimension getMaximumSize(JComponent c){
164         return getMinimumSize(c);
165     }
166 
167     /**
168       * Returns the default desktop icon.
169       */
170     public Icon getDefaultIcon() {
171   return defaultIcon;
172     }
173 
174     /**
175       * Sets the icon used as the default desktop icon.
176       */
177     public void setDefaultIcon(Icon newIcon) {
178   defaultIcon = newIcon;
179     }
180 
181     protected class IconLabel extends JPanel {
182   JInternalFrame frame;
183 
184         IconLabel(JInternalFrame f) {
185             super();
186       this.frame = f;
187             setFont(defaultTitleFont);
188 
189             // Forward mouse events to titlebar for moves.
190             addMouseMotionListener(new MouseMotionListener() {
191                 public void mouseDragged(MouseEvent e) {
192                     forwardEventToParent(e);
193                 }
194                 public void mouseMoved(MouseEvent e) {
195                     forwardEventToParent(e);
196                 }
197             });
198             addMouseListener(new MouseListener() {
199                 public void mouseClicked(MouseEvent e) {
200                     forwardEventToParent(e);
201                 }
202                 public void mousePressed(MouseEvent e) {
203                     forwardEventToParent(e);
204                 }
205                 public void mouseReleased(MouseEvent e) {
206                     forwardEventToParent(e);
207                 }
208                 public void mouseEntered(MouseEvent e) {
209                     forwardEventToParent(e);
210                 }
211                 public void mouseExited(MouseEvent e) {
212                     forwardEventToParent(e);
213                 }
214             });
215         }
216 
217         void forwardEventToParent(MouseEvent e) {
218             getParent().dispatchEvent(new MouseEvent(
219                 getParent(), e.getID(), e.getWhen(), e.getModifiers(),
220                 e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
221         }
222 
223         public boolean isFocusTraversable() { 
224             return false; 
225         }
226 
227         public Dimension getMinimumSize() {
228             return new Dimension(defaultIcon.getIconWidth() + 1,
229                                  LABEL_HEIGHT + LABEL_DIVIDER);
230         }
231 
232         public Dimension getPreferredSize() {
233             String title = frame.getTitle();
234             FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(defaultTitleFont);
235             int w = fm.stringWidth(title) + 4;
236             return new Dimension(w, LABEL_HEIGHT + LABEL_DIVIDER);
237         }
238 
239         public void paint(Graphics g) {
240             super.paint(g);
241 
242             // touch-up frame
243             int maxX = getWidth() - 1;
244             Color shadow = 
245                 UIManager.getColor("inactiveCaptionBorder").darker().darker();
246             g.setColor(shadow);
247             g.setClip(0, 0, getWidth(), getHeight());
248             g.drawLine(maxX - 1, 1, maxX - 1, 1);
249             g.drawLine(maxX, 0, maxX, 0);
250 
251             // fill background
252             g.setColor(UIManager.getColor("inactiveCaption"));
253             g.fillRect(2, 1, maxX - 3, LABEL_HEIGHT + 1);
254 
255             // draw text -- clipping to truncate text like CDE/Gtk
256             g.setClip(2, 1, maxX - 4, LABEL_HEIGHT);
257             int y = LABEL_HEIGHT - g.getFontMetrics().getDescent();
258             g.setColor(UIManager.getColor("inactiveCaptionText"));
259             g.drawString(frame.getTitle(), 4, y);
260         }
261     }
262 
263     protected class IconButton extends JButton {
264         Icon icon;
265 
266         IconButton(Icon icon) {
267             super(icon);
268             this.icon = icon;
269             // Forward mouse events to titlebar for moves.
270             addMouseMotionListener(new MouseMotionListener() {
271                 public void mouseDragged(MouseEvent e) {
272                     forwardEventToParent(e);
273                 }
274                 public void mouseMoved(MouseEvent e) {
275                     forwardEventToParent(e);
276                 }
277             });
278             addMouseListener(new MouseListener() {
279                 public void mouseClicked(MouseEvent e) {
280                     forwardEventToParent(e);
281                 }
282                 public void mousePressed(MouseEvent e) {
283                     forwardEventToParent(e);
284                 }
285                 public void mouseReleased(MouseEvent e) {
286                     if (!systemMenu.isShowing()) {
287                         forwardEventToParent(e);
288                     }
289                 }
290                 public void mouseEntered(MouseEvent e) {
291                     forwardEventToParent(e);
292                 }
293                 public void mouseExited(MouseEvent e) {
294                     forwardEventToParent(e);
295                 }
296             });
297         }
298 
299         void forwardEventToParent(MouseEvent e) {
300             getParent().dispatchEvent(new MouseEvent(
301                 getParent(), e.getID(), e.getWhen(), e.getModifiers(),
302                 e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
303         }
304 
305         public boolean isFocusTraversable() { 
306             return false; 
307         }
308     }
309 
310 
311     protected class DesktopIconActionListener implements ActionListener {
312         public void actionPerformed(ActionEvent e){
313       systemMenu.show(iconButton, 0, getDesktopIcon().getHeight());
314   }
315     }
316 
317     protected class DesktopIconMouseListener extends MouseAdapter {
318   // if we drag or move we should deengage the popup
319         public void mousePressed(MouseEvent e){
320       if (e.getClickCount() == 2){
321     try {
322         getFrame().setIcon(false);
323     } catch (PropertyVetoException e2){ }
324     systemMenu.setVisible(false);
325       }
326   }
327     }
328 }