Java Drawing Test Codes Component library - Drag and Drop
2012.09.16

Language

Topics

Mouse hit test for a closed shape
2010.08.26


Mouse hit test for an unclosed shape
2010.08.26


Intersection between two arbitrary curves
2010.09.01


Normal lines to a arbitrary curve
2010.09.01


Common normal lines between two shapes
2016.10.20


Text Editor using the Input Method Framework
2010.12.01


Component Library
2012.09.16


Costom Color Chooser
2012.9.23



1. Source code download

=> ComponentLib.zip

2. Test items
(1) Moves a component to the desiered location on the dialog by "Drag and Drop" operation (Figure 1).
(2) Copies a component to a desired location on the canvas by "Drag and Drop" operation (Figure 2).
Note: The "add component" and "del component" buttons are disabled in this test code.

 

(a) Drag the component to the left-top position. (b)The component is dropped at the left-top position
Figure1 Move a component in the "Component lib" panel

  

 

Figure 2 Move a component to the canvas (Draw panel)
by "Drag and Drop" operation.


3. Test codes

Component

Description

DrawMain

The main class.
・Creates the components to be displayed on the ComponentLibPanel. A rectangle and an ellipse are available in this test code.
・Shows the dialog by the showDialog method of the ComponentLibDialog.

ObjectTable The table with static fields and methods to register important objects.
For example, the ComponentLibDialog object is given by ObjectTable.getComponentLibDialog().
ComponentLibDialog

Creates and shows the dialog.
・The "add component" and the "del Component" buttons don't work, because their action processes are not implemented.

・showDialog

The components' data are given as follow.
ShapeContainer[] containers=this.getContainerManager().getContainers();

ComponentLibPanel

This code can be used in an application without any changes.
・This class is a panel to display components.
・The component locations can be rearranged by "Drag and Drop" operation.
・The component can be copied on the DrawPanel by "Drag and Drop" operation.

ComponentManager The code for test use.
・The writeComponentList and ReadComponentList methods for file i/o are dummy.
・The moveComponent method is available.
ContainerManager The code for test use.
・Stores component data to a ArrayList.
DrawPanel The code for test use.
・The drawing panel (canvas).
・The DropTarget of "Drag and Drop" operation.
TransferableComponent Stores a component data to be transferred by "Drag and Drop" operation.
ShapeContainer The code for test use.
Stores a shape data of a component and has the minimum functions to be used in this test. The following functions are available.
・Draws shapes on the DrawPanel.
・Creates a clone of this object.
・Translates this object to be placed at a dropped point.
・Sets an id of this object.
MultiLineToolTip The tool tip to display multiple lines of text.
=>http://www.java2s.com/Code/Java/Swing-JFC/MultiLineToolTipExample.htm


3. Drag and Drop operations

3.1 Moves a component on the dialog (Figure 1)

・Related class:ComponentLibPanel

"Drag and Drop operation" is performed on the "Component Lib" panel, therefor only the "ComponentLibPanel class is related.

Class

Description

DragSource

Specifies the ComponentLibPanel as a DragSource.
The declaration is set in the ComponentLibPanel constructor as follow.

DragSource dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, this);

DropTarget

Specifies the ComponentLibPanel as a DropTarget.
The declaration is set in the ComponentLibPanel constructor as follow.

DropTarget dropTarget = new DropTarget(this, this);

DropSouceListener Not implemented.
DragGestureListener

This listener is implemented in the ComponentLibPanel.
(1) dragGestureRecognized method

Sets a component shape data to be transfered to a TransferableComponent, and calls the startDrag method of the DragGestureEventのstartDrag.

TransferableComponent transfer=new TransferableComponent((ShapeContainer)container.clone());
e.startDrag(DragSource.DefaultCopyDrop, transfer);

DropTargetListener

This listener is implemented in the ComponentLibPanel.
(1) dragOver method

Calls the mouseDragged method of the MouseListener (see the next section).

(2)drop method

Checks if the drop position is on the ComponentLibPanel. If it is true, calls the mouseReleased method of the MouseListener (see the next section).

MouseListener, MouseMotionListener

This listener is implemented in the ComponentLibPanel.

It is possible to move a component on the dialog using these two listeners. However, implementing the DragGestureListener to the ComponentLibPanel for the following 3.2 processing prevents that the mouseDragged and mouseReleased methods work collectly (the mousePressed method is OK).

Then, the dragOver method is substituted for the mouseDragged method and the dragOver method calls the mouseDragged method.

Moreover, the drop method of the DragTargetListener is substituted for the mouseReleased method, and the drop method calls the mouseReleased method.

The mousePressed method is used for processing to select a component to be moved and to display a black border at the cell boundary in which the selected component is displayed.



3.2 Copies a component to the canvas (Figure 2)

・Related classes: ComponentLibPanel, DrawPanel class

The "Drag and Drop" start position is on the ComponentLibPanel, and the "Drag and Drop" end position is on the DrawPanel, therefor the ComponentLibPanel and DrawPanel classes are related.

 

Class

Description

DragSource

Specifies the ComponentLibPanel as a DragSource.
The declaration is set in the ComponentLibPanel constructor as follows (same as the 3.1).

DragSource dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, this);

DropTarget

Specifies the DrawPanel as a DropTarget.
The declaration is set in theDrawPanel constructor as follows.

DropTarget dropTarget = new DropTarget(this, this);
this.setDropTarget(dropTarget);

DropSouceListener Not implemented.
DragGestureListener

This listener is implemented in the ComponentLibPanel.
(1) dragGestureRecognized method (same as the 3.1)

Sets a component shape data to be transfered to a TransferableComponent, and calls the startDrag method of the DragGestureEventのstartDrag.

TransferableComponent transfer=new TransferableComponent((ShapeContainer)container.clone());
e.startDrag(DragSource.DefaultCopyDrop, transfer);

DropTargetListener

This listener is implemented in the DrawPanel
(1) dragOver method

Nothis is done.

(2)drop method

Checks if the drop position is on the ComponentLibPanel. If it is true, nothing is done.

If it is false that means the drop position is on the DrawPanel, then gets the component data from the TransferableComponent, translates the component to the drop position, and finally store it to the ContainerManager. After storing to the ContainerManager, the component is displayed on the DrawPanel by calling its repaint method.

MouseListener, MouseMotionListener

Nothing is done.


Copyright (c) 2009-2013
All other trademarks are property of their respective owners.