KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > client > grapheditor > WFGraph


1 package hero.client.grapheditor;
2
3 /**
4  * WFGraph.java
5  *
6  *
7  * Created: Mon Aug 12 11:23:48 2002
8  *
9  * @version 1.0
10  */

11
12 import hero.interfaces.Constants;
13 import hero.net.ProjectSession.StrutsNodeValue;
14 import hero.net.ProjectSession.BnIterationLightValue;
15
16 import java.awt.Color JavaDoc;
17 import java.awt.Dimension JavaDoc;
18 import java.awt.Font JavaDoc;
19 import java.awt.Graphics JavaDoc;
20 import java.awt.Graphics2D JavaDoc;
21 import java.awt.Point JavaDoc;
22 import java.awt.Rectangle JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.awt.event.MouseAdapter JavaDoc;
26 import java.awt.event.MouseEvent JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Observable JavaDoc;
33 import java.util.Observer JavaDoc;
34 import java.util.Set JavaDoc;
35
36 import javax.swing.BorderFactory JavaDoc;
37 import javax.swing.JPopupMenu JavaDoc;
38 import javax.swing.JScrollPane JavaDoc;
39 import javax.swing.JOptionPane JavaDoc;
40
41 import com.jgraph.JGraph;
42 import com.jgraph.event.GraphModelEvent;
43 import com.jgraph.event.GraphModelListener;
44 import com.jgraph.event.GraphSelectionEvent;
45 import com.jgraph.event.GraphSelectionListener;
46 import com.jgraph.graph.BasicMarqueeHandler;
47 import com.jgraph.graph.CellView;
48 import com.jgraph.graph.ConnectionSet;
49 import com.jgraph.graph.DefaultEdge;
50 import com.jgraph.graph.DefaultGraphCell;
51 import com.jgraph.graph.DefaultGraphModel;
52 import com.jgraph.graph.DefaultPort;
53 import com.jgraph.graph.EdgeView;
54 import com.jgraph.graph.GraphConstants;
55 import com.jgraph.graph.GraphView;
56 import com.jgraph.graph.Port;
57 import com.jgraph.graph.PortView;
58 import com.jgraph.graph.VertexView;
59 import java.awt.Toolkit JavaDoc;
60 import java.awt.Image JavaDoc;
61 import javax.swing.ImageIcon JavaDoc;
62 import java.net.URL JavaDoc;
63
64 import java.util.Collection JavaDoc;
65
66 import com.jgraph.graph.CellMapper;
67
68 public class WFGraph extends JGraph {
69
70 static java.util.ResourceBundle JavaDoc resource = java.util.ResourceBundle.getBundle("resources.Traduction")/*#BundleType=List*/;
71
72     private DefaultGraphModel model;
73     private WFManager manager;
74     private WFPersistence persistence;
75     public Hashtable JavaDoc nodes;
76     private Hashtable JavaDoc edges;
77     private DefaultGraphCell sourceCell;
78     private boolean NEWEDGE = false;
79     private Touch layout2;
80     private boolean automaticLayout=false;
81     private LayoutThread expand;
82     private Image JavaDoc background;
83     
84     public final static String JavaDoc imageBase = "images/";
85     public final static ImageIcon JavaDoc icon = new ImageIcon JavaDoc(Thread.currentThread().getContextClassLoader().getResource(imageBase + "icon.png"));
86
87     public WFGraph ( final WFManager manager,WFPersistence persist){
88     super();
89     this.manager=manager;
90     this.persistence=persist;
91     nodes=new Hashtable JavaDoc();
92     edges=new Hashtable JavaDoc();
93     setMarqueeHandler(new WFMarqueeHandler());
94     expand= new LayoutThread(this);
95     layout2 = new Touch(this);
96     WFListener listener =new WFListener();
97     getView().addObserver(listener);
98     addGraphSelectionListener(listener);
99     getModel().addGraphModelListener(listener);
100
101     setDragEnabled(false);
102     setDropEnabled(false);
103
104     // set resizable false
105
setSizeable(false);
106     setEditable(false);
107     setConnectable(false);
108     setDisconnectable(false);
109     setBendable(false);
110     setAntiAliased(true);
111     setCloneable(false);
112
113    //setBackground(new Color(0,153,204));
114

115     setAutomaticLayout(true);
116     this.addMouseListener(new MouseAdapter JavaDoc() {
117         public void mousePressed(MouseEvent JavaDoc e){
118             if (e.getClickCount() == 1)
119             {
120              try{
121               if (!(persistence.soapclient.getProjectName()).equals(persistence.getProjectName()))
122                 persistence.openProject(persistence.getProjectName());
123              }catch(Exception JavaDoc openProject){}
124             }
125             if (e.getClickCount() == 2)
126             {
127             DefaultGraphCell myCell =(DefaultGraphCell) getFirstCellForLocation(e.getX(), e.getY());
128             if (myCell != null && isVertex(myCell))
129             {
130                 Map JavaDoc props = myCell.getAttributes();
131                 String JavaDoc subProcess = (String JavaDoc)props.get("subProcess");
132                 if (subProcess.equals("no")) {
133                    try {
134                      BrowserControl.displayURL(java.lang.System.getProperty("bonita.host")+"/bonita/protected/Action.jsp?projectname="+persistence.getProjectName()+"&nodename="+(String JavaDoc)myCell.getUserObject());
135                    }catch(Exception JavaDoc ex){}
136                 }else
137                 {
138                 try{
139                   Frame frame = new hero.client.grapheditor.Frame((String JavaDoc)props.get("name"),persistence.soapclient, true);
140                   frame.setSize(new Dimension JavaDoc(500, 400));
141                   frame.setVisible(true);
142                   frame.requestFocus();
143                 }catch(Exception JavaDoc exce){exce.printStackTrace();};
144                 }
145             }
146             if (myCell != null && isEdge(myCell))
147             {
148                 try {
149                 Map JavaDoc props = myCell.getAttributes();
150                 manager.setEdgeCondition((String JavaDoc)props.get("name"));
151                 }catch(Exception JavaDoc ex){ex.printStackTrace();}
152             }
153             }
154         }
155         });
156         
157         // load background
158
URL JavaDoc backgroundUrl = Thread.currentThread().getContextClassLoader().getResource(imageBase + "background.png");
159         this.background = Toolkit.getDefaultToolkit().getImage(backgroundUrl);
160         setUI(new BonitaGraphUI());
161
162         // background
163
setBackground(Color.decode("#F7F7F7"));
164         
165     }
166
167     public Image JavaDoc getBackgroundImage() {
168         return background;
169     }
170
171     public void setAutomaticLayout(boolean value){
172     automaticLayout=value;
173     if (value){
174         layout2.start();
175         layout2.resetDamper();
176     }else{
177         layout2.stop();
178     }
179     }
180
181     protected VertexView createVertexView(Object JavaDoc v, CellMapper cm) {
182         if (v instanceof BonitaActivityCell) {
183             return new BonitaActivityView(v, this, cm);
184         }
185         return super.createVertexView(v, cm);
186     }
187     
188     public void addNewNode(String JavaDoc name, int state, int type){
189     Map JavaDoc map;
190
191     if (!nodes.containsKey(name)){
192         Hashtable JavaDoc attributes = new Hashtable JavaDoc();
193         //DefaultGraphCell vertex = new DefaultGraphCell(name);
194
BonitaActivityCell vertex = new BonitaActivityCell(name);
195         Map JavaDoc vertexAttrib = GraphConstants.createMap();
196         Map JavaDoc props = new HashMap JavaDoc();
197         props.put("type", "node");
198         props.put("state", new Integer JavaDoc(state).toString());
199         props.put("name", name);
200         props.put("iterate", "false");
201          
202         
203         if (type != Constants.Nd.SUB_PROCESS_NODE)
204         {
205             GraphConstants.setBounds(vertexAttrib, new Rectangle JavaDoc(20, 100, 85, 27));
206             props.put("subProcess","no");
207         }
208         else
209         {
210             GraphConstants.setBounds(vertexAttrib, new Rectangle JavaDoc(20, 100, 85, 33));
211             props.put("subProcess","yes");
212         }
213         GraphConstants.setBorder(vertexAttrib, BorderFactory.createRaisedBevelBorder());
214         GraphConstants.setForeground(vertexAttrib, Color.white);
215         GraphConstants.setFontStyle(vertexAttrib, Font.BOLD);
216         GraphConstants.setFontSize(vertexAttrib,(float) 11);
217         GraphConstants.setOpaque(vertexAttrib, true);
218         
219         // GraphConstants.setAutoSize(vertexAttrib, true);
220

221         vertex.setAttributes(props);
222            
223         switch (state) {
224         case Constants.Nd.INITIAL:
225         GraphConstants.setBackground(vertexAttrib,(Color.lightGray).darker()); break;
226         case Constants.Nd.READY:
227         GraphConstants.setBackground(vertexAttrib,(Color.yellow).darker()); break;
228         case Constants.Nd.ANTICIPABLE:
229         GraphConstants.setBackground(vertexAttrib,(Color.green).darker()); break;
230         case Constants.Nd.ANTICIPATING:
231         GraphConstants.setBackground(vertexAttrib,(Color.magenta).darker()); break;
232         case Constants.Nd.EXECUTING:
233         GraphConstants.setBackground(vertexAttrib,(Color.red).darker()); break;
234         case Constants.Nd.EXECUTED:
235         GraphConstants.setBackground(vertexAttrib,(Color.orange).darker()); break;
236         case Constants.Nd.TERMINATED:
237         GraphConstants.setBackground(vertexAttrib,(Color.cyan).darker()); break;
238         case Constants.Nd.ANT_SUSPENDED:
239         GraphConstants.setBackground(vertexAttrib,(Color.blue).darker()); break;
240         case Constants.Nd.EXEC_SUSPENDED:
241         GraphConstants.setBackground(vertexAttrib,(Color.gray).darker()); break;
242         case Constants.Nd.DEAD:
243         GraphConstants.setBackground(vertexAttrib,(Color.blue).darker()); break;
244         }
245         
246         GraphConstants.setSizeable(vertexAttrib,false);
247         
248         attributes.put(vertex, vertexAttrib);
249         vertex.add(new DefaultPort());
250         nodes.put(name,vertex);
251         Object JavaDoc[] cell = new Object JavaDoc[]{vertex};
252         model.insert(cell,null,null,attributes);
253        }
254     }
255     
256     public void updateIterationNodes(String JavaDoc from, String JavaDoc to)
257     {
258         BonitaActivityCell vertexFrom = (BonitaActivityCell)nodes.get(from);
259         if(nodes.containsKey(from)){
260             Hashtable JavaDoc attributes = new Hashtable JavaDoc();
261                     
262             Map JavaDoc propsFrom = vertexFrom.getAttributes();
263             propsFrom.put("iterate","from");
264             attributes.put(nodes.get(from), propsFrom);
265             model.insert(null,null,null,propsFrom);
266             repaint();
267         }
268         BonitaActivityCell vertexTo = (BonitaActivityCell)nodes.get(to);
269         if(nodes.containsKey(to)){
270             Hashtable JavaDoc attributes = new Hashtable JavaDoc();
271                         
272             Map JavaDoc propsTo = vertexTo.getAttributes();
273             propsTo.put("iterate","to");
274             attributes.put(nodes.get(to), propsTo);
275             model.insert(null,null,null,propsTo);
276             repaint();
277         }
278     }
279
280     public void addNewEdge(DefaultGraphCell in, DefaultGraphCell out,String JavaDoc name){
281     Hashtable JavaDoc attributes = new Hashtable JavaDoc();
282     DefaultEdge e = new DefaultEdge();
283
284     Map JavaDoc edgeAttrib = GraphConstants.createMap();
285     GraphConstants.setLineBegin(edgeAttrib, GraphConstants.TECHNICAL);
286     GraphConstants.setBeginFill(edgeAttrib, true);
287     GraphConstants.setBeginSize(edgeAttrib, 8);
288     GraphConstants.setBendable(edgeAttrib,false);
289     GraphConstants.setDisconnectable(edgeAttrib,false);
290     attributes.put(e, edgeAttrib);
291     
292     Map JavaDoc inn = in.getAttributes();
293     Map JavaDoc outn = out.getAttributes();
294
295     Map JavaDoc props = new HashMap JavaDoc();
296     props.put("type", "edge");
297     props.put("name", name);
298     props.put("nodein", inn.get("name"));
299     props.put("nodeout", outn.get("name"));
300     e.setAttributes(props);
301
302     ConnectionSet cs = new ConnectionSet();
303     cs.connect(e,(DefaultPort)in.getChildAt(0),false);
304     cs.connect(e,(DefaultPort)out.getChildAt(0),true);
305     edges.put(name,e);
306     Object JavaDoc[] cell = new Object JavaDoc[]{e};
307     model.insert(cell,cs,null,attributes);
308     }
309
310     public void openGraph(String JavaDoc projectName) {
311             try {
312                 newGraph();
313                 nodes.clear();
314                 edges.clear();
315
316                 Object JavaDoc[] nd = persistence.getNodes();
317                 for (int i = 0; i < nd.length; i++) {
318                     String JavaDoc nodeName = (String JavaDoc) nd[i];
319                     int state = persistence.getNodeState(nodeName);
320                     int type = persistence.getNodeType(nodeName);
321                     addNewNode(nodeName, state, type);
322                 }
323
324                 Object JavaDoc[] ed = persistence.getEdges();
325                 for (int i = 0; i < ed.length; i++) {
326                     String JavaDoc nEdge = (String JavaDoc) ed[i];
327                     String JavaDoc nodeIn = persistence.getEdgeInNode(nEdge);
328                     String JavaDoc nodeOut = persistence.getEdgeOutNode(nEdge);
329                     addNewEdge((DefaultGraphCell) nodes.get(nodeIn),(DefaultGraphCell) nodes.get(nodeOut),nEdge);
330                 }
331
332                 if (automaticLayout)
333                     layout2.resetDamper();
334                 
335             Collection JavaDoc iterations = persistence.getIterations();
336                 Iterator JavaDoc ite = iterations.iterator();
337                 while (ite.hasNext())
338                 {
339                     BnIterationLightValue i = (BnIterationLightValue)ite.next();
340                     this.updateIterationNodes(i.getFromNode(),i.getToNode());
341                 }
342                 
343             /* Collection properties = persistence.getProperties();
344                 Iterator prop = properties.iterator();
345                 ArrayList itNodes = new ArrayList();
346                 while (prop.hasNext())
347                 {
348                     String pProp = (String)prop.next();
349                     if (pProp.matches("iterate_"+".*"))
350                     {
351                         itNodes = getItNodes(pProp);
352                         this.updateIterationNodes((String)itNodes.get(0),(String)itNodes.get(1));
353                     }
354                 }*/

355
356             } catch (Exception JavaDoc e1) {e1.printStackTrace();
357             }
358
359         }
360         
361     /**
362      * Method that returns iteration nodes of this property
363      */

364
365     private ArrayList JavaDoc getItNodes(String JavaDoc prop)
366         throws Exception JavaDoc {
367         ArrayList JavaDoc nodes = new ArrayList JavaDoc();
368         int index = prop.indexOf("iterate_");
369         prop = prop.substring(index+8);
370         index = prop.indexOf("-");
371         String JavaDoc to = prop.substring(index+1);
372         String JavaDoc from = prop.substring(0,index);
373         nodes.add(from);
374         nodes.add(to);
375         return(nodes);
376     }
377
378     public void newGraph(){
379     nodes.clear();
380     edges.clear();
381     model = new DefaultGraphModel();
382     setModel(model);
383     manager.setStatusBar(" ");
384     };
385     
386
387     public void setLayout1(){
388     basicLayout(this);
389     }
390     public void setExpand(){
391     expand.step();
392     }
393     public void setZoom(int value){
394     setScale(1);
395     }
396     public void zoomIn(){
397     setScale(getScale()*2);
398     }
399     
400     public void zoomOut(){
401     if (getScale() > 0.01)
402         setScale(getScale()/2);
403     
404     }
405
406     public void fitWindow(JScrollPane JavaDoc scrollPane){
407     Dimension JavaDoc p = getPreferredSize();
408     Dimension JavaDoc s = scrollPane.getViewport().getBounds().getSize();
409     if (Math.abs (s.getWidth () - p.getWidth ()) <
410         Math.abs (s.getHeight () - p.getHeight ()))
411         setScale ((double) s.getWidth () / p.getWidth ());
412     else setScale ((double) s.getHeight () / p.getHeight ());
413     }
414
415     public void selectAll(){
416     addSelectionCells(getRoots());
417
418     }
419
420     public boolean isGroup(Object JavaDoc cell) {
421     // Map the Cell to its View
422
CellView view = getView().getMapping(cell, false);
423     if (view != null)
424         return !view.isLeaf();
425     return false;
426     }
427
428
429
430     public boolean isVertex(Object JavaDoc object) {
431     if (!(object instanceof Port) && !(object instanceof com.jgraph.graph.Edge))
432         return !isGroup(object);
433     return false;
434     }
435
436     public boolean isEdge(Object JavaDoc object) {
437     return (object instanceof com.jgraph.graph.Edge);
438     }
439
440     public Object JavaDoc[] getSelectionNodes() {
441     Object JavaDoc[] tmp = getSelectionCells();
442     Object JavaDoc[] all = DefaultGraphModel.getDescendants(getModel(), tmp).toArray();
443     Object JavaDoc[] cells = getVertices(all);
444     Object JavaDoc[] elems = new Object JavaDoc[cells.length];
445     for(int i=0;i<cells.length;i++){
446         elems[i]=(String JavaDoc)(((DefaultGraphCell)cells[i]).getUserObject());
447     }
448     return elems;
449     }
450     
451     public boolean isSubProcess(String JavaDoc name) {
452      Object JavaDoc[] tmp = getSelectionCells();
453      Object JavaDoc[] all = DefaultGraphModel.getDescendants(getModel(), tmp).toArray();
454      Object JavaDoc[] cells = getVertices(all);
455      Object JavaDoc[] elems = new Object JavaDoc[cells.length];
456      for(int i=0;i<cells.length;i++){
457         if (name.equals((String JavaDoc)((DefaultGraphCell)cells[i]).getUserObject()))
458         {
459           DefaultGraphCell myCell = (DefaultGraphCell)cells[i];
460           Map JavaDoc props = myCell.getAttributes();
461           String JavaDoc subProcess = (String JavaDoc)props.get("subProcess");
462           if (subProcess.equals("yes"))
463             return(true);
464          }
465       }
466      return false;
467     }
468
469     public Object JavaDoc[] getVertices(Object JavaDoc[] cells) {
470     if (cells != null) {
471         ArrayList JavaDoc result = new ArrayList JavaDoc();
472         for (int i = 0; i < cells.length; i++)
473         if (isVertex(cells[i]))
474             result.add(cells[i]);
475         return result.toArray();
476     }
477     return null;
478     }
479
480     public Object JavaDoc[] getSelectionEdges() {
481     Object JavaDoc[] all= getSelectionCells();
482     Object JavaDoc[] cells = getEdges(all);
483     Object JavaDoc[] elems = new Object JavaDoc[cells.length];
484     for(int i=0;i<cells.length;i++){
485         elems[i]=(String JavaDoc)(((DefaultEdge)cells[i]).getAttributes().get("name"));
486     }
487     return elems;
488
489     }
490
491     public Object JavaDoc[] getEdges(Object JavaDoc[] cells) {
492     if (cells != null) {
493         ArrayList JavaDoc result = new ArrayList JavaDoc();
494         for (int i = 0; i < cells.length; i++)
495         if (isEdge(cells[i]))
496             result.add(cells[i]);
497         return result.toArray();
498     }
499     return null;
500     }
501
502     public Object JavaDoc[] getEdge(Object JavaDoc[] cells, String JavaDoc name) {
503     if (cells != null) {
504         ArrayList JavaDoc result = new ArrayList JavaDoc();
505         for (int i = 0; i < cells.length; i++)
506         {
507         if (isEdge(cells[i]))
508         {
509             Map JavaDoc a = ((DefaultEdge)cells[i]).getAttributes();
510             if (name.equals(a.get("name")))
511             result.add(cells[i]);
512         }
513         }
514         return result.toArray();
515     }
516     return null;
517     }
518
519     public void removeNodeEdges(Object JavaDoc[] cells, String JavaDoc name) {
520     if (cells != null) {
521         for (int i = 0; i < cells.length; i++)
522         {
523         if (isEdge(cells[i]))
524         {
525             Map JavaDoc a = ((DefaultEdge)cells[i]).getAttributes();
526             if (name.equals(a.get("nodein")) || name.equals(a.get("nodeout")))
527             deleteEdge((String JavaDoc)a.get("name"));
528         }
529         }
530     }
531     }
532
533     public void deleteNode(String JavaDoc node) throws Exception JavaDoc{
534       if(nodes.containsKey(node)){
535           Object JavaDoc[] all=getRoots();
536           removeNodeEdges(all,node);
537           getModel().remove(new Object JavaDoc[]{nodes.get(node)});
538           nodes.remove(node);
539       }
540     }
541
542     public void deleteEdge(String JavaDoc edge){
543       if(edges.containsKey(edge)){
544         Object JavaDoc[] all=getRoots();
545         getModel().remove(getEdge(all,edge));
546         edges.remove(edge);
547      }
548     }
549
550     public boolean existEdge(String JavaDoc name){
551     return(edges.containsKey(name));
552     }
553
554     public void restoreEdges() {
555         try {
556             Object JavaDoc[] all = getRoots();
557             Object JavaDoc[] ed = getEdges(all);
558
559             edges.clear();
560             if (ed != null) {
561                 getModel().remove(ed);
562                 Object JavaDoc[] edv = persistence.getEdges();
563                 for (int i = 0; i < edv.length; i++) {
564                     String JavaDoc nEdge = (String JavaDoc) ed[i];
565                     String JavaDoc nodeIn = persistence.getEdgeInNode(nEdge);
566                     String JavaDoc nodeOut = persistence.getEdgeOutNode(nEdge);
567                     addNewEdge(
568                         (DefaultGraphCell) nodes.get(nodeIn),
569                         (DefaultGraphCell) nodes.get(nodeOut),
570                         nEdge);
571                 }
572             }
573         } catch (Exception JavaDoc e1) {
574             JOptionPane.showMessageDialog(
575                 null,
576                 resource.getString("wfgraph.edgeerror"));
577         }
578
579     }
580
581     public void restoreEdge(String JavaDoc nEdge, String JavaDoc nodeIn, String JavaDoc nodeOut){
582     
583     addNewEdge((DefaultGraphCell)nodes.get(nodeIn),(DefaultGraphCell)nodes.get(nodeOut),nEdge);
584     }
585
586
587
588     public void changeNodeState(String JavaDoc node,int state){
589     if(nodes.containsKey(node)){
590         Hashtable JavaDoc attributes = new Hashtable JavaDoc();
591         Map JavaDoc vertexAttrib = GraphConstants.createMap();
592         switch (state) {
593         case Constants.Nd.INITIAL:
594         GraphConstants.setBackground(vertexAttrib,(Color.lightGray).darker()); break;
595         case Constants.Nd.READY:
596         GraphConstants.setBackground(vertexAttrib,(Color.yellow).darker()); break;
597         case Constants.Nd.ANTICIPABLE:
598         GraphConstants.setBackground(vertexAttrib,(Color.green).darker()); break;
599         case Constants.Nd.ANTICIPATING:
600         GraphConstants.setBackground(vertexAttrib,(Color.magenta).darker()); break;
601         case Constants.Nd.EXECUTING:
602         GraphConstants.setBackground(vertexAttrib,(Color.red).darker()); break;
603         case Constants.Nd.EXECUTED:
604         GraphConstants.setBackground(vertexAttrib,(Color.orange).darker()); break;
605         case Constants.Nd.TERMINATED:
606         GraphConstants.setBackground(vertexAttrib,(Color.cyan).darker()); break;
607         case Constants.Nd.ANT_SUSPENDED:
608         GraphConstants.setBackground(vertexAttrib,(Color.blue).darker()); break;
609         case Constants.Nd.EXEC_SUSPENDED:
610         GraphConstants.setBackground(vertexAttrib,(Color.gray).darker()); break;
611         case Constants.Nd.DEAD:
612         GraphConstants.setBackground(vertexAttrib,(Color.blue).darker()); break;
613         }
614         attributes.put(nodes.get(node), vertexAttrib);
615         model.insert(null,null,null,attributes);
616         repaint();
617     }
618     }
619
620     public Object JavaDoc getNeighbour(Object JavaDoc edge, Object JavaDoc vertex) {
621       Object JavaDoc source = getSourceVertex(edge);
622       if (vertex == source)
623     return getTargetVertex(edge);
624       else
625     return source;
626     }
627
628     public Object JavaDoc getSourceVertex(Object JavaDoc edge) {
629       Object JavaDoc sourcePort = graphModel.getSource(edge);
630       return graphModel.getParent(sourcePort);
631     }
632
633     public Object JavaDoc getTargetVertex(Object JavaDoc edge) {
634       Object JavaDoc targetPort = graphModel.getTarget(edge);
635       return graphModel.getParent(targetPort);
636     }
637
638     public CellView getSourceView(Object JavaDoc edge) {
639       Object JavaDoc source = getSourceVertex(edge);
640       return getView().getMapping(source, false);
641     }
642
643     public CellView getTargetView(Object JavaDoc edge) {
644       Object JavaDoc target = getTargetVertex(edge);
645       return getView().getMapping(target, false);
646     }
647
648     public Object JavaDoc[] getEdgesBetween(Object JavaDoc vertex1, Object JavaDoc vertex2) {
649       ArrayList JavaDoc result = new ArrayList JavaDoc();
650       Set JavaDoc edges = DefaultGraphModel.getEdges(graphModel, new Object JavaDoc[]{vertex1});
651       Iterator JavaDoc it = edges.iterator();
652       while (it.hasNext()) {
653     Object JavaDoc edge = it.next();
654     Object JavaDoc source = getSourceVertex(edge);
655     Object JavaDoc target = getTargetVertex(edge);
656     if ((source == vertex1 && target == vertex2) ||
657         (source == vertex2 && target == vertex1))
658           result.add(edge);
659       }
660       return result.toArray();
661     }
662
663     //Override JGraph getToolTipText method
664
public String JavaDoc getToolTipText(MouseEvent JavaDoc e) {
665         try {
666
667             if (e != null) {
668                 //Fetch Cell under Mousepointer
669
if (!(persistence.soapclient.getProjectName()).equals(persistence.getProjectName()))
670                     persistence.openProject(persistence.getProjectName());
671                 Object JavaDoc c = getFirstCellForLocation(e.getX(), e.getY());
672                 if (c != null && isVertex(c)) {
673                     String JavaDoc nodeName = convertValueToString(c);
674                     StrutsNodeValue nl = persistence.getProjectNode(nodeName);
675
676                     String JavaDoc s =
677                         "<html><center><b>"
678                             + nodeName
679                             + "</b></center>"
680                             + resource.getString("wfgraph.state")
681                             + nl.getState()
682                             + "<br>"
683                             + resource.getString("wfgraph.role")
684                             + nl.getRole()
685                             + "<br>"
686                             + resource.getString("wfgraph.type")
687                             + nl.getType()
688                             + "<br>"
689                             + resource.getString("wfgraph.deadline")
690                             + nl.getDeadline()
691                         // +"<TEXTAREA cols=30 rows=5 name=description style=overflow:hidden;height:auto>"
692
// + nl.getDescription()
693
// +"</TEXTAREA>"
694
+ "<br></html>";
695
696                     return s;
697                 }
698             }
699         } catch (Exception JavaDoc e1) {
700             JOptionPane.showMessageDialog(null, resource.getString("wfgraph.errorgettool"));
701         }
702
703         return null;
704     }
705
706     public Object JavaDoc[] getAll() {
707       return getDescendants(getRoots());
708     }
709
710     public void setAddEdgeState(boolean b){
711     NEWEDGE = b;
712     }
713
714     public void toFront(String JavaDoc nodeName){
715     Object JavaDoc[] cells = new Object JavaDoc[]{nodes.get(nodeName)};
716     // Object[] cells = graph.getSelectionCells();
717
if (cells != null) {
718         GraphView gv = getView();
719         CellView[] views = gv.getMapping(cells);
720         gv.toFront(views);
721       }
722     }
723
724     public void toBack(String JavaDoc nodeName){
725     Object JavaDoc[] cells = new Object JavaDoc[]{nodes.get(nodeName)};
726     // Object[] cells = graph.getSelectionCells();
727
if (cells != null) {
728         GraphView gv = getView();
729         CellView[] views = gv.getMapping(cells);
730         gv.toBack(views);
731       }
732     }
733
734
735     public static java.awt.image.BufferedImage JavaDoc toImage(JGraph graph) {
736     Object JavaDoc[] cells = graph.getRoots();
737     
738     if (cells.length > 0) {
739         Rectangle JavaDoc bounds = graph.getCellBounds(cells);
740         graph.toScreen(bounds);
741         
742         // Create a Buffered Image
743
Dimension JavaDoc d = bounds.getSize();
744         java.awt.image.BufferedImage JavaDoc img = new java.awt.image.BufferedImage JavaDoc(d.width+10,
745                           d.height+10, java.awt.image.BufferedImage.TYPE_INT_RGB);
746         Graphics2D JavaDoc graphics = img.createGraphics();
747         graphics.translate(-bounds.x+5, -bounds.y+5);
748         graph.paint(graphics);
749         
750         return img;
751     }
752     return null;
753     }
754
755     private static String JavaDoc addNodeToVRML(Point JavaDoc p,Color JavaDoc c){
756     String JavaDoc vrml="Transform{\n"+
757         "translation "+p.getX()+" "+p.getY()+" 0\n"+
758         " children[\n"+
759         " Shape{\n"+
760         " appearance Appearance\n"+
761             " {\n"+
762         " material Material\n"+
763         " {"+
764         " diffuseColor "+c.getRed()+" "+c.getGreen()+" "+c.getBlue()+"\n"+
765         " shininess 0.01\n"+
766             " }"+
767             " }"+
768         " geometry Box { size 25 25 25 }\n"+
769         " }\n"+
770         "]\n"+
771         "}\n";
772     return vrml;
773     }
774     private static String JavaDoc addEdgeToVRML(Point JavaDoc in,Point JavaDoc out){
775     String JavaDoc vrml= "Shape{appearance Appearance { \n"+
776     "material Material {\n"+
777         " emissiveColor 1 1 1 }\n"+
778         " }\n"+
779         " geometry IndexedLineSet {\n"+
780         " coord Coordinate {point [ "+in.getX()+" "+in.getY()+" 0\n"+
781         " "+out.getX()+" "+out.getY()+" 0\n"+
782         " ] }\n"+
783         " coordIndex [0 1 ]\n"+
784         " color Color { color [1 0 0, 0 1 0, 0 0 1]}\n"+
785         " colorIndex [0 1 ]\n"+
786         " colorPerVertex TRUE\n"+
787         " }\n"+
788         "}\n";
789     
790     return vrml;
791     }
792
793     public static String JavaDoc vrmlView(JGraph graph){
794     CellView[] views = graph.getView().getRoots();
795     int max= 0;
796     String JavaDoc vrml="#VRML V2.0 utf8\n Background{skyColor 0 0 0.5}\n";
797     for(int i=0; i<views.length; i++){
798         if (views[i] instanceof VertexView){
799         Point JavaDoc p = ((VertexView)views[i]).getCenterPoint();
800         Color JavaDoc c=(Color JavaDoc)((Map JavaDoc)((VertexView)views[i]).getAttributes()).get("backgroundColor");
801         vrml= vrml+addNodeToVRML(p,c);
802         }else if (views[i] instanceof EdgeView){
803         Point JavaDoc in=((EdgeView)views[i]).getPoint(0);
804         Point JavaDoc out=((EdgeView)views[i]).getPoint(((EdgeView)views[i]).getPointCount()-1);
805         vrml= vrml+addEdgeToVRML(in,out);
806         }
807     }
808     return vrml;
809
810     }
811
812
813     //Basic Layout
814
public static void basicLayout (JGraph graph){
815     //fetch all cell views
816
CellView[] views = graph.getView().getRoots();
817     //list to store the vertices
818
java.util.List JavaDoc vertices = new ArrayList JavaDoc();
819     int max= 0;
820     //loop through all views
821
for(int i=0; i<views.length; i++){
822         //Add vertex to list
823
if (views[i] instanceof VertexView){
824         vertices.add(views[i]);
825         //fetch bounds
826
Rectangle JavaDoc b = views[i].getBounds();
827         //update Maximum
828
if(b !=null)
829             max = Math.max(Math.max(b.width, b.height), max);
830         }
831     }
832     //compute radius
833
int r= (int) Math.max(vertices.size()*max/Math.PI, 100);
834     //Compute Radial step
835
double phi = 2*Math.PI/vertices.size();
836     //Arrange vertices in a circle
837
for (int i= 0; i<vertices.size(); i++){
838         //Cast the object to a CellView
839
CellView view = (CellView) vertices.get(i);
840         //Fetch the Bounds
841
Rectangle JavaDoc bounds = view.getBounds();
842         //Update the Location
843
if(bounds !=null)
844         bounds.setLocation(r+(int) (r*Math.sin(i*phi)), r+(int) (r*Math.cos(i*phi)));
845     }
846     graph.repaint();
847     }
848
849
850     public class WFListener implements Observer JavaDoc,GraphModelListener,GraphSelectionListener{
851     public WFListener(){}
852     //
853
// Listeners
854
//
855

856     public void update(Observable JavaDoc o, Object JavaDoc obj){
857         if(automaticLayout)
858         layout2.resetDamper();
859     }
860         
861     // GraphSelectionListener
862
public void valueChanged(GraphSelectionEvent e) {
863         if (!isSelectionEmpty()){
864         if(automaticLayout)
865             layout2.setDamper(0);
866             //layout2.resetDamper();
867
}
868         manager.updateStatus();
869
870     }
871         
872     // GraphModelListener
873
public void graphChanged(GraphModelEvent e) {
874         if(automaticLayout)
875         layout2.resetDamper();
876     }
877     }
878     
879    /**
880     * MarqueeHandler that can insert cells.
881    */

882     public class WFMarqueeHandler extends BasicMarqueeHandler {
883
884     protected Point JavaDoc start, current;
885     
886     protected Rectangle JavaDoc bounds;
887     
888     protected PortView port, firstPort, lastPort;
889
890     protected DefaultGraphCell sourceCell,destCell;
891     
892     protected boolean exception=false;
893
894       /* Return true if this handler should be preferred over other handlers. */
895     public boolean isForceMarqueeEvent(MouseEvent JavaDoc e) {
896         if ((e.getModifiers() & java.awt.event.InputEvent.BUTTON3_MASK) == 0)
897         return NEWEDGE || super.isForceMarqueeEvent(e);
898         return true;
899       }
900
901     public void mousePressed(MouseEvent JavaDoc event) {
902
903         if (NEWEDGE) {
904         start = snap(event.getPoint());
905         sourceCell = (DefaultGraphCell)getFirstCellForLocation(event.getX(), event.getY());
906         event.consume();
907         }else{
908         // Popup Menu
909
if ((event.getModifiers() & java.awt.event.InputEvent.BUTTON3_MASK) != 0) {
910
911             Object JavaDoc cell = getFirstCellForLocation(event.getX(), event.getY());
912             JPopupMenu JavaDoc menu = WFContextMenu.popupMenu(manager.getFrame(), event.getPoint(), convertValueToString(cell),manager);
913             
914             // Flip along screen
915
int x = event.getX();
916             int y = event.getY();
917             
918             menu.show(manager.getGraph(), x, y);
919         }
920         }
921
922         super.mousePressed(event);
923     }
924     
925     public void mouseDragged(MouseEvent JavaDoc event) {
926         if(NEWEDGE){
927         Graphics JavaDoc g = getGraphics();
928         Color JavaDoc bg = getBackground();
929         Color JavaDoc fg = Color.black;
930         g.setColor(fg);
931         g.setXORMode(bg);
932         current = snap(event.getPoint());
933         overlay(g);
934         }
935         super.mouseDragged(event);
936     }
937     
938     public void mouseReleased(MouseEvent JavaDoc event) {
939         try {
940             if (NEWEDGE) {
941                 destCell =(DefaultGraphCell) getFirstCellForLocation(event.getX(), event.getY());
942                 if ((sourceCell != null) && (destCell != null)) {
943                     String JavaDoc name = persistence.addEdge((String JavaDoc) sourceCell.getUserObject(),(String JavaDoc) destCell.getUserObject());
944                     if (name != null) {
945                         addNewEdge(sourceCell, destCell, name);
946                     } else {
947                         exception = true;
948                     }
949                 }
950                 NEWEDGE = false;
951                 sourceCell = null;
952                 destCell = null;
953             }
954
955             super.mouseReleased(event);
956             if (exception) {
957                 exception = false;
958             }
959
960         } catch (Exception JavaDoc e1) {
961         }
962
963     }
964     
965     public void mouseMoved(MouseEvent JavaDoc event) {
966         super.mouseMoved(event);
967     }
968     
969     public void overlay(Graphics JavaDoc g) {
970         try{
971         super.overlay(g);
972         if(NEWEDGE){
973             repaint();
974             g.drawLine(start.x, start.y, current.x, current.y);
975         }
976         }catch(Exception JavaDoc e){}
977     }
978     }
979     
980     public class LayoutThread extends javax.swing.Timer JavaDoc {
981
982     public LayoutThread(final WFGraph graph) {
983         super(200,new ActionListener JavaDoc() {
984             public void actionPerformed(ActionEvent JavaDoc e) {
985             int n = graph.getModel().getRootCount();
986             Spring s = new Spring(10*n);
987             s.compute(graph);
988             graph.repaint();
989             }
990         });
991     }
992     
993     public void step() {
994         super.fireActionPerformed(null);
995     }
996     
997     }
998
999
1000}// WFGraph
1001
Popular Tags