1 package com.opensymphony.workflow.designer; 2 3 import java.awt.*; 4 import java.awt.geom.Rectangle2D ; 5 import java.awt.geom.Point2D ; 6 import java.awt.event.MouseEvent ; 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 19 public class WorkflowMarqueeHandler extends BasicMarqueeHandler 20 { 21 protected Point2D start, current; 23 24 protected PortView port, firstPort; 26 private WorkflowGraph graph; 27 28 protected boolean readyToConnect = false; 30 31 public WorkflowMarqueeHandler(WorkflowGraph graph) 32 { 33 this.graph = graph; 34 } 35 36 public boolean isForceMarqueeEvent(MouseEvent e) 38 { 39 if(SwingUtilities.isRightMouseButton(e) || e.isPopupTrigger()) 41 return true; 43 port = getSourcePortAt(e.getPoint()); 45 if(port != null && graph.isPortsVisible()) 47 return true; 48 return super.isForceMarqueeEvent(e); 50 } 51 52 protected void paintConnector(Color fg, Color bg, Graphics g) 54 { 55 g.setColor(fg); 57 g.setXORMode(bg); 59 paintPort(graph.getGraphics()); 61 if(firstPort != null && start != null && current != null) 63 g.drawLine((int)start.getX(), (int)start.getY(), (int)current.getX(), (int)current.getY()); 65 } 66 67 protected void paintPort(Graphics g) 69 { 70 if(port != null) 72 { 73 boolean isFloating = (GraphConstants.getOffset(port.getAttributes()) != null); 75 Rectangle2D r = isFloating ? port.getBounds() : port.getParentView().getBounds(); 77 r = graph.toScreen(r); 79 Rectangle2D growRect = new Rectangle2D.Double (r.getX() - 3, r.getY() - 3, r.getWidth() + 6, r.getHeight() + 6); 81 graph.getUI().paintCell(g, port, growRect, true); 84 } 85 } 86 87 public PortView getSourcePortAt(Point point) 88 { 89 Point2D tmp = graph.fromScreen(new Point(point)); 91 return graph.getPortViewAt(tmp.getX(), tmp.getY()); 93 } 94 95 protected PortView getTargetPortAt(Point point) 97 { 98 Object cell = graph.getFirstCellForLocation(point.x, point.y); 100 for(int i = 0; i < graph.getModel().getChildCount(cell); i++) 102 { 103 Object tmp = graph.getModel().getChild(cell, i); 105 tmp = graph.getGraphLayoutCache().getMapping(tmp, false); 107 if(tmp instanceof PortView && tmp != firstPort) 109 return (PortView)tmp; 111 } 112 return getSourcePortAt(point); 114 } 115 116 public void mousePressed(final MouseEvent e) 118 { 119 if(SwingUtilities.isRightMouseButton(e) || e.isPopupTrigger()) 121 { 123 Object cell = graph.getFirstCellForLocation(e.getX(), e.getY()); 127 if(cell == null) 129 { 130 graph.showMenu(e.getX(), e.getY()); 131 } 132 else 133 { 134 if(!(cell instanceof InitialActionCell)) 135 { 136 graph.showDelete(e.getX(), e.getY()); 138 } 139 } 140 141 } 143 else if(port != null && !e.isConsumed() && graph.isPortsVisible()) 144 { 145 start = graph.toScreen(port.getLocation(null)); 147 firstPort = port; 149 e.consume(); 151 readyToConnect = false; 152 } 153 else 154 super.mousePressed(e); 156 } 157 158 public void mouseDragged(MouseEvent e) 160 { 161 if(start != null && !e.isConsumed()) 163 { 164 readyToConnect = true; 166 167 Graphics g = graph.getGraphics(); 169 paintConnector(Color.black, graph.getBackground(), g); 171 PortView newPort = getTargetPortAt(e.getPoint()); 173 if(port != newPort && port != null) 174 { 175 paintPort(g); 176 } 177 port = newPort; 178 if(port != null) 180 current = graph.toScreen(port.getLocation(null)); 181 else 183 current = graph.snap(e.getPoint()); 184 paintConnector(graph.getBackground(), Color.black, g); 186 e.consume(); 188 } 189 super.mouseDragged(e); 191 } 192 193 public void mouseReleased(MouseEvent e) 195 { 196 if(e != null && !e.isConsumed() && port != null && firstPort != null && readyToConnect) 199 { 200 readyToConnect = false; 201 WorkflowCell source = (WorkflowCell)((WorkflowPort)firstPort.getCell()).getParent(); 203 WorkflowCell target = (WorkflowCell)((WorkflowPort)port.getCell()).getParent(); 204 ConnectHelper.connect(source, target, (WorkflowGraphModel)graph.getModel()); 206 208 e.consume(); 211 } 212 graph.repaint(); 213 firstPort = port = null; 215 start = current = null; 216 super.mouseReleased(e); 218 } 219 220 public void mouseMoved(MouseEvent e) 222 { 223 if(e != null && getSourcePortAt(e.getPoint()) != null && !e.isConsumed()) 225 { 226 graph.setCursor(new Cursor(Cursor.HAND_CURSOR)); 228 e.consume(); 230 } 231 super.mouseReleased(e); 233 } 234 } 235 | Popular Tags |