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: if (c != null)
128: {
129: if (frame.isIcon())
130: c.remove(frame.getDesktopIcon());
131: else
132: c.remove(frame);
133: c.repaint();
134: }
135: }
136:
137:
142: public void maximizeFrame(JInternalFrame frame)
143: {
144:
145:
146:
147: if (frame.isIcon())
148: return;
149: frame.setNormalBounds(frame.getBounds());
150:
151: Container p = frame.getParent();
152: if (p != null)
153: {
154: Rectangle pBounds = p.getBounds();
155: Insets insets = p.getInsets();
156: pBounds.width -= insets.left + insets.right;
157: pBounds.height -= insets.top + insets.bottom;
158:
159: setBoundsForFrame(frame, 0, 0, pBounds.width, pBounds.height);
160: }
161: if (p instanceof JDesktopPane)
162: ((JDesktopPane) p).setSelectedFrame(frame);
163: else
164: {
165: try
166: {
167: frame.setSelected(true);
168: }
169: catch (PropertyVetoException e)
170: {
171:
172: }
173: }
174: }
175:
176:
182: public void minimizeFrame(JInternalFrame frame)
183: {
184: Rectangle normalBounds = frame.getNormalBounds();
185:
186: JDesktopPane p = frame.getDesktopPane();
187: if (p != null)
188: p.setSelectedFrame(frame);
189: else
190: {
191: try
192: {
193: frame.setSelected(true);
194: }
195: catch (PropertyVetoException e)
196: {
197:
198: }
199: }
200:
201: setBoundsForFrame(frame, normalBounds.x, normalBounds.y,
202: normalBounds.width, normalBounds.height);
203: }
204:
205:
211: public void iconifyFrame(JInternalFrame frame)
212: {
213: JDesktopPane p = frame.getDesktopPane();
214: JDesktopIcon icon = frame.getDesktopIcon();
215: if (p != null && p.getSelectedFrame() == frame)
216: p.setSelectedFrame(null);
217: else
218: {
219: try
220: {
221: frame.setSelected(false);
222: }
223: catch (PropertyVetoException e)
224: {
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: Rectangle b = frame.getBounds();
246: c.remove(frame);
247: c.repaint(b.x, b.y, b.width, b.height);
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: {
505: Rectangle b = icon.getBounds();
506: c.remove(icon);
507: c.repaint(b.x, b.y, b.width, b.height);
508: }
509: }
510:
511:
520: protected Rectangle getBoundsForIconOf(JInternalFrame frame)
521: {
522:
523:
524:
525:
526:
527: JDesktopPane desktopPane = frame.getDesktopPane();
528:
529: if (desktopPane == null)
530: return frame.getDesktopIcon().getBounds();
531:
532: Rectangle paneBounds = desktopPane.getBounds();
533: Insets insets = desktopPane.getInsets();
534: Dimension pref = frame.getDesktopIcon().getPreferredSize();
535:
536: Component[] frames = desktopPane.getComponents();
537:
538: int count = 0;
539: for (int i = 0, j = 0; i < frames.length; i++)
540: if (frames[i] instanceof JDesktopIcon
541: || frames[i] instanceof JInternalFrame
542: && ((JInternalFrame) frames[i]).getWasIcon() && frames[i] != frame)
543: count++;
544: iconRects = new Rectangle[count];
545: for (int i = 0, j = 0; i < frames.length; i++)
546: if (frames[i] instanceof JDesktopIcon)
547: iconRects[--count] = frames[i].getBounds();
548: else if (frames[i] instanceof JInternalFrame
549: && ((JInternalFrame) frames[i]).getWasIcon()
550: && frames[i] != frame)
551: iconRects[--count] = ((JInternalFrame) frames[i])
552: .getDesktopIcon().getBounds();
553:
554: int startingX = insets.left;
555: int startingY = paneBounds.height - insets.bottom - pref.height;
556: Rectangle ideal = new Rectangle(startingX, startingY, pref.width,
557: pref.height);
558: boolean clear = true;
559:
560: while (iconRects.length > 0)
561: {
562: clear = true;
563: for (int i = 0; i < iconRects.length; i++)
564: {
565: if (iconRects[i] != null && iconRects[i].intersects(ideal))
566: {
567: clear = false;
568: break;
569: }
570: }
571: if (clear)
572: return ideal;
573:
574: startingX += pref.width;
575: if (startingX + pref.width > paneBounds.width - insets.right)
576: {
577: startingX = insets.left;
578: startingY -= pref.height;
579: }
580: ideal.setBounds(startingX, startingY, pref.width, pref.height);
581: }
582:
583: return ideal;
584: }
585:
586:
593: protected void setPreviousBounds(JInternalFrame frame, Rectangle rect)
594: {
595: frame.setNormalBounds(rect);
596: }
597:
598:
606: protected Rectangle getPreviousBounds(JInternalFrame frame)
607: {
608: return frame.getNormalBounds();
609: }
610:
611:
619: protected void setWasIcon(JInternalFrame frame, Boolean value)
620: {
621: frame.setWasIcon(value.booleanValue(), WAS_ICON_ONCE_PROPERTY);
622: }
623:
624:
633: protected boolean wasIcon(JInternalFrame frame)
634: {
635: return frame.getWasIcon();
636: }
637: }