1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50:
51:
57: public class DefaultDesktopManager implements DesktopManager, Serializable
58: {
59:
60: private static final long serialVersionUID = 4657624909838017887L;
61:
62:
63: static final String WAS_ICON_ONCE_PROPERTY = "wasIconOnce";
64:
65:
69: private int currentDragMode = 0;
70:
71:
75: private transient Rectangle dragCache = new Rectangle();
76:
77:
81: private transient Container pane;
82:
83:
87: private transient Rectangle[] iconRects;
88:
89:
92: public DefaultDesktopManager()
93: {
94: }
95:
96:
104: public void openFrame(JInternalFrame frame)
105: {
106: Container c = frame.getParent();
107: if (c == null)
108: c = frame.getDesktopIcon().getParent();
109: if (c == null)
110: return;
111:
112: c.remove(frame.getDesktopIcon());
113: c.add(frame);
114: frame.setVisible(true);
115: }
116:
117:
123: public void closeFrame(JInternalFrame frame)
124: {
125: Container c = frame.getParent();
126: frame.doDefaultCloseAction();
127:
128: if (c != null)
129: {
130: if (frame.isIcon())
131: c.remove(frame.getDesktopIcon());
132: else
133: c.remove(frame);
134: c.repaint();
135: }
136: }
137:
138:
143: public void maximizeFrame(JInternalFrame frame)
144: {
145:
146:
147:
148: if (frame.isIcon())
149: return;
150: frame.setNormalBounds(frame.getBounds());
151:
152: Container p = frame.getParent();
153: if (p != null)
154: {
155: Rectangle pBounds = p.getBounds();
156: Insets insets = p.getInsets();
157: pBounds.width -= insets.left + insets.right;
158: pBounds.height -= insets.top + insets.bottom;
159:
160: setBoundsForFrame(frame, 0, 0, pBounds.width, pBounds.height);
161: }
162: if (p instanceof JDesktopPane)
163: ((JDesktopPane) p).setSelectedFrame(frame);
164: else
165: {
166: try
167: {
168: frame.setSelected(true);
169: }
170: catch (PropertyVetoException e)
171: {
172:
173: }
174: }
175: }
176:
177:
183: public void minimizeFrame(JInternalFrame frame)
184: {
185: Rectangle normalBounds = frame.getNormalBounds();
186:
187: JDesktopPane p = frame.getDesktopPane();
188: if (p != null)
189: p.setSelectedFrame(frame);
190: else
191: {
192: try
193: {
194: frame.setSelected(true);
195: }
196: catch (PropertyVetoException e)
197: {
198:
199: }
200: }
201:
202: setBoundsForFrame(frame, normalBounds.x, normalBounds.y,
203: normalBounds.width, normalBounds.height);
204: }
205:
206:
212: public void iconifyFrame(JInternalFrame frame)
213: {
214: JDesktopPane p = frame.getDesktopPane();
215: JDesktopIcon icon = frame.getDesktopIcon();
216: if (p != null && p.getSelectedFrame() == frame)
217: p.setSelectedFrame(null);
218: else
219: {
220: try
221: {
222: frame.setSelected(false);
223: }
224: catch (PropertyVetoException e)
225: {
226: }
227: }
228:
229: Container c = frame.getParent();
230:
231: if (!wasIcon(frame))
232: {
233: Rectangle r = getBoundsForIconOf(frame);
234: icon.setBounds(r);
235: setWasIcon(frame, Boolean.TRUE);
236: }
237:
238: if (c != null)
239: {
240: if (icon != null)
241: {
242: c.add(icon);
243: icon.setVisible(true);
244: }
245: c.remove(frame);
246: }
247: }
248:
249:
255: public void deiconifyFrame(JInternalFrame frame)
256: {
257: JDesktopIcon icon = frame.getDesktopIcon();
258: Container c = icon.getParent();
259:
260: removeIconFor(frame);
261: c.add(frame);
262: frame.setVisible(true);
263:
264: if (!frame.isSelected())
265: {
266: JDesktopPane p = frame.getDesktopPane();
267: if (p != null)
268: p.setSelectedFrame(frame);
269: else
270: {
271: try
272: {
273: frame.setSelected(true);
274: }
275: catch (PropertyVetoException e)
276: {
277:
278: }
279: }
280: }
281:
282: c.invalidate();
283: }
284:
285:
291: public void activateFrame(JInternalFrame frame)
292: {
293: JDesktopPane p = frame.getDesktopPane();
294:
295: if (p != null)
296: p.setSelectedFrame(frame);
297: else
298: {
299: try
300: {
301: frame.setSelected(true);
302: }
303: catch (PropertyVetoException e)
304: {
305: }
306: }
307:
308: frame.toFront();
309: }
310:
311:
316: public void deactivateFrame(JInternalFrame frame)
317: {
318: JDesktopPane p = frame.getDesktopPane();
319: if (p != null)
320: {
321: if (p.getSelectedFrame() == frame)
322: p.setSelectedFrame(null);
323: }
324: else
325: {
326: try
327: {
328: frame.setSelected(false);
329: }
330: catch (PropertyVetoException e)
331: {
332: }
333: }
334: }
335:
336:
343: public void beginDraggingFrame(JComponent component)
344: {
345: if (component instanceof JDesktopIcon)
346: pane = ((JDesktopIcon) component).getInternalFrame().getDesktopPane();
347: else
348: pane = ((JInternalFrame) component).getDesktopPane();
349: if (pane == null)
350: return;
351:
352: dragCache = component.getBounds();
353:
354: if (! (pane instanceof JDesktopPane))
355: currentDragMode = JDesktopPane.LIVE_DRAG_MODE;
356: else
357: currentDragMode = ((JDesktopPane) pane).getDragMode();
358: }
359:
360:
368: public void dragFrame(JComponent component, int newX, int newY)
369: {
370: if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
371: {
372:
373: }
374: else
375: {
376: Rectangle b = component.getBounds();
377: if (component instanceof JDesktopIcon)
378: component.setBounds(newX, newY, b.width, b.height);
379: else
380: setBoundsForFrame((JInternalFrame) component, newX, newY, b.width,
381: b.height);
382: }
383: }
384:
385:
391: public void endDraggingFrame(JComponent component)
392: {
393: if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
394: {
395: setBoundsForFrame((JInternalFrame) component, dragCache.x, dragCache.y,
396: dragCache.width, dragCache.height);
397: pane = null;
398: dragCache = null;
399: }
400: component.repaint();
401: }
402:
403:
411: public void beginResizingFrame(JComponent component, int direction)
412: {
413: pane = ((JInternalFrame) component).getDesktopPane();
414: if (pane == null)
415: return;
416:
417: dragCache = component.getBounds();
418: if (! (pane instanceof JDesktopPane))
419: currentDragMode = JDesktopPane.LIVE_DRAG_MODE;
420: else
421: currentDragMode = ((JDesktopPane) pane).getDragMode();
422: }
423:
424:
433: public void resizeFrame(JComponent component, int newX, int newY,
434: int newWidth, int newHeight)
435: {
436: dragCache.setBounds(newX, newY, newWidth, newHeight);
437:
438: if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
439: {
440:
441: }
442: else
443: setBoundsForFrame(component, dragCache.x, dragCache.y, dragCache.width,
444: dragCache.height);
445: }
446:
447:
454: public void endResizingFrame(JComponent component)
455: {
456: if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
457: {
458: setBoundsForFrame((JInternalFrame) component, dragCache.x, dragCache.y,
459: dragCache.width, dragCache.height);
460: pane = null;
461: dragCache = null;
462: }
463: component.repaint();
464: }
465:
466:
476: public void setBoundsForFrame(JComponent component, int newX, int newY,
477: int newWidth, int newHeight)
478: {
479: component.setBounds(newX, newY, newWidth, newHeight);
480: component.revalidate();
481:
482:
483: if (component.getParent() != null)
484: component.getParent().repaint();
485: else
486: component.repaint();
487: }
488:
489:
495: protected void removeIconFor(JInternalFrame frame)
496: {
497: JDesktopIcon icon = frame.getDesktopIcon();
498: Container c = icon.getParent();
499: if (c != null && icon != null)
500: c.remove(icon);
501: }
502:
503:
512: protected Rectangle getBoundsForIconOf(JInternalFrame frame)
513: {
514:
515:
516:
517:
518:
519: JDesktopPane desktopPane = frame.getDesktopPane();
520:
521: if (desktopPane == null)
522: return frame.getDesktopIcon().getBounds();
523:
524: Rectangle paneBounds = desktopPane.getBounds();
525: Insets insets = desktopPane.getInsets();
526: Dimension pref = frame.getDesktopIcon().getPreferredSize();
527:
528: Component[] frames = desktopPane.getComponents();
529:
530: int count = 0;
531: for (int i = 0, j = 0; i < frames.length; i++)
532: if (frames[i] instanceof JDesktopIcon
533: || frames[i] instanceof JInternalFrame
534: && ((JInternalFrame) frames[i]).getWasIcon() && frames[i] != frame)
535: count++;
536: iconRects = new Rectangle[count];
537: for (int i = 0, j = 0; i < frames.length; i++)
538: if (frames[i] instanceof JDesktopIcon)
539: iconRects[--count] = frames[i].getBounds();
540: else if (frames[i] instanceof JInternalFrame
541: && ((JInternalFrame) frames[i]).getWasIcon()
542: && frames[i] != frame)
543: iconRects[--count] = ((JInternalFrame) frames[i])
544: .getDesktopIcon().getBounds();
545:
546: int startingX = insets.left;
547: int startingY = paneBounds.height - insets.bottom - pref.height;
548: Rectangle ideal = new Rectangle(startingX, startingY, pref.width,
549: pref.height);
550: boolean clear = true;
551:
552: while (iconRects.length > 0)
553: {
554: clear = true;
555: for (int i = 0; i < iconRects.length; i++)
556: {
557: if (iconRects[i] != null && iconRects[i].intersects(ideal))
558: {
559: clear = false;
560: break;
561: }
562: }
563: if (clear)
564: return ideal;
565:
566: startingX += pref.width;
567: if (startingX + pref.width > paneBounds.width - insets.right)
568: {
569: startingX = insets.left;
570: startingY -= pref.height;
571: }
572: ideal.setBounds(startingX, startingY, pref.width, pref.height);
573: }
574:
575: return ideal;
576: }
577:
578:
585: protected void setPreviousBounds(JInternalFrame frame, Rectangle rect)
586: {
587: frame.setNormalBounds(rect);
588: }
589:
590:
598: protected Rectangle getPreviousBounds(JInternalFrame frame)
599: {
600: return frame.getNormalBounds();
601: }
602:
603:
611: protected void setWasIcon(JInternalFrame frame, Boolean value)
612: {
613: frame.setWasIcon(value.booleanValue(), WAS_ICON_ONCE_PROPERTY);
614: }
615:
616:
625: protected boolean wasIcon(JInternalFrame frame)
626: {
627: return frame.getWasIcon();
628: }
629: }