KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > queries > VisibilityQuery


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.api.queries;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.LinkedHashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.event.ChangeEvent JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import org.netbeans.spi.queries.VisibilityQueryImplementation;
31 import org.openide.filesystems.FileObject;
32 import org.openide.util.Lookup;
33 import org.openide.util.LookupEvent;
34 import org.openide.util.LookupListener;
35
36 /**
37  * Determine whether files should be hidden in views presented to the user.
38  * <p>
39  * This query should be considered only as a recommendation. Particular views
40  * may decide to display all files and ignore this query.
41  * </p>
42  * @see org.netbeans.spi.queries.VisibilityQueryImplementation
43  * @author Radek Matous
44  */

45 public final class VisibilityQuery {
46     private static final VisibilityQuery INSTANCE = new VisibilityQuery();
47
48     private final ResultListener resultListener = new ResultListener();
49     private final VqiChangedListener vqiListener = new VqiChangedListener ();
50
51     private final List JavaDoc<ChangeListener JavaDoc> listeners = Collections.synchronizedList(new ArrayList JavaDoc<ChangeListener JavaDoc>());
52     private Lookup.Result<VisibilityQueryImplementation> vqiResult = null;
53     private Set JavaDoc<VisibilityQueryImplementation> cachedVqiInstances = null;
54
55     /**
56      * Get default instance of VisibilityQuery.
57      * @return instance of VisibilityQuery
58      */

59     public static final VisibilityQuery getDefault() {
60         return INSTANCE;
61     }
62
63     private VisibilityQuery() {
64     }
65
66     /**
67      * Check whether a file is recommended to be visible.
68      * Default return value is visible unless at least one VisibilityQueryImplementation
69      * provider says hidden.
70      * @param file a file which should be checked
71      * @return true if it is recommended to show this file
72      */

73     public boolean isVisible(FileObject file) {
74         boolean retVal = true;
75
76         for (VisibilityQueryImplementation vqi : getVqiInstances()) {
77             retVal = vqi.isVisible(file);
78             if (!retVal) {
79                 break;
80             }
81         }
82
83         return retVal;
84     }
85
86     /**
87      * Add a listener to changes.
88      * @param l a listener to add
89      */

90     public void addChangeListener(ChangeListener JavaDoc l) {
91         listeners.add(l);
92     }
93
94     /**
95      * Stop listening to changes.
96      * @param l a listener to remove
97      */

98     public void removeChangeListener(ChangeListener JavaDoc l) {
99         listeners.remove(l);
100     }
101
102     private void fireChange() {
103         ChangeListener JavaDoc[] _listeners;
104         synchronized (listeners) {
105             if (listeners.isEmpty()) {
106                 return;
107             }
108             _listeners = listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
109         }
110         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
111         for (ChangeListener JavaDoc l : _listeners) {
112             l.stateChanged(ev);
113         }
114     }
115
116     private synchronized Set JavaDoc<VisibilityQueryImplementation> getVqiInstances() {
117         if (cachedVqiInstances == null) {
118             vqiResult = Lookup.getDefault().lookupResult(VisibilityQueryImplementation.class);
119             vqiResult.addLookupListener(resultListener);
120             setupChangeListeners(cachedVqiInstances, new LinkedHashSet JavaDoc<VisibilityQueryImplementation>(vqiResult.allInstances()));
121         }
122         return cachedVqiInstances;
123     }
124
125     private synchronized void setupChangeListeners(final Set JavaDoc<VisibilityQueryImplementation> oldVqiInstances, final Set JavaDoc<VisibilityQueryImplementation> newVqiInstances) {
126         if (oldVqiInstances != null) {
127             Set JavaDoc<VisibilityQueryImplementation> removed = new HashSet JavaDoc<VisibilityQueryImplementation>(oldVqiInstances);
128             removed.removeAll(newVqiInstances);
129             for (VisibilityQueryImplementation vqi : removed) {
130                 vqi.removeChangeListener(vqiListener);
131             }
132         }
133
134         Set JavaDoc<VisibilityQueryImplementation> added = new HashSet JavaDoc<VisibilityQueryImplementation>(newVqiInstances);
135         if (oldVqiInstances != null) {
136             added.removeAll(oldVqiInstances);
137         }
138         for (VisibilityQueryImplementation vqi : added) {
139             vqi.addChangeListener(vqiListener);
140         }
141
142         cachedVqiInstances = newVqiInstances;
143     }
144
145     private class ResultListener implements LookupListener {
146         public void resultChanged(LookupEvent ev) {
147             setupChangeListeners(cachedVqiInstances, new LinkedHashSet JavaDoc<VisibilityQueryImplementation>(vqiResult.allInstances()));
148             fireChange();
149         }
150     }
151
152     private class VqiChangedListener implements ChangeListener JavaDoc {
153         public void stateChanged(ChangeEvent JavaDoc e) {
154             fireChange();
155         }
156     }
157
158 }
159
Popular Tags