1 19 package org.netbeans.modules.xml.core.sync; 20 21 import java.io.*; 22 23 import org.openide.loaders.*; 24 import org.openide.cookies.*; 25 import org.openide.nodes.*; 26 import org.openide.filesystems.*; 27 import org.openide.util.*; 28 import java.util.Vector ; 29 import java.util.Arrays ; 30 31 49 public abstract class SyncSupport { 50 51 private DataObject dobj; 52 53 54 protected static final int JUST_SYNCHRONIZING = 1; 55 56 57 protected static final int JUST_RESOLVING_CONFLICT = 2; 58 59 60 protected static final int NOP = 0; 61 62 private int syncOperation; 64 private final Object syncOperationLock = new SyncSupportLock(); 66 67 69 70 public SyncSupport (DataObject dobj) throws IllegalArgumentException { 71 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Creating sychronizator " + System.identityHashCode(this) + " for: " + dobj.getPrimaryFile() ); 73 this.dobj = dobj; 74 syncOperation = NOP; 75 } 76 77 78 private void waitFor(int state) throws InterruptedException { 80 while (getSyncOp() != NOP) syncOperationLock.wait(); 81 } 82 83 private void setSyncOp(int op) { 85 synchronized (syncOperationLock) { 86 87 if (op == syncOperation) return; 88 89 if ( Util.THIS.isLoggable() ) Util.THIS.debug ( "" + System.identityHashCode(this) + " syncOperation " + syncOperation + " => " + op + " Thread:" + Thread.currentThread().getName()); 91 syncOperation = op; 92 syncOperationLock.notifyAll(); 93 } 94 } 95 96 private int getSyncOp() { 97 synchronized (syncOperationLock) { 98 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("" + System.identityHashCode(this) + " syncOperation = " + syncOperation + " Thread:" + Thread.currentThread().getName()); 100 return syncOperation; 101 } 102 } 103 104 105 108 public boolean isInSync() { 109 return getSyncOp() == JUST_SYNCHRONIZING; 110 } 111 112 113 116 public void postRequest(Runnable task) { 117 118 boolean leave = false; 119 120 try { 121 synchronized (syncOperationLock) { 122 waitFor(NOP); 123 leave = true; 124 setSyncOp(JUST_SYNCHRONIZING); 125 } 126 127 task.run(); 128 } catch (InterruptedException ex) { 129 } finally { 131 if (leave) setSyncOp(NOP); 132 } 133 } 134 135 138 protected final Node.Cookie getCookie(Class klass) { 139 return getDO().getCookie(klass); 140 } 141 142 145 protected final DataObject getDO() { 146 return dobj; 147 } 148 149 150 152 156 public abstract void addRepresentation(Representation rep); 157 158 162 public abstract void removeRepresentation(Representation rep); 163 164 167 protected void representationChanged(Class type) { 168 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("" + System.identityHashCode(this) + " entering synchronizator: " + type); 170 if (isInSync()) return; 172 try { 173 setSyncOp(JUST_SYNCHRONIZING); 174 175 Representation master = null; 176 Representation all[] = getRepresentations(null); 177 178 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("\tReps: " + Arrays.asList(all)); 180 182 Vector modified = new Vector (); 183 for (int i=0; i<all.length; i++) { 184 if (all[i].represents(type)) { 185 master = all[i]; 186 modified.add(all[i]); 187 continue; 188 } 189 190 if (all[i].isModified()) { 191 modified.add(all[i]); 192 } 193 } 194 195 if (modified.size() > 1) { 196 master = selectMasterRepresentation((Representation[])modified.toArray(new Representation[0])); 197 } 198 199 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("\t" + System.identityHashCode(this) + " master: " + master); 201 if (master == null) return; 202 203 205 for (int i=0; i<all.length; i++) { 206 if (all[i] == master) continue; 207 208 if ( Util.THIS.isLoggable() ) Util.THIS.debug("\tpreffered update class: " + all[i].getUpdateClass()); 211 Object change = master.getChange(all[i].getUpdateClass()); 212 if (change == null) change = master.getChange(null); 213 214 if (change != null) { 215 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("\t" + System.identityHashCode(this) + " updating: " + all[i] + " with:" + change.getClass()); 217 all[i].update(change); 218 } 219 } 220 } finally { 221 setSyncOp(NOP); 222 } 223 } 224 225 226 231 protected abstract Representation[] getRepresentations(); 232 233 237 protected Representation[] getRepresentations(Class type) { 238 239 Representation[] all = getRepresentations(); 240 if (type == null) { 241 return all; 242 } else { 243 throw new RuntimeException ("Not Implemened."); } 245 } 246 247 248 249 253 protected Representation selectMasterRepresentation(Representation[] choices){ 254 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("COMODIFICATION:"); 259 return choices[0]; 260 } 261 262 263 264 private static class SyncSupportLock { 265 } 266 267 } 268 | Popular Tags |