1 56 package org.objectstyle.cayenne.graph; 57 58 import java.util.ArrayList ; 59 import java.util.Collection ; 60 import java.util.HashMap ; 61 import java.util.Iterator ; 62 import java.util.Map ; 63 64 73 public class GraphManager implements GraphMap, GraphChangeHandler { 74 75 protected Map nodes; 76 77 protected Collection localChangeHandlers; 78 protected GraphChangeHandler remoteChangeHandler; 79 protected boolean recordingLocalChanges; 80 81 84 public GraphManager() { 85 this.nodes = new HashMap (); 86 this.recordingLocalChanges = true; 87 } 88 89 92 public synchronized void mergeRemoteChange(GraphDiff change) { 93 if (remoteChangeHandler == null) { 94 return; 95 } 96 97 setRecordingLocalChanges(false); 99 100 try { 101 change.apply(remoteChangeHandler); 102 } 103 finally { 104 setRecordingLocalChanges(true); 105 } 106 } 107 108 public boolean isRecordingLocalChanges() { 109 return recordingLocalChanges; 110 } 111 112 public void setRecordingLocalChanges(boolean active) { 113 this.recordingLocalChanges = active; 114 } 115 116 public synchronized void addLocalChangeHandler(GraphChangeHandler handler) { 117 if (handler != null) { 118 if (localChangeHandlers == null) { 119 localChangeHandlers = new ArrayList (); 120 } 121 122 localChangeHandlers.add(handler); 123 } 124 } 125 126 public synchronized void removeLocalChangeHandler(GraphChangeHandler handler) { 127 if (handler != null && localChangeHandlers != null) { 128 localChangeHandlers.remove(handler); 129 130 if (localChangeHandlers.isEmpty()) { 131 localChangeHandlers = null; 132 } 133 } 134 } 135 136 public GraphChangeHandler getRemoteChangeHandler() { 137 return remoteChangeHandler; 138 } 139 140 public void setRemoteChangeHandler(GraphChangeHandler handler) { 141 this.remoteChangeHandler = handler; 142 } 143 144 protected Collection getLocalChangeHandlers() { 145 return localChangeHandlers; 146 } 147 148 150 public synchronized Object getNode(Object nodeId) { 151 return nodes.get(nodeId); 152 } 153 154 public synchronized void registerNode(Object nodeId, Object nodeObject) { 155 nodes.put(nodeId, nodeObject); 156 } 157 158 public synchronized Object unregisterNode(Object nodeId) { 159 return nodes.remove(nodeId); 160 } 161 162 164 public synchronized void arcCreated(Object nodeId, Object targetNodeId, Object arcId) { 165 if (recordingLocalChanges && localChangeHandlers != null) { 166 Iterator it = localChangeHandlers.iterator(); 167 while (it.hasNext()) { 168 ((GraphChangeHandler) it.next()).arcCreated(nodeId, targetNodeId, arcId); 169 } 170 } 171 } 172 173 public synchronized void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) { 174 if (recordingLocalChanges && localChangeHandlers != null) { 175 Iterator it = localChangeHandlers.iterator(); 176 while (it.hasNext()) { 177 ((GraphChangeHandler) it.next()).arcDeleted(nodeId, targetNodeId, arcId); 178 } 179 } 180 } 181 182 public synchronized void nodeCreated(Object nodeId) { 183 if (recordingLocalChanges && localChangeHandlers != null) { 184 Iterator it = localChangeHandlers.iterator(); 185 while (it.hasNext()) { 186 ((GraphChangeHandler) it.next()).nodeCreated(nodeId); 187 } 188 } 189 } 190 191 public synchronized void nodeRemoved(Object nodeId) { 192 if (recordingLocalChanges && localChangeHandlers != null) { 193 Iterator it = localChangeHandlers.iterator(); 194 while (it.hasNext()) { 195 ((GraphChangeHandler) it.next()).nodeRemoved(nodeId); 196 } 197 } 198 } 199 200 public synchronized void nodeIdChanged(Object nodeId, Object newId) { 201 if (recordingLocalChanges && localChangeHandlers != null) { 202 Iterator it = localChangeHandlers.iterator(); 203 while (it.hasNext()) { 204 ((GraphChangeHandler) it.next()).nodeIdChanged(nodeId, newId); 205 } 206 } 207 } 208 209 public synchronized void nodePropertyChanged( 210 Object nodeId, 211 String property, 212 Object oldValue, 213 Object newValue) { 214 if (recordingLocalChanges && localChangeHandlers != null) { 215 Iterator it = localChangeHandlers.iterator(); 216 while (it.hasNext()) { 217 ((GraphChangeHandler) it.next()).nodePropertyChanged( 218 nodeId, 219 property, 220 oldValue, 221 newValue); 222 } 223 } 224 } 225 } 226 | Popular Tags |