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:
97:
105: public void openFrame(JInternalFrame frame)
106: {
107: Container c = frame.getParent();
108: if (c == null)
109: c = frame.getDesktopIcon().getParent();
110: if (c == null)
111: return;
112:
113: c.remove(frame.getDesktopIcon());
114: c.add(frame);
115: frame.setVisible(true);
116: }
117:
118:
124: public void closeFrame(JInternalFrame frame)
125: {
126: Container c = frame.getParent();
127: frame.doDefaultCloseAction();
128:
129: if (c != null)
130: {
131: if (frame.isIcon())
132: c.remove(frame.getDesktopIcon());
133: else
134: c.remove(frame);
135: c.repaint();
136: }
137: }
138:
139:
144: public void maximizeFrame(JInternalFrame frame)
145: {
146:
147:
148:
149: if (frame.isIcon())
150: return;
151: frame.setNormalBounds(frame.getBounds());
152:
153: Container p = frame.getParent();
154: if (p != null)
155: {
156: Rectangle pBounds = p.getBounds();
157: Insets insets = p.getInsets();
158: pBounds.width -= insets.left + insets.right;
159: pBounds.height -= insets.top + insets.bottom;
160:
161: setBoundsForFrame(frame, 0, 0, pBounds.width, pBounds.height);
162: }
163: if (p instanceof JDesktopPane)
164: ((JDesktopPane) p).setSelectedFrame(frame);
165: else
166: {
167: try
168: {
169: frame.setSelected(true);
170: }
171: catch (PropertyVetoException e)
172: {
173:
174: }
175: }
176: }
177:
178:
184: public void minimizeFrame(JInternalFrame frame)
185: {
186: Rectangle normalBounds = frame.getNormalBounds();
187:
188: JDesktopPane p = frame.getDesktopPane();
189: if (p != null)
190: p.setSelectedFrame(frame);
191: else
192: {
193: try
194: {
195: frame.setSelected(true);
196: }
197: catch (PropertyVetoException e)
198: {
199:
200: }
201: }
202:
203: setBoundsForFrame(frame, normalBounds.x, normalBounds.y,
204: normalBounds.width, normalBounds.height);
205: }
206:
207:
213: public void iconifyFrame(JInternalFrame frame)
214: {
215: JDesktopPane p = frame.getDesktopPane();
216: JDesktopIcon icon = frame.getDesktopIcon();
217: if (p != null && p.getSelectedFrame() == frame)
218: p.setSelectedFrame(null);
219: else
220: {
221: try
222: {
223: frame.setSelected(false);
224: }
225: catch (PropertyVetoException e)
226: {
227:
228: }
229: }
230:
231: Container c = frame.getParent();
232:
233: if (!wasIcon(frame))
234: {
235: Rectangle r = getBoundsForIconOf(frame);
236: icon.setBounds(r);
237: setWasIcon(frame, Boolean.TRUE);
238: }
239:
240: if (c != null)
241: {
242: if (icon != null)
243: {
244: c.add(icon);
245: icon.setVisible(true);
246: }
247: c.remove(frame);
248: }
249: }
250:
251:
257: public void deiconifyFrame(JInternalFrame frame)
258: {
259: JDesktopIcon icon = frame.getDesktopIcon();
260: Container c = icon.getParent();
261:
262: removeIconFor(frame);
263: c.add(frame);
264: frame.setVisible(true);
265:
266: if (!frame.isSelected())
267: {
268: JDesktopPane p = frame.getDesktopPane();
269: if (p != null)
270: p.setSelectedFrame(frame);
271: else
272: {
273: try
274: {
275: frame.setSelected(true);
276: }
277: catch (PropertyVetoException e)
278: {
279:
280: }
281: }
282: }
283:
284: c.invalidate();
285: }
286:
287:
293: public void activateFrame(JInternalFrame frame)
294: {
295: JDesktopPane p = frame.getDesktopPane();
296:
297: if (p != null)
298: p.setSelectedFrame(frame);
299: else
300: {
301: try
302: {
303: frame.setSelected(true);
304: }
305: catch (PropertyVetoException e)
306: {
307:
308: }
309: }
310:
311: frame.toFront();
312: }
313:
314:
319: public void deactivateFrame(JInternalFrame frame)
320: {
321: JDesktopPane p = frame.getDesktopPane();
322: if (p != null)
323: {
324: if (p.getSelectedFrame() == frame)
325: p.setSelectedFrame(null);
326: }
327: else
328: {
329: try
330: {
331: frame.setSelected(false);
332: }
333: catch (PropertyVetoException e)
334: {
335:
336: }
337: }
338: }
339:
340:
347: public void beginDraggingFrame(JComponent component)
348: {
349: if (component instanceof JDesktopIcon)
350: pane = ((JDesktopIcon) component).getInternalFrame().getDesktopPane();
351: else
352: pane = ((JInternalFrame) component).getDesktopPane();
353: if (pane == null)
354: return;
355:
356: dragCache = component.getBounds();
357:
358: if (! (pane instanceof JDesktopPane))
359: currentDragMode = JDesktopPane.LIVE_DRAG_MODE;
360: else
361: currentDragMode = ((JDesktopPane) pane).getDragMode();
362: }
363:
364:
372: public void dragFrame(JComponent component, int newX, int newY)
373: {
374: if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
375: {
376:
377: }
378: else
379: {
380: Rectangle b = component.getBounds();
381: if (component instanceof JDesktopIcon)
382: component.setBounds(newX, newY, b.width, b.height);
383: else
384: setBoundsForFrame((JInternalFrame) component, newX, newY, b.width,
385: b.height);
386: }
387: }
388:
389:
395: public void endDraggingFrame(JComponent component)
396: {
397: if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
398: {
399: setBoundsForFrame((JInternalFrame) component, dragCache.x, dragCache.y,
400: dragCache.width, dragCache.height);
401: pane = null;
402: dragCache = null;
403: }
404: component.repaint();
405: }
406:
407:
415: public void beginResizingFrame(JComponent component, int direction)
416: {
417: pane = ((JInternalFrame) component).getDesktopPane();
418: if (pane == null)
419: return;
420:
421: dragCache = component.getBounds();
422: if (! (pane instanceof JDesktopPane))
423: currentDragMode = JDesktopPane.LIVE_DRAG_MODE;
424: else
425: currentDragMode = ((JDesktopPane) pane).getDragMode();
426: }
427:
428:
437: public void resizeFrame(JComponent component, int newX, int newY,
438: int newWidth, int newHeight)
439: {
440: dragCache.setBounds(newX, newY, newWidth, newHeight);
441:
442: if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
443: {
444:
445: }
446: else
447: setBoundsForFrame(component, dragCache.x, dragCache.y, dragCache.width,
448: dragCache.height);
449: }
450:
451:
458: public void endResizingFrame(JComponent component)
459: {
460: if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
461: {
462: setBoundsForFrame((JInternalFrame) component, dragCache.x, dragCache.y,
463: dragCache.width, dragCache.height);
464: pane = null;
465: dragCache = null;
466: }
467: component.repaint();
468: }
469:
470:
480: public void setBoundsForFrame(JComponent component, int newX, int newY,
481: int newWidth, int newHeight)
482: {
483: component.setBounds(newX, newY, newWidth, newHeight);
484: component.revalidate();
485:
486:
487: if (component.getParent() != null)
488: component.getParent().repaint();
489: else
490: component.repaint();
491: }
492:
493:
499: protected void removeIconFor(JInternalFrame frame)
500: {
501: JDesktopIcon icon = frame.getDesktopIcon();
502: Container c = icon.getParent();
503: if (c != null && icon != null)
504: c.remove(icon);
505: }
506:
507:
516: protected Rectangle getBoundsForIconOf(JInternalFrame frame)
517: {
518:
519:
520:
521:
522:
523: JDesktopPane desktopPane = frame.getDesktopPane();
524:
525: if (desktopPane == null)
526: return frame.getDesktopIcon().getBounds();
527:
528: Rectangle paneBounds = desktopPane.getBounds();
529: Insets insets = desktopPane.getInsets();
530: Dimension pref = frame.getDesktopIcon().getPreferredSize();
531:
532: Component[] frames = desktopPane.getComponents();
533:
534: int count = 0;
535: for (int i = 0, j = 0; i < frames.length; i++)
536: if (frames[i] instanceof JDesktopIcon
537: || frames[i] instanceof JInternalFrame
538: && ((JInternalFrame) frames[i]).getWasIcon() && frames[i] != frame)
539: count++;
540: iconRects = new Rectangle[count];
541: for (int i = 0, j = 0; i < frames.length; i++)
542: if (frames[i] instanceof JDesktopIcon)
543: iconRects[--count] = frames[i].getBounds();
544: else if (frames[i] instanceof JInternalFrame
545: && ((JInternalFrame) frames[i]).getWasIcon()
546: && frames[i] != frame)
547: iconRects[--count] = ((JInternalFrame) frames[i])
548: .getDesktopIcon().getBounds();
549:
550: int startingX = insets.left;
551: int startingY = paneBounds.height - insets.bottom - pref.height;
552: Rectangle ideal = new Rectangle(startingX, startingY, pref.width,
553: pref.height);
554: boolean clear = true;
555:
556: while (iconRects.length > 0)
557: {
558: clear = true;
559: for (int i = 0; i < iconRects.length; i++)
560: {
561: if (iconRects[i] != null && iconRects[i].intersects(ideal))
562: {
563: clear = false;
564: break;
565: }
566: }
567: if (clear)
568: return ideal;
569:
570: startingX += pref.width;
571: if (startingX + pref.width > paneBounds.width - insets.right)
572: {
573: startingX = insets.left;
574: startingY -= pref.height;
575: }
576: ideal.setBounds(startingX, startingY, pref.width, pref.height);
577: }
578:
579: return ideal;
580: }
581:
582:
589: protected void setPreviousBounds(JInternalFrame frame, Rectangle rect)
590: {
591: frame.setNormalBounds(rect);
592: }
593:
594:
602: protected Rectangle getPreviousBounds(JInternalFrame frame)
603: {
604: return frame.getNormalBounds();
605: }
606:
607:
615: protected void setWasIcon(JInternalFrame frame, Boolean value)
616: {
617: frame.setWasIcon(value.booleanValue(), WAS_ICON_ONCE_PROPERTY);
618: }
619:
620:
629: protected boolean wasIcon(JInternalFrame frame)
630: {
631: return frame.getWasIcon();
632: }
633: }