9.4 The DesktopManager Interface
This
interface is responsible for much of
the management of internal frames contained by
JDesktopPanes. It allows
an L&F to define exactly how it wants
to manage things such as frame activation, movement, and
iconification. Most of the methods in
InternalFrameUI implementations should delegate to
a DesktopManager object. As described earlier,
DesktopManagers are contained by
JDesktopPane objects and are intended to be set by
the L&F. You can also create your own variations on the supplied
implementations to provide custom behavior, as shown in the example
that concludes this chapter.
9.4.1 Methods
The majority of the methods in this interface act on a given
JInternalFrame. However, those methods that could
be applied to other types of components do not restrict the parameter
unnecessarily (they accept any JComponent),
despite the fact that they are typically used only with
JInternalFrames. If you implement your own
DesktopManager or other L&F classes, you may
find a need for this flexibility.
- public abstract void activateFrame( JInternalFrame f )
-
Called to indicate that the specified frame should become active (is
gaining focus).
- public abstract void beginDraggingFrame( JComponent f )
-
Called to indicate that the specified frame is now being dragged. The
given component is normally a JInternalFrame.
- public abstract void beginResizingFrame( JComponent f, int direction)
-
Called to indicate that the specified frame will be resized. The
direction comes from SwingConstants and must be
NORTH, SOUTH,
EAST, WEST,
NORTH_EAST, NORTH_WEST,
SOUTH_EAST, or SOUTH_WEST,
representing the edge or corner being dragged (although this value is
currently ignored by all provided implementations). The given
component is normally a JInternalFrame. When
resizing is complete, endResizingFrame( ) is
called.
- public abstract void closeFrame( JInternalFrame f )
-
Called to indicate that the specified frame should be closed.
- public abstract void deactivateFrame( JInternalFrame f )
-
Called to indicate that the specified frame is no longer active (has
lost focus).
- public abstract void deiconifyFrame( JInternalFrame f )
-
Called to indicate that the specified frame should no longer be
iconified.
- public abstract void dragFrame( JComponent f, int newX, int newY)
-
Called to indicate that the specified frame should be moved from its
current location to the newly specified coordinates. The given
component is normally a JInternalFrame.
- public abstract void endDraggingFrame( JComponent f )
-
Called to indicate that the specified frame is no longer being
dragged. The given component is normally a
JInternalFrame.
- public abstract void endResizingFrame( JComponent f )
-
Called to indicate that the specified frame is no longer being
resized. The given component is normally a
JInternalFrame.
- public abstract void iconifyFrame( JInternalFrame f )
-
Called to indicate that the specified frame should be iconified.
- public abstract void maximizeFrame( JInternalFrame f )
-
Called to indicate that the specified frame should be maximized.
- public abstract void minimizeFrame( JInternalFrame f )
-
Called to indicate that the specified frame should be minimized. Note
that this is not the same as iconifying the frame. Typically, calling
this method causes the frame to return to its size and position
before it was maximized.
- public abstract void openFrame( JInternalFrame f )
-
Called to add a frame and display it at a reasonable location. This
is not often called because frames are normally added directly to
their parent.
- public abstract void resizeFrame( JComponent f, int newX, int newY, int newWidth, int newHeight)
-
Called to indicate that the specified frame has been resized. Note
that resizing is still in progress (many calls to this method may be
made while the frame is being resized) after this method completes.
The given component is normally a JInternalFrame.
- public abstract void setBoundsForFrame( JComponent f, int newX, int newY, int newWidth, int newHeight)
-
Called to set a new size and location for a frame. The given
component will normally be a JInternalFrame.
9.4.2 The DefaultDesktopManager Class
DefaultDesktopManager
is a default implementation of the DesktopManager
interface. It serves as the base class for the Windows and Motif
L&Fs, while the Metal L&F uses it without modification. In
this section, we'll give a brief explanation of how
each of the methods in the interface is implemented by this class.
9.4.2.1 Methods
- public void activateFrame( JInternalFrame f )
-
Call setSelected(false) on all other
JInternalFrames contained by the specified
frame's parent that are in the same layer as the
given frame. It then moves the given frame to the front of its layer
and selects it.
- public void closeFrame( JInternalFrame f )
-
Remove the given frame from its parent. It also removes the
frame's icon (if displayed). It sets the
frame's previous bounds to null.
- public void deiconifyFrame( JInternalFrame f )
-
Remove the given frame's icon from its parent and
add the frame itself in its place. This method then tries to select
the given frame if it can receive focus.
- public void dragFrame( JComponent f, int newX, int newY)
-
Call setBoundsForFrame( ) with the given location
and current dimensions.
- public void iconifyFrame( JInternalFrame f )
-
Remove the given frame from its parent and add the
frame's desktop icon. Before adding the icon, it
checks to see if it has ever been iconified. If not, it calls
getBoundsForIconOf( ) to set the
icon's bounds. This is done only once for a given
frame, ensuring that each time a frame is iconified, it returns to
the same location on the desktop.
- public void maximizeFrame( JInternalFrame f )
-
Maximize the given frame so that it fills its parent. This method
also saves the frame's previous bounds for use in
minimizeFrame( ). Once the frame has been
maximized, it is also selected. This method can be called on an
iconified frame, causing it to be deiconified and maximized.
- public void minimizeFrame( JInternalFrame f )
-
Set the frame's bounds to its previous bounds. If
there are no previous bounds (previous bounds are set by calling
maximizeFrame( )), the frame is not resized.
- public void openFrame( JInternalFrame f )
-
Get the desktop icon for the given frame. If the
icon's parent is non-null, the
icon is removed from the parent, and the frame is added. If its
parent is null, this method does nothing.
- public void resizeFrame( JComponent f, int newX, int newY, int newWidth, int newHeight)
-
Call setBoundsForFrame( ) with the given location
and dimensions.
- public void setBoundsForFrame( JComponent f, int newX, int newY, int newWidth, int newHeight)
-
Move and resize the given frame (using setBounds( )) and validate the frame if the size was actually changed.
- public void beginDraggingFrame( JComponent f )
- public void beginResizingFrame( JComponent f, int direction)
- public void endDraggingFrame( JComponent f )
- public void endResizingFrame( JComponent f )
-
Provide support for faster dragging if the desktop's
dragMode requires it.
- public void deactivateFrame( JInternalFrame f )
-
If the frame was selected, deselect it.
9.4.2.2 Protected methods
This default implementation provides several convenience methods,
which it uses in the methods described above. The methods relate to
desktop icon management and the management of
a frame's previous size (when maximized). If you
subclass DefaultDesktopManager, these methods will
probably be of use to you.
The frame's previous bounds and an indication of
whether it has ever been iconified are stored in client properties on
the frame itself. The property names used are
previousBounds (which holds a
Rectangle) and wasIconOnce
(which holds a Boolean).
- protected Rectangle getBoundsForIconOf( JInternalFrame f )
-
Get the bounds for the given frame's icon. The width
and height are taken directly from the size of the icon. The icon is
placed in the lower-left corner of the desktop. If an icon has
already been placed in this corner, the icon is placed directly to
the right, continuing until an unclaimed position along the bottom of
the frame is found. If there is no space along the bottom, a new row
of icons is started directly above the first row. Once a frame has
been iconified, its icon's location is set, and the
icon always returns to the same spot (unless it is moved by the
user).
- protected Rectangle getPreviousBounds( JInternalFrame f )
-
Return the frame's previous bounds (set when the
frame is maximized). These bounds are retrieved from the
frame's previousBounds client
property.
- protected void removeIconFor( JInternalFrame f )
-
Remove the given frame's icon from its parent and
repaint the region under the icon.
- protected void setPreviousBounds( JInternalFrame f, Rectangle r)
-
Save the previous bounds of a frame. This is done by saving the
frame's previous bounds in the frame itself, using
the client property, previousBounds. This is
generally called by maximizeFrame( ), with the
data being used in a subsequent minimizeFrame( )
call.
- protected void setWasIcon( JInternalFrame f, Boolean value)
-
Called by iconifyFrame( ) to indicate whether the
frame has, at some time, been iconified. This is done by saving the
boolean value in the frame itself, using the
client property wasIconOnce. This is used to
determine whether the icon's bounds have been
defined.
- protected boolean wasIcon( JInternalFrame f )
-
Determine whether a frame has ever been iconified (if it has, bounds
are already defined for the icon). This is done by returning the
wasIconOnce client property on the frame.
|