001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.api.queries;
043:
044: import java.util.ArrayList;
045: import java.util.HashSet;
046: import java.util.List;
047: import java.util.Set;
048: import javax.swing.event.ChangeEvent;
049: import javax.swing.event.ChangeListener;
050: import org.netbeans.spi.queries.VisibilityQueryImplementation;
051: import org.openide.filesystems.FileObject;
052: import org.openide.util.ChangeSupport;
053: import org.openide.util.Lookup;
054: import org.openide.util.LookupEvent;
055: import org.openide.util.LookupListener;
056:
057: /**
058: * Determine whether files should be hidden in views presented to the user.
059: * <p>
060: * This query should be considered only as a recommendation. Particular views
061: * may decide to display all files and ignore this query.
062: * </p>
063: * @see org.netbeans.spi.queries.VisibilityQueryImplementation
064: * @author Radek Matous
065: */
066: public final class VisibilityQuery {
067: private static final VisibilityQuery INSTANCE = new VisibilityQuery();
068:
069: private final ResultListener resultListener = new ResultListener();
070: private final VqiChangedListener vqiListener = new VqiChangedListener();
071:
072: private final ChangeSupport changeSupport = new ChangeSupport(this );
073: private Lookup.Result<VisibilityQueryImplementation> vqiResult = null;
074: private List<VisibilityQueryImplementation> cachedVqiInstances = null;
075:
076: /**
077: * Get default instance of VisibilityQuery.
078: * @return instance of VisibilityQuery
079: */
080: public static final VisibilityQuery getDefault() {
081: return INSTANCE;
082: }
083:
084: private VisibilityQuery() {
085: }
086:
087: /**
088: * Check whether a file is recommended to be visible.
089: * Default return value is visible unless at least one VisibilityQueryImplementation
090: * provider says hidden.
091: * @param file a file which should be checked
092: * @return true if it is recommended to show this file
093: */
094: public boolean isVisible(FileObject file) {
095: for (VisibilityQueryImplementation vqi : getVqiInstances()) {
096: if (!vqi.isVisible(file)) {
097: return false;
098: }
099: }
100: return true;
101: }
102:
103: /**
104: * Add a listener to changes.
105: * @param l a listener to add
106: */
107: public void addChangeListener(ChangeListener l) {
108: changeSupport.addChangeListener(l);
109: }
110:
111: /**
112: * Stop listening to changes.
113: * @param l a listener to remove
114: */
115: public void removeChangeListener(ChangeListener l) {
116: changeSupport.removeChangeListener(l);
117: }
118:
119: private synchronized List<VisibilityQueryImplementation> getVqiInstances() {
120: if (cachedVqiInstances == null) {
121: vqiResult = Lookup.getDefault().lookupResult(
122: VisibilityQueryImplementation.class);
123: vqiResult.addLookupListener(resultListener);
124: setupChangeListeners(null,
125: new ArrayList<VisibilityQueryImplementation>(
126: vqiResult.allInstances()));
127: }
128: return cachedVqiInstances;
129: }
130:
131: private synchronized void setupChangeListeners(
132: final List<VisibilityQueryImplementation> oldVqiInstances,
133: final List<VisibilityQueryImplementation> newVqiInstances) {
134: if (oldVqiInstances != null) {
135: Set<VisibilityQueryImplementation> removed = new HashSet<VisibilityQueryImplementation>(
136: oldVqiInstances);
137: removed.removeAll(newVqiInstances);
138: for (VisibilityQueryImplementation vqi : removed) {
139: vqi.removeChangeListener(vqiListener);
140: }
141: }
142:
143: Set<VisibilityQueryImplementation> added = new HashSet<VisibilityQueryImplementation>(
144: newVqiInstances);
145: if (oldVqiInstances != null) {
146: added.removeAll(oldVqiInstances);
147: }
148: for (VisibilityQueryImplementation vqi : added) {
149: vqi.addChangeListener(vqiListener);
150: }
151:
152: cachedVqiInstances = newVqiInstances;
153: }
154:
155: private class ResultListener implements LookupListener {
156: public void resultChanged(LookupEvent ev) {
157: setupChangeListeners(cachedVqiInstances,
158: new ArrayList<VisibilityQueryImplementation>(
159: vqiResult.allInstances()));
160: changeSupport.fireChange();
161: }
162: }
163:
164: private class VqiChangedListener implements ChangeListener {
165: public void stateChanged(ChangeEvent e) {
166: changeSupport.fireChange();
167: }
168: }
169:
170: }
|