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.modules.tasklist.impl;
043:
044: import java.beans.PropertyChangeListener;
045: import java.beans.PropertyChangeSupport;
046: import java.util.ArrayList;
047: import java.util.List;
048: import org.netbeans.spi.tasklist.FileTaskScanner;
049: import org.netbeans.spi.tasklist.PushTaskScanner;
050: import org.openide.util.Lookup;
051: import org.openide.util.LookupEvent;
052: import org.openide.util.LookupListener;
053: import org.openide.util.lookup.Lookups;
054:
055: /**
056: *
057: * @author S. Aubrecht
058: */
059: public final class ScannerList<T> {
060:
061: public static final String PROP_TASK_SCANNERS = "TaskScannerList"; //NOI18N
062:
063: private static final String SCANNER_LIST_PATH = "TaskList/Scanners"; //NOI18N
064:
065: private static ScannerList<FileTaskScanner> fileInstance;
066: private static ScannerList<PushTaskScanner> pushInstance;
067:
068: private PropertyChangeSupport propertySupport;
069:
070: private Lookup.Result<T> lkpResult;
071:
072: private List<T> scanners;
073: private Class<T> clazz;
074:
075: /** Creates a new instance of ProviderList */
076: private ScannerList(Class<T> clazz) {
077: this .clazz = clazz;
078: }
079:
080: public static ScannerList<FileTaskScanner> getFileScannerList() {
081: if (null == fileInstance) {
082: fileInstance = new ScannerList<FileTaskScanner>(
083: FileTaskScanner.class);
084: }
085: return fileInstance;
086: }
087:
088: public static ScannerList<PushTaskScanner> getPushScannerList() {
089: if (null == pushInstance) {
090: pushInstance = new ScannerList<PushTaskScanner>(
091: PushTaskScanner.class);
092: }
093: return pushInstance;
094: }
095:
096: public List<? extends T> getScanners() {
097: init();
098: return scanners;
099: }
100:
101: // private Class<? extends T> getCheckedClass() {
102: // return T.class;
103: // }
104:
105: private void init() {
106: if (null == scanners) {
107: if (null == lkpResult) {
108: lkpResult = initLookup();
109: lkpResult.addLookupListener(new LookupListener() {
110: public void resultChanged(LookupEvent ev) {
111: scanners = null;
112: firePropertyChange();
113: }
114: });
115: }
116: scanners = new ArrayList<T>(lkpResult.allInstances());
117: }
118: }
119:
120: public void addPropertyChangeListener(PropertyChangeListener pcl) {
121: if (null == propertySupport)
122: propertySupport = new PropertyChangeSupport(this );
123: propertySupport.addPropertyChangeListener(pcl);
124: }
125:
126: public void removePropertyChangeListener(PropertyChangeListener pcl) {
127: if (null != propertySupport)
128: propertySupport.removePropertyChangeListener(pcl);
129: }
130:
131: private void firePropertyChange() {
132: if (null != propertySupport) {
133: propertySupport.firePropertyChange(PROP_TASK_SCANNERS,
134: null, getScanners());
135: }
136: }
137:
138: private Lookup.Result<T> initLookup() {
139: Lookup lkp = Lookups.forPath(SCANNER_LIST_PATH);
140:
141: Lookup.Template<T> template = new Lookup.Template<T>(clazz);
142: Lookup.Result<T> res = lkp.lookup(template);
143: return res;
144: }
145: }
|