1 10 11 package org.enhydra.jawe.graph; 12 13 import org.enhydra.jawe.*; 14 15 import org.jgraph.graph.*; 16 import org.jgraph.JGraph; 17 18 import java.util.*; 19 import java.awt.*; 20 import java.awt.geom.*; 21 import java.awt.event.*; 22 import javax.swing.undo.UndoableEdit ; 23 24 27 public class TransitionView extends EdgeView { 28 29 30 35 public TransitionView(Object cell, JGraph graph, CellMapper mapper) { 36 super(cell,graph,mapper); 37 renderer=new TransitionRenderer(); 38 45 } 46 47 50 public CellHandle getHandle(GraphContext context) { 51 return new TransitionHandle(this, context); 52 } 53 54 58 public void addPoint (Point popupPoint) { 59 boolean bendable = graph.isBendable() && 60 GraphConstants.isBendable(getAttributes()); 61 if (bendable) { 62 int index=-1; 63 int s = graph.getHandleSize(); 64 Rectangle rect = new Rectangle(popupPoint.x-s,popupPoint.y-s,2*s,2*s); 66 if (intersects(graph.getGraphics(),rect)) { 67 Point point = (Point)graph.snap(new Point(popupPoint)); double min = Double.MAX_VALUE, dist = 0; 69 for (int i = 0; i < getPointCount()-1; i++) { 70 Point p = new Point((int)getPoint(i).getX(),(int)getPoint(i).getY()); Point p1 = new Point((int)getPoint(i+1).getX(),(int)getPoint(i+1).getY()); dist = new Line2D.Double(p, p1).ptLineDistSq(point); 73 if (dist < min) { 74 min = dist; 75 index = i+1; 76 } 77 } 78 if (index != -1) { 79 addPoint(index,point); 80 Map propertyMap = new HashMap(); 81 Map edgeMap=GraphConstants.cloneMap(((GraphCell)cell).getAttributes()); 82 GraphConstants.setPoints(edgeMap,points); 83 propertyMap.put(cell,edgeMap); 84 String undoMsg=ResourceManager.getLanguageDependentString("MessageAddingBreakPointAtTransition"); 85 ((JaWEGraphModel)getModel()).insertAndEdit(null,propertyMap,null,null,null,undoMsg); 86 } 87 } 88 } 89 } 90 91 94 public void addPointProgramatically (Point popupPoint,int index) { 95 if (popupPoint==null) return; 96 int s = graph.getHandleSize(); 97 if (index<1) index=1; 98 if (index>getPointCount()-1) index=getPointCount()-1; 99 try { 100 addPoint(index,new Point(popupPoint)); 101 Map propertyMap = new HashMap(); 102 Map edgeMap=GraphConstants.cloneMap(((GraphCell)cell).getAttributes()); 103 GraphConstants.setPoints(edgeMap,points); 104 propertyMap.put(cell,edgeMap); 105 String undoMsg=ResourceManager.getLanguageDependentString("MessageAddingBreakPointAtTransition"); 106 ((JaWEGraphModel)getModel()).insertAndEdit(null,propertyMap,null,null,null,undoMsg); 107 } catch (Exception ex) {} 108 109 } 110 111 112 116 public void removePoint (Point popupPoint) { 117 boolean bendable = graph.isBendable() && 118 GraphConstants.isBendable(getAttributes()); 119 if (bendable) { 120 int index=-1; 121 int s = graph.getHandleSize(); 122 Rectangle rect = new Rectangle(popupPoint.x-s,popupPoint.y-s,2*s,2*s); 124 if (intersects(graph.getGraphics(),rect)) { 125 Point point = (Point)graph.snap(new Point(popupPoint)); double min = Double.MAX_VALUE, dist = 0; 127 for (int i = 0; i < getPointCount(); i++) { 128 Point p = new Point((int)getPoint(i).getX(),(int)getPoint(i).getY()); dist = Math.sqrt(((Point2D)point).distanceSq(p)); 130 if (dist < min) { 131 min = dist; 132 index = i; 133 } 134 } 135 if (index != -1 && min<=s+2 && index!=0 && index!=getPointCount()-1) { 136 removePoint(index); 137 Map propertyMap = new HashMap(); 138 Map edgeMap=GraphConstants.cloneMap(((GraphCell)cell).getAttributes()); 139 GraphConstants.setPoints(edgeMap,points); 140 propertyMap.put(cell,edgeMap); 141 String undoMsg=ResourceManager.getLanguageDependentString("MessageRemovingBreakPointFromTransition"); 142 ((JaWEGraphModel)getModel()).insertAndEdit(null,propertyMap,null,null,null,undoMsg); 143 } 144 } 145 } 146 } 147 148 152 public boolean isAddPointEvent(MouseEvent event) { 153 return false; 154 } 155 156 160 public boolean isRemovePointEvent(MouseEvent event) { 161 return false; 162 } 163 164 public static class TransitionHandle extends EdgeHandle { 165 166 public TransitionHandle(EdgeView edge, GraphContext ctx) { 167 super(edge,ctx); 168 } 169 170 public void mouseReleased(MouseEvent e) { 171 try { 172 if (source || target) { 173 Transition tr=(Transition)edge.getCell(); 174 PortView ss=(PortView)graph.getGraphLayoutCache().getMapping(tr.getSource(),false); 175 Object tt=(PortView)graph.getGraphLayoutCache().getMapping(tr.getTarget(),false); 176 PortView pvs=(PortView)edge.getSource(); 177 PortView pvt=(PortView)edge.getTarget(); 178 Activity s1=tr.getSourceActivity(); 179 Activity t1=tr.getTargetActivity(); 180 Activity s2=null; 181 try { 182 s2=(Activity)((DefaultPort)pvs.getCell()).getParent(); 183 } catch (Exception ex) { 184 } 185 Activity t2=null; 186 try { 187 t2=(Activity)((DefaultPort)pvt.getCell()).getParent(); 188 } catch (Exception ex) { 189 } 190 191 if (s2==null || t2==null) { 192 clean(tr); 193 return; 194 } 195 202 203 if (ss!=pvs || tt!=pvt) { 204 JaWEMarqueeHandler jmh=(JaWEMarqueeHandler)graph.getMarqueeHandler(); 205 org.enhydra.jawe.xml.elements.Transition uo= 206 (org.enhydra.jawe.xml.elements.Transition)tr.getPropertyObject(); 207 if (ss!=pvs) { 208 boolean accept= 209 !(s1 instanceof Start) && 210 !(s2 instanceof Start) && 211 !(t1 instanceof End) && 212 jmh.acceptsSourceOrTarget(pvs,true,pvt,false); 213 System.out.println("accept-"+accept); 214 if (!accept) { 215 clean(tr); 216 return; 217 } 218 setChanges(); 219 220 uo.setFrom(s2.getPropertyObject()); 222 ((AbstractGraph)graph).getWorkflowManager().updateSplit(s2); 223 ((AbstractGraph)graph).getWorkflowManager().updateSplit(s1); 224 } else { 225 boolean accept= 226 !(t1 instanceof End) && 227 !(t2 instanceof End) && 228 !(s1 instanceof Start) && 229 jmh.acceptsSourceOrTarget(pvt,false,pvs,false); 230 if (!accept) { 231 clean(tr); 232 return; 233 } 234 setChanges(); 235 236 uo.setTo(t2.getPropertyObject()); 238 ((AbstractGraph)graph).getWorkflowManager().updateJoin(t2); 239 ((AbstractGraph)graph).getWorkflowManager().updateJoin(t1); 240 } 241 return; 242 } 243 } 244 setChanges(); 245 } finally { 246 e.consume(); 247 } 248 } 249 250 private void setChanges () { 251 ConnectionSet cs = createConnectionSet(edge, edge.getCell(), false); 252 Map nested = 253 GraphConstants.createAttributes(new CellView[] { edge }, null); 254 graph.getGraphLayoutCache().edit(nested, cs, null, null); 255 } 256 257 private void clean (Transition tr) { 258 Graphics g = graph.getGraphics(); 259 overlay(g); 260 firstOverlayCall=true; 261 graph.removeSelectionCell(tr); 262 return; 263 } 264 265 } 266 267 } 268 269 270 | Popular Tags |