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-2006 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:
042: package org.netbeans.modules.web.freeform.ui;
043:
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.io.File;
047: import java.net.MalformedURLException;
048: import java.util.List;
049: import javax.swing.DefaultListModel;
050: import javax.swing.JFileChooser;
051: import javax.swing.filechooser.FileFilter;
052: import org.netbeans.modules.ant.freeform.spi.support.Util;
053: import org.netbeans.modules.web.freeform.WebProjectGenerator;
054: import org.netbeans.spi.project.AuxiliaryConfiguration;
055: import org.netbeans.spi.project.support.ant.AntProjectHelper;
056: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
057: import org.netbeans.spi.project.support.ant.PropertyUtils;
058: import org.openide.filesystems.FileUtil;
059: import org.openide.util.Exceptions;
060: import org.openide.util.HelpCtx;
061: import org.openide.util.NbBundle;
062:
063: /**
064: *
065: * @author Petr Pisl
066: */
067: public class WebClasspathPanel extends javax.swing.JPanel implements
068: HelpCtx.Provider {
069:
070: private DefaultListModel listModel;
071: /** Original project folder (not nbproject folder) */
072: private File projectFolder = null;
073: /** Freeform Project base folder */
074: private File nbProjectFolder;
075: private File lastChosenFile = null;
076: private boolean isSeparateClasspath = true;
077: private boolean ignoreEvent;
078: private static String JAVA_SOURCES_CLASSPATH = org.openide.util.NbBundle
079: .getMessage(WebClasspathPanel.class,
080: "LBL_JAVA_SOURCE_CLASSPATH");
081:
082: //private ProjectModel model;
083:
084: /** Creates new form ClasspathPanel */
085: public WebClasspathPanel() {
086: this (true);
087: }
088:
089: public WebClasspathPanel(boolean isWizard) {
090: initComponents();
091: jTextArea1.setBackground(getBackground());
092: listModel = new DefaultListModel();
093: listModel.add(0, JAVA_SOURCES_CLASSPATH);
094: classpath.setModel(listModel);
095: if (!isWizard) {
096: jTextArea1.setText(org.openide.util.NbBundle.getMessage(
097: WebClasspathPanel.class,
098: "LBL_ClasspathPanel_Explanation"));
099: }
100:
101: }
102:
103: public HelpCtx getHelpCtx() {
104: return new HelpCtx(WebClasspathPanel.class);
105: }
106:
107: /** This method is called from within the constructor to
108: * initialize the form.
109: * WARNING: Do NOT modify this code. The content of this method is
110: * always regenerated by the Form Editor.
111: */
112: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
113: private void initComponents() {
114: java.awt.GridBagConstraints gridBagConstraints;
115:
116: jLabel3 = new javax.swing.JLabel();
117: addClasspath = new javax.swing.JButton();
118: removeClasspath = new javax.swing.JButton();
119: jScrollPane1 = new javax.swing.JScrollPane();
120: classpath = new javax.swing.JList();
121: jPanel1 = new javax.swing.JPanel();
122: moveUp = new javax.swing.JButton();
123: moveDown = new javax.swing.JButton();
124: jTextArea1 = new javax.swing.JTextArea();
125: jPanel2 = new javax.swing.JPanel();
126: jLabel5 = new javax.swing.JLabel();
127: jTextArea2 = new javax.swing.JTextArea();
128:
129: setLayout(new java.awt.GridBagLayout());
130:
131: jLabel3.setLabelFor(classpath);
132: org.openide.awt.Mnemonics.setLocalizedText(jLabel3,
133: org.openide.util.NbBundle.getMessage(
134: WebClasspathPanel.class,
135: "LBL_ClasspathPanel_jLabel3")); // NOI18N
136: gridBagConstraints = new java.awt.GridBagConstraints();
137: gridBagConstraints.gridx = 0;
138: gridBagConstraints.gridy = 2;
139: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
140: gridBagConstraints.insets = new java.awt.Insets(11, 0, 0, 0);
141: add(jLabel3, gridBagConstraints);
142: jLabel3.getAccessibleContext().setAccessibleDescription(
143: org.openide.util.NbBundle.getMessage(
144: WebClasspathPanel.class,
145: "ACSD_ClasspathPanel_jLabel3")); // NOI18N
146:
147: org.openide.awt.Mnemonics.setLocalizedText(addClasspath,
148: org.openide.util.NbBundle.getMessage(
149: WebClasspathPanel.class,
150: "BTN_ClasspathPanel_addClasspath")); // NOI18N
151: addClasspath
152: .addActionListener(new java.awt.event.ActionListener() {
153: public void actionPerformed(
154: java.awt.event.ActionEvent evt) {
155: addClasspathActionPerformed(evt);
156: }
157: });
158: gridBagConstraints = new java.awt.GridBagConstraints();
159: gridBagConstraints.gridx = 1;
160: gridBagConstraints.gridy = 3;
161: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
162: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
163: add(addClasspath, gridBagConstraints);
164: addClasspath.getAccessibleContext().setAccessibleDescription(
165: org.openide.util.NbBundle.getMessage(
166: WebClasspathPanel.class,
167: "ACSD_ClasspathPanel_addClasspath")); // NOI18N
168:
169: org.openide.awt.Mnemonics.setLocalizedText(removeClasspath,
170: org.openide.util.NbBundle.getMessage(
171: WebClasspathPanel.class,
172: "BTN_ClasspathPanel_removeClasspath")); // NOI18N
173: removeClasspath
174: .addActionListener(new java.awt.event.ActionListener() {
175: public void actionPerformed(
176: java.awt.event.ActionEvent evt) {
177: removeClasspathActionPerformed(evt);
178: }
179: });
180: gridBagConstraints = new java.awt.GridBagConstraints();
181: gridBagConstraints.gridx = 1;
182: gridBagConstraints.gridy = 4;
183: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
184: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
185: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
186: add(removeClasspath, gridBagConstraints);
187: removeClasspath.getAccessibleContext()
188: .setAccessibleDescription(
189: org.openide.util.NbBundle.getMessage(
190: WebClasspathPanel.class,
191: "ACSD_ClasspathPanel_removeClasspath")); // NOI18N
192:
193: classpath
194: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
195: public void valueChanged(
196: javax.swing.event.ListSelectionEvent evt) {
197: classpathValueChanged(evt);
198: }
199: });
200: jScrollPane1.setViewportView(classpath);
201: classpath.getAccessibleContext().setAccessibleDescription(
202: org.openide.util.NbBundle.getMessage(
203: WebClasspathPanel.class,
204: "ACSD_ClasspathPanel_classpath")); // NOI18N
205:
206: gridBagConstraints = new java.awt.GridBagConstraints();
207: gridBagConstraints.gridx = 0;
208: gridBagConstraints.gridy = 3;
209: gridBagConstraints.gridheight = 4;
210: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
211: gridBagConstraints.weightx = 1.0;
212: gridBagConstraints.weighty = 1.0;
213: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
214: add(jScrollPane1, gridBagConstraints);
215:
216: jPanel1.setLayout(new java.awt.GridBagLayout());
217: gridBagConstraints = new java.awt.GridBagConstraints();
218: gridBagConstraints.gridx = 0;
219: gridBagConstraints.gridy = 1;
220: gridBagConstraints.gridwidth = 2;
221: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
222: add(jPanel1, gridBagConstraints);
223:
224: moveUp.setMnemonic(org.openide.util.NbBundle.getMessage(
225: WebClasspathPanel.class, "LBL_MoveUp_MNE").charAt(0));
226: moveUp.setText(org.openide.util.NbBundle.getMessage(
227: WebClasspathPanel.class, "LBL_ClasspathPanel_Move_Up")); // NOI18N
228: moveUp.addActionListener(new java.awt.event.ActionListener() {
229: public void actionPerformed(java.awt.event.ActionEvent evt) {
230: moveUpActionPerformed(evt);
231: }
232: });
233: gridBagConstraints = new java.awt.GridBagConstraints();
234: gridBagConstraints.gridx = 1;
235: gridBagConstraints.gridy = 5;
236: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
237: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
238: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
239: add(moveUp, gridBagConstraints);
240: java.util.ResourceBundle bundle = java.util.ResourceBundle
241: .getBundle("org/netbeans/modules/web/freeform/ui/Bundle"); // NOI18N
242: moveUp.getAccessibleContext().setAccessibleDescription(
243: bundle.getString("AD_ClasspathPanel_noveUp")); // NOI18N
244:
245: moveDown.setMnemonic(org.openide.util.NbBundle.getMessage(
246: WebClasspathPanel.class, "LBL_MoveDown_MNE").charAt(0));
247: moveDown.setText(org.openide.util.NbBundle
248: .getMessage(WebClasspathPanel.class,
249: "LBL_ClasspathPanel_Move_Down")); // NOI18N
250: moveDown.addActionListener(new java.awt.event.ActionListener() {
251: public void actionPerformed(java.awt.event.ActionEvent evt) {
252: moveDownActionPerformed(evt);
253: }
254: });
255: gridBagConstraints = new java.awt.GridBagConstraints();
256: gridBagConstraints.gridx = 1;
257: gridBagConstraints.gridy = 6;
258: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
259: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
260: add(moveDown, gridBagConstraints);
261: moveDown.getAccessibleContext().setAccessibleDescription(
262: bundle.getString("AD_ClasspathPanel_moveDown")); // NOI18N
263:
264: jTextArea1.setEditable(false);
265: jTextArea1.setLineWrap(true);
266: jTextArea1.setText(org.openide.util.NbBundle
267: .getMessage(WebClasspathPanel.class,
268: "MSG_ClasspathPanel_jTextArea")); // NOI18N
269: jTextArea1.setWrapStyleWord(true);
270: jTextArea1.setDisabledTextColor(java.awt.Color.black);
271: jTextArea1.setEnabled(false);
272: gridBagConstraints = new java.awt.GridBagConstraints();
273: gridBagConstraints.gridx = 0;
274: gridBagConstraints.gridy = 0;
275: gridBagConstraints.gridwidth = 2;
276: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
277: gridBagConstraints.weightx = 1.0;
278: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
279: add(jTextArea1, gridBagConstraints);
280: jTextArea1.getAccessibleContext().setAccessibleName(
281: org.openide.util.NbBundle.getMessage(
282: WebClasspathPanel.class,
283: "ACSN_ClasspathPanel_jTextArea")); // NOI18N
284: jTextArea1.getAccessibleContext().setAccessibleDescription(
285: org.openide.util.NbBundle.getMessage(
286: WebClasspathPanel.class,
287: "ACSD_ClasspathPanel_jTextArea")); // NOI18N
288:
289: jPanel2.setBorder(javax.swing.BorderFactory
290: .createLineBorder(javax.swing.UIManager.getDefaults()
291: .getColor("Label.disabledForeground")));
292: jPanel2.setLayout(new java.awt.GridBagLayout());
293:
294: jLabel5
295: .setIcon(new javax.swing.ImageIcon(
296: getClass()
297: .getResource(
298: "/org/netbeans/modules/web/freeform/resources/alert_32.png"))); // NOI18N
299: gridBagConstraints = new java.awt.GridBagConstraints();
300: gridBagConstraints.gridx = 0;
301: gridBagConstraints.gridy = 0;
302: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
303: gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 0);
304: jPanel2.add(jLabel5, gridBagConstraints);
305:
306: jTextArea2.setBackground(javax.swing.UIManager.getDefaults()
307: .getColor("Panel.background"));
308: jTextArea2.setEditable(false);
309: jTextArea2.setLineWrap(true);
310: jTextArea2.setText(org.openide.util.NbBundle.getMessage(
311: WebClasspathPanel.class, "Freeform_Warning_Message")); // NOI18N
312: jTextArea2.setWrapStyleWord(true);
313: gridBagConstraints = new java.awt.GridBagConstraints();
314: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
315: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
316: gridBagConstraints.weightx = 1.0;
317: gridBagConstraints.insets = new java.awt.Insets(4, 10, 4, 4);
318: jPanel2.add(jTextArea2, gridBagConstraints);
319:
320: gridBagConstraints = new java.awt.GridBagConstraints();
321: gridBagConstraints.gridx = 0;
322: gridBagConstraints.gridy = 7;
323: gridBagConstraints.gridwidth = 2;
324: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
325: gridBagConstraints.weightx = 1.0;
326: gridBagConstraints.insets = new java.awt.Insets(8, 0, 0, 0);
327: add(jPanel2, gridBagConstraints);
328: }// </editor-fold>//GEN-END:initComponents
329:
330: private void classpathValueChanged(
331: javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_classpathValueChanged
332: updateButtons();
333: }//GEN-LAST:event_classpathValueChanged
334:
335: private void moveDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveDownActionPerformed
336: int indices[] = classpath.getSelectedIndices();
337: if (indices.length == 0
338: || indices[indices.length - 1] == listModel.getSize() - 1) {
339: return;
340: }
341: for (int i = 0; i < indices.length; i++) {
342: int index = indices[i];
343: Object o = listModel.remove(index);
344: index++;
345: listModel.add(index, o);
346: indices[i] = index;
347: }
348: classpath.setSelectedIndices(indices);
349: updateButtons();
350: }//GEN-LAST:event_moveDownActionPerformed
351:
352: private void moveUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveUpActionPerformed
353: int indices[] = classpath.getSelectedIndices();
354: if (indices.length == 0 || indices[0] == 0) {
355: return;
356: }
357: for (int i = 0; i < indices.length; i++) {
358: int index = indices[i];
359: Object o = listModel.remove(index);
360: index--;
361: listModel.add(index, o);
362: indices[i] = index;
363: }
364: classpath.setSelectedIndices(indices);
365: updateButtons();
366: }//GEN-LAST:event_moveUpActionPerformed
367:
368: void updateButtons() {
369: int indices[] = classpath.getSelectedIndices();
370: removeClasspath.setEnabled(listModel.getSize() > 0
371: && indices.length != 0 && indices[0] != 0);
372: moveUp.setEnabled(indices.length > 0 && indices[0] > 1);
373: moveDown
374: .setEnabled(indices.length > 0
375: && indices[indices.length - 1] != listModel
376: .getSize() - 1 && indices[0] != 0);
377: }
378:
379: private void removeClasspathActionPerformed(
380: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeClasspathActionPerformed
381: int entries[] = classpath.getSelectedIndices();
382: for (int i = 0; i < entries.length; i++) {
383: listModel.remove(entries[i] - i);
384: }
385: updateButtons();
386: }//GEN-LAST:event_removeClasspathActionPerformed
387:
388: private void addClasspathActionPerformed(
389: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addClasspathActionPerformed
390: JFileChooser chooser = new JFileChooser();
391: FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
392: chooser
393: .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
394: chooser.setMultiSelectionEnabled(true);
395: if (lastChosenFile != null) {
396: chooser.setSelectedFile(lastChosenFile);
397: } else {
398: if (projectFolder != null)
399: chooser.setSelectedFile(projectFolder);
400: }
401: chooser.setDialogTitle(NbBundle.getMessage(
402: WebClasspathPanel.class, "LBL_Browse_Classpath"));
403:
404: //#77911: prevent adding a non-folder element on the classpath:
405: FileFilter fileFilter = new SimpleFileFilter(NbBundle
406: .getMessage(WebClasspathPanel.class,
407: "LBL_ZipJarFolderFilter")); // NOI18N
408: chooser.setFileFilter(fileFilter);
409: chooser.setAcceptAllFileFilterUsed(false);
410:
411: if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this )) {
412: File files[] = chooser.getSelectedFiles();
413: for (int i = 0; i < files.length; i++) {
414: File file = FileUtil.normalizeFile(files[i]);
415:
416: //Check if the file is acceted by the FileFilter,
417: //user may enter the name of non displayed file into JFileChooser
418: if (!fileFilter.accept(file)) {
419: continue;
420: }
421:
422: listModel.addElement(file.getAbsolutePath());
423: lastChosenFile = file;
424: }
425: updateButtons();
426: }
427: }//GEN-LAST:event_addClasspathActionPerformed
428:
429: /** Called from WizardDescriptor.Panel and ProjectCustomizer.Panel
430: * to set base folder. Panel will use this for default position of JFileChooser.
431: * @param baseFolder original project base folder
432: * @param nbProjectFolder Freeform Project base folder
433: */
434: public void setProjectFolders(File baseFolder, File nbProjectFolder) {
435: this .projectFolder = baseFolder;
436: this .nbProjectFolder = nbProjectFolder;
437: }
438:
439: public String getClasspath() {
440: StringBuffer sf = new StringBuffer();
441: for (int i = 1; i < listModel.getSize(); i++) {
442: File f = new File((String) listModel.get(i));
443: String path = org.netbeans.modules.ant.freeform.spi.support.Util
444: .relativizeLocation(projectFolder, nbProjectFolder,
445: f);
446: sf.append(path);
447: if (i < listModel.getSize() - 1)
448: sf.append(File.pathSeparatorChar);
449: }
450: return sf.toString();
451: }
452:
453: void setClasspath(String classpath, PropertyEvaluator evaluator) {
454: if (classpath == null)
455: return;
456: listModel.clear();
457: listModel.addElement(JAVA_SOURCES_CLASSPATH);
458:
459: String[] cpa = PropertyUtils.tokenizePath(evaluator
460: .evaluate(classpath));
461: for (int i = 0; i < cpa.length; i++) {
462: String path = cpa[i];
463: path = PropertyUtils.resolveFile(nbProjectFolder, path)
464: .getAbsolutePath();
465: if (path != null) {
466: listModel.addElement(path);
467: }
468: }
469: }
470:
471: ActionListener getCustomizerOkListener(
472: final AntProjectHelper projectHelper) {
473: return new ActionListener() {
474: public void actionPerformed(ActionEvent arg0) {
475: AuxiliaryConfiguration aux = Util
476: .getAuxiliaryConfiguration(projectHelper);
477: List<WebProjectGenerator.WebModule> l = WebProjectGenerator
478: .getWebmodules(projectHelper, aux);
479: if (l != null) {
480: WebProjectGenerator.WebModule wm = (WebProjectGenerator.WebModule) l
481: .get(0);
482: wm.classpath = getClasspath();
483: WebProjectGenerator.putWebModules(projectHelper,
484: aux, l);
485: }
486: //mkleint: why updating buttons on saving??
487: updateButtons();
488: }
489: };
490: }
491:
492: // Variables declaration - do not modify//GEN-BEGIN:variables
493: private javax.swing.JButton addClasspath;
494: private javax.swing.JList classpath;
495: private javax.swing.JLabel jLabel3;
496: private javax.swing.JLabel jLabel5;
497: private javax.swing.JPanel jPanel1;
498: private javax.swing.JPanel jPanel2;
499: private javax.swing.JScrollPane jScrollPane1;
500: private javax.swing.JTextArea jTextArea1;
501: private javax.swing.JTextArea jTextArea2;
502: private javax.swing.JButton moveDown;
503: private javax.swing.JButton moveUp;
504: private javax.swing.JButton removeClasspath;
505:
506: // End of variables declaration//GEN-END:variables
507:
508: private static class SimpleFileFilter extends FileFilter {
509:
510: private String description;
511:
512: public SimpleFileFilter(String description) {
513: this .description = description;
514: }
515:
516: public boolean accept(File f) {
517: if (f.isDirectory())
518: return true;
519: try {
520: return FileUtil.isArchiveFile(f.toURI().toURL());
521: } catch (MalformedURLException mue) {
522: Exceptions.printStackTrace(mue);
523: return false;
524: }
525: }
526:
527: public String getDescription() {
528: return this.description;
529: }
530: }
531:
532: }
|