DefaultDesktopManager.java [plain text]
package javax.swing;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import java.beans.PropertyVetoException;
import java.io.Serializable;
import javax.swing.JInternalFrame.JDesktopIcon;
public class DefaultDesktopManager implements DesktopManager, Serializable
{
private static final long serialVersionUID = 4657624909838017887L;
static final String WAS_ICON_ONCE_PROPERTY = "wasIconOnce";
private int currentDragMode = 0;
private transient Rectangle dragCache = new Rectangle();
private transient Container pane;
private transient Rectangle[] iconRects;
public DefaultDesktopManager()
{
}
public void openFrame(JInternalFrame frame)
{
Container c = frame.getParent();
if (c == null)
c = frame.getDesktopIcon().getParent();
if (c == null)
return;
c.remove(frame.getDesktopIcon());
c.add(frame);
frame.setVisible(true);
}
public void closeFrame(JInternalFrame frame)
{
Container c = frame.getParent();
frame.doDefaultCloseAction();
if (c != null)
{
if (frame.isIcon())
c.remove(frame.getDesktopIcon());
else
c.remove(frame);
c.repaint();
}
}
public void maximizeFrame(JInternalFrame frame)
{
if (frame.isIcon())
return;
frame.setNormalBounds(frame.getBounds());
Container p = frame.getParent();
if (p != null)
{
Rectangle pBounds = p.getBounds();
Insets insets = p.getInsets();
pBounds.width -= insets.left + insets.right;
pBounds.height -= insets.top + insets.bottom;
setBoundsForFrame(frame, 0, 0, pBounds.width, pBounds.height);
}
if (p instanceof JDesktopPane)
((JDesktopPane) p).setSelectedFrame(frame);
else
{
try
{
frame.setSelected(true);
}
catch (PropertyVetoException e)
{
}
}
}
public void minimizeFrame(JInternalFrame frame)
{
Rectangle normalBounds = frame.getNormalBounds();
JDesktopPane p = frame.getDesktopPane();
if (p != null)
p.setSelectedFrame(frame);
else
{
try
{
frame.setSelected(true);
}
catch (PropertyVetoException e)
{
}
}
setBoundsForFrame(frame, normalBounds.x, normalBounds.y,
normalBounds.width, normalBounds.height);
}
public void iconifyFrame(JInternalFrame frame)
{
JDesktopPane p = frame.getDesktopPane();
JDesktopIcon icon = frame.getDesktopIcon();
if (p != null && p.getSelectedFrame() == frame)
p.setSelectedFrame(null);
else
{
try
{
frame.setSelected(false);
}
catch (PropertyVetoException e)
{
}
}
Container c = frame.getParent();
if (! wasIcon(frame))
{
Rectangle r = getBoundsForIconOf(frame);
icon.setBounds(r);
setWasIcon(frame, Boolean.TRUE);
}
if (c != null)
{
if (icon != null)
{
c.add(icon);
icon.setVisible(true);
}
c.remove(frame);
}
}
public void deiconifyFrame(JInternalFrame frame)
{
JDesktopIcon icon = frame.getDesktopIcon();
Container c = icon.getParent();
removeIconFor(frame);
c.add(frame);
frame.setVisible(true);
if (! frame.isSelected())
{
JDesktopPane p = frame.getDesktopPane();
if (p != null)
p.setSelectedFrame(frame);
else
{
try
{
frame.setSelected(true);
}
catch (PropertyVetoException e)
{
}
}
}
c.invalidate();
}
public void activateFrame(JInternalFrame frame)
{
JDesktopPane p = frame.getDesktopPane();
if (p != null)
p.setSelectedFrame(frame);
else
{
try
{
frame.setSelected(true);
}
catch (PropertyVetoException e)
{
}
}
frame.toFront();
}
public void deactivateFrame(JInternalFrame frame)
{
JDesktopPane p = frame.getDesktopPane();
if (p != null)
{
if (p.getSelectedFrame() == frame)
p.setSelectedFrame(null);
}
else
{
try
{
frame.setSelected(false);
}
catch (PropertyVetoException e)
{
}
}
}
public void beginDraggingFrame(JComponent component)
{
if (component instanceof JDesktopIcon)
pane = ((JDesktopIcon) component).getInternalFrame().getDesktopPane();
else
pane = ((JInternalFrame) component).getDesktopPane();
if (pane == null)
return;
dragCache = component.getBounds();
if (! (pane instanceof JDesktopPane))
currentDragMode = JDesktopPane.LIVE_DRAG_MODE;
else
currentDragMode = ((JDesktopPane) pane).getDragMode();
}
public void dragFrame(JComponent component, int newX, int newY)
{
if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
{
}
else
{
Rectangle b = component.getBounds();
if (component instanceof JDesktopIcon)
component.setBounds(newX, newY, b.width, b.height);
else
setBoundsForFrame((JInternalFrame) component, newX, newY, b.width,
b.height);
}
}
public void endDraggingFrame(JComponent component)
{
if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
{
setBoundsForFrame((JInternalFrame) component, dragCache.x,
dragCache.y, dragCache.width, dragCache.height);
pane = null;
dragCache = null;
}
component.repaint();
}
public void beginResizingFrame(JComponent component, int direction)
{
pane = ((JInternalFrame) component).getDesktopPane();
if (pane == null)
return;
dragCache = component.getBounds();
if (! (pane instanceof JDesktopPane))
currentDragMode = JDesktopPane.LIVE_DRAG_MODE;
else
currentDragMode = ((JDesktopPane) pane).getDragMode();
}
public void resizeFrame(JComponent component, int newX, int newY,
int newWidth, int newHeight)
{
dragCache.setBounds(newX, newY, newWidth, newHeight);
if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
{
}
else
setBoundsForFrame(component, dragCache.x, dragCache.y, dragCache.width,
dragCache.height);
}
public void endResizingFrame(JComponent component)
{
if (currentDragMode == JDesktopPane.OUTLINE_DRAG_MODE)
{
setBoundsForFrame((JInternalFrame) component, dragCache.x,
dragCache.y, dragCache.width, dragCache.height);
pane = null;
dragCache = null;
}
component.repaint();
}
public void setBoundsForFrame(JComponent component, int newX, int newY,
int newWidth, int newHeight)
{
component.setBounds(newX, newY, newWidth, newHeight);
component.revalidate();
if (component.getParent() != null)
component.getParent().repaint();
else
component.repaint();
}
protected void removeIconFor(JInternalFrame frame)
{
JDesktopIcon icon = frame.getDesktopIcon();
Container c = icon.getParent();
if (c != null && icon != null)
c.remove(icon);
}
protected Rectangle getBoundsForIconOf(JInternalFrame frame)
{
JDesktopPane desktopPane = frame.getDesktopPane();
Rectangle paneBounds = desktopPane.getBounds();
Insets insets = desktopPane.getInsets();
Dimension pref = frame.getDesktopIcon().getPreferredSize();
if (desktopPane == null)
return frame.getDesktopIcon().getBounds();
Component[] frames = desktopPane.getComponents();
int count = 0;
for (int i = 0, j = 0; i < frames.length; i++)
if (frames[i] instanceof JDesktopIcon
|| frames[i] instanceof JInternalFrame
&& ((JInternalFrame) frames[i]).getWasIcon() && frames[i] != frame)
count++;
iconRects = new Rectangle[count];
for (int i = 0, j = 0; i < frames.length; i++)
if (frames[i] instanceof JDesktopIcon)
iconRects[--count] = frames[i].getBounds();
else if (frames[i] instanceof JInternalFrame
&& ((JInternalFrame) frames[i]).getWasIcon()
&& frames[i] != frame)
iconRects[--count] = ((JInternalFrame) frames[i]).getDesktopIcon()
.getBounds();
int startingX = insets.left;
int startingY = paneBounds.height - insets.bottom - pref.height;
Rectangle ideal = new Rectangle(startingX, startingY, pref.width,
pref.height);
boolean clear = true;
while (iconRects.length > 0)
{
clear = true;
for (int i = 0; i < iconRects.length; i++)
{
if (iconRects[i] != null && iconRects[i].intersects(ideal))
{
clear = false;
break;
}
}
if (clear)
return ideal;
startingX += pref.width;
if (startingX + pref.width > paneBounds.width - insets.right)
{
startingX = insets.left;
startingY -= pref.height;
}
ideal.setBounds(startingX, startingY, pref.width, pref.height);
}
return ideal;
}
protected void setPreviousBounds(JInternalFrame frame, Rectangle rect)
{
frame.setNormalBounds(rect);
}
protected Rectangle getPreviousBounds(JInternalFrame frame)
{
return frame.getNormalBounds();
}
protected void setWasIcon(JInternalFrame frame, Boolean value)
{
frame.setWasIcon(value.booleanValue(), WAS_ICON_ONCE_PROPERTY);
}
protected boolean wasIcon(JInternalFrame frame)
{
return frame.getWasIcon();
}
}