1 19 package org.openharmonise.workfloweditor.flowchart; 20 21 import java.awt.*; 22 import java.awt.geom.*; 23 import java.util.*; 24 import java.util.List ; 25 26 import javax.swing.*; 27 28 import org.openharmonise.workfloweditor.*; 29 import org.openharmonise.workfloweditor.flowchart.shapes.*; 30 import org.openharmonise.workfloweditor.model.*; 31 import org.openharmonise.workfloweditor.vfs.*; 32 33 34 41 public class FlowChart extends JComponent { 42 43 46 private ArrayList m_shapes = new ArrayList(); 47 48 51 private WorkflowModel m_model = null; 52 53 56 private ArrayList m_stages = new ArrayList(); 57 58 61 private HashMap m_lines = new HashMap(); 62 63 66 private ArrayList m_tempShapes = new ArrayList(); 67 68 71 public FlowChart() { 72 super(); 73 this.setup(); 74 } 75 76 80 private void setup() { 81 MoveMouseHandler moveHandler = new MoveMouseHandler(this); 82 this.addMouseListener(moveHandler); 83 this.addMouseMotionListener(moveHandler); 84 85 ConnectMouseHandler connectHandler = new ConnectMouseHandler(this); 86 this.addMouseListener(connectHandler); 87 this.addMouseMotionListener(connectHandler); 88 89 LineSelectionMouseHandler lineHandler = new LineSelectionMouseHandler(this); 90 this.addMouseListener(lineHandler); 91 this.addMouseMotionListener(lineHandler); 92 93 AttributeMouseHandler attHandler = new AttributeMouseHandler(this); 94 this.addMouseListener(attHandler); 95 this.addMouseMotionListener(attHandler); 96 this.repaint(); 97 } 98 99 100 103 public void paint(Graphics g) { 104 Graphics2D g2D = (Graphics2D) g; 105 g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 106 g2D.setPaint( new Color(173, 169, 143) ); 107 g2D.fillRect(0, 0, this.getWidth(), this.getHeight()); 108 109 for(int i=this.m_stages.size()-1; i>=0; i--) { 110 StageShape stage = (StageShape) this.m_stages.get(i); 111 stage.draw(g2D); 112 if(this.m_lines.get(stage)!=null) { 113 ArrayList lines = ((ArrayList)this.m_lines.get(stage)); 114 for(int j=lines.size()-1; j>=0; j--) { 115 ConnectionLine line = (ConnectionLine) lines.get(j); 116 line.draw(g2D); 117 } 118 } 119 } 120 Iterator itor = this.m_tempShapes.iterator(); 121 while (itor.hasNext()) { 122 AbstractWorkflowShape shape = (AbstractWorkflowShape) itor.next(); 123 shape.draw(g2D); 124 } 125 } 126 127 132 public void addTempShape(AbstractWorkflowShape shape) { 133 this.m_tempShapes.add(shape); 134 } 135 136 141 public void removeTempShape(AbstractWorkflowShape shape) { 142 this.m_tempShapes.remove(shape); 143 } 144 145 150 public List getShapes() { 151 return (List ) this.m_shapes.clone(); 152 } 153 154 159 public void addShape(AbstractWorkflowShape shape) { 160 this.m_shapes.add(shape); 161 this.indexShapes(); 162 } 163 164 169 public void removeShape(AbstractWorkflowShape shape) { 170 if(shape instanceof StageShape) { 171 StageShape stageShape = (StageShape) shape; 172 Iterator itor = this.getShapes().iterator(); 173 while (itor.hasNext()) { 174 AbstractWorkflowShape shape2 = (AbstractWorkflowShape) itor.next(); 175 if(shape2 instanceof ConnectionLine) { 176 ConnectionLine line = (ConnectionLine) shape2; 177 if(line.getStartShape()==stageShape) { 178 this.removeShape(line); 179 } 180 if(line.getEndShape()==stageShape) { 181 this.removeShape(line); 182 } 183 } 184 } 185 itor = stageShape.getDependancies().iterator(); 186 while (itor.hasNext()) { 187 StageShape dependancy = (StageShape) itor.next(); 188 stageShape.removeDependancy(dependancy); 189 } 190 191 } else if(shape instanceof ConnectionLine) { 192 ConnectionLine line = (ConnectionLine) shape; 193 if(line.getEndShape()!=null && line.getStartShape()!=null) { 194 line.getEndShape().removeDependancy(line.getStartShape()); 195 } 196 } 197 this.m_shapes.remove(shape); 198 this.indexShapes(); 199 } 200 201 206 public WorkflowModel getModel() { 207 return this.m_model; 208 } 209 210 215 public void setModel(WorkflowModel model) { 216 this.m_model = model; 217 218 int nYpos = 10; 219 220 Iterator itor = model.getWorkflowStages().iterator(); 221 while (itor.hasNext()) { 222 VFSWorkflowStage stage = (VFSWorkflowStage) itor.next(); 223 String sWorkflowPath = ((VFSWorkflowModel)model).getStagesCollection().getFullPath(); 224 String sStagePath = stage.getPath(); 225 226 int nXtemp = 10; 227 int nYtemp = nYpos; 228 229 Point2D.Float point = WorkflowLayoutStore.getInstance().getStageLayout(sWorkflowPath, sStagePath); 230 if(point!=null) { 231 nXtemp = new Float (point.x).intValue(); 232 nYtemp = new Float (point.y).intValue(); 233 } 234 235 StageShape stageShape = new StageShape(nXtemp,nYtemp, stage); 236 this.m_shapes.add(stageShape); 237 238 nYpos = nYpos+120; 239 } 240 241 itor = model.getWorkflowStages().iterator(); 242 while (itor.hasNext()) { 243 WorkflowStage stage = (WorkflowStage) itor.next(); 244 Iterator dependItor = stage.getDependancies().iterator(); 245 while (dependItor.hasNext()) { 246 WorkflowStage dependStage = (WorkflowStage) dependItor.next(); 247 248 StageShape startShape = null; 249 StageShape endShape = null; 250 Iterator itor2 = this.m_shapes.iterator(); 251 while (itor2.hasNext()) { 252 AbstractWorkflowShape shape = (AbstractWorkflowShape) itor2.next(); 253 if(shape instanceof StageShape && ((StageShape)shape).getStage()==dependStage) { 254 startShape = (StageShape) shape; 255 } else if(shape instanceof StageShape && ((StageShape)shape).getStage()==stage) { 256 endShape = (StageShape) shape; 257 } 258 } 259 if(startShape!=null && endShape!=null) { 260 ConnectionLine connection = new ConnectionLine(10,10); 261 connection.setStartShape(startShape); 262 connection.setEndShape(endShape); 263 this.m_shapes.add(connection); 264 } 265 } 266 } 267 this.indexShapes(); 268 } 269 270 275 private void indexShapes() { 276 this.m_stages.clear(); 277 this.m_lines.clear(); 278 279 Iterator itor = this.m_shapes.iterator(); 280 while (itor.hasNext()) { 281 AbstractWorkflowShape shape = (AbstractWorkflowShape) itor.next(); 282 if(shape instanceof StageShape) { 283 if(!this.m_stages.contains(shape)) { 284 this.m_stages.add(shape); 285 } 286 Iterator itor2 = ((StageShape)shape).getDependancies().iterator(); 287 while (itor2.hasNext()) { 288 StageShape dependStage = (StageShape) itor2.next(); 289 if(!this.m_stages.contains(dependStage)) { 290 this.m_stages.add(0, dependStage); 291 } else if(this.m_stages.indexOf(dependStage)>this.m_stages.indexOf(shape)) { 292 this.m_stages.remove(dependStage); 293 this.m_stages.add(this.m_stages.indexOf(shape), dependStage); 294 } 295 } 296 } else if(shape instanceof ConnectionLine) { 297 StageShape endStage = ((ConnectionLine)shape).getEndShape(); 298 if(!this.m_lines.keySet().contains(endStage)) { 299 ArrayList lines = new ArrayList(); 300 lines.add(shape); 301 this.m_lines.put(endStage, lines); 302 } else { 303 ArrayList lines = (ArrayList) this.m_lines.get(endStage); 304 lines.add(shape); 305 } 306 } 307 } 308 309 this.m_shapes.clear(); 310 for(int i=this.m_stages.size()-1; i>=0; i--) { 311 StageShape stage = (StageShape) this.m_stages.get(i); 312 this.m_shapes.add(0,stage); 313 if(this.m_lines.get(stage)!=null) { 314 ArrayList lines = ((ArrayList)this.m_lines.get(stage)); 315 for(int j=lines.size()-1; j>=0; j--) { 316 ConnectionLine line = (ConnectionLine) lines.get(j); 317 this.m_shapes.add(0,line); 318 } 319 } 320 } 321 322 this.paintImmediately(this.getBounds()); 323 } 324 325 } 326 | Popular Tags |