Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2013 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 2013 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.spi.project.support.ant.ui; |
43 |
|
44 |
import java.awt.event.ActionEvent; |
45 |
import java.awt.event.ActionListener; |
46 |
import java.io.IOException; |
47 |
import javax.swing.Action; |
48 |
import javax.swing.DefaultComboBoxModel; |
49 |
import javax.swing.event.DocumentEvent; |
50 |
import javax.swing.event.DocumentListener; |
51 |
import org.netbeans.spi.project.ui.support.ProjectCustomizer; |
52 |
import org.openide.filesystems.FileAttributeEvent; |
53 |
import org.openide.filesystems.FileChangeAdapter; |
54 |
import org.openide.filesystems.FileEvent; |
55 |
import org.openide.filesystems.FileObject; |
56 |
import org.openide.filesystems.FileRenameEvent; |
57 |
import org.openide.filesystems.FileUtil; |
58 |
import org.openide.util.Exceptions; |
59 |
|
60 |
/** |
61 |
* |
62 |
* @author mkleint |
63 |
*/ |
64 |
class LicenseHeadersPanel extends javax.swing.JPanel { |
65 |
|
66 |
private static final String defaultProjectLicense = |
67 |
"<#if licenseFirst??>\n" + |
68 |
"${licenseFirst}\n" + |
69 |
"</#if>\n" + |
70 |
"${licensePrefix}Here comes the text of your license\n" + |
71 |
"${licensePrefix}Each line should be prefixed with ${licensePrefix}\n" + |
72 |
"<#if licenseLast??>\n" + |
73 |
"${licenseLast}\n" + |
74 |
"</#if>"; |
75 |
private final CustomizerUtilities.LicensePanelContentHandler handler; |
76 |
private final ProjectCustomizer.Category category; |
77 |
private final FileChangeAdapter fslistener; |
78 |
private final DocumentListener editorListener; |
79 |
/** |
80 |
* Creates new form LicenseHeadersPanel |
81 |
*/ |
82 |
LicenseHeadersPanel(ProjectCustomizer.Category category, CustomizerUtilities.LicensePanelContentHandler handler) { |
83 |
this.handler = handler; |
84 |
this.category = category; |
85 |
|
86 |
initComponents(); |
87 |
|
88 |
btnProject.setVisible(false); //how to implement browse?? |
89 |
|
90 |
|
91 |
editorListener = new DocumentListener() { |
92 |
|
93 |
@Override |
94 |
public void insertUpdate(DocumentEvent e) { |
95 |
LicenseHeadersPanel.this.handler.setProjectLicenseContent(epLicense.getText()); |
96 |
} |
97 |
|
98 |
@Override |
99 |
public void removeUpdate(DocumentEvent e) { |
100 |
LicenseHeadersPanel.this.handler.setProjectLicenseContent(epLicense.getText()); |
101 |
} |
102 |
|
103 |
@Override |
104 |
public void changedUpdate(DocumentEvent e) { |
105 |
LicenseHeadersPanel.this.handler.setProjectLicenseContent(epLicense.getText()); |
106 |
} |
107 |
}; |
108 |
|
109 |
loadGlobalLicenses(); |
110 |
initValues(); |
111 |
txtProject.getDocument().addDocumentListener(new DocumentListener() { |
112 |
|
113 |
@Override |
114 |
public void insertUpdate(DocumentEvent e) { |
115 |
changedUpdate(e); |
116 |
} |
117 |
|
118 |
@Override |
119 |
public void removeUpdate(DocumentEvent e) { |
120 |
changedUpdate(e); |
121 |
} |
122 |
|
123 |
@Override |
124 |
public void changedUpdate(DocumentEvent e) { |
125 |
LicenseHeadersPanel.this.handler.setProjectLicenseLocation(txtProject.getText().trim()); |
126 |
setTextToProjectLicense(); |
127 |
} |
128 |
}); |
129 |
|
130 |
rbGlobal.addActionListener(new ActionListener() { |
131 |
|
132 |
@Override |
133 |
public void actionPerformed(ActionEvent e) { |
134 |
txtProject.setEnabled(false); |
135 |
epLicense.setEditable(false); |
136 |
btnProject.setEnabled(false); |
137 |
comGlobal.setEnabled(true); |
138 |
LicenseHeadersPanel.this.category.setErrorMessage(null); |
139 |
epLicense.getDocument().removeDocumentListener(editorListener); |
140 |
LicenseHeadersPanel.this.handler.setProjectLicenseContent(null); |
141 |
setTextToGlobalLicense(); |
142 |
LicenseHeadersPanel.this.handler.setProjectLicenseLocation(null); |
143 |
} |
144 |
}); |
145 |
rbProject.addActionListener(new ActionListener() { |
146 |
|
147 |
@Override |
148 |
public void actionPerformed(ActionEvent e) { |
149 |
txtProject.setEnabled(true); |
150 |
epLicense.setEditable(true); |
151 |
btnProject.setEnabled(true); |
152 |
comGlobal.setEnabled(false); |
153 |
epLicense.getDocument().addDocumentListener(editorListener); |
154 |
|
155 |
LicenseHeadersPanel.this.handler.setProjectLicenseLocation(txtProject.getText().trim()); |
156 |
setTextToProjectLicense(); |
157 |
} |
158 |
|
159 |
}); |
160 |
comGlobal.addActionListener(new ActionListener() { |
161 |
@Override |
162 |
public void actionPerformed(ActionEvent e) { |
163 |
|
164 |
setTextToGlobalLicense(); |
165 |
GlobalItem item = (GlobalItem) comGlobal.getSelectedItem(); |
166 |
LicenseHeadersPanel.this.handler.setGlobalLicenseName(item.getName()); |
167 |
} |
168 |
|
169 |
}); |
170 |
FileObject root = FileUtil.getConfigFile("Templates/Licenses"); |
171 |
fslistener = new FileChangeAdapter() { |
172 |
@Override |
173 |
public void fileDataCreated(FileEvent fe) { |
174 |
reloadGlobalTemplatesCombo(); |
175 |
} |
176 |
|
177 |
@Override |
178 |
public void fileDeleted(FileEvent fe) { |
179 |
reloadGlobalTemplatesCombo(); |
180 |
} |
181 |
|
182 |
@Override |
183 |
public void fileRenamed(FileRenameEvent fe) { |
184 |
reloadGlobalTemplatesCombo(); |
185 |
} |
186 |
|
187 |
@Override |
188 |
public void fileAttributeChanged(FileAttributeEvent fe) { |
189 |
if ("displayName".equals(fe.getName())) { |
190 |
reloadGlobalTemplatesCombo(); |
191 |
} |
192 |
} |
193 |
|
194 |
@Override |
195 |
public void fileChanged(FileEvent fe) { |
196 |
if (rbGlobal.isSelected()) { |
197 |
setTextToGlobalLicense(); |
198 |
} |
199 |
} |
200 |
}; |
201 |
root.addRecursiveListener(FileUtil.weakFileChangeListener(fslistener, root)); |
202 |
|
203 |
} |
204 |
|
205 |
|
206 |
|
207 |
private void reloadGlobalTemplatesCombo() { |
208 |
category.setErrorMessage(null); |
209 |
GlobalItem item = (GlobalItem) comGlobal.getSelectedItem(); |
210 |
String selection = item != null ? item.getName() : null; |
211 |
loadGlobalLicenses(); |
212 |
if (selection != null) { |
213 |
boolean found = selectComboBoxItem(selection, rbGlobal.isSelected()); |
214 |
if (!found) { |
215 |
category.setErrorMessage("The project's license with name '" + selection + "' was not found in IDE's license headers."); |
216 |
} |
217 |
} |
218 |
} |
219 |
|
220 |
|
221 |
/** |
222 |
* This method is called from within the constructor to initialize the form. |
223 |
* WARNING: Do NOT modify this code. The content of this method is always |
224 |
* regenerated by the Form Editor. |
225 |
*/ |
226 |
@SuppressWarnings("unchecked") |
227 |
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
228 |
private void initComponents() { |
229 |
|
230 |
buttonGroup1 = new javax.swing.ButtonGroup(); |
231 |
rbGlobal = new javax.swing.JRadioButton(); |
232 |
rbProject = new javax.swing.JRadioButton(); |
233 |
comGlobal = new javax.swing.JComboBox(); |
234 |
btnGlobal = new javax.swing.JButton(); |
235 |
txtProject = new javax.swing.JTextField(); |
236 |
btnProject = new javax.swing.JButton(); |
237 |
lblLicense = new javax.swing.JLabel(); |
238 |
jScrollPane2 = new javax.swing.JScrollPane(); |
239 |
epLicense = new javax.swing.JEditorPane(); |
240 |
|
241 |
buttonGroup1.add(rbGlobal); |
242 |
rbGlobal.setSelected(true); |
243 |
org.openide.awt.Mnemonics.setLocalizedText(rbGlobal, org.openide.util.NbBundle.getMessage(LicenseHeadersPanel.class, "LicenseHeadersPanel.rbGlobal.text")); // NOI18N |
244 |
|
245 |
buttonGroup1.add(rbProject); |
246 |
org.openide.awt.Mnemonics.setLocalizedText(rbProject, org.openide.util.NbBundle.getMessage(LicenseHeadersPanel.class, "LicenseHeadersPanel.rbProject.text")); // NOI18N |
247 |
|
248 |
org.openide.awt.Mnemonics.setLocalizedText(btnGlobal, org.openide.util.NbBundle.getMessage(LicenseHeadersPanel.class, "LicenseHeadersPanel.btnGlobal.text")); // NOI18N |
249 |
btnGlobal.addActionListener(new java.awt.event.ActionListener() { |
250 |
public void actionPerformed(java.awt.event.ActionEvent evt) { |
251 |
btnGlobalActionPerformed(evt); |
252 |
} |
253 |
}); |
254 |
|
255 |
org.openide.awt.Mnemonics.setLocalizedText(btnProject, org.openide.util.NbBundle.getMessage(LicenseHeadersPanel.class, "LicenseHeadersPanel.btnProject.text")); // NOI18N |
256 |
|
257 |
org.openide.awt.Mnemonics.setLocalizedText(lblLicense, org.openide.util.NbBundle.getMessage(LicenseHeadersPanel.class, "LicenseHeadersPanel.lblLicense.text")); // NOI18N |
258 |
|
259 |
epLicense.setContentType("text/x-freemarker"); // NOI18N |
260 |
jScrollPane2.setViewportView(epLicense); |
261 |
|
262 |
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); |
263 |
this.setLayout(layout); |
264 |
layout.setHorizontalGroup( |
265 |
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
266 |
.add(layout.createSequentialGroup() |
267 |
.addContainerGap() |
268 |
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
269 |
.add(rbProject) |
270 |
.add(rbGlobal)) |
271 |
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
272 |
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
273 |
.add(comGlobal, 0, 221, Short.MAX_VALUE) |
274 |
.add(txtProject)) |
275 |
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
276 |
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
277 |
.add(btnGlobal) |
278 |
.add(btnProject))) |
279 |
.add(layout.createSequentialGroup() |
280 |
.add(35, 35, 35) |
281 |
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
282 |
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() |
283 |
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) |
284 |
.addContainerGap()) |
285 |
.add(layout.createSequentialGroup() |
286 |
.add(lblLicense) |
287 |
.add(0, 0, Short.MAX_VALUE)))) |
288 |
); |
289 |
|
290 |
layout.linkSize(new java.awt.Component[] {btnGlobal, btnProject}, org.jdesktop.layout.GroupLayout.HORIZONTAL); |
291 |
|
292 |
layout.setVerticalGroup( |
293 |
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
294 |
.add(layout.createSequentialGroup() |
295 |
.addContainerGap() |
296 |
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) |
297 |
.add(rbGlobal) |
298 |
.add(comGlobal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) |
299 |
.add(btnGlobal)) |
300 |
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
301 |
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) |
302 |
.add(rbProject) |
303 |
.add(txtProject, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) |
304 |
.add(btnProject)) |
305 |
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
306 |
.add(lblLicense) |
307 |
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
308 |
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE) |
309 |
.addContainerGap()) |
310 |
); |
311 |
}// </editor-fold>//GEN-END:initComponents |
312 |
|
313 |
private void btnGlobalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGlobalActionPerformed |
314 |
// TODO add your handling code here: |
315 |
Action action = FileUtil.getConfigObject("Actions/System/org-netbeans-modules-favorites-templates-TemplatesAction.instance", Action.class); |
316 |
if (action != null) { |
317 |
action.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "perform")); |
318 |
} else { |
319 |
Exceptions.printStackTrace(new Exception("Actions/System/org-netbeans-modules-favorites-templates-TemplatesAction.instance not found")); |
320 |
} |
321 |
}//GEN-LAST:event_btnGlobalActionPerformed |
322 |
|
323 |
// Variables declaration - do not modify//GEN-BEGIN:variables |
324 |
private javax.swing.JButton btnGlobal; |
325 |
private javax.swing.JButton btnProject; |
326 |
private javax.swing.ButtonGroup buttonGroup1; |
327 |
private javax.swing.JComboBox comGlobal; |
328 |
private javax.swing.JEditorPane epLicense; |
329 |
private javax.swing.JScrollPane jScrollPane2; |
330 |
private javax.swing.JLabel lblLicense; |
331 |
private javax.swing.JRadioButton rbGlobal; |
332 |
private javax.swing.JRadioButton rbProject; |
333 |
private javax.swing.JTextField txtProject; |
334 |
// End of variables declaration//GEN-END:variables |
335 |
|
336 |
|
337 |
private void loadGlobalLicenses() { |
338 |
FileObject root = FileUtil.getConfigFile("Templates/Licenses"); |
339 |
DefaultComboBoxModel<GlobalItem> model = new DefaultComboBoxModel<GlobalItem>(); |
340 |
for (FileObject fo : root.getChildren()) { |
341 |
if (fo.getAttribute("template") == null) { |
342 |
continue; |
343 |
} |
344 |
String displayName = (String) fo.getAttribute("displayName"); |
345 |
if (displayName == null) { |
346 |
displayName = fo.getName(); |
347 |
if (displayName.startsWith("license-")) { |
348 |
displayName = displayName.substring("license-".length()); |
349 |
} |
350 |
} |
351 |
model.addElement(new GlobalItem(displayName, fo)); |
352 |
} |
353 |
comGlobal.setModel(model); |
354 |
} |
355 |
|
356 |
private void setTextToGlobalLicense() { |
357 |
GlobalItem item = (GlobalItem) comGlobal.getSelectedItem(); |
358 |
if (item == null) { |
359 |
epLicense.setText(""); |
360 |
} else { |
361 |
try { |
362 |
if (item.fileObject != null) { |
363 |
epLicense.setText(item.fileObject.asText()); |
364 |
} else { |
365 |
epLicense.setText("<License header template not found for name '" + item.getName() + "'>"); |
366 |
} |
367 |
} catch (IOException ex) { |
368 |
Exceptions.printStackTrace(ex); |
369 |
} |
370 |
} |
371 |
} |
372 |
|
373 |
private void setTextToProjectLicense() { |
374 |
category.setErrorMessage(null); |
375 |
String path = txtProject.getText(); |
376 |
FileObject fo = handler.resolveProjectLocation(path); |
377 |
if (fo != null) { |
378 |
try { |
379 |
epLicense.setText(fo.asText()); |
380 |
} catch (IOException ex) { |
381 |
Exceptions.printStackTrace(ex); |
382 |
} |
383 |
} else { |
384 |
category.setErrorMessage("File at path '" + path + "' doesn't exist."); |
385 |
epLicense.setText(defaultProjectLicense); |
386 |
} |
387 |
|
388 |
} |
389 |
|
390 |
|
391 |
|
392 |
private void initValues() { |
393 |
String name = handler.getGlobalLicenseName(); |
394 |
String path = handler.getProjectLicenseLocation(); |
395 |
if (name == null) { |
396 |
name = "default"; |
397 |
} |
398 |
boolean found = selectComboBoxItem(name, true); |
399 |
if (path == null) { |
400 |
path = handler.getDefaultProjectLicenseLocation(); |
401 |
txtProject.setText(path); |
402 |
rbGlobal.setSelected(true); //has to come last |
403 |
if (!found) { |
404 |
category.setErrorMessage("The project's license with name '" + name + "' was not found in IDE's license headers."); |
405 |
} |
406 |
} else { |
407 |
txtProject.setText(path); |
408 |
rbProject.setSelected(true); //has to come last |
409 |
setTextToProjectLicense(); |
410 |
epLicense.getDocument().addDocumentListener(editorListener); |
411 |
} |
412 |
} |
413 |
|
414 |
private boolean selectComboBoxItem(String name, boolean setText) { |
415 |
boolean found = false; |
416 |
DefaultComboBoxModel model = (DefaultComboBoxModel) comGlobal.getModel(); |
417 |
for (int i = 0; i < model.getSize(); i++) { |
418 |
GlobalItem gi = (GlobalItem) model.getElementAt(i); |
419 |
if (gi.getName().equals(name)) { |
420 |
comGlobal.setSelectedItem(gi); |
421 |
found = true; |
422 |
if (setText) { |
423 |
setTextToGlobalLicense(); |
424 |
} |
425 |
break; |
426 |
} |
427 |
} |
428 |
if (!found) { |
429 |
GlobalItem itm = new GlobalItem(name, null); |
430 |
model.insertElementAt(itm, 0); |
431 |
comGlobal.setSelectedItem(itm); |
432 |
if (setText) { |
433 |
setTextToGlobalLicense(); |
434 |
} |
435 |
} |
436 |
return found; |
437 |
} |
438 |
|
439 |
|
440 |
private class GlobalItem { |
441 |
|
442 |
final String displayName; |
443 |
final FileObject fileObject; |
444 |
|
445 |
public GlobalItem(String name, FileObject fileObject) { |
446 |
this.displayName = name; |
447 |
this.fileObject = fileObject; |
448 |
} |
449 |
|
450 |
public String getName() { |
451 |
if (fileObject == null) { |
452 |
return displayName; |
453 |
} |
454 |
String name = fileObject.getName(); |
455 |
if (name.startsWith("license-")) { |
456 |
name = name.substring("license-".length()); |
457 |
} |
458 |
return name; |
459 |
} |
460 |
|
461 |
@Override |
462 |
public String toString() { |
463 |
return displayName; |
464 |
} |
465 |
} |
466 |
|
467 |
|
468 |
|
469 |
} |