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-2007 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: package org.netbeans.modules.java.hints.infrastructure;
042:
043: import org.netbeans.modules.java.hints.infrastructure.LazyHintComputationFactory;
044: import com.sun.source.util.TreePath;
045: import java.beans.PropertyChangeListener;
046: import java.beans.PropertyChangeSupport;
047: import java.util.ArrayList;
048: import java.util.Collection;
049: import java.util.Collections;
050: import java.util.LinkedList;
051: import java.util.List;
052: import java.util.Map;
053: import java.util.concurrent.atomic.AtomicBoolean;
054: import org.netbeans.api.java.source.CompilationInfo;
055: import org.netbeans.modules.java.hints.spi.ErrorRule;
056: import org.netbeans.modules.java.hints.spi.ErrorRule.Data;
057: import org.netbeans.spi.editor.hints.Fix;
058: import org.netbeans.spi.editor.hints.LazyFixList;
059: import org.openide.filesystems.FileObject;
060:
061: /**
062: *
063: * @author Jan Lahoda
064: */
065: public class CreatorBasedLazyFixList implements LazyFixList {
066:
067: private PropertyChangeSupport pcs;
068: private boolean computed;
069: private boolean computing;
070: private List<Fix> fixes;
071:
072: private FileObject file;
073: private String diagnosticKey;
074: private int offset;
075: private final Collection<ErrorRule> c;
076: private final Map<Class, Data> class2Data;
077:
078: /** Creates a new instance of CreatorBasedLazyFixList */
079: public CreatorBasedLazyFixList(FileObject file,
080: String diagnosticKey, int offset, Collection<ErrorRule> c,
081: Map<Class, Data> class2Data) {
082: this .pcs = new PropertyChangeSupport(this );
083: this .file = file;
084: this .diagnosticKey = diagnosticKey;
085: this .offset = offset;
086: this .c = c;
087: this .class2Data = class2Data;
088: this .fixes = Collections.<Fix> emptyList();
089: }
090:
091: public void addPropertyChangeListener(PropertyChangeListener l) {
092: pcs.addPropertyChangeListener(l);
093: }
094:
095: public void removePropertyChangeListener(PropertyChangeListener l) {
096: pcs.removePropertyChangeListener(l);
097: }
098:
099: public boolean probablyContainsFixes() {
100: return true;
101: }
102:
103: public synchronized List<Fix> getFixes() {
104: if (!computed && !computing) {
105: LazyHintComputationFactory.addToCompute(file, this );
106: computing = true;
107: }
108: return fixes;
109: }
110:
111: public synchronized boolean isComputed() {
112: return computed;
113: }
114:
115: private ErrorRule<?> currentRule;
116:
117: private synchronized void setCurrentRule(ErrorRule currentRule) {
118: this .currentRule = currentRule;
119: }
120:
121: public void compute(CompilationInfo info, AtomicBoolean cancelled) {
122: synchronized (this ) {
123: //resume:
124: if (this .computed) {
125: return; //already done.
126: }
127: }
128:
129: List<Fix> fixes = new ArrayList<Fix>();
130: TreePath path = info.getTreeUtilities().pathFor(offset + 1);
131:
132: for (ErrorRule rule : c) {
133: if (cancelled.get()) {
134: //has been canceled, the computation was not finished:
135: return;
136: }
137:
138: setCurrentRule(rule);
139:
140: try {
141: Data data = class2Data.get(rule.getClass());
142:
143: if (data == null) {
144: class2Data.put(rule.getClass(), data = new Data());
145: }
146:
147: List<Fix> currentRuleFixes = rule.run(info,
148: diagnosticKey, offset, path, data);
149:
150: if (currentRuleFixes == CANCELLED) {
151: cancelled.set(true);
152: return;
153: }
154:
155: if (currentRuleFixes != null) {
156: fixes.addAll(currentRuleFixes);
157: }
158: } finally {
159: setCurrentRule(null);
160: }
161: }
162:
163: if (cancelled.get()) {
164: //has been canceled, the computation was not finished:
165: return;
166: }
167:
168: synchronized (this ) {
169: this .fixes = fixes;
170: this .computed = true;
171: }
172:
173: pcs.firePropertyChange(PROP_FIXES, null, null);
174: pcs.firePropertyChange(PROP_COMPUTED, null, null);
175: }
176:
177: public void cancel() {
178: synchronized (this ) {
179: if (currentRule != null) {
180: currentRule.cancel();
181: }
182: }
183: }
184:
185: public static final List<Fix> CANCELLED = Collections
186: .unmodifiableList(new LinkedList<Fix>());
187:
188: }
|