001 /*
002 * Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.plaf.synth;
027
028 import javax.swing.*;
029 import javax.swing.border.*;
030 import javax.swing.plaf.*;
031 import javax.swing.plaf.basic.BasicDesktopPaneUI;
032
033 import java.beans.*;
034
035 import java.awt.event.*;
036 import java.awt.Dimension;
037 import java.awt.Insets;
038 import java.awt.Graphics;
039 import java.awt.KeyboardFocusManager;
040 import java.awt.*;
041 import java.util.Vector;
042 import sun.swing.plaf.synth.SynthUI;
043
044 /**
045 * Synth L&F for a desktop.
046 *
047 * @version 1.25, 05/05/07
048 * @author Joshua Outwater
049 * @author Steve Wilson
050 */
051 class SynthDesktopPaneUI extends BasicDesktopPaneUI implements
052 PropertyChangeListener, SynthUI {
053 private SynthStyle style;
054 private TaskBar taskBar;
055 private DesktopManager oldDesktopManager;
056
057 public static ComponentUI createUI(JComponent c) {
058 return new SynthDesktopPaneUI();
059 }
060
061 protected void installListeners() {
062 super .installListeners();
063 desktop.addPropertyChangeListener(this );
064 if (taskBar != null) {
065 // Listen for desktop being resized
066 desktop.addComponentListener(taskBar);
067 // Listen for frames being added to desktop
068 desktop.addContainerListener(taskBar);
069 }
070 }
071
072 protected void installDefaults() {
073 updateStyle(desktop);
074
075 if (UIManager.getBoolean("InternalFrame.useTaskBar")) {
076 taskBar = new TaskBar();
077
078 for (Component comp : desktop.getComponents()) {
079 JInternalFrame.JDesktopIcon desktopIcon;
080
081 if (comp instanceof JInternalFrame.JDesktopIcon) {
082 desktopIcon = (JInternalFrame.JDesktopIcon) comp;
083 } else if (comp instanceof JInternalFrame) {
084 desktopIcon = ((JInternalFrame) comp)
085 .getDesktopIcon();
086 } else {
087 continue;
088 }
089 // Move desktopIcon from desktop to taskBar
090 if (desktopIcon.getParent() == desktop) {
091 desktop.remove(desktopIcon);
092 }
093 if (desktopIcon.getParent() != taskBar) {
094 taskBar.add(desktopIcon);
095 desktopIcon.getInternalFrame()
096 .addComponentListener(taskBar);
097 }
098 }
099 taskBar.setBackground(desktop.getBackground());
100 desktop.add(taskBar, new Integer(JLayeredPane.PALETTE_LAYER
101 .intValue() + 1));
102 if (desktop.isShowing()) {
103 taskBar.adjustSize();
104 }
105 }
106 }
107
108 private void updateStyle(JDesktopPane c) {
109 SynthStyle oldStyle = style;
110 SynthContext context = getContext(c, ENABLED);
111 style = SynthLookAndFeel.updateStyle(context, this );
112 if (oldStyle != null) {
113 uninstallKeyboardActions();
114 installKeyboardActions();
115 }
116 context.dispose();
117 }
118
119 protected void uninstallListeners() {
120 if (taskBar != null) {
121 desktop.removeComponentListener(taskBar);
122 desktop.removeContainerListener(taskBar);
123 }
124 desktop.removePropertyChangeListener(this );
125 super .uninstallListeners();
126 }
127
128 protected void uninstallDefaults() {
129 SynthContext context = getContext(desktop, ENABLED);
130
131 style.uninstallDefaults(context);
132 context.dispose();
133 style = null;
134
135 if (taskBar != null) {
136 for (Component comp : taskBar.getComponents()) {
137 JInternalFrame.JDesktopIcon desktopIcon = (JInternalFrame.JDesktopIcon) comp;
138 taskBar.remove(desktopIcon);
139 desktopIcon.setPreferredSize(null);
140 JInternalFrame f = desktopIcon.getInternalFrame();
141 if (f.isIcon()) {
142 desktop.add(desktopIcon);
143 }
144 f.removeComponentListener(taskBar);
145 }
146 desktop.remove(taskBar);
147 taskBar = null;
148 }
149 }
150
151 protected void installDesktopManager() {
152 if (UIManager.getBoolean("InternalFrame.useTaskBar")) {
153 desktopManager = oldDesktopManager = desktop
154 .getDesktopManager();
155 if (!(desktopManager instanceof SynthDesktopManager)) {
156 desktopManager = new SynthDesktopManager();
157 desktop.setDesktopManager(desktopManager);
158 }
159 } else {
160 super .installDesktopManager();
161 }
162 }
163
164 protected void uninstallDesktopManager() {
165 if (oldDesktopManager != null
166 && !(oldDesktopManager instanceof UIResource)) {
167 desktopManager = desktop.getDesktopManager();
168 if (desktopManager == null
169 || desktopManager instanceof UIResource) {
170 desktop.setDesktopManager(oldDesktopManager);
171 }
172 }
173 oldDesktopManager = null;
174 super .uninstallDesktopManager();
175 }
176
177 static class TaskBar extends JPanel implements ComponentListener,
178 ContainerListener {
179 TaskBar() {
180 setOpaque(true);
181 setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0) {
182 public void layoutContainer(Container target) {
183 // First shrink buttons to fit
184 Component[] comps = target.getComponents();
185 int n = comps.length;
186 if (n > 0) {
187 // Start with the largest preferred width
188 int prefWidth = 0;
189 for (Component c : comps) {
190 c.setPreferredSize(null);
191 Dimension prefSize = c.getPreferredSize();
192 if (prefSize.width > prefWidth) {
193 prefWidth = prefSize.width;
194 }
195 }
196 // Shrink equally to fit if needed
197 Insets insets = target.getInsets();
198 int tw = target.getWidth() - insets.left
199 - insets.right;
200 int w = Math.min(prefWidth, Math
201 .max(10, tw / n));
202 for (Component c : comps) {
203 Dimension prefSize = c.getPreferredSize();
204 c.setPreferredSize(new Dimension(w,
205 prefSize.height));
206 }
207 }
208 super .layoutContainer(target);
209 }
210 });
211
212 // PENDING: This should be handled by the painter
213 setBorder(new BevelBorder(BevelBorder.RAISED) {
214 protected void paintRaisedBevel(Component c,
215 Graphics g, int x, int y, int w, int h) {
216 Color oldColor = g.getColor();
217 g.translate(x, y);
218 g.setColor(getHighlightOuterColor(c));
219 g.drawLine(0, 0, 0, h - 2);
220 g.drawLine(1, 0, w - 2, 0);
221 g.setColor(getShadowOuterColor(c));
222 g.drawLine(0, h - 1, w - 1, h - 1);
223 g.drawLine(w - 1, 0, w - 1, h - 2);
224 g.translate(-x, -y);
225 g.setColor(oldColor);
226 }
227 });
228 }
229
230 void adjustSize() {
231 JDesktopPane desktop = (JDesktopPane) getParent();
232 if (desktop != null) {
233 int height = getPreferredSize().height;
234 Insets insets = getInsets();
235 if (height == insets.top + insets.bottom) {
236 if (getHeight() <= height) {
237 // Initial size, because we have no buttons yet
238 height += 21;
239 } else {
240 // We already have a good height
241 height = getHeight();
242 }
243 }
244 setBounds(0, desktop.getHeight() - height, desktop
245 .getWidth(), height);
246 revalidate();
247 repaint();
248 }
249 }
250
251 // ComponentListener interface
252
253 public void componentResized(ComponentEvent e) {
254 if (e.getSource() instanceof JDesktopPane) {
255 adjustSize();
256 }
257 }
258
259 public void componentMoved(ComponentEvent e) {
260 }
261
262 public void componentShown(ComponentEvent e) {
263 if (e.getSource() instanceof JInternalFrame) {
264 adjustSize();
265 }
266 }
267
268 public void componentHidden(ComponentEvent e) {
269 if (e.getSource() instanceof JInternalFrame) {
270 ((JInternalFrame) e.getSource()).getDesktopIcon()
271 .setVisible(false);
272 revalidate();
273 }
274 }
275
276 // ContainerListener interface
277
278 public void componentAdded(ContainerEvent e) {
279 if (e.getChild() instanceof JInternalFrame) {
280 JDesktopPane desktop = (JDesktopPane) e.getSource();
281 JInternalFrame f = (JInternalFrame) e.getChild();
282 JInternalFrame.JDesktopIcon desktopIcon = f
283 .getDesktopIcon();
284 for (Component comp : getComponents()) {
285 if (comp == desktopIcon) {
286 // We have it already
287 return;
288 }
289 }
290 add(desktopIcon);
291 f.addComponentListener(this );
292 if (getComponentCount() == 1) {
293 adjustSize();
294 }
295 }
296 }
297
298 public void componentRemoved(ContainerEvent e) {
299 if (e.getChild() instanceof JInternalFrame) {
300 JInternalFrame f = (JInternalFrame) e.getChild();
301 if (!f.isIcon()) {
302 // Frame was removed without using setClosed(true)
303 remove(f.getDesktopIcon());
304 f.removeComponentListener(this );
305 revalidate();
306 repaint();
307 }
308 }
309 }
310 }
311
312 class SynthDesktopManager extends DefaultDesktopManager implements
313 UIResource {
314
315 public void maximizeFrame(JInternalFrame f) {
316 if (f.isIcon()) {
317 try {
318 f.setIcon(false);
319 } catch (PropertyVetoException e2) {
320 }
321 } else {
322 f.setNormalBounds(f.getBounds());
323 Component desktop = f.getParent();
324 setBoundsForFrame(f, 0, 0, desktop.getWidth(), desktop
325 .getHeight()
326 - taskBar.getHeight());
327 }
328
329 try {
330 f.setSelected(true);
331 } catch (PropertyVetoException e2) {
332 }
333 }
334
335 public void iconifyFrame(JInternalFrame f) {
336 JInternalFrame.JDesktopIcon desktopIcon;
337 Container c = f.getParent();
338 JDesktopPane d = f.getDesktopPane();
339 boolean findNext = f.isSelected();
340
341 if (c == null) {
342 return;
343 }
344
345 desktopIcon = f.getDesktopIcon();
346
347 if (!f.isMaximum()) {
348 f.setNormalBounds(f.getBounds());
349 }
350 c.remove(f);
351 c.repaint(f.getX(), f.getY(), f.getWidth(), f.getHeight());
352 try {
353 f.setSelected(false);
354 } catch (PropertyVetoException e2) {
355 }
356
357 // Get topmost of the remaining frames
358 if (findNext) {
359 for (Component comp : c.getComponents()) {
360 if (comp instanceof JInternalFrame) {
361 try {
362 ((JInternalFrame) comp).setSelected(true);
363 } catch (PropertyVetoException e2) {
364 }
365 ((JInternalFrame) comp).moveToFront();
366 return;
367 }
368 }
369 }
370 }
371
372 public void deiconifyFrame(JInternalFrame f) {
373 JInternalFrame.JDesktopIcon desktopIcon = f
374 .getDesktopIcon();
375 Container c = desktopIcon.getParent();
376 if (c != null) {
377 c = c.getParent();
378 if (c != null) {
379 c.add(f);
380 if (f.isMaximum()) {
381 int w = c.getWidth();
382 int h = c.getHeight() - taskBar.getHeight();
383 if (f.getWidth() != w || f.getHeight() != h) {
384 setBoundsForFrame(f, 0, 0, w, h);
385 }
386 }
387 if (f.isSelected()) {
388 f.moveToFront();
389 } else {
390 try {
391 f.setSelected(true);
392 } catch (PropertyVetoException e2) {
393 }
394 }
395 }
396 }
397 }
398
399 protected void removeIconFor(JInternalFrame f) {
400 super .removeIconFor(f);
401 taskBar.validate();
402 }
403
404 public void setBoundsForFrame(JComponent f, int newX, int newY,
405 int newWidth, int newHeight) {
406 super .setBoundsForFrame(f, newX, newY, newWidth, newHeight);
407 if (taskBar != null && newY >= taskBar.getY()) {
408 f.setLocation(f.getX(), taskBar.getY()
409 - f.getInsets().top);
410 }
411 }
412 }
413
414 public SynthContext getContext(JComponent c) {
415 return getContext(c, getComponentState(c));
416 }
417
418 private SynthContext getContext(JComponent c, int state) {
419 return SynthContext.getContext(SynthContext.class, c,
420 SynthLookAndFeel.getRegion(c), style, state);
421 }
422
423 private Region getRegion(JComponent c) {
424 return SynthLookAndFeel.getRegion(c);
425 }
426
427 private int getComponentState(JComponent c) {
428 return SynthLookAndFeel.getComponentState(c);
429 }
430
431 public void update(Graphics g, JComponent c) {
432 SynthContext context = getContext(c);
433
434 SynthLookAndFeel.update(context, g);
435 context.getPainter().paintDesktopPaneBackground(context, g, 0,
436 0, c.getWidth(), c.getHeight());
437 paint(context, g);
438 context.dispose();
439 }
440
441 public void paint(Graphics g, JComponent c) {
442 SynthContext context = getContext(c);
443
444 paint(context, g);
445 context.dispose();
446 }
447
448 protected void paint(SynthContext context, Graphics g) {
449 }
450
451 public void paintBorder(SynthContext context, Graphics g, int x,
452 int y, int w, int h) {
453 context.getPainter().paintDesktopPaneBorder(context, g, x, y,
454 w, h);
455 }
456
457 public void propertyChange(PropertyChangeEvent evt) {
458 if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
459 updateStyle((JDesktopPane) evt.getSource());
460 }
461 if (evt.getPropertyName() == "ancestor" && taskBar != null) {
462 taskBar.adjustSize();
463 }
464 }
465 }
|