1 | package com.algomagic.atn; |
---|
2 | |
---|
3 | import java.util.*; |
---|
4 | import att.grappa.*; |
---|
5 | |
---|
6 | public class EdgeUpdate |
---|
7 | extends AbstractUpdate |
---|
8 | implements Update, VisualizationConstants |
---|
9 | { |
---|
10 | private String _parentNodeName; |
---|
11 | private NodeUpdate _childNode; |
---|
12 | private String _type; |
---|
13 | private Collection _evidence; |
---|
14 | private boolean _local; |
---|
15 | |
---|
16 | public EdgeUpdate( String parentNodeName, |
---|
17 | NodeUpdate childNode, |
---|
18 | String type, |
---|
19 | boolean local, |
---|
20 | Collection evidence, |
---|
21 | int side ) { |
---|
22 | _parentNodeName = parentNodeName; |
---|
23 | _childNode = childNode; |
---|
24 | _type = type; |
---|
25 | _local = local; |
---|
26 | _evidence = evidence; |
---|
27 | _side = side; |
---|
28 | } |
---|
29 | |
---|
30 | public int initialize( VisualizationPanel visPanel ) { |
---|
31 | Graph graph = visPanel.getGraph( ); |
---|
32 | |
---|
33 | _childNode.initialize( visPanel ); |
---|
34 | Node n1 = graph.findNodeByName( _parentNodeName ); |
---|
35 | Node n2 = graph.findNodeByName( _childNode.getName( ) ); |
---|
36 | |
---|
37 | if( n1 != null && n2 != null ) { |
---|
38 | new Edge( graph, n1, n2 ); |
---|
39 | } else { |
---|
40 | if( n1 == null && n1 == null ) { |
---|
41 | throw new RuntimeException( _parentNodeName + " === " + _childNode.getName( ) ); |
---|
42 | } else if( n1 == null ) { |
---|
43 | throw new RuntimeException( _parentNodeName ); |
---|
44 | } else if( n2 == null ) { |
---|
45 | throw new RuntimeException( _childNode.getName( ) ); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | if( _local ) { |
---|
50 | visPanel.addCredentials( _evidence, _local ); |
---|
51 | } |
---|
52 | |
---|
53 | return 3; |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | public Collection getEvidence( ) { |
---|
58 | return _evidence; |
---|
59 | } |
---|
60 | |
---|
61 | public boolean getLocal( ) { |
---|
62 | return _local; |
---|
63 | } |
---|
64 | |
---|
65 | public String getType( ) { |
---|
66 | return _type; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | public boolean apply( VisualizationPanel visPanel ) { |
---|
71 | Graph graph = visPanel.getGraph( ); |
---|
72 | Node n1 = graph.findNodeByName( _parentNodeName ); |
---|
73 | Node n2 = graph.findNodeByName( _childNode.getName( ) ); |
---|
74 | |
---|
75 | Edge e = getEdgeBetween( graph, n1, n2 ); |
---|
76 | visPanel.setEdgeProperties( e, _type, _local ); |
---|
77 | |
---|
78 | visPanel.clearSelection( ); |
---|
79 | |
---|
80 | _childNode.setNew( ); |
---|
81 | _childNode.apply( visPanel ); |
---|
82 | |
---|
83 | if( _local == false ) { |
---|
84 | visPanel.addCredentials( _evidence, _local ); |
---|
85 | } |
---|
86 | |
---|
87 | visPanel.highlightCredentials( _evidence, _local ); |
---|
88 | |
---|
89 | return true; |
---|
90 | } |
---|
91 | |
---|
92 | public void undo( VisualizationPanel visPanel ) { |
---|
93 | Graph graph = visPanel.getGraph( ); |
---|
94 | Node n1 = graph.findNodeByName( _parentNodeName ); |
---|
95 | Node n2 = graph.findNodeByName( _childNode.getName( ) ); |
---|
96 | |
---|
97 | Edge e = getEdgeBetween( graph, n1, n2 ); |
---|
98 | _childNode.undo( visPanel ); |
---|
99 | |
---|
100 | |
---|
101 | if( visPanel.getLocal( ) == false ) { |
---|
102 | visPanel.setShadow( e ); |
---|
103 | } else { |
---|
104 | visPanel.setInvis( e ); |
---|
105 | } |
---|
106 | |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | public Element getElement( VisualizationPanel visPanel ) { |
---|
111 | Graph graph = visPanel.getGraph( ); |
---|
112 | Node n1 = graph.findNodeByName( _parentNodeName ); |
---|
113 | Node n2 = graph.findNodeByName( _childNode.getName( ) ); |
---|
114 | |
---|
115 | return getEdgeBetween( graph, n1, n2 ); |
---|
116 | } |
---|
117 | |
---|
118 | public Node getChild( VisualizationPanel visPanel ) { |
---|
119 | Graph graph = visPanel.getGraph( ); |
---|
120 | return graph.findNodeByName( _childNode.getName( ) ); |
---|
121 | } |
---|
122 | |
---|
123 | public String getChildName( ) { |
---|
124 | return _childNode.getName( ); |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | |
---|
129 | private Edge getEdgeBetween( Graph g, Node head, Node tail ) { |
---|
130 | Enumeration edges = head.edgeElements( ); |
---|
131 | while( edges.hasMoreElements( ) ) { |
---|
132 | Edge e = (Edge) edges.nextElement( ); |
---|
133 | Node t = e.getHead( ); |
---|
134 | Node h = e.getTail( ); |
---|
135 | |
---|
136 | if( h.getName( ).equals( head.getName( ) ) && t.getName( ).equals( tail.getName( ) ) ) { |
---|
137 | return e; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | return null; |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | } |
---|
146 | |
---|