KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > synth > SynthDesktopPaneUI


1 /*
2  * @(#)SynthDesktopPaneUI.java 1.18 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.synth;
9
10 import javax.swing.*;
11 import javax.swing.border.*;
12 import javax.swing.plaf.*;
13 import javax.swing.plaf.basic.BasicDesktopPaneUI JavaDoc;
14
15 import java.beans.*;
16
17 import java.awt.event.*;
18 import java.awt.Dimension JavaDoc;
19 import java.awt.Insets JavaDoc;
20 import java.awt.Graphics JavaDoc;
21 import java.awt.KeyboardFocusManager JavaDoc;
22 import java.awt.*;
23 import java.util.Vector JavaDoc;
24 import sun.swing.plaf.synth.SynthUI;
25
26 /**
27  * Synth L&F for a desktop.
28  *
29  * @version 1.18, 12/19/03
30  * @author Joshua Outwater
31  * @author Steve Wilson
32  */

33 class SynthDesktopPaneUI extends BasicDesktopPaneUI JavaDoc implements
34                   PropertyChangeListener, SynthUI {
35     private SynthStyle JavaDoc style;
36     private TaskBar taskBar;
37     private DesktopManager oldDesktopManager;
38
39     public static ComponentUI createUI(JComponent c) {
40         return new SynthDesktopPaneUI JavaDoc();
41     }
42
43     protected void installListeners() {
44         super.installListeners();
45         desktop.addPropertyChangeListener(this);
46         if (taskBar != null) {
47             // Listen for desktop being resized
48
desktop.addComponentListener(taskBar);
49             // Listen for frames being added to desktop
50
desktop.addContainerListener(taskBar);
51         }
52     }
53
54     protected void installDefaults() {
55         updateStyle(desktop);
56
57         if (UIManager.getBoolean("InternalFrame.useTaskBar")) {
58             taskBar = new TaskBar();
59
60             for (Component comp : desktop.getComponents()) {
61                 JInternalFrame.JDesktopIcon desktopIcon;
62
63                 if (comp instanceof JInternalFrame.JDesktopIcon) {
64                     desktopIcon = (JInternalFrame.JDesktopIcon)comp;
65                 } else if (comp instanceof JInternalFrame) {
66                     desktopIcon = ((JInternalFrame)comp).getDesktopIcon();
67                 } else {
68                     continue;
69                 }
70                 // Move desktopIcon from desktop to taskBar
71
if (desktopIcon.getParent() == desktop) {
72                     desktop.remove(desktopIcon);
73                 }
74                 if (desktopIcon.getParent() != taskBar) {
75                     taskBar.add(desktopIcon);
76                     desktopIcon.getInternalFrame().addComponentListener(
77                         taskBar);
78                 }
79             }
80             taskBar.setBackground(desktop.getBackground());
81             desktop.add(taskBar,
82                 new Integer JavaDoc(JLayeredPane.PALETTE_LAYER.intValue() + 1));
83             if (desktop.isShowing()) {
84                 taskBar.adjustSize();
85             }
86         }
87     }
88
89     private void updateStyle(JDesktopPane c) {
90         SynthStyle JavaDoc oldStyle = style;
91         SynthContext JavaDoc context = getContext(c, ENABLED);
92         style = SynthLookAndFeel.updateStyle(context, this);
93         if (oldStyle != null) {
94             uninstallKeyboardActions();
95             installKeyboardActions();
96         }
97         context.dispose();
98     }
99
100     protected void uninstallListeners() {
101         if (taskBar != null) {
102             desktop.removeComponentListener(taskBar);
103             desktop.removeContainerListener(taskBar);
104         }
105         desktop.removePropertyChangeListener(this);
106         super.uninstallListeners();
107     }
108
109     protected void uninstallDefaults() {
110         SynthContext JavaDoc context = getContext(desktop, ENABLED);
111
112         style.uninstallDefaults(context);
113         context.dispose();
114         style = null;
115
116         if (taskBar != null) {
117             for (Component comp : taskBar.getComponents()) {
118                 JInternalFrame.JDesktopIcon desktopIcon =
119                     (JInternalFrame.JDesktopIcon)comp;
120                 taskBar.remove(desktopIcon);
121                 desktopIcon.setPreferredSize(null);
122                 JInternalFrame f = desktopIcon.getInternalFrame();
123                 if (f.isIcon()) {
124                     desktop.add(desktopIcon);
125                 }
126                 f.removeComponentListener(taskBar);
127             }
128             desktop.remove(taskBar);
129             taskBar = null;
130         }
131     }
132
133     protected void installDesktopManager() {
134         if (UIManager.getBoolean("InternalFrame.useTaskBar")) {
135             desktopManager = oldDesktopManager = desktop.getDesktopManager();
136             if (!(desktopManager instanceof SynthDesktopManager)) {
137                 desktopManager = new SynthDesktopManager();
138                 desktop.setDesktopManager(desktopManager);
139             }
140         } else {
141             super.installDesktopManager();
142         }
143     }
144
145     protected void uninstallDesktopManager() {
146         if (oldDesktopManager != null && !(oldDesktopManager instanceof UIResource)) {
147             desktopManager = desktop.getDesktopManager();
148             if (desktopManager == null || desktopManager instanceof UIResource) {
149                 desktop.setDesktopManager(oldDesktopManager);
150             }
151         }
152         oldDesktopManager = null;
153         super.uninstallDesktopManager();
154     }
155
156     static class TaskBar extends JPanel implements ComponentListener, ContainerListener {
157         TaskBar() {
158             setOpaque(true);
159             setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0) {
160                 public void layoutContainer(Container target) {
161                     // First shrink buttons to fit
162
Component[] comps = target.getComponents();
163                     int n = comps.length;
164                     if (n > 0) {
165                         // Start with the largest preferred width
166
int prefWidth = 0;
167                         for (Component c : comps) {
168                             c.setPreferredSize(null);
169                             Dimension JavaDoc prefSize = c.getPreferredSize();
170                             if (prefSize.width > prefWidth) {
171                                 prefWidth = prefSize.width;
172                             }
173                         }
174                         // Shrink equally to fit if needed
175
Insets JavaDoc insets = target.getInsets();
176                         int tw = target.getWidth() - insets.left - insets.right;
177                         int w = Math.min(prefWidth, Math.max(10, tw/n));
178                         for (Component c : comps) {
179                             Dimension JavaDoc prefSize = c.getPreferredSize();
180                             c.setPreferredSize(new Dimension JavaDoc(w, prefSize.height));
181                         }
182                     }
183                     super.layoutContainer(target);
184                 }
185             });
186
187             // PENDING: This should be handled by the painter
188
setBorder(new BevelBorder(BevelBorder.RAISED) {
189                 protected void paintRaisedBevel(Component c, Graphics JavaDoc g,
190                                                 int x, int y, int w, int h) {
191                     Color oldColor = g.getColor();
192                     g.translate(x, y);
193                     g.setColor(getHighlightOuterColor(c));
194                     g.drawLine(0, 0, 0, h-2);
195                     g.drawLine(1, 0, w-2, 0);
196                     g.setColor(getShadowOuterColor(c));
197                     g.drawLine(0, h-1, w-1, h-1);
198                     g.drawLine(w-1, 0, w-1, h-2);
199                     g.translate(-x, -y);
200                     g.setColor(oldColor);
201                 }
202             });
203         }
204
205         void adjustSize() {
206             JDesktopPane desktop = (JDesktopPane)getParent();
207             if (desktop != null) {
208                 int height = getPreferredSize().height;
209                 Insets JavaDoc insets = getInsets();
210                 if (height == insets.top + insets.bottom) {
211                     if (getHeight() <= height) {
212                         // Initial size, because we have no buttons yet
213
height += 21;
214                     } else {
215                         // We already have a good height
216
height = getHeight();
217                     }
218                 }
219                 setBounds(0, desktop.getHeight() - height, desktop.getWidth(), height);
220                 revalidate();
221                 repaint();
222             }
223         }
224
225         // ComponentListener interface
226

227         public void componentResized(ComponentEvent e) {
228             if (e.getSource() instanceof JDesktopPane) {
229                 adjustSize();
230             }
231         }
232
233         public void componentMoved(ComponentEvent e){}
234
235         public void componentShown(ComponentEvent e) {
236             if (e.getSource() instanceof JInternalFrame) {
237                 adjustSize();
238             }
239         }
240
241         public void componentHidden(ComponentEvent e) {
242             if (e.getSource() instanceof JInternalFrame) {
243                 ((JInternalFrame)e.getSource()).getDesktopIcon().setVisible(false);
244                 revalidate();
245             }
246         }
247
248         // ContainerListener interface
249

250         public void componentAdded(ContainerEvent e) {
251             if (e.getChild() instanceof JInternalFrame) {
252                 JDesktopPane desktop = (JDesktopPane)e.getSource();
253                 JInternalFrame f = (JInternalFrame)e.getChild();
254                 JInternalFrame.JDesktopIcon desktopIcon = f.getDesktopIcon();
255                 for (Component comp : getComponents()) {
256                     if (comp == desktopIcon) {
257                         // We have it already
258
return;
259                     }
260                 }
261                 add(desktopIcon);
262                 f.addComponentListener(this);
263                 if (getComponentCount() == 1) {
264                     adjustSize();
265                 }
266             }
267         }
268
269         public void componentRemoved(ContainerEvent e) {
270             if (e.getChild() instanceof JInternalFrame) {
271                 JInternalFrame f = (JInternalFrame)e.getChild();
272                 if (!f.isIcon()) {
273                     // Frame was removed without using setClosed(true)
274
remove(f.getDesktopIcon());
275                     f.removeComponentListener(this);
276                     revalidate();
277                     repaint();
278                 }
279             }
280         }
281     }
282
283
284     class SynthDesktopManager extends DefaultDesktopManager implements UIResource {
285
286         public void maximizeFrame(JInternalFrame f) {
287             if (f.isIcon()) {
288                 try {
289                     f.setIcon(false);
290                 } catch (PropertyVetoException e2) {
291                 }
292             } else {
293                 f.setNormalBounds(f.getBounds());
294                 Component desktop = f.getParent();
295                 setBoundsForFrame(f, 0, 0,
296                                   desktop.getWidth(),
297                                   desktop.getHeight() - taskBar.getHeight());
298             }
299
300             try {
301                 f.setSelected(true);
302             } catch (PropertyVetoException e2) {
303             }
304         }
305
306         public void iconifyFrame(JInternalFrame f) {
307             JInternalFrame.JDesktopIcon desktopIcon;
308             Container c = f.getParent();
309             JDesktopPane d = f.getDesktopPane();
310             boolean findNext = f.isSelected();
311
312             if (c == null) {
313                 return;
314             }
315
316             desktopIcon = f.getDesktopIcon();
317
318             if (!f.isMaximum()) {
319                 f.setNormalBounds(f.getBounds());
320             }
321             c.remove(f);
322             c.repaint(f.getX(), f.getY(), f.getWidth(), f.getHeight());
323             try {
324                 f.setSelected(false);
325             } catch (PropertyVetoException e2) {
326             }
327
328             // Get topmost of the remaining frames
329
if (findNext) {
330                 for (Component comp : c.getComponents()) {
331                     if (comp instanceof JInternalFrame) {
332                         try {
333                             ((JInternalFrame)comp).setSelected(true);
334                         } catch (PropertyVetoException e2) {
335                         }
336                         ((JInternalFrame)comp).moveToFront();
337                         return;
338                     }
339                 }
340             }
341         }
342
343
344         public void deiconifyFrame(JInternalFrame f) {
345             JInternalFrame.JDesktopIcon desktopIcon = f.getDesktopIcon();
346             Container c = desktopIcon.getParent();
347             if (c != null) {
348                 c = c.getParent();
349                 if (c != null) {
350                     c.add(f);
351                     if (f.isMaximum()) {
352                         int w = c.getWidth();
353                         int h = c.getHeight() - taskBar.getHeight();
354                         if (f.getWidth() != w || f.getHeight() != h) {
355                             setBoundsForFrame(f, 0, 0, w, h);
356                         }
357                     }
358                     if (f.isSelected()) {
359                         f.moveToFront();
360                     } else {
361                         try {
362                             f.setSelected(true);
363                         } catch (PropertyVetoException e2) {
364                         }
365                     }
366                 }
367             }
368         }
369
370         protected void removeIconFor(JInternalFrame f) {
371             super.removeIconFor(f);
372             taskBar.validate();
373         }
374
375         public void setBoundsForFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
376             super.setBoundsForFrame(f, newX, newY, newWidth, newHeight);
377             if (taskBar != null && newY >= taskBar.getY()) {
378                 f.setLocation(f.getX(), taskBar.getY()-f.getInsets().top);
379             }
380         }
381     }
382
383
384     public SynthContext JavaDoc getContext(JComponent c) {
385         return getContext(c, getComponentState(c));
386     }
387
388     private SynthContext JavaDoc getContext(JComponent c, int state) {
389         return SynthContext.getContext(SynthContext JavaDoc.class, c,
390                      SynthLookAndFeel.getRegion(c), style, state);
391     }
392
393     private Region JavaDoc getRegion(JComponent c) {
394         return SynthLookAndFeel.getRegion(c);
395     }
396
397     private int getComponentState(JComponent c) {
398         return SynthLookAndFeel.getComponentState(c);
399     }
400
401     public void update(Graphics JavaDoc g, JComponent c) {
402         SynthContext JavaDoc context = getContext(c);
403
404         SynthLookAndFeel.update(context, g);
405         context.getPainter().paintDesktopPaneBackground(context, g, 0, 0,
406                                                   c.getWidth(), c.getHeight());
407         paint(context, g);
408         context.dispose();
409     }
410
411     public void paint(Graphics JavaDoc g, JComponent c) {
412         SynthContext JavaDoc context = getContext(c);
413
414         paint(context, g);
415         context.dispose();
416     }
417
418     protected void paint(SynthContext JavaDoc context, Graphics JavaDoc g) {
419     }
420
421     public void paintBorder(SynthContext JavaDoc context, Graphics JavaDoc g, int x,
422                             int y, int w, int h) {
423         context.getPainter().paintDesktopPaneBorder(context, g, x, y, w, h);
424     }
425
426     public void propertyChange(PropertyChangeEvent evt) {
427         if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
428             updateStyle((JDesktopPane)evt.getSource());
429         }
430         if (evt.getPropertyName() == "ancestor" && taskBar != null) {
431             taskBar.adjustSize();
432         }
433     }
434 }
435
Popular Tags