KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > WorkflowMarqueeHandler


1 package com.opensymphony.workflow.designer;
2
3 import java.awt.*;
4 import java.awt.geom.Rectangle2D JavaDoc;
5 import java.awt.geom.Point2D JavaDoc;
6 import java.awt.event.MouseEvent JavaDoc;
7
8 import javax.swing.*;
9
10 import org.jgraph.graph.BasicMarqueeHandler;
11 import org.jgraph.graph.PortView;
12 import org.jgraph.graph.GraphConstants;
13
14 /**
15  * @author Hani Suleiman (hani@formicary.net)
16  * Date: Oct 25 2003
17  * Time: 6:56:10 PM
18  */

19 public class WorkflowMarqueeHandler extends BasicMarqueeHandler
20 {
21   // Holds the Start and the Current Point
22
protected Point2D JavaDoc start, current;
23
24   // Holds the First and the Current Port
25
protected PortView port, firstPort;
26   private WorkflowGraph graph;
27
28   //
29
protected boolean readyToConnect = false;
30
31   public WorkflowMarqueeHandler(WorkflowGraph graph)
32   {
33     this.graph = graph;
34   }
35
36   // Override to Gain Control (for PopupMenu and ConnectMode)
37
public boolean isForceMarqueeEvent(MouseEvent JavaDoc e)
38   {
39     // If Right Mouse Button we want to Display the PopupMenu
40
if(SwingUtilities.isRightMouseButton(e) || e.isPopupTrigger())
41     // Return Immediately
42
return true;
43     // Find and Remember Port
44
port = getSourcePortAt(e.getPoint());
45     // If Port Found and in ConnectMode (=Ports Visible)
46
if(port != null && graph.isPortsVisible())
47       return true;
48     // Else Call Superclass
49
return super.isForceMarqueeEvent(e);
50   }
51
52   // Use Xor-Mode on Graphics to Paint Connector
53
protected void paintConnector(Color fg, Color bg, Graphics g)
54   {
55     // Set Foreground
56
g.setColor(fg);
57     // Set Xor-Mode Color
58
g.setXORMode(bg);
59     // Highlight the Current Port
60
paintPort(graph.getGraphics());
61     // If Valid First Port, Start and Current Point
62
if(firstPort != null && start != null && current != null)
63     // Then Draw A Line From Start to Current Point
64
g.drawLine((int)start.getX(), (int)start.getY(), (int)current.getX(), (int)current.getY());
65   }
66
67   // Use the Preview Flag to Draw a Highlighted Port
68
protected void paintPort(Graphics g)
69   {
70     // If Current Port is Valid
71
if(port != null)
72     {
73       // If Not Floating Port...
74
boolean isFloating = (GraphConstants.getOffset(port.getAttributes()) != null);
75       // ...Then use Parent's Bounds
76
Rectangle2D JavaDoc r = isFloating ? port.getBounds() : port.getParentView().getBounds();
77       // Scale from Model to Screen
78
r = graph.toScreen(r);
79       // Add Space For the Highlight Border
80
Rectangle2D JavaDoc growRect = new Rectangle2D.Double JavaDoc(r.getX() - 3, r.getY() - 3, r.getWidth() + 6, r.getHeight() + 6);
81       //r.setFrame(r.getX() - 3, r.getY() - 3, r.getWidth() + 6, r.getHeight() + 6);
82
// Paint Port in Preview (=Highlight) Mode
83
graph.getUI().paintCell(g, port, growRect, true);
84     }
85   }
86
87   public PortView getSourcePortAt(Point point)
88   {
89     // Scale from Screen to Model
90
Point2D JavaDoc tmp = graph.fromScreen(new Point(point));
91     // Find a Port View in Model Coordinates and Remember
92
return graph.getPortViewAt(tmp.getX(), tmp.getY());
93   }
94
95   // Find a Cell at point and Return its first Port as a PortView
96
protected PortView getTargetPortAt(Point point)
97   {
98     // Find Cell at point (No scaling needed here)
99
Object JavaDoc cell = graph.getFirstCellForLocation(point.x, point.y);
100     // Loop Children to find PortView
101
for(int i = 0; i < graph.getModel().getChildCount(cell); i++)
102     {
103       // Get Child from Model
104
Object JavaDoc tmp = graph.getModel().getChild(cell, i);
105       // Get View for Child using the Graph's View as a Cell Mapper
106
tmp = graph.getGraphLayoutCache().getMapping(tmp, false);
107       // If Child View is a Port View and not equal to First Port
108
if(tmp instanceof PortView && tmp != firstPort)
109       // Return as PortView
110
return (PortView)tmp;
111     }
112     // No Port View found
113
return getSourcePortAt(point);
114   }
115
116   // Display PopupMenu or Remember Start Location and First Port
117
public void mousePressed(final MouseEvent JavaDoc e)
118   {
119     // If Right Mouse Button
120
if(SwingUtilities.isRightMouseButton(e) || e.isPopupTrigger())
121 // if(e.isPopupTrigger())
122
{
123       // Scale From Screen to Model
124
//Point loc = fromScreen(e.getPoint());
125
// Find Cell in Model Coordinates
126
Object JavaDoc cell = graph.getFirstCellForLocation(e.getX(), e.getY());
127       // Create PopupMenu for the Cell
128
if(cell == null)
129       {
130         graph.showMenu(e.getX(), e.getY());
131       }
132       else
133       {
134         if(!(cell instanceof InitialActionCell))
135         {
136           // System.out.println(cell);
137
graph.showDelete(e.getX(), e.getY());
138         }
139       }
140
141       // Else if in ConnectMode and Remembered Port is Valid
142
}
143     else if(port != null && !e.isConsumed() && graph.isPortsVisible())
144     {
145       // Remember Start Location
146
start = graph.toScreen(port.getLocation(null));
147       // Remember First Port
148
firstPort = port;
149       // Consume Event
150
e.consume();
151       readyToConnect = false;
152     }
153     else
154     // Call Superclass
155
super.mousePressed(e);
156   }
157
158   // Find Port under Mouse and Repaint Connector
159
public void mouseDragged(MouseEvent JavaDoc e)
160   {
161     // If remembered Start Point is Valid
162
if(start != null && !e.isConsumed())
163     {
164       // ready to connect
165
readyToConnect = true;
166
167       // Fetch Graphics from Graph
168
Graphics g = graph.getGraphics();
169       // Xor-Paint the old Connector (Hide old Connector)
170
paintConnector(Color.black, graph.getBackground(), g);
171       // Reset Remembered Port
172
PortView newPort = getTargetPortAt(e.getPoint());
173       if(port != newPort && port != null)
174       {
175         paintPort(g);
176       }
177       port = newPort;
178       // If Port was found then Point to Port Location
179
if(port != null)
180         current = graph.toScreen(port.getLocation(null));
181       // Else If no Port was found then Point to Mouse Location
182
else
183         current = graph.snap(e.getPoint());
184       // Xor-Paint the new Connector
185
paintConnector(graph.getBackground(), Color.black, g);
186       // Consume Event
187
e.consume();
188     }
189     // Call Superclass
190
super.mouseDragged(e);
191   }
192
193   // Connect the First Port and the Current Port in the Graph or Repaint
194
public void mouseReleased(MouseEvent JavaDoc e)
195   {
196     // If Valid Event, Current and First Port
197
// first connect first is OK
198
if(e != null && !e.isConsumed() && port != null && firstPort != null && readyToConnect)
199     {
200       readyToConnect = false;
201       // Then Establish Connection
202
WorkflowCell source = (WorkflowCell)((WorkflowPort)firstPort.getCell()).getParent();
203       WorkflowCell target = (WorkflowCell)((WorkflowPort)port.getCell()).getParent();
204       // System.out.println("connect " + source + "->" + target);
205
ConnectHelper.connect(source, target, (WorkflowGraphModel)graph.getModel());
206       // System.out.println("connectable "+ ConnectHelper.connect(source, target, (WorkflowGraphModel)graph.getModel()));
207

208       // connect((Port) firstPort.getCell(), (Port) port.getCell());
209
// Consume Event
210
e.consume();
211     }
212     graph.repaint();
213     // Reset Global Vars
214
firstPort = port = null;
215     start = current = null;
216     // Call Superclass
217
super.mouseReleased(e);
218   }
219
220   // Show Special Cursor if Over Port
221
public void mouseMoved(MouseEvent JavaDoc e)
222   {
223     // Check Mode and Find Port
224
if(e != null && getSourcePortAt(e.getPoint()) != null && !e.isConsumed())
225     {
226       // Set Cusor on Graph (Automatically Reset)
227
graph.setCursor(new Cursor(Cursor.HAND_CURSOR));
228       // Consume Event
229
e.consume();
230     }
231     // Call Superclass
232
super.mouseReleased(e);
233   }
234 }
235
Popular Tags