Package net.hironico.common.swing
Class ComponentMover
java.lang.Object
java.awt.event.MouseAdapter
net.hironico.common.swing.ComponentMover
- All Implemented Interfaces:
MouseListener,MouseMotionListener,MouseWheelListener,EventListener
This class allows you to move a Component by using a mouse. The Component
moved can be a high level Window (ie. Window, Frame, Dialog) in which case
the Window is moved within the desktop. Or the Component can belong to a
Container in which case the Component is moved within the Container.
When moving a Window, the listener can be added to a child Component of
the Window. In this case attempting to move the child will result in the
Window moving. For example, you might create a custom "Title Bar" for an
undecorated Window and moving of the Window is accomplished by moving the
title bar only. Multiple components can be registered as "window movers".
Components can be registered when the class is created. Additional
components can be added at any time using the registerComponent() method.
Source:
...
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor for moving individual components.ComponentMover(Component destinationComponent, Component... components) Constructor to specify a parent component that will be moved when drag events are generated on a registered child component.ComponentMover(Class destinationClass, Component... components) Constructor to specify a Class of Component that will be moved when drag events are generated on a registered child component. -
Method Summary
Modifier and TypeMethodDescriptionvoidderegisterComponent(Component... components) Remove listeners from the specified componentGet the drag insetsGet the bounds insetsGet the snap sizebooleanGet the auto layout propertybooleanGet the change cursor propertyvoidMove the component to its new location.voidSetup the variables used to control the moving of the component: source - the source component of the mouse event destination - the component that will ultimately be moved pressed - the Point where the mouse was pressed in the destination component coordinates.voidRestore the original state of the ComponentvoidregisterComponent(Component... components) Add the required listeners to the specified componentvoidsetAutoLayout(boolean autoLayout) Set the auto layout propertyvoidsetChangeCursor(boolean changeCursor) Set the change cursor propertyvoidsetDragInsets(Insets dragInsets) Set the drag insets.voidsetEdgeInsets(Insets edgeInsets) Set the edge insets.voidsetSnapSize(Dimension snapSize) Set the snap size.Methods inherited from class java.awt.event.MouseAdapter
mouseClicked, mouseEntered, mouseExited, mouseMoved, mouseWheelMoved
-
Constructor Details
-
ComponentMover
public ComponentMover()Constructor for moving individual components. The components must be regisetered using the registerComponent() method. -
ComponentMover
Constructor to specify a Class of Component that will be moved when drag events are generated on a registered child component. The events will be passed to the first ancestor of this specified class.- Parameters:
destinationClass- the Class of the ancestor componentcomponents- the Components to be registered for forwarding drag events to the ancestor Component.
-
ComponentMover
Constructor to specify a parent component that will be moved when drag events are generated on a registered child component.- Parameters:
destinationComponent- the component drage events should be forwareded tocomponents- the Components to be registered for forwarding drag events to the parent component to be moved
-
-
Method Details
-
isAutoLayout
public boolean isAutoLayout()Get the auto layout property- Returns:
- the auto layout property
-
setAutoLayout
public void setAutoLayout(boolean autoLayout) Set the auto layout property- Parameters:
autoLayout- when true layout will be invoked on the parent container
-
isChangeCursor
public boolean isChangeCursor()Get the change cursor property- Returns:
- the change cursor property
-
setChangeCursor
public void setChangeCursor(boolean changeCursor) Set the change cursor property- Parameters:
changeCursor- when true the cursor will be changed to the Cursor.MOVE_CURSOR while the mouse is pressed
-
getDragInsets
Get the drag insets- Returns:
- the drag insets
-
setDragInsets
Set the drag insets. The insets specify an area where mouseDragged events should be ignored and therefore the component will not be moved. This will prevent these events from being confused with a MouseMotionListener that supports component resizing.- Parameters:
dragInsets-
-
getEdgeInsets
Get the bounds insets- Returns:
- the bounds insets
-
setEdgeInsets
Set the edge insets. The insets specify how close to each edge of the parent component that the child component can be moved. Positive values means the component must be contained within the parent. Negative values means the component can be moved outside the parent.- Parameters:
edgeInsets-
-
deregisterComponent
Remove listeners from the specified component- Parameters:
components- the component the listeners are removed from
-
registerComponent
Add the required listeners to the specified component- Parameters:
components- the component the listeners are added to
-
getSnapSize
Get the snap size- Returns:
- the snap size
-
setSnapSize
Set the snap size. Forces the component to be snapped to the closest grid position. Snapping will occur when the mouse is dragged half way. -
mousePressed
Setup the variables used to control the moving of the component: source - the source component of the mouse event destination - the component that will ultimately be moved pressed - the Point where the mouse was pressed in the destination component coordinates.- Specified by:
mousePressedin interfaceMouseListener- Overrides:
mousePressedin classMouseAdapter
-
mouseDragged
Move the component to its new location. The dragged Point must be in the destination coordinates.- Specified by:
mouseDraggedin interfaceMouseMotionListener- Overrides:
mouseDraggedin classMouseAdapter
-
mouseReleased
Restore the original state of the Component- Specified by:
mouseReleasedin interfaceMouseListener- Overrides:
mouseReleasedin classMouseAdapter
-