KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > GlobalVisibilityQueryImpl


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.modules.masterfs;
21
22 import java.util.prefs.PreferenceChangeEvent JavaDoc;
23 import java.util.prefs.PreferenceChangeListener JavaDoc;
24 import org.netbeans.spi.queries.VisibilityQueryImplementation;
25 import org.openide.filesystems.FileObject;
26 import javax.swing.event.ChangeEvent JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.prefs.Preferences JavaDoc;
32 import java.util.regex.Pattern JavaDoc;
33 import org.openide.util.NbPreferences;
34
35 // XXX - one would expect this class in core but there is problem that
36
//core can't depened on project/queries at the moment
37

38 /**
39  *
40  * Implemenent VisibilityQueryImplementation based on regular expression provided
41  * by users via property in IDESettings with property name IDESettings.PROP_IGNORED_FILES
42  * in Tools/Options.
43  *
44  * This class has hidden dependency on IDESettings in module org.netbeans.core.
45  */

46 public class GlobalVisibilityQueryImpl implements VisibilityQueryImplementation {
47     static GlobalVisibilityQueryImpl INSTANCE;
48     private final List JavaDoc/*<ChangeListener>*/ listeners = new ArrayList JavaDoc();
49     
50     /**
51      * Keep it synchronized with IDESettings.PROP_IGNORED_FILES
52      */

53     private static final String JavaDoc PROP_IGNORED_FILES = "IgnoredFiles"; // NOI18N
54
private static Method JavaDoc mIgnoredFiles;
55     private Pattern JavaDoc ignoreFilesPattern = null;
56
57     /** Default instance for lookup. */
58     public GlobalVisibilityQueryImpl() {
59         INSTANCE = this;
60     }
61
62     private static Preferences JavaDoc getPreferences() {
63         return NbPreferences.root().node("/org/netbeans/core");
64     }
65     
66     public boolean isVisible(FileObject file) {
67         return isVisible(file.getNameExt());
68     }
69
70     boolean isVisible(final String JavaDoc fileName) {
71         Pattern JavaDoc ignoreFilesPattern = getIgnoreFilesPattern();
72         return (ignoreFilesPattern != null) ? !(ignoreFilesPattern.matcher(fileName).find()) : true;
73     }
74
75     /**
76      * Add a listener to changes.
77      * @param l a listener to add
78      */

79     public synchronized void addChangeListener(ChangeListener JavaDoc l) {
80         listeners.add(l);
81     }
82
83     /**
84      * Stop listening to changes.
85      * @param l a listener to remove
86      */

87     public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
88         listeners.remove(l);
89     }
90
91     private void fireChange() {
92         ChangeListener JavaDoc[] _listeners;
93         synchronized (this) {
94             if (listeners.isEmpty()) {
95                 return;
96             }
97             _listeners = (ChangeListener JavaDoc[]) listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
98         }
99         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
100         for (int i = 0; i < _listeners.length; i++) {
101             _listeners[i].stateChanged(ev);
102         }
103     }
104
105     private Pattern JavaDoc getIgnoreFilesPattern() {
106         if (ignoreFilesPattern == null) {
107             String JavaDoc ignoredFiles = getIgnoredFiles();
108             ignoreFilesPattern = (ignoredFiles != null && ignoredFiles.length() > 0) ? Pattern.compile(ignoredFiles) : null;
109         }
110         return ignoreFilesPattern;
111     }
112
113     protected String JavaDoc getIgnoredFiles() {
114         // XXX probably matching \.(cvsignore|svn|DS_Store) is pointless as would anyway match ^\..*$
115
String JavaDoc retval = getPreferences().get(PROP_IGNORED_FILES, "^(CVS|SCCS|vssver\\.scc|#.*#|%.*%|\\.(cvsignore|svn|DS_Store)|_svn)$|~$|^\\..*$");//NOI18N;
116
getPreferences().addPreferenceChangeListener(new PreferenceChangeListener JavaDoc() {
117             public void preferenceChange(PreferenceChangeEvent JavaDoc evt) {
118                 if (PROP_IGNORED_FILES.equals(evt.getKey())) {
119                     ignoreFilesPattern = null;
120                     fireChange();
121                 }
122                 
123             }
124         });
125         return retval;
126     }
127 }
128
Popular Tags