1 19 20 package org.netbeans.core.windows.persistence; 21 22 import java.io.IOException ; 23 import java.util.Iterator ; 24 import java.util.logging.Level ; 25 import org.netbeans.core.windows.Debug; 26 import org.netbeans.core.windows.WindowManagerImpl; 27 import org.openide.filesystems.*; 28 29 import javax.swing.*; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 33 40 class ModuleChangeHandler implements FileChangeListener { 41 42 private static final boolean DEBUG = Debug.isLoggable(ModuleChangeHandler.class); 43 44 private boolean started = false; 45 46 private FileSystem fs = null; 47 48 private FileObject modesModuleFolder; 49 50 private FileObject groupsModuleFolder; 51 52 private FileObject componentsModuleFolder; 53 54 56 private List <FileObject> tcRefsWaitingOnSettings; 57 58 59 public ModuleChangeHandler() { 60 } 61 62 void startHandling () { 63 if (started) { 64 return; 65 } 66 PersistenceManager pm = PersistenceManager.getDefault(); 67 68 try { 69 modesModuleFolder = pm.getModesModuleFolder(); 70 groupsModuleFolder = pm.getGroupsModuleFolder(); 71 componentsModuleFolder = pm.getComponentsModuleFolder(); 72 } catch (IOException exc) { 73 PersistenceManager.LOG.log(Level.WARNING, 74 "[WinSys.ModuleChangeHandler.startHandling]" + " Cannot get data folders.", exc); return; 77 } 78 79 try { 80 fs = modesModuleFolder.getFileSystem(); 81 } catch (FileStateInvalidException exc) { 82 PersistenceManager.LOG.log(Level.WARNING, 83 "[WinSys.ModuleChangeHandler.startHandling]" + " Cannot get filesystem.", exc); return; 86 } 87 fs.addFileChangeListener(this); 88 started = true; 89 } 90 91 void stopHandling () { 92 if (!started) { 93 return; 94 } 95 fs.removeFileChangeListener(this); 96 fs = null; 97 started = false; 98 } 99 100 103 private boolean acceptEvent (FileObject fo) { 104 FileObject parent = fo.getParent(); 105 if (parent == null) { 106 return false; 107 } 108 if (parent.getPath().equals(modesModuleFolder.getPath())) { 110 if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ MODE ++"); 111 return true; 112 } 113 if (parent.getPath().equals(groupsModuleFolder.getPath())) { 115 if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ GROUP ++"); 116 return true; 117 } 118 119 if (parent.getPath().equals(componentsModuleFolder.getPath())) { 120 if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ COMPONENT ++"); 121 return true; 122 } 123 124 parent = parent.getParent(); 125 if (parent == null) { 126 return false; 127 } 128 if (parent.getPath().equals(modesModuleFolder.getPath())) { 130 if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ tcRef ++"); 131 return true; 132 } 133 if (parent.getPath().equals(groupsModuleFolder.getPath())) { 135 if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ tcGroup ++"); 136 return true; 137 } 138 return false; 139 } 140 141 public void fileAttributeChanged (FileAttributeEvent fe) { 142 } 143 144 public void fileChanged (FileEvent fe) { 145 } 146 147 public void fileDataCreated (FileEvent fe) { 148 FileObject fo = fe.getFile(); 149 boolean accepted = acceptEvent(fo); 150 if (!accepted) { 151 return; 152 } 153 if (DEBUG) { 154 Debug.log(ModuleChangeHandler.class, "-- fileDataCreated fo: " + fo 155 + " isFolder:" + fo.isFolder() 156 + " ACCEPTED" 157 + " th:" + Thread.currentThread().getName()); 158 if (accepted && fo.isFolder()) { 159 FileObject [] files = fo.getChildren(); 160 for (int i = 0; i < files.length; i++) { 161 System.err.println("fo[" + i + "]: " + files[i]); 162 } 163 } 164 } 165 processDataOrFolderCreated(fo); 166 } 167 168 public void fileFolderCreated (FileEvent fe) { 169 FileObject fo = fe.getFile(); 170 boolean accepted = acceptEvent(fo); 171 if (!accepted) { 172 return; 173 } 174 if (DEBUG) { 175 Debug.log(ModuleChangeHandler.class, "-- fileFolderCreated fo: " + fo 176 + " isFolder:" + fo.isFolder() 177 + " ACCEPTED" 178 + " th:" + Thread.currentThread().getName()); 179 if (accepted && fo.isFolder()) { 180 FileObject [] files = fo.getChildren(); 181 for (int i = 0; i < files.length; i++) { 182 Debug.log(ModuleChangeHandler.class, "fo[" + i + "]: " + files[i]); 183 } 184 } 185 } 186 processDataOrFolderCreated(fo); 187 } 188 189 private void processDataOrFolderCreated (FileObject fo) { 190 FileObject parent1 = fo.getParent(); 191 if (parent1.getPath().equals(modesModuleFolder.getPath())) { 192 if (!fo.isFolder() && PersistenceManager.MODE_EXT.equals(fo.getExt())) { 193 if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ process MODE ADD ++"); 194 addMode(fo.getName()); 195 } 196 } else if (parent1.getPath().equals(groupsModuleFolder.getPath())) { 197 if (!fo.isFolder() && PersistenceManager.GROUP_EXT.equals(fo.getExt())) { 198 if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ process GROUP ADD ++"); 199 addGroup(fo.getName()); 200 } 201 } else if (parent1.getPath().equals(componentsModuleFolder.getPath())) { 202 if (!fo.isFolder() && PersistenceManager.COMPONENT_EXT.equals(fo.getExt())) { 203 if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ process COMPONENT ADD ++"); 204 addComponent(fo); 205 } 206 } 207 208 209 FileObject parent2 = parent1.getParent(); 210 if (parent2.getPath().equals(modesModuleFolder.getPath())) { 211 if (!fo.isFolder() && PersistenceManager.TCREF_EXT.equals(fo.getExt())) { 212 if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ process tcRef ADD ++"); 213 processTCRef(parent1.getName(), fo); 214 } 215 } else if (parent2.getPath().equals(groupsModuleFolder.getPath())) { 216 if (!fo.isFolder() && PersistenceManager.TCGROUP_EXT.equals(fo.getExt())) { 217 if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ process tcGroup ADD ++"); 218 addTCGroup(parent1.getName(), fo.getName()); 219 } 220 } 221 } 222 223 public void fileDeleted (FileEvent fe) { 224 FileObject fo = fe.getFile(); 225 boolean accepted = acceptEvent(fo); 226 if (!accepted) { 227 return; 228 } 229 230 if (DEBUG) Debug.log(ModuleChangeHandler.class, "-- fileDeleted fo: " + fo 231 + " isFolder:" + fo.isFolder() 232 + " isValid:" + fo.isValid() 233 + " ACCEPTED" 234 + " th:" + Thread.currentThread().getName()); 235 236 FileObject parent1 = fo.getParent(); 237 if (parent1.getPath().equals(modesModuleFolder.getPath())) { 238 if (!fo.isFolder() && PersistenceManager.MODE_EXT.equals(fo.getExt())) { 239 if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ process MODE REMOVE ++"); 240 removeMode(fo.getName()); 241 } 242 } else if (parent1.getPath().equals(groupsModuleFolder.getPath())) { 243 if (!fo.isFolder() && PersistenceManager.GROUP_EXT.equals(fo.getExt())) { 244 if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ process GROUP REMOVE ++"); 245 removeGroup(fo.getName()); 246 } 247 } 248 FileObject parent2 = parent1.getParent(); 249 if (parent2.getPath().equals(modesModuleFolder.getPath())) { 250 if (!fo.isFolder() && PersistenceManager.TCREF_EXT.equals(fo.getExt())) { 251 if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ process tcRef REMOVE ++"); 252 removeTCRef(fo.getName()); 253 } 254 } else if (parent2.getPath().equals(groupsModuleFolder.getPath())) { 255 if (!fo.isFolder() && PersistenceManager.TCGROUP_EXT.equals(fo.getExt())) { 256 if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ process tcGroup REMOVE ++"); 257 removeTCGroup(parent1.getName(), fo.getName()); 258 } 259 } 260 } 261 262 public void fileRenamed (FileRenameEvent fe) { 263 } 264 265 private void addMode (String modeName) { 266 if (DEBUG) Debug.log(ModuleChangeHandler.class, "addMode" + " mo:" + modeName); 267 WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser(); 268 final ModeConfig modeConfig = wmParser.addMode(modeName); 269 if (modeConfig != null) { 270 SwingUtilities.invokeLater(new Runnable () { 272 public void run() { 273 WindowManagerImpl.getInstance().getPersistenceObserver().modeConfigAdded(modeConfig); 274 } 275 }); 276 } 277 } 278 279 private void addGroup (String groupName) { 280 if (DEBUG) Debug.log(ModuleChangeHandler.class, "addGroup group:" + groupName); 281 WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser(); 282 final GroupConfig groupConfig = wmParser.addGroup(groupName); 283 if (groupConfig != null) { 284 SwingUtilities.invokeLater(new Runnable () { 286 public void run() { 287 WindowManagerImpl.getInstance().getPersistenceObserver().groupConfigAdded(groupConfig); 288 } 289 }); 290 } 291 } 292 293 296 private void processTCRef (final String modeName, FileObject tcRefFO) { 297 FileObject compsFO = null; 298 try { 299 compsFO = PersistenceManager.getDefault().getComponentsLocalFolder(); 300 } catch (IOException exc) { 301 PersistenceManager.LOG.log(Level.WARNING, 302 "[WinSys.ModuleChangeHandler.processTCRef]" + " Cannot get components folder.", exc); return; 305 } 306 307 FileObject localSettings = compsFO.getFileObject(tcRefFO.getName(), 308 PersistenceManager.COMPONENT_EXT); 309 if (localSettings != null) { 310 addTCRef(modeName, tcRefFO.getName()); 312 } else { 313 if (tcRefsWaitingOnSettings == null) { 315 tcRefsWaitingOnSettings = new ArrayList <FileObject>(5); 316 } 317 tcRefsWaitingOnSettings.add(tcRefFO); 318 } 319 } 320 321 private void addTCRef (final String modeName, String tcRefName) { 322 if (DEBUG) Debug.log(ModuleChangeHandler.class, "addTCRef modeName:" + modeName + " tcRefName:" + tcRefName); 323 WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser(); 324 List <String > tcRefNameList = new ArrayList <String >(10); 325 final TCRefConfig tcRefConfig = wmParser.addTCRef(modeName, tcRefName, tcRefNameList); 326 if (tcRefConfig != null) { 327 final String [] tcRefNameArray = tcRefNameList.toArray(new String [tcRefNameList.size()]); 328 SwingUtilities.invokeLater(new Runnable () { 330 public void run() { 331 WindowManagerImpl.getInstance().getPersistenceObserver().topComponentRefConfigAdded(modeName, tcRefConfig, tcRefNameArray); 332 } 333 }); 334 } 335 } 336 337 private void addTCGroup (final String groupName, String tcGroupName) { 338 if (DEBUG) Debug.log(ModuleChangeHandler.class, "addTCGroup groupName:" + groupName + " tcGroupName:" + tcGroupName); 339 WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser(); 340 final TCGroupConfig tcGroupConfig = wmParser.addTCGroup(groupName, tcGroupName); 341 if (tcGroupConfig != null) { 342 SwingUtilities.invokeLater(new Runnable () { 344 public void run() { 345 WindowManagerImpl.getInstance().getPersistenceObserver().topComponentGroupConfigAdded(groupName, tcGroupConfig); 346 } 347 }); 348 } 349 } 350 351 354 private void addComponent(FileObject fo) { 355 if (DEBUG) Debug.log(ModuleChangeHandler.class, "addComponent settingsName:" + fo.getNameExt()); 356 try { 357 PersistenceManager.getDefault().copySettingsFileIfNeeded(fo); 358 } catch (IOException exc) { 359 PersistenceManager.LOG.log(Level.WARNING, 360 "[WinSys.ModuleChangeHandler.addComponent]" + " Cannot copy settings files.", exc); return; 363 } 364 FileObject waitingTcRef = findWaitingTcRef(fo); 366 if (waitingTcRef != null) { 367 tcRefsWaitingOnSettings.remove(waitingTcRef); 368 addTCRef(waitingTcRef.getParent().getName(), waitingTcRef.getName()); 369 } 370 } 371 372 375 private FileObject findWaitingTcRef (FileObject settingsFo) { 376 if (tcRefsWaitingOnSettings == null) { 377 return null; 378 } 379 FileObject curTcRef; 380 String settingsName = settingsFo.getName(); 381 for (Iterator iter = tcRefsWaitingOnSettings.iterator(); iter.hasNext(); ) { 382 curTcRef = (FileObject)iter.next(); 383 if (settingsName.equals(curTcRef.getName())) { 384 return curTcRef; 385 } 386 } 387 return null; 388 } 389 390 private void removeMode (final String modeName) { 391 if (DEBUG) Debug.log(ModuleChangeHandler.class, "removeMode mo:" + modeName); 392 WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser(); 393 wmParser.removeMode(modeName); 394 } 403 404 private void removeGroup (final String groupName) { 405 if (DEBUG) Debug.log(ModuleChangeHandler.class, "removeGroup group:" + groupName); 406 WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser(); 407 wmParser.removeGroup(groupName); 408 SwingUtilities.invokeLater(new Runnable () { 410 public void run() { 411 WindowManagerImpl.getInstance().getPersistenceObserver().groupConfigRemoved(groupName); 412 } 413 }); 414 } 415 416 private void removeTCRef (final String tcRefName) { 417 if (DEBUG) Debug.log(ModuleChangeHandler.class, "removeTCRef tcRefName:" + tcRefName); 418 WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser(); 419 if (wmParser.removeTCRef(tcRefName)) { 420 SwingUtilities.invokeLater(new Runnable () { 422 public void run() { 423 WindowManagerImpl.getInstance().getPersistenceObserver().topComponentRefConfigRemoved(tcRefName); 424 } 425 }); 426 } 427 } 428 429 private void removeTCGroup (final String groupName, final String tcGroupName) { 430 if (DEBUG) Debug.log(ModuleChangeHandler.class, "removeTCGroup groupName:" + groupName + " tcGroupName:" + tcGroupName); 431 WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser(); 432 if (wmParser.removeTCGroup(groupName, tcGroupName)) { 433 SwingUtilities.invokeLater(new Runnable () { 435 public void run() { 436 WindowManagerImpl.getInstance().getPersistenceObserver().topComponentGroupConfigRemoved(groupName, tcGroupName); 437 } 438 }); 439 } 440 } 441 442 private void log (String s) { 443 Debug.log(ModuleChangeHandler.class, s); 444 } 445 446 } 447 | Popular Tags |