Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 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 2011 Sun Microsystems, Inc. |
41 |
*/ |
42 |
|
43 |
/* |
44 |
* WhiteListCategoryPanel.java |
45 |
* |
46 |
* Created on 22/07/2011, 7:37:04 AM |
47 |
*/ |
48 |
package org.netbeans.modules.whitelist.project; |
49 |
|
50 |
import java.awt.Dimension; |
51 |
import java.awt.event.ActionEvent; |
52 |
import java.awt.event.ActionListener; |
53 |
import java.lang.ref.Reference; |
54 |
import java.lang.ref.WeakReference; |
55 |
import java.util.ArrayList; |
56 |
import java.util.Collection; |
57 |
import java.util.List; |
58 |
import java.util.Map; |
59 |
import java.util.WeakHashMap; |
60 |
import java.util.prefs.Preferences; |
61 |
import javax.swing.DefaultCellEditor; |
62 |
import javax.swing.JCheckBox; |
63 |
import javax.swing.JComponent; |
64 |
import javax.swing.event.TableModelListener; |
65 |
import javax.swing.table.TableModel; |
66 |
import org.netbeans.api.annotations.common.NonNull; |
67 |
import org.netbeans.api.project.Project; |
68 |
import org.netbeans.api.project.ProjectManager; |
69 |
import org.netbeans.api.project.ProjectUtils; |
70 |
import org.netbeans.api.whitelist.WhiteListQuery; |
71 |
import org.netbeans.spi.project.ui.support.ProjectCustomizer; |
72 |
import org.netbeans.spi.whitelist.WhiteListQueryImplementation; |
73 |
import org.netbeans.spi.whitelist.WhiteListQueryImplementation.UserSelectable; |
74 |
import org.openide.util.Lookup; |
75 |
import org.openide.util.NbBundle; |
76 |
import org.openide.util.lookup.Lookups; |
77 |
import org.openide.util.lookup.ProxyLookup; |
78 |
|
79 |
/** |
80 |
* |
81 |
* @author david |
82 |
*/ |
83 |
public class WhiteListCategoryPanel extends javax.swing.JPanel implements ActionListener { |
84 |
|
85 |
private static final String WHITELISTS_PATH = "org-netbeans-api-java/whitelists/"; //NOI18N |
86 |
|
87 |
private static Map<Project,Reference<WhiteListLookup>> lookupCache = |
88 |
new WeakHashMap<Project, Reference<WhiteListLookup>>(); |
89 |
|
90 |
private Project p; |
91 |
|
92 |
/** Creates new form WhiteListCategoryPanel */ |
93 |
public WhiteListCategoryPanel(Project p) { |
94 |
this.p = p; |
95 |
initComponents(); |
96 |
WhiteListsModel model = new WhiteListsModel(getTableContent()); |
97 |
jTable1.setModel(model); |
98 |
jTable1.getTableHeader().setVisible(false); |
99 |
jTable1.getTableHeader().setPreferredSize(new Dimension(0, 0)); |
100 |
jTable1.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(new JCheckBox())); |
101 |
jTable1.getColumnModel().getColumn(0).setMaxWidth(25); |
102 |
jTable1.getColumnModel().getColumn(0).setMinWidth(25); |
103 |
} |
104 |
|
105 |
private List<Desc> getTableContent() { |
106 |
List<Desc> l = new ArrayList<Desc>(); |
107 |
for (WhiteListQueryImplementation.UserSelectable impl : getUserSelectableWhiteLists()) { |
108 |
l.add(new Desc(impl, isWhiteListEnabledInProject(p, impl.getId()))); |
109 |
} |
110 |
return l; |
111 |
} |
112 |
|
113 |
public static ProjectCustomizer.CompositeCategoryProvider createWhiteListCategoryProvider(Map attrs) { |
114 |
return new Factory(); //NOI18N |
115 |
} |
116 |
|
117 |
@Override |
118 |
public void actionPerformed(ActionEvent e) { |
119 |
for (Desc d : ((WhiteListsModel)jTable1.getModel()).whitelists) { |
120 |
enableWhiteListInProject(p, d.w.getId(), d.active); |
121 |
} |
122 |
} |
123 |
|
124 |
public static class Factory implements ProjectCustomizer.CompositeCategoryProvider { |
125 |
|
126 |
private static final String CATEGORY_WHITELIST = "WhiteList"; // NOI18N |
127 |
|
128 |
public Factory() { |
129 |
} |
130 |
|
131 |
public ProjectCustomizer.Category createCategory(Lookup context) { |
132 |
Project p = context.lookup(Project.class); |
133 |
if (p == null) { |
134 |
return null; |
135 |
} |
136 |
if (!isWhiteListPanelEnabled(p) || getUserSelectableWhiteLists().size() == 0) { |
137 |
return null; |
138 |
} |
139 |
return ProjectCustomizer.Category.create( |
140 |
CATEGORY_WHITELIST, |
141 |
NbBundle.getMessage(WhiteListCategoryPanel.class, "LBL_CategoryWhitelist"), //NOI18N |
142 |
null); |
143 |
} |
144 |
|
145 |
public JComponent createComponent(ProjectCustomizer.Category category, Lookup context) { |
146 |
Project p = context.lookup(Project.class); |
147 |
assert p != null; |
148 |
WhiteListCategoryPanel customizerPanel = new WhiteListCategoryPanel(p); |
149 |
category.setStoreListener(customizerPanel); |
150 |
return customizerPanel; |
151 |
} |
152 |
} // End of Factory class |
153 |
|
154 |
public static final String PROP_WHITELIST_ENABLED = "whitelist-enabled"; |
155 |
public static final String PROP_WHITELIST = "whitelist-"; |
156 |
|
157 |
public static boolean isWhiteListPanelEnabled(@NonNull Project p) { |
158 |
Preferences prefs = ProjectUtils.getPreferences(p, WhiteListQuery.class, true); |
159 |
return prefs.getBoolean(PROP_WHITELIST_ENABLED, false); |
160 |
} |
161 |
|
162 |
public static boolean isWhiteListEnabledInProject(@NonNull Project p, @NonNull String whiteListId) { |
163 |
Preferences prefs = ProjectUtils.getPreferences(p, WhiteListQuery.class, true); |
164 |
return prefs.getBoolean(PROP_WHITELIST+whiteListId, false); |
165 |
} |
166 |
|
167 |
public static void enableWhiteListInProject(@NonNull Project p, final @NonNull String whiteListId, final boolean enable) { |
168 |
final Preferences prefs = ProjectUtils.getPreferences(p, WhiteListQuery.class, true); |
169 |
ProjectManager.mutex().writeAccess(new Runnable() { |
170 |
@Override |
171 |
public void run() { |
172 |
prefs.putBoolean(PROP_WHITELIST+whiteListId, enable); |
173 |
if (enable) { |
174 |
prefs.putBoolean(PROP_WHITELIST_ENABLED, true); |
175 |
} |
176 |
} |
177 |
}); |
178 |
synchronized (WhiteListCategoryPanel.class) { |
179 |
final Reference<WhiteListLookup> lkpRef = lookupCache.get(p); |
180 |
final WhiteListLookup lkp; |
181 |
if (lkpRef != null && (lkp=lkpRef.get())!=null) { |
182 |
lkp.updateLookup(p); |
183 |
} |
184 |
} |
185 |
} |
186 |
|
187 |
public static Collection<? extends WhiteListQueryImplementation.UserSelectable> getUserSelectableWhiteLists() { |
188 |
return Lookups.forPath(WHITELISTS_PATH).lookupResult(WhiteListQueryImplementation.UserSelectable.class).allInstances(); |
189 |
} |
190 |
|
191 |
public static Lookup getEnabledUserSelectableWhiteLists(@NonNull Project p) { |
192 |
Reference<WhiteListLookup> lkpRef = lookupCache.get(p); |
193 |
WhiteListLookup lkp; |
194 |
if (lkpRef == null || (lkp=lkpRef.get())==null) { |
195 |
lkp = new WhiteListLookup(); |
196 |
lkp.updateLookup(p); |
197 |
lookupCache.put(p,new WeakReference<WhiteListLookup>(lkp)); |
198 |
} |
199 |
return lkp; |
200 |
} |
201 |
|
202 |
/** This method is called from within the constructor to |
203 |
* initialize the form. |
204 |
* WARNING: Do NOT modify this code. The content of this method is |
205 |
* always regenerated by the Form Editor. |
206 |
*/ |
207 |
@SuppressWarnings("unchecked") |
208 |
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
209 |
private void initComponents() { |
210 |
|
211 |
jLabel1 = new javax.swing.JLabel(); |
212 |
jScrollPane1 = new javax.swing.JScrollPane(); |
213 |
jTable1 = new javax.swing.JTable(); |
214 |
jLabel2 = new javax.swing.JLabel(); |
215 |
|
216 |
jLabel1.setText(org.openide.util.NbBundle.getMessage(WhiteListCategoryPanel.class, "WhiteListCategoryPanel.jLabel1.text")); // NOI18N |
217 |
|
218 |
jScrollPane1.setViewportView(jTable1); |
219 |
|
220 |
jLabel2.setText(org.openide.util.NbBundle.getMessage(WhiteListCategoryPanel.class, "WhiteListCategoryPanel.jLabel2.text")); // NOI18N |
221 |
|
222 |
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); |
223 |
this.setLayout(layout); |
224 |
layout.setHorizontalGroup( |
225 |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
226 |
.addGroup(layout.createSequentialGroup() |
227 |
.addComponent(jLabel1) |
228 |
.addContainerGap(283, Short.MAX_VALUE)) |
229 |
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE) |
230 |
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE) |
231 |
); |
232 |
layout.setVerticalGroup( |
233 |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
234 |
.addGroup(layout.createSequentialGroup() |
235 |
.addComponent(jLabel1) |
236 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
237 |
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE) |
238 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) |
239 |
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) |
240 |
); |
241 |
}// </editor-fold>//GEN-END:initComponents |
242 |
// Variables declaration - do not modify//GEN-BEGIN:variables |
243 |
private javax.swing.JLabel jLabel1; |
244 |
private javax.swing.JLabel jLabel2; |
245 |
private javax.swing.JScrollPane jScrollPane1; |
246 |
private javax.swing.JTable jTable1; |
247 |
// End of variables declaration//GEN-END:variables |
248 |
|
249 |
private static class Desc { |
250 |
private WhiteListQueryImplementation.UserSelectable w; |
251 |
private boolean active; |
252 |
|
253 |
public Desc(UserSelectable w, boolean active) { |
254 |
this.w = w; |
255 |
this.active = active; |
256 |
} |
257 |
} |
258 |
|
259 |
private static class WhiteListsModel implements TableModel { |
260 |
|
261 |
private List<Desc> whitelists; |
262 |
|
263 |
public WhiteListsModel(List<Desc> whitelists) { |
264 |
assert whitelists.size() > 0; |
265 |
this.whitelists = new ArrayList(whitelists); |
266 |
} |
267 |
|
268 |
@Override |
269 |
public int getRowCount() { |
270 |
return whitelists.size(); |
271 |
} |
272 |
|
273 |
@Override |
274 |
public int getColumnCount() { |
275 |
return 2; |
276 |
} |
277 |
|
278 |
private String[] header = new String[]{"Enabled", "Whitelist"}; |
279 |
private Class[] headerClass = new Class[]{Boolean.class, String.class}; |
280 |
|
281 |
@Override |
282 |
public String getColumnName(int columnIndex) { |
283 |
return header[columnIndex]; |
284 |
} |
285 |
|
286 |
@Override |
287 |
public Class<?> getColumnClass(int columnIndex) { |
288 |
return headerClass[columnIndex]; |
289 |
} |
290 |
|
291 |
@Override |
292 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
293 |
return columnIndex == 0; |
294 |
} |
295 |
|
296 |
@Override |
297 |
public Object getValueAt(int rowIndex, int columnIndex) { |
298 |
Desc srd = whitelists.get(rowIndex); |
299 |
if (columnIndex == 0) { |
300 |
return Boolean.valueOf(srd.active); |
301 |
} else { |
302 |
return srd.w.getDisplayName(); |
303 |
} |
304 |
} |
305 |
|
306 |
@Override |
307 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
308 |
if (aValue instanceof Boolean) { |
309 |
whitelists.get(rowIndex).active = ((Boolean)aValue).booleanValue(); |
310 |
} |
311 |
} |
312 |
|
313 |
@Override |
314 |
public void addTableModelListener(TableModelListener l) { |
315 |
} |
316 |
|
317 |
@Override |
318 |
public void removeTableModelListener(TableModelListener l) { |
319 |
} |
320 |
|
321 |
} |
322 |
|
323 |
private static class WhiteListLookup extends ProxyLookup { |
324 |
|
325 |
public void updateLookup(final Project p) { |
326 |
final List<WhiteListQueryImplementation.UserSelectable> impls = new ArrayList<WhiteListQueryImplementation.UserSelectable>(); |
327 |
for (WhiteListQueryImplementation.UserSelectable w : |
328 |
Lookups.forPath(WHITELISTS_PATH).lookupAll(WhiteListQueryImplementation.UserSelectable.class)) { |
329 |
if (WhiteListCategoryPanel.isWhiteListEnabledInProject(p, w.getId())) { |
330 |
impls.add(w); |
331 |
} |
332 |
} |
333 |
setLookups(Lookups.fixed((UserSelectable[])impls.toArray(new UserSelectable[impls.size()]))); |
334 |
} |
335 |
} |
336 |
} |