1 19 package org.netbeans.modules.localhistory; 20 21 import java.util.HashMap ; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.HashSet ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import org.netbeans.modules.localhistory.store.LocalHistoryStore; 28 import org.netbeans.modules.versioning.spi.VCSInterceptor; 29 30 36 class LocalHistoryVCSInterceptor extends VCSInterceptor { 37 38 private class StorageMoveHandler { 39 private long ts = -1; 40 41 private final File from; 42 private final File to; 43 44 StorageMoveHandler(File from, File to) { 45 this.from = from; 46 this.to = to; 47 } 48 49 public void delete() { 50 getStore().fileDeleteFromMove(from, to, ts); 51 } 52 53 public void create() { 54 ts = to.lastModified(); 55 getStore().fileCreateFromMove(from, to, ts); 56 } 57 }; 58 59 private LocalHistoryStore getStore() { 60 return LocalHistory.getInstance().getLocalHistoryStore(); 61 } 62 63 private Map <String , StorageMoveHandler> moveHandlerMap; 64 65 private Set <File > toBeDeleted = new HashSet <File >(); 67 private Set <File > toBeCreated = new HashSet <File >(); 68 private Set <File > wasJustCreated = new HashSet <File >(); 69 70 71 public LocalHistoryVCSInterceptor() { 72 73 } 74 75 public boolean beforeDelete(File file) { 79 if(!accept(file)) { 80 return false; 81 } 82 toBeDeleted.add(file); storeFile(file); return false; 85 } 86 87 public void doDelete(File file) throws IOException { 88 } 90 91 public void afterDelete(File file) { 92 if(!toBeDeleted.remove(file)) { 93 return; 96 } 97 98 String key = file.getAbsolutePath(); 99 if(getMoveHandlerMap().containsKey(key)) { 100 StorageMoveHandler handler = getMoveHandlerMap().get(key); 101 try { 102 handler.delete(); 103 } finally { 104 getMoveHandlerMap().remove(key); 105 } 106 } else { 107 getStore().fileDelete(file, System.currentTimeMillis()); 108 } 109 } 110 111 115 public boolean beforeMove(final File from, final File to) { 116 if(!accept(from)) { 117 return false; 118 } 119 120 StorageMoveHandler handler = new StorageMoveHandler(from, to); 124 getMoveHandlerMap().put(to.getAbsolutePath(), handler); 125 getMoveHandlerMap().put(from.getAbsolutePath(), handler); 126 return false; 127 } 128 129 public void doMove(File from, File to) throws IOException { 130 } 132 133 public void afterMove(File from, File to) { 134 String key = to.getAbsolutePath(); 135 if(getMoveHandlerMap().containsKey(key)) { 136 StorageMoveHandler handler = getMoveHandlerMap().get(key); 137 try { 138 handler.create(); 139 handler.delete(); 140 } finally { 141 getMoveHandlerMap().remove(key); 142 getMoveHandlerMap().remove(from.getAbsolutePath()); 143 } 144 } 145 } 146 147 151 public boolean beforeCreate(File file, boolean isDirectory) { 152 toBeCreated.add(file); 153 return false; 154 } 155 156 public void doCreate(File file, boolean isDirectory) throws IOException { 157 } 159 160 public void afterCreate(File file) { 161 toBeCreated.remove(file); 162 if(file.isFile()) { 163 wasJustCreated.add(file); 165 } 166 167 String key = file.getAbsolutePath(); 168 if(getMoveHandlerMap().containsKey(key)) { 169 StorageMoveHandler handler = getMoveHandlerMap().get(key); 170 try { 171 handler.create(); 172 } finally { 173 getMoveHandlerMap().remove(key); 174 } 175 } 176 } 177 178 182 public void beforeChange(File file) { 183 if(toBeCreated.contains(file) || 184 wasJustCreated.remove(file)) 185 { 186 return; 190 } 191 if(!accept(file)) { 192 return; 193 } 194 storeFile(file); 195 } 196 197 public void afterChange(File file) { 198 wasJustCreated.remove(file); 200 } 201 202 private void storeFile(File file) { 203 getStore().fileChange(file, file.lastModified()); 204 } 205 206 private Map <String , StorageMoveHandler> getMoveHandlerMap() { 207 if(moveHandlerMap == null) { 208 moveHandlerMap = new HashMap <String , StorageMoveHandler>(); 209 } 210 return moveHandlerMap; 211 } 212 213 220 private boolean accept(File file) { 221 if(!LocalHistory.getInstance().isManaged(file)) { 222 return false; 223 } 224 return true; 225 } 226 227 } 228 | Popular Tags |