KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > persistence > ModuleChangeHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.windows.persistence;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.logging.Level JavaDoc;
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 JavaDoc;
31 import java.util.List JavaDoc;
32
33 /**
34  * Handler of changes in module folder. Changes can be for example
35  * generated by enabling/disabling module which defines some winsys
36  * element(s).
37  *
38  * @author Marek Slama
39  */

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     /** List of <FileObject> which contains file objects of tc refs waiting
55      * for their related settings files to be processed */

56     private List JavaDoc<FileObject> tcRefsWaitingOnSettings;
57     
58     /** Creates a new instance of ModuleChangeHandler */
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 JavaDoc exc) {
73             PersistenceManager.LOG.log(Level.WARNING,
74             "[WinSys.ModuleChangeHandler.startHandling]" // NOI18N
75
+ " Cannot get data folders.", exc); // NOI18N
76
return;
77         }
78             
79         try {
80             fs = modesModuleFolder.getFileSystem();
81         } catch (FileStateInvalidException exc) {
82             PersistenceManager.LOG.log(Level.WARNING,
83             "[WinSys.ModuleChangeHandler.startHandling]" // NOI18N
84
+ " Cannot get filesystem.", exc); // NOI18N
85
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     /** Used to detect if FileEvent event is interesting for us.
101      * @return true if event should be accepted
102      */

103     private boolean acceptEvent (FileObject fo) {
104         FileObject parent = fo.getParent();
105         if (parent == null) {
106             return false;
107         }
108         //Change of mode config file or mode folder
109
if (parent.getPath().equals(modesModuleFolder.getPath())) {
110             if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ MODE ++");
111             return true;
112         }
113         //Change of mode config file or mode folder
114
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         //Change of tcRef config file
129
if (parent.getPath().equals(modesModuleFolder.getPath())) {
130             if (DEBUG) Debug.log(ModuleChangeHandler.class, "++ tcRef ++");
131             return true;
132         }
133         //Change of tcGroup config file
134
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 JavaDoc 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             // #37529 WindowsAPI to be called from AWT thread only.
271
SwingUtilities.invokeLater(new Runnable JavaDoc() {
272                 public void run() {
273                     WindowManagerImpl.getInstance().getPersistenceObserver().modeConfigAdded(modeConfig);
274                 }
275             });
276         }
277     }
278     
279     private void addGroup (String JavaDoc 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             // #37529 WindowsAPI to be called from AWT thread only.
285
SwingUtilities.invokeLater(new Runnable JavaDoc() {
286                 public void run() {
287                     WindowManagerImpl.getInstance().getPersistenceObserver().groupConfigAdded(groupConfig);
288                 }
289             });
290         }
291     }
292     
293     /** Adds tcref if related settings file was already copied, shcedules for
294      * later processing otherwise
295      */

296     private void processTCRef (final String JavaDoc modeName, FileObject tcRefFO) {
297         FileObject compsFO = null;
298         try {
299             compsFO = PersistenceManager.getDefault().getComponentsLocalFolder();
300         } catch (IOException JavaDoc exc) {
301             PersistenceManager.LOG.log(Level.WARNING,
302             "[WinSys.ModuleChangeHandler.processTCRef]" // NOI18N
303
+ " Cannot get components folder.", exc); // NOI18N
304
return;
305         }
306         
307         FileObject localSettings = compsFO.getFileObject(tcRefFO.getName(),
308                 PersistenceManager.COMPONENT_EXT);
309         if (localSettings != null) {
310             // OK, settings file already processed, go on and add tc ref
311
addTCRef(modeName, tcRefFO.getName());
312         } else {
313             // alas, settings file not yet ready, postpone tc ref adding
314
if (tcRefsWaitingOnSettings == null) {
315                 tcRefsWaitingOnSettings = new ArrayList JavaDoc<FileObject>(5);
316             }
317             tcRefsWaitingOnSettings.add(tcRefFO);
318         }
319     }
320     
321     private void addTCRef (final String JavaDoc modeName, String JavaDoc tcRefName) {
322         if (DEBUG) Debug.log(ModuleChangeHandler.class, "addTCRef modeName:" + modeName + " tcRefName:" + tcRefName);
323         WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser();
324         List JavaDoc<String JavaDoc> tcRefNameList = new ArrayList JavaDoc<String JavaDoc>(10);
325         final TCRefConfig tcRefConfig = wmParser.addTCRef(modeName, tcRefName, tcRefNameList);
326         if (tcRefConfig != null) {
327             final String JavaDoc [] tcRefNameArray = tcRefNameList.toArray(new String JavaDoc[tcRefNameList.size()]);
328             // #37529 WindowsAPI to be called from AWT thread only.
329
SwingUtilities.invokeLater(new Runnable JavaDoc() {
330                 public void run() {
331                     WindowManagerImpl.getInstance().getPersistenceObserver().topComponentRefConfigAdded(modeName, tcRefConfig, tcRefNameArray);
332                 }
333             });
334         }
335     }
336     
337     private void addTCGroup (final String JavaDoc groupName, String JavaDoc 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             // #37529 WindowsAPI to be called from AWT thread only.
343
SwingUtilities.invokeLater(new Runnable JavaDoc() {
344                 public void run() {
345                     WindowManagerImpl.getInstance().getPersistenceObserver().topComponentGroupConfigAdded(groupName, tcGroupConfig);
346                 }
347             });
348         }
349     }
350
351     /** Copies settings file into local directory if needed and also triggers
352      * related tc ref adding if needed (if tc ref was "waiting")
353      */

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 JavaDoc exc) {
359             PersistenceManager.LOG.log(Level.WARNING,
360             "[WinSys.ModuleChangeHandler.addComponent]" // NOI18N
361
+ " Cannot copy settings files.", exc); // NOI18N
362
return;
363         }
364         // now process tc ref if it is waiting for us
365
FileObject waitingTcRef = findWaitingTcRef(fo);
366         if (waitingTcRef != null) {
367             tcRefsWaitingOnSettings.remove(waitingTcRef);
368             addTCRef(waitingTcRef.getParent().getName(), waitingTcRef.getName());
369         }
370     }
371
372     /** Finds and returns tcRef related to given settingsFo. List of "waiting"
373      * tcRefs is searched. Returns null if related tc ref is not found.
374      */

375     private FileObject findWaitingTcRef (FileObject settingsFo) {
376         if (tcRefsWaitingOnSettings == null) {
377             return null;
378         }
379         FileObject curTcRef;
380         String JavaDoc settingsName = settingsFo.getName();
381         for (Iterator JavaDoc 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 JavaDoc modeName) {
391         if (DEBUG) Debug.log(ModuleChangeHandler.class, "removeMode mo:" + modeName);
392         WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser();
393         wmParser.removeMode(modeName);
394         //Mode is not removed from model because it can already contain TCs added
395
//by user using GUI eg.D&D.
396
// #37529 WindowsAPI to be called from AWT thread only.
397
//SwingUtilities.invokeLater(new Runnable() {
398
// public void run() {
399
// WindowManagerImpl.getInstance().getPersistenceObserver().modeConfigRemoved(modeName);
400
// }
401
//});
402
}
403     
404     private void removeGroup (final String JavaDoc groupName) {
405         if (DEBUG) Debug.log(ModuleChangeHandler.class, "removeGroup group:" + groupName);
406         WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser();
407         wmParser.removeGroup(groupName);
408         // #37529 WindowsAPI to be called from AWT thread only.
409
SwingUtilities.invokeLater(new Runnable JavaDoc() {
410             public void run() {
411                 WindowManagerImpl.getInstance().getPersistenceObserver().groupConfigRemoved(groupName);
412             }
413         });
414     }
415     
416     private void removeTCRef (final String JavaDoc tcRefName) {
417         if (DEBUG) Debug.log(ModuleChangeHandler.class, "removeTCRef tcRefName:" + tcRefName);
418         WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser();
419         if (wmParser.removeTCRef(tcRefName)) {
420             // #37529 WindowsAPI to be called from AWT thread only.
421
SwingUtilities.invokeLater(new Runnable JavaDoc() {
422                 public void run() {
423                     WindowManagerImpl.getInstance().getPersistenceObserver().topComponentRefConfigRemoved(tcRefName);
424                 }
425             });
426         }
427     }
428     
429     private void removeTCGroup (final String JavaDoc groupName, final String JavaDoc 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             // #37529 WindowsAPI to be called from AWT thread only.
434
SwingUtilities.invokeLater(new Runnable JavaDoc() {
435                 public void run() {
436                     WindowManagerImpl.getInstance().getPersistenceObserver().topComponentGroupConfigRemoved(groupName, tcGroupName);
437                 }
438             });
439         }
440     }
441     
442     private void log (String JavaDoc s) {
443         Debug.log(ModuleChangeHandler.class, s);
444     }
445     
446 }
447
Popular Tags