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: component.repaint();
404: }
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: component.repaint();
467: }
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: }
485:
486:
492: protected void removeIconFor(JInternalFrame frame)
493: {
494: JDesktopIcon icon = frame.getDesktopIcon();
495: Container c = icon.getParent();
496: if (c != null && icon != null)
497: {
498: Rectangle b = icon.getBounds();
499: c.remove(icon);
500: c.repaint(b.x, b.y, b.width, b.height);
501: }
502: }
503:
504:
513: protected Rectangle getBoundsForIconOf(JInternalFrame frame)
514: {
515:
516:
517:
518:
519:
520: JDesktopPane desktopPane = frame.getDesktopPane();
521:
522: if (desktopPane == null)
523: return frame.getDesktopIcon().getBounds();
524:
525: Rectangle paneBounds = desktopPane.getBounds();
526: Insets insets = desktopPane.getInsets();
527: Dimension pref = frame.getDesktopIcon().getPreferredSize();
528:
529: Component[] frames = desktopPane.getComponents();
530:
531: int count = 0;
532: for (int i = 0, j = 0; i < frames.length; i++)
533: if (frames[i] instanceof JDesktopIcon
534: || frames[i] instanceof JInternalFrame
535: && ((JInternalFrame) frames[i]).getWasIcon() && frames[i] != frame)
536: count++;
537: iconRects = new Rectangle[count];
538: for (int i = 0, j = 0; i < frames.length; i++)
539: if (frames[i] instanceof JDesktopIcon)
540: iconRects[--count] = frames[i].getBounds();
541: else if (frames[i] instanceof JInternalFrame
542: && ((JInternalFrame) frames[i]).getWasIcon()
543: && frames[i] != frame)
544: iconRects[--count] = ((JInternalFrame) frames[i])
545: .getDesktopIcon().getBounds();
546:
547: int startingX = insets.left;
548: int startingY = paneBounds.height - insets.bottom - pref.height;
549: Rectangle ideal = new Rectangle(startingX, startingY, pref.width,
550: pref.height);
551: boolean clear = true;
552:
553: while (iconRects.length > 0)
554: {
555: clear = true;
556: for (int i = 0; i < iconRects.length; i++)
557: {
558: if (iconRects[i] != null && iconRects[i].intersects(ideal))
559: {
560: clear = false;
561: break;
562: }
563: }
564: if (clear)
565: return ideal;
566:
567: startingX += pref.width;
568: if (startingX + pref.width > paneBounds.width - insets.right)
569: {
570: startingX = insets.left;
571: startingY -= pref.height;
572: }
573: ideal.setBounds(startingX, startingY, pref.width, pref.height);
574: }
575:
576: return ideal;
577: }
578:
579:
586: protected void setPreviousBounds(JInternalFrame frame, Rectangle rect)
587: {
588: frame.setNormalBounds(rect);
589: }
590:
591:
599: protected Rectangle getPreviousBounds(JInternalFrame frame)
600: {
601: return frame.getNormalBounds();
602: }
603:
604:
612: protected void setWasIcon(JInternalFrame frame, Boolean value)
613: {
614: frame.setWasIcon(value.booleanValue(), WAS_ICON_ONCE_PROPERTY);
615: }
616:
617:
626: protected boolean wasIcon(JInternalFrame frame)
627: {
628: return frame.getWasIcon();
629: }
630: }