1 19 20 package org.netbeans.api.queries; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.HashSet ; 25 import java.util.LinkedHashSet ; 26 import java.util.List ; 27 import java.util.Set ; 28 import javax.swing.event.ChangeEvent ; 29 import javax.swing.event.ChangeListener ; 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 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 <ChangeListener > listeners = Collections.synchronizedList(new ArrayList <ChangeListener >()); 52 private Lookup.Result<VisibilityQueryImplementation> vqiResult = null; 53 private Set <VisibilityQueryImplementation> cachedVqiInstances = null; 54 55 59 public static final VisibilityQuery getDefault() { 60 return INSTANCE; 61 } 62 63 private VisibilityQuery() { 64 } 65 66 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 90 public void addChangeListener(ChangeListener l) { 91 listeners.add(l); 92 } 93 94 98 public void removeChangeListener(ChangeListener l) { 99 listeners.remove(l); 100 } 101 102 private void fireChange() { 103 ChangeListener [] _listeners; 104 synchronized (listeners) { 105 if (listeners.isEmpty()) { 106 return; 107 } 108 _listeners = listeners.toArray(new ChangeListener [listeners.size()]); 109 } 110 ChangeEvent ev = new ChangeEvent (this); 111 for (ChangeListener l : _listeners) { 112 l.stateChanged(ev); 113 } 114 } 115 116 private synchronized Set <VisibilityQueryImplementation> getVqiInstances() { 117 if (cachedVqiInstances == null) { 118 vqiResult = Lookup.getDefault().lookupResult(VisibilityQueryImplementation.class); 119 vqiResult.addLookupListener(resultListener); 120 setupChangeListeners(cachedVqiInstances, new LinkedHashSet <VisibilityQueryImplementation>(vqiResult.allInstances())); 121 } 122 return cachedVqiInstances; 123 } 124 125 private synchronized void setupChangeListeners(final Set <VisibilityQueryImplementation> oldVqiInstances, final Set <VisibilityQueryImplementation> newVqiInstances) { 126 if (oldVqiInstances != null) { 127 Set <VisibilityQueryImplementation> removed = new HashSet <VisibilityQueryImplementation>(oldVqiInstances); 128 removed.removeAll(newVqiInstances); 129 for (VisibilityQueryImplementation vqi : removed) { 130 vqi.removeChangeListener(vqiListener); 131 } 132 } 133 134 Set <VisibilityQueryImplementation> added = new HashSet <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 <VisibilityQueryImplementation>(vqiResult.allInstances())); 148 fireChange(); 149 } 150 } 151 152 private class VqiChangedListener implements ChangeListener { 153 public void stateChanged(ChangeEvent e) { 154 fireChange(); 155 } 156 } 157 158 } 159 | Popular Tags |