| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
package net.sourceforge.combean.adapters.jgraph; |
| 23 | |
|
| 24 | |
import org.jgraph.graph.GraphModel; |
| 25 | |
|
| 26 | |
import net.sourceforge.combean.graph.iterators.IncidentEdgeAsIncomingEdgeIterator; |
| 27 | |
import net.sourceforge.combean.graph.iterators.IncidentEdgeAsOutgoingEdgeIterator; |
| 28 | |
import net.sourceforge.combean.graph.iterators.IndexedEdgeIterator; |
| 29 | |
import net.sourceforge.combean.graph.iterators.IndexedNodeIterator; |
| 30 | |
import net.sourceforge.combean.interfaces.graph.Edge; |
| 31 | |
import net.sourceforge.combean.interfaces.graph.EdgeIterator; |
| 32 | |
import net.sourceforge.combean.interfaces.graph.Graph; |
| 33 | |
import net.sourceforge.combean.interfaces.graph.Node; |
| 34 | |
import net.sourceforge.combean.interfaces.graph.NodeIterator; |
| 35 | |
import net.sourceforge.combean.interfaces.graph.prop.DirectedEdgeNeighborhoodGraphProp; |
| 36 | |
import net.sourceforge.combean.interfaces.graph.prop.GlobalIndexedEdgesGraphProp; |
| 37 | |
import net.sourceforge.combean.interfaces.graph.prop.GlobalIndexedNodesGraphProp; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public class JGraphModelAsGraph implements |
| 46 | |
Graph, |
| 47 | |
DirectedEdgeNeighborhoodGraphProp, |
| 48 | |
GlobalIndexedNodesGraphProp, |
| 49 | |
GlobalIndexedEdgesGraphProp { |
| 50 | |
|
| 51 | 39 | private GraphModel graphModel = null; |
| 52 | |
|
| 53 | 39 | private Node[] allNodes = null; |
| 54 | 39 | private Edge[] allEdges = null; |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
public JGraphModelAsGraph(GraphModel jgraph) { |
| 62 | 39 | super(); |
| 63 | 39 | this.graphModel = jgraph; |
| 64 | 39 | int edgeCount = countEdgesInGraphModel(); |
| 65 | 39 | this.allEdges = new Edge[edgeCount]; |
| 66 | 39 | this.allNodes = new Node[this.graphModel.getRootCount()-edgeCount]; |
| 67 | 39 | copyNodesAndEdgesFromGraphModel(); |
| 68 | 39 | } |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
private final int countEdgesInGraphModel() { |
| 76 | 39 | int edgeCount = 0; |
| 77 | 222 | for (int i = 0; i < this.graphModel.getRootCount(); i++) { |
| 78 | 183 | Object o = this.graphModel.getRootAt(i); |
| 79 | 183 | if (this.graphModel.isEdge(o)) { |
| 80 | 81 | edgeCount++; |
| 81 | |
} |
| 82 | |
} |
| 83 | 39 | return edgeCount; |
| 84 | |
} |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
private final void copyNodesAndEdgesFromGraphModel() { |
| 91 | 39 | int edgeCount = 0; |
| 92 | 39 | int nodeCount = 0; |
| 93 | 222 | for (int i = 0; i < this.graphModel.getRootCount(); i++) { |
| 94 | 183 | Object o = this.graphModel.getRootAt(i); |
| 95 | 183 | if (this.graphModel.isEdge(o)) { |
| 96 | 81 | this.allEdges[edgeCount++] = |
| 97 | |
new JGraphEdgeAsEdge((org.jgraph.graph.Edge) o); |
| 98 | |
} |
| 99 | |
else { |
| 100 | 102 | this.allNodes[nodeCount++] = new JGraphCellAsNode(o); |
| 101 | |
} |
| 102 | |
} |
| 103 | 39 | } |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
public Class getNodeClass() { |
| 109 | 0 | return JGraphCellAsNode.class; |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public Class getEdgeClass() { |
| 116 | 0 | return JGraphEdgeAsEdge.class; |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
public EdgeIterator getIncidentEdges(Node v) { |
| 123 | 33 | return new JGraphEdgeIterator(this.graphModel, |
| 124 | |
((JGraphCellAsNode)v).getInnerObject()); |
| 125 | |
} |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
public EdgeIterator getOutgoingEdges(Node v) { |
| 131 | 15 | return new IncidentEdgeAsOutgoingEdgeIterator( |
| 132 | |
this, v, getIncidentEdges(v)); |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
public EdgeIterator getIncomingEdges(Node v) { |
| 139 | 3 | return new IncidentEdgeAsIncomingEdgeIterator( |
| 140 | |
this, v, getIncidentEdges(v)); |
| 141 | |
} |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public Node getOtherNode(Edge e, Node v) { |
| 146 | 6 | Node w = getFirstNode(e); |
| 147 | 6 | if (w.equals(v)) { |
| 148 | 0 | return getSecondNode(e); |
| 149 | |
} |
| 150 | 6 | return w; |
| 151 | |
} |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
private final Object findRootOf(Object o) { |
| 160 | 111 | Object result = o; |
| 161 | 222 | while (this.graphModel.getParent(result) != null) { |
| 162 | 111 | result = this.graphModel.getParent(result); |
| 163 | |
} |
| 164 | 111 | return result; |
| 165 | |
} |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
public Node getFirstNode(Edge e) { |
| 171 | 69 | org.jgraph.graph.Edge jgraphEdge = |
| 172 | |
((JGraphEdgeAsEdge)e).getJGraphEdge(); |
| 173 | 69 | Object source = this.graphModel.getSource(jgraphEdge); |
| 174 | 69 | return new JGraphCellAsNode(findRootOf(source)); |
| 175 | |
} |
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
public Node getSecondNode(Edge e) { |
| 181 | 42 | org.jgraph.graph.Edge jgraphEdge = |
| 182 | |
((JGraphEdgeAsEdge)e).getJGraphEdge(); |
| 183 | 42 | Object target = this.graphModel.getTarget(jgraphEdge); |
| 184 | 42 | return new JGraphCellAsNode(findRootOf(target)); |
| 185 | |
} |
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
public NodeIterator getAllNodesIterator() { |
| 191 | 6 | return new IndexedNodeIterator(this); |
| 192 | |
} |
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
public int getNumNodes() { |
| 198 | 57 | return this.allNodes.length; |
| 199 | |
} |
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
public Node getNode(int index) { |
| 205 | 18 | return this.allNodes[index]; |
| 206 | |
} |
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
public EdgeIterator getAllEdgesIterator() { |
| 212 | 3 | return new IndexedEdgeIterator(this); |
| 213 | |
} |
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
public int getNumEdges() { |
| 219 | 12 | return this.allEdges.length; |
| 220 | |
} |
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
public Edge getEdge(int index) { |
| 226 | 30 | return this.allEdges[index]; |
| 227 | |
} |
| 228 | |
} |