2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.java.freeform.ui;
44
import java.awt.event.ItemEvent;
46
import java.util.ArrayList;
47
import java.util.List;
48
import javax.swing.DefaultListModel;
49
import javax.swing.JFileChooser;
50
import javax.swing.ListSelectionModel;
51
import javax.swing.event.ChangeEvent;
52
import javax.swing.event.ChangeListener;
53
import javax.swing.event.DocumentEvent;
54
import org.netbeans.modules.ant.freeform.spi.support.Util;
55
import org.netbeans.modules.java.freeform.JavaProjectGenerator;
56
import org.openide.filesystems.FileUtil;
57
import org.openide.util.HelpCtx;
58
import org.openide.util.NbBundle;
62
* @author David Konecny
64
public class OutputPanel extends javax.swing.JPanel implements HelpCtx.Provider {
66
private DefaultListModel listModel;
67
private File lastChosenFile = null;
68
private boolean isSeparateClasspath = true;
69
private List<ProjectModel.CompilationUnitKey> compUnitsKeys;
70
private boolean ignoreEvent;
71
private ProjectModel model;
73
public OutputPanel() {
75
jTextArea1.setBackground(getBackground());
76
listModel = new DefaultListModel();
77
output.setModel(listModel);
78
// XXX: for now only single selection
79
output.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
80
javadoc.getDocument().addDocumentListener(new javax.swing.event.DocumentListener () {
81
public void insertUpdate(DocumentEvent e) {
85
public void removeUpdate(DocumentEvent e) {
89
public void changedUpdate(DocumentEvent e) {
93
jTextArea1.setDisabledTextColor(jLabel2.getForeground());
96
private void update() {
97
int index = sourceFolder.getSelectedIndex();
99
ProjectModel.CompilationUnitKey key = compUnitsKeys.get(index);
100
JavaProjectGenerator.JavaCompilationUnit cu = model.getCompilationUnit(key, model.isTestSourceFolder(index));
101
updateCompilationUnitJavadoc(cu);
105
public HelpCtx getHelpCtx() {
106
return new HelpCtx( OutputPanel.class );
109
private void updateControls() {
110
sourceFolder.removeAllItems();
111
compUnitsKeys = model.createCompilationUnitKeys();
112
isSeparateClasspath = !ProjectModel.isSingleCompilationUnit(compUnitsKeys);
113
List<String> names = ClasspathPanel.createComboContent(compUnitsKeys, model.getEvaluator(), model.getNBProjectFolder());
114
for (String nm : names) {
115
sourceFolder.addItem(nm);
117
if (names.size() > 0) {
119
sourceFolder.setSelectedIndex(0);
125
// enable/disable "Separate Classpath" checkbox
126
boolean sepClasspath = model.canHaveSeparateClasspath();
127
jLabel2.setEnabled(sepClasspath && isSeparateClasspath);
128
sourceFolder.setEnabled(sepClasspath && isSeparateClasspath);
130
// disable ouput panel and Add Output button if there is
131
// no compilation unit ot be configured
132
addOutput.setEnabled(compUnitsKeys.size() > 0);
133
output.setEnabled(compUnitsKeys.size() > 0);
134
javadoc.setEnabled(compUnitsKeys.size() > 0);
135
javadocBrowse.setEnabled(compUnitsKeys.size() > 0);
139
/** This method is called from within the constructor to
140
* initialize the form.
141
* WARNING: Do NOT modify this code. The content of this method is
142
* always regenerated by the Form Editor.
144
private void initComponents() {//GEN-BEGIN:initComponents
145
java.awt.GridBagConstraints gridBagConstraints;
147
jLabel2 = new javax.swing.JLabel();
148
jLabel3 = new javax.swing.JLabel();
149
addOutput = new javax.swing.JButton();
150
removeOutput = new javax.swing.JButton();
151
jScrollPane1 = new javax.swing.JScrollPane();
152
output = new javax.swing.JList();
153
jPanel1 = new javax.swing.JPanel();
154
sourceFolder = new javax.swing.JComboBox();
155
jTextArea1 = new javax.swing.JTextArea();
156
jPanel2 = new javax.swing.JPanel();
157
javadoc = new javax.swing.JTextField();
158
javadocBrowse = new javax.swing.JButton();
159
javadocLabel = new javax.swing.JLabel();
161
setLayout(new java.awt.GridBagLayout());
163
setPreferredSize(new java.awt.Dimension(275, 202));
164
jLabel2.setLabelFor(sourceFolder);
165
org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(OutputPanel.class, "LBL_OutputPanel_jLabel1"));
166
gridBagConstraints = new java.awt.GridBagConstraints();
167
gridBagConstraints.gridx = 0;
168
gridBagConstraints.gridy = 2;
169
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
170
gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
171
add(jLabel2, gridBagConstraints);
173
jLabel3.setLabelFor(output);
174
org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(OutputPanel.class, "LBL_OutputPanel_jLabel3"));
175
gridBagConstraints = new java.awt.GridBagConstraints();
176
gridBagConstraints.gridx = 0;
177
gridBagConstraints.gridy = 3;
178
gridBagConstraints.gridwidth = 2;
179
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
180
gridBagConstraints.insets = new java.awt.Insets(11, 0, 0, 0);
181
add(jLabel3, gridBagConstraints);
182
jLabel3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(OutputPanel.class, "ACSD_OutputPanel_jLabel3"));
184
org.openide.awt.Mnemonics.setLocalizedText(addOutput, org.openide.util.NbBundle.getMessage(OutputPanel.class, "BTN_OutputPanel_addOutput"));
185
addOutput.addActionListener(new java.awt.event.ActionListener() {
186
public void actionPerformed(java.awt.event.ActionEvent evt) {
187
addOutputActionPerformed(evt);
191
gridBagConstraints = new java.awt.GridBagConstraints();
192
gridBagConstraints.gridx = 2;
193
gridBagConstraints.gridy = 4;
194
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
195
gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
196
add(addOutput, gridBagConstraints);
197
addOutput.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(OutputPanel.class, "ACSD_OutputPanel_addOutput"));
199
org.openide.awt.Mnemonics.setLocalizedText(removeOutput, org.openide.util.NbBundle.getMessage(OutputPanel.class, "BTN_OutputPanel_removeOutput"));
200
removeOutput.addActionListener(new java.awt.event.ActionListener() {
201
public void actionPerformed(java.awt.event.ActionEvent evt) {
202
removeOutputActionPerformed(evt);
206
gridBagConstraints = new java.awt.GridBagConstraints();
207
gridBagConstraints.gridx = 2;
208
gridBagConstraints.gridy = 5;
209
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
210
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
211
gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
212
add(removeOutput, gridBagConstraints);
213
removeOutput.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(OutputPanel.class, "ACSD_OutputPanel_removeOutput"));
215
output.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
216
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
217
outputValueChanged(evt);
221
jScrollPane1.setViewportView(output);
222
output.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(OutputPanel.class, "ACSD_OutputPanel_output"));
224
gridBagConstraints = new java.awt.GridBagConstraints();
225
gridBagConstraints.gridx = 0;
226
gridBagConstraints.gridy = 4;
227
gridBagConstraints.gridwidth = 2;
228
gridBagConstraints.gridheight = 4;
229
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
230
gridBagConstraints.weightx = 1.0;
231
gridBagConstraints.weighty = 1.0;
232
gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
233
add(jScrollPane1, gridBagConstraints);
234
jScrollPane1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(OutputPanel.class, "ACSD_OutputPanel_jScrollPanel1"));
236
jPanel1.setLayout(new java.awt.GridBagLayout());
238
sourceFolder.addItemListener(new java.awt.event.ItemListener() {
239
public void itemStateChanged(java.awt.event.ItemEvent evt) {
240
sourceFolderItemStateChanged(evt);
244
gridBagConstraints = new java.awt.GridBagConstraints();
245
gridBagConstraints.gridx = 0;
246
gridBagConstraints.gridy = 0;
247
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
248
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
249
gridBagConstraints.weightx = 1.0;
250
jPanel1.add(sourceFolder, gridBagConstraints);
251
sourceFolder.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/java/freeform/ui/Bundle").getString("AD_OutputPanel_sourceFolder"));
253
gridBagConstraints = new java.awt.GridBagConstraints();
254
gridBagConstraints.gridx = 1;
255
gridBagConstraints.gridy = 2;
256
gridBagConstraints.gridwidth = 2;
257
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
258
add(jPanel1, gridBagConstraints);
260
jTextArea1.setEditable(false);
261
jTextArea1.setLineWrap(true);
262
jTextArea1.setText(org.openide.util.NbBundle.getMessage(OutputPanel.class, "MSG_OutputPanel_jTextArea1"));
263
jTextArea1.setWrapStyleWord(true);
264
jTextArea1.setEnabled(false);
265
gridBagConstraints = new java.awt.GridBagConstraints();
266
gridBagConstraints.gridwidth = 3;
267
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
268
gridBagConstraints.insets = new java.awt.Insets(0, 0, 11, 0);
269
add(jTextArea1, gridBagConstraints);
270
jTextArea1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(OutputPanel.class, "ACSN_OutputPanel_jTextArea1"));
271
jTextArea1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(OutputPanel.class, "ACSD_OutputPanel_jTextArea1"));
273
jPanel2.setLayout(new java.awt.GridBagLayout());
275
gridBagConstraints = new java.awt.GridBagConstraints();
276
gridBagConstraints.gridx = 1;
277
gridBagConstraints.gridy = 0;
278
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
279
gridBagConstraints.weightx = 1.0;
280
gridBagConstraints.weighty = 1.0;
281
gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
282
jPanel2.add(javadoc, gridBagConstraints);
283
javadoc.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/java/freeform/ui/Bundle").getString("AD_OutputPanel_javadoc"));
285
org.openide.awt.Mnemonics.setLocalizedText(javadocBrowse, org.openide.util.NbBundle.getMessage(OutputPanel.class, "BTN_OutputPanel_browseJavadoc"));
286
javadocBrowse.addActionListener(new java.awt.event.ActionListener() {
287
public void actionPerformed(java.awt.event.ActionEvent evt) {
288
javadocBrowseActionPerformed(evt);
292
gridBagConstraints = new java.awt.GridBagConstraints();
293
gridBagConstraints.gridx = 2;
294
gridBagConstraints.gridy = 0;
295
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
296
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
297
jPanel2.add(javadocBrowse, gridBagConstraints);
298
javadocBrowse.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/java/freeform/ui/Bundle").getString("AD_OutputPanel_javadocBrowse"));
300
javadocLabel.setLabelFor(javadoc);
301
org.openide.awt.Mnemonics.setLocalizedText(javadocLabel, org.openide.util.NbBundle.getMessage(OutputPanel.class, "LBL_OutputPanel_JavadocLabel"));
302
gridBagConstraints = new java.awt.GridBagConstraints();
303
gridBagConstraints.gridx = 0;
304
gridBagConstraints.gridy = 0;
305
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
306
gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
307
jPanel2.add(javadocLabel, gridBagConstraints);
309
gridBagConstraints = new java.awt.GridBagConstraints();
310
gridBagConstraints.gridx = 0;
311
gridBagConstraints.gridy = 8;
312
gridBagConstraints.gridwidth = 3;
313
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
314
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
315
gridBagConstraints.insets = new java.awt.Insets(11, 0, 0, 0);
316
add(jPanel2, gridBagConstraints);
318
}//GEN-END:initComponents
320
private void javadocBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_javadocBrowseActionPerformed
321
JFileChooser chooser = new JFileChooser();
322
FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
323
chooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES);
324
chooser.setMultiSelectionEnabled(false);
325
if (lastChosenFile != null) {
326
chooser.setSelectedFile(lastChosenFile);
327
} else if (javadoc.getText().length() > 0) {
328
chooser.setSelectedFile(new File(javadoc.getText()));
330
File files[] = model.getBaseFolder().listFiles();
331
if (files != null && files.length > 0) {
332
chooser.setSelectedFile(files[0]);
334
chooser.setSelectedFile(model.getBaseFolder());
337
chooser.setDialogTitle(NbBundle.getMessage(OutputPanel.class, "LBL_Browse_Javadoc")); // NOI18N
338
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
339
File file = chooser.getSelectedFile();
340
file = FileUtil.normalizeFile(file);
341
javadoc.setText(file.getAbsolutePath());
342
lastChosenFile = file;
344
}//GEN-LAST:event_javadocBrowseActionPerformed
346
private void outputValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_outputValueChanged
348
}//GEN-LAST:event_outputValueChanged
350
private void sourceFolderItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_sourceFolderItemStateChanged
354
if (evt.getStateChange() == ItemEvent.DESELECTED) {
355
int index = findIndex(evt.getItem());
356
// if index == -1 then item was removed and will not be saved
363
}//GEN-LAST:event_sourceFolderItemStateChanged
365
private int findIndex(Object o) {
366
for (int i=0; i<sourceFolder.getModel().getSize(); i++) {
367
if (sourceFolder.getModel().getElementAt(i).equals(o)) {
374
/** Source package combo is changing - take output from the listbox and
375
* store it in compilaiton unit identified by the index.*/
376
private void saveOutput(int index) {
377
ProjectModel.CompilationUnitKey key = compUnitsKeys.get(index);
378
JavaProjectGenerator.JavaCompilationUnit cu = model.getCompilationUnit(key, model.isTestSourceFolder(index));
379
updateCompilationUnitOutput(cu);
380
updateCompilationUnitJavadoc(cu);
383
/** Source package has changes - find current source package and read its compilation unit and
384
* update classpath listbox with it.*/
385
private void loadOutput() {
387
if (isSeparateClasspath) {
388
index = sourceFolder.getSelectedIndex();
395
ProjectModel.CompilationUnitKey key = compUnitsKeys.get(index);
396
JavaProjectGenerator.JavaCompilationUnit cu = model.getCompilationUnit(key, model.isTestSourceFolder(index));
397
updateJListOutput(cu.output);
398
updateJavadocField(cu.javadoc);
401
private void updateJavadocField(List<String> jd) {
403
boolean enabled = true;
406
value = getListAsString(jd);
408
} else if (jd.size() == 1) {
409
File f = Util.resolveFile(model.getEvaluator(), model.getNBProjectFolder(), jd.get(0));
411
value = f.getAbsolutePath();
415
javadoc.setEnabled(enabled);
416
javadocBrowse.setEnabled(enabled);
417
javadoc.setText(value);
420
private String getListAsString(List<String> list) {
422
StringBuffer sb = new StringBuffer();
423
for (String s : list) {
424
File f = Util.resolveFile(model.getEvaluator(), model.getNBProjectFolder(), s);
427
sb.append(", "); // NOI18N
429
sb.append(f.getAbsolutePath());
432
return sb.toString();
435
private void updateCompilationUnitJavadoc(JavaProjectGenerator.JavaCompilationUnit cu) {
436
if (javadoc.isEnabled()) {
437
if (javadoc.getText().length() > 0) {
438
cu.javadoc = new ArrayList<String>();
439
for (String part : javadoc.getText().split(",")) {
440
File f = FileUtil.normalizeFile(new File(part.trim()));
441
String path = Util.relativizeLocation(model.getBaseFolder(), model.getNBProjectFolder(), f);
442
cu.javadoc.add (path);
450
/** Read content of list box and update compilation unit's output.*/
451
private void updateCompilationUnitOutput(JavaProjectGenerator.JavaCompilationUnit cu) {
452
if (output.getModel().getSize() == 0) {
456
List<String> l = new ArrayList<String>();
457
for (int i=0; i<output.getModel().getSize(); i++) {
458
File f = new File((String)output.getModel().getElementAt(i));
459
String path = Util.relativizeLocation(model.getBaseFolder(), model.getNBProjectFolder(), f);
465
/** Update panel's listbox with given output list. */
466
private void updateJListOutput(List<String> l) {
467
listModel.removeAllElements();
469
for (String out : l) {
470
File f = Util.resolveFile(model.getEvaluator(), model.getNBProjectFolder(), out);
472
listModel.addElement(f.getAbsolutePath());
479
private void updateButtons() {
480
removeOutput.setEnabled(output.isEnabled() && listModel.getSize() > 0 && output.getSelectedIndex() != -1);
483
private void removeOutputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeOutputActionPerformed
484
int i = output.getSelectedIndex();
490
}//GEN-LAST:event_removeOutputActionPerformed
492
private void addOutputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addOutputActionPerformed
493
JFileChooser chooser = new JFileChooser();
494
FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
495
chooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES);
496
chooser.setMultiSelectionEnabled(true);
497
if (lastChosenFile != null) {
498
chooser.setSelectedFile(lastChosenFile);
500
File files[] = model.getBaseFolder().listFiles();
501
if (files != null && files.length > 0) {
502
chooser.setSelectedFile(files[0]);
504
chooser.setSelectedFile(model.getBaseFolder());
507
chooser.setDialogTitle(NbBundle.getMessage(OutputPanel.class, "LBL_Browse_Output")); // NOI18N
508
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
509
for (File file : chooser.getSelectedFiles()) {
510
file = FileUtil.normalizeFile(file);
511
listModel.addElement(file.getAbsolutePath());
512
lastChosenFile = file;
517
}//GEN-LAST:event_addOutputActionPerformed
519
private void applyChanges() {
520
if (isSeparateClasspath) {
521
if (sourceFolder.getSelectedIndex() != -1) {
522
saveOutput(sourceFolder.getSelectedIndex());
529
void setModel(ProjectModel model) {
532
model.addChangeListener(new ChangeListener() {
533
public void stateChanged(ChangeEvent arg0) {
540
// Variables declaration - do not modify//GEN-BEGIN:variables
541
private javax.swing.JButton addOutput;
542
private javax.swing.JLabel jLabel2;
543
private javax.swing.JLabel jLabel3;
544
private javax.swing.JPanel jPanel1;
545
private javax.swing.JPanel jPanel2;
546
private javax.swing.JScrollPane jScrollPane1;
547
private javax.swing.JTextArea jTextArea1;
548
private javax.swing.JTextField javadoc;
549
private javax.swing.JButton javadocBrowse;
550
private javax.swing.JLabel javadocLabel;
551
private javax.swing.JList output;
552
private javax.swing.JButton removeOutput;
553
private javax.swing.JComboBox sourceFolder;
554
// End of variables declaration//GEN-END:variables