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: JInternalFrame active = null;
297: if (p != null)
298: active = p.getSelectedFrame();
299: if (active == null)
300: {
301: if (p != null)
302: {
303: p.setSelectedFrame(frame);
304: }
305: }
306: else if (active != frame)
307: {
308: if (active.isSelected())
309: {
310: try
311: {
312: active.setSelected(false);
313: }
314: catch (PropertyVetoException ex)
315: {
316:
317: }
318: }
319: if (p != null)
320: {
321: p.setSelectedFrame(frame);
322: }
323:
324: }
325: frame.toFront();
326: }
327:
328:
333: public void deactivateFrame(JInternalFrame frame)
334: {
335: JDesktopPane p = frame.getDesktopPane();
336: if (p != null)
337: {
338: if (p.getSelectedFrame() == frame)
339: p.setSelectedFrame(null);
340: }
341: else
342: {
343: try
344: {
345: frame.setSelected(false);
346: }
347: catch (PropertyVetoException e)
348: {
349:
350: }
351: }
352: }
353:
354:
361: public void beginDraggingFrame(JComponent component)
362: {
363: if (component instanceof JDesktopIcon)
364: pane = ((JDesktopIcon) component).getInternalFrame().getDesktopPane();
365: else
366: pane = ((JInternalFrame) component).getDesktopPane();
367: if (pane == null)
368: return;
369:
370: dragCache = component.getBounds();
371:
372: if (! (pane instanceof JDesktopPane))
373: currentDragMode = JDesktopPane.LIVE_DRAG_MODE;
374: else
375: currentDragMode = ((JDesktopPane) pane).getDragMode();
376: }
377:
378:
386: public void dragFrame(JComponent component, int newX, int newY)
387: {
388: if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
389: {
390:
391: }
392: else
393: {
394: Rectangle b = component.getBounds();
395: if (component instanceof JDesktopIcon)
396: component.setBounds(newX, newY, b.width, b.height);
397: else
398: setBoundsForFrame((JInternalFrame) component, newX, newY, b.width,
399: b.height);
400: }
401: }
402:
403:
409: public void endDraggingFrame(JComponent component)
410: {
411: if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
412: {
413: setBoundsForFrame((JInternalFrame) component, dragCache.x, dragCache.y,
414: dragCache.width, dragCache.height);
415: pane = null;
416: dragCache = null;
417: component.repaint();
418: }
419: }
420:
421:
429: public void beginResizingFrame(JComponent component, int direction)
430: {
431: pane = ((JInternalFrame) component).getDesktopPane();
432: if (pane == null)
433: return;
434:
435: dragCache = component.getBounds();
436: if (! (pane instanceof JDesktopPane))
437: currentDragMode = JDesktopPane.LIVE_DRAG_MODE;
438: else
439: currentDragMode = ((JDesktopPane) pane).getDragMode();
440: }
441:
442:
451: public void resizeFrame(JComponent component, int newX, int newY,
452: int newWidth, int newHeight)
453: {
454: dragCache.setBounds(newX, newY, newWidth, newHeight);
455:
456: if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
457: {
458:
459: }
460: else
461: setBoundsForFrame(component, dragCache.x, dragCache.y, dragCache.width,
462: dragCache.height);
463: }
464:
465:
472: public void endResizingFrame(JComponent component)
473: {
474: if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
475: {
476: setBoundsForFrame((JInternalFrame) component, dragCache.x, dragCache.y,
477: dragCache.width, dragCache.height);
478: pane = null;
479: dragCache = null;
480: component.repaint();
481: }
482: }
483:
484:
494: public void setBoundsForFrame(JComponent component, int newX, int newY,
495: int newWidth, int newHeight)
496: {
497: component.setBounds(newX, newY, newWidth, newHeight);
498: }
499:
500:
506: protected void removeIconFor(JInternalFrame frame)
507: {
508: JDesktopIcon icon = frame.getDesktopIcon();
509: Container c = icon.getParent();
510: if (c != null && icon != null)
511: {
512: Rectangle b = icon.getBounds();
513: c.remove(icon);
514: c.repaint(b.x, b.y, b.width, b.height);
515: }
516: }
517:
518:
527: protected Rectangle getBoundsForIconOf(JInternalFrame frame)
528: {
529:
530:
531:
532:
533:
534: JDesktopPane desktopPane = frame.getDesktopPane();
535:
536: if (desktopPane == null)
537: return frame.getDesktopIcon().getBounds();
538:
539: Rectangle paneBounds = desktopPane.getBounds();
540: Insets insets = desktopPane.getInsets();
541: Dimension pref = frame.getDesktopIcon().getPreferredSize();
542:
543: Component[] frames = desktopPane.getComponents();
544:
545: int count = 0;
546: for (int i = 0, j = 0; i < frames.length; i++)
547: if (frames[i] instanceof JDesktopIcon
548: || frames[i] instanceof JInternalFrame
549: && ((JInternalFrame) frames[i]).getWasIcon() && frames[i] != frame)
550: count++;
551: iconRects = new Rectangle[count];
552: for (int i = 0, j = 0; i < frames.length; i++)
553: if (frames[i] instanceof JDesktopIcon)
554: iconRects[--count] = frames[i].getBounds();
555: else if (frames[i] instanceof JInternalFrame
556: && ((JInternalFrame) frames[i]).getWasIcon()
557: && frames[i] != frame)
558: iconRects[--count] = ((JInternalFrame) frames[i])
559: .getDesktopIcon().getBounds();
560:
561: int startingX = insets.left;
562: int startingY = paneBounds.height - insets.bottom - pref.height;
563: Rectangle ideal = new Rectangle(startingX, startingY, pref.width,
564: pref.height);
565: boolean clear = true;
566:
567: while (iconRects.length > 0)
568: {
569: clear = true;
570: for (int i = 0; i < iconRects.length; i++)
571: {
572: if (iconRects[i] != null && iconRects[i].intersects(ideal))
573: {
574: clear = false;
575: break;
576: }
577: }
578: if (clear)
579: return ideal;
580:
581: startingX += pref.width;
582: if (startingX + pref.width > paneBounds.width - insets.right)
583: {
584: startingX = insets.left;
585: startingY -= pref.height;
586: }
587: ideal.setBounds(startingX, startingY, pref.width, pref.height);
588: }
589:
590: return ideal;
591: }
592:
593:
600: protected void setPreviousBounds(JInternalFrame frame, Rectangle rect)
601: {
602: frame.setNormalBounds(rect);
603: }
604:
605:
613: protected Rectangle getPreviousBounds(JInternalFrame frame)
614: {
615: return frame.getNormalBounds();
616: }
617:
618:
626: protected void setWasIcon(JInternalFrame frame, Boolean value)
627: {
628: frame.setWasIcon(value.booleanValue(), WAS_ICON_ONCE_PROPERTY);
629: }
630:
631:
640: protected boolean wasIcon(JInternalFrame frame)
641: {
642: return frame.getWasIcon();
643: }
644: }