Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.spi.project.ui.support; |
43 |
|
44 |
import java.beans.PropertyChangeListener; |
45 |
import java.beans.PropertyChangeSupport; |
46 |
import java.util.ArrayList; |
47 |
import java.util.Collection; |
48 |
import java.util.Collections; |
49 |
import org.netbeans.api.annotations.common.NonNull; |
50 |
import org.netbeans.spi.project.ui.ProjectProblemsProvider; |
51 |
import org.openide.util.Parameters; |
52 |
|
53 |
/** |
54 |
* Support for {@link ProjectProblemsProvider provider of project metadata problems}. |
55 |
* <p> |
56 |
* This class is thread-safe. |
57 |
* @since 1.63 |
58 |
*/ |
59 |
public final class ProjectProblemsProviderSupport { |
60 |
|
61 |
private final PropertyChangeSupport propertyChangeSupport; |
62 |
private final Object problemsLock = new Object(); |
63 |
|
64 |
// @GuardedBy("problemsLock") |
65 |
private Collection<ProjectProblemsProvider.ProjectProblem> problems; |
66 |
// @GuardedBy("problemsLock") |
67 |
private long changeId; |
68 |
|
69 |
|
70 |
/** |
71 |
* Create a new {@code ProjectProblemsProviderSupport}. |
72 |
* |
73 |
* @param source an instance to be given as the source for events, never {@code null} |
74 |
*/ |
75 |
public ProjectProblemsProviderSupport(Object source) { |
76 |
Parameters.notNull("source", source); |
77 |
propertyChangeSupport = new PropertyChangeSupport(source); |
78 |
} |
79 |
|
80 |
/** |
81 |
* Add a listener to the listener list. |
82 |
* @param listener {@link PropertyChangeListener} to be added |
83 |
* @see PropertyChangeSupport#addPropertyChangeListener(PropertyChangeListener) |
84 |
*/ |
85 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
86 |
propertyChangeSupport.addPropertyChangeListener(listener); |
87 |
} |
88 |
|
89 |
/** |
90 |
* Remove a listener from the listener list. |
91 |
* @param listener {@link PropertyChangeListener} to be removed |
92 |
* @see PropertyChangeSupport#removePropertyChangeListener(PropertyChangeListener) |
93 |
*/ |
94 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
95 |
propertyChangeSupport.removePropertyChangeListener(listener); |
96 |
} |
97 |
|
98 |
/** |
99 |
* Get project problems using the given {@link ProblemsCollector problems collector}. |
100 |
* @param problemsCollector {@link ProblemsCollector problems collector} to be used, never {@code null} |
101 |
* @return collection of project problems, never {@code null} |
102 |
*/ |
103 |
@NonNull |
104 |
public Collection<? extends ProjectProblemsProvider.ProjectProblem> getProblems(@NonNull ProblemsCollector problemsCollector) { |
105 |
Parameters.notNull("problemsCollector", problemsCollector); |
106 |
|
107 |
Collection<ProjectProblemsProvider.ProjectProblem> currentProblems; |
108 |
long currentChangeId; |
109 |
synchronized (problemsLock) { |
110 |
currentProblems = problems; |
111 |
currentChangeId = changeId; |
112 |
} |
113 |
if (currentProblems != null) { |
114 |
return currentProblems; |
115 |
} |
116 |
currentProblems = new ArrayList<ProjectProblemsProvider.ProjectProblem>(); |
117 |
currentProblems.addAll(problemsCollector.collectProblems()); |
118 |
if (currentProblems.isEmpty()) { |
119 |
currentProblems = Collections.<ProjectProblemsProvider.ProjectProblem>emptySet(); |
120 |
} |
121 |
synchronized (problemsLock) { |
122 |
if (currentChangeId == changeId) { |
123 |
problems = currentProblems; |
124 |
} else if (problems != null) { |
125 |
currentProblems = problems; |
126 |
} |
127 |
} |
128 |
assert currentProblems != null; |
129 |
return currentProblems; |
130 |
} |
131 |
|
132 |
/** |
133 |
* Fire {@link ProjectProblemsProvider#PROP_PROBLEMS project problems property} change event to all registered listeners. |
134 |
*/ |
135 |
public void fireProblemsChange() { |
136 |
synchronized (problemsLock) { |
137 |
problems = null; |
138 |
changeId++; |
139 |
} |
140 |
propertyChangeSupport.firePropertyChange(ProjectProblemsProvider.PROP_PROBLEMS, null, null); |
141 |
} |
142 |
|
143 |
//~ Inner classes |
144 |
|
145 |
/** |
146 |
* Collector of current project problems. |
147 |
*/ |
148 |
public interface ProblemsCollector { |
149 |
|
150 |
/** |
151 |
* Collect current project problems. |
152 |
* @return list of current project problems, can be empty but never {@code null} |
153 |
*/ |
154 |
@NonNull |
155 |
Collection<? extends ProjectProblemsProvider.ProjectProblem> collectProblems(); |
156 |
|
157 |
} |
158 |
|
159 |
} |