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.mobility.cldcplatform.customwizard;
043:
044: import org.netbeans.modules.mobility.cldcplatform.DetectPanel;
045: import org.netbeans.modules.mobility.cldcplatform.J2MEPlatform;
046: import org.netbeans.modules.mobility.cldcplatform.UEIEmulatorConfiguratorImpl;
047: import org.openide.WizardDescriptor;
048: import org.openide.util.NbBundle;
049:
050: import javax.swing.*;
051: import javax.swing.text.html.HTMLEditorKit;
052: import javax.swing.event.ListSelectionEvent;
053: import javax.swing.event.ListSelectionListener;
054: import javax.swing.filechooser.FileFilter;
055: import java.io.File;
056: import java.util.ArrayList;
057: import java.util.Arrays;
058: import java.util.List;
059:
060: /**
061: *
062: * @author dave
063: */
064: public class ClasspathPanel extends javax.swing.JPanel implements
065: WizardPanel.ComponentDescriptor {
066:
067: private static final String PROP_CLASSPATH = "Classpath"; // NOI18N
068:
069: final private DefaultListModel classpathListModel = new DefaultListModel();
070: private WizardPanel wizardPanel;
071: private String fileChooserValue;
072: private J2MEPlatform.J2MEProfile[] profiles;
073:
074: /** Creates new form ClasspathPanel */
075: public ClasspathPanel() {
076: initComponents();
077: infoPanel.setEditorKitForContentType("text/html",
078: new HTMLEditorKit()); // NOI18N
079: infoPanel.setContentType("text/html;charset=UTF-8"); // NOI18N
080: classpathList
081: .addListSelectionListener(new ListSelectionListener() {
082: @SuppressWarnings("synthetic-access")
083: public void valueChanged(
084: @SuppressWarnings("unused")
085: final ListSelectionEvent e) {
086: final Object[] selectedValues = classpathList
087: .getSelectedValues();
088: removeButton.setEnabled(selectedValues != null
089: && selectedValues.length > 0);
090: }
091: });
092: }
093:
094: /** This method is called from within the constructor to
095: * initialize the form.
096: * WARNING: Do NOT modify this code. The content of this method is
097: * always regenerated by the Form Editor.
098: */
099: @SuppressWarnings("synthetic-access")
100: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
101: private void initComponents() {
102: java.awt.GridBagConstraints gridBagConstraints;
103:
104: jLabel1 = new javax.swing.JLabel();
105: jScrollPane1 = new javax.swing.JScrollPane();
106: classpathList = new javax.swing.JList();
107: addButton = new javax.swing.JButton();
108: removeButton = new javax.swing.JButton();
109: jLabel2 = new javax.swing.JLabel();
110: jScrollPane2 = new javax.swing.JScrollPane();
111: infoPanel = new javax.swing.JTextPane();
112:
113: setName(NbBundle.getMessage(ClasspathPanel.class,
114: "Title_CPPanel_Bootstrap_Libraries")); // NOI18N
115: setPreferredSize(new java.awt.Dimension(600, 400));
116: setLayout(new java.awt.GridBagLayout());
117:
118: jLabel1.setLabelFor(classpathList);
119: org.openide.awt.Mnemonics.setLocalizedText(jLabel1, NbBundle
120: .getMessage(ClasspathPanel.class,
121: "LBL_CPPanel_Classpath")); // NOI18N
122: gridBagConstraints = new java.awt.GridBagConstraints();
123: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
124: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
125: add(jLabel1, gridBagConstraints);
126: jLabel1.getAccessibleContext()
127: .setAccessibleName(
128: org.openide.util.NbBundle.getMessage(
129: ClasspathPanel.class,
130: "ACSD_CPPanel_Classpath")); // NOI18N
131: jLabel1.getAccessibleContext()
132: .setAccessibleDescription(
133: org.openide.util.NbBundle.getMessage(
134: ClasspathPanel.class,
135: "ACSD_CPPanel_Classpath")); // NOI18N
136:
137: classpathList.setModel(classpathListModel);
138: jScrollPane1.setViewportView(classpathList);
139:
140: gridBagConstraints = new java.awt.GridBagConstraints();
141: gridBagConstraints.gridx = 0;
142: gridBagConstraints.gridy = 1;
143: gridBagConstraints.gridheight = 3;
144: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
145: gridBagConstraints.weightx = 1.0;
146: gridBagConstraints.weighty = 4.0;
147: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
148: add(jScrollPane1, gridBagConstraints);
149:
150: org.openide.awt.Mnemonics.setLocalizedText(addButton, NbBundle
151: .getMessage(ClasspathPanel.class, "LBL_CPPanel_Add")); // NOI18N
152: addButton
153: .addActionListener(new java.awt.event.ActionListener() {
154: public void actionPerformed(
155: java.awt.event.ActionEvent evt) {
156: addButtonActionPerformed(evt);
157: }
158: });
159: gridBagConstraints = new java.awt.GridBagConstraints();
160: gridBagConstraints.gridx = 1;
161: gridBagConstraints.gridy = 1;
162: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
163: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
164: add(addButton, gridBagConstraints);
165: addButton.getAccessibleContext().setAccessibleName(
166: org.openide.util.NbBundle.getMessage(
167: ClasspathPanel.class, "ACSD_CPPanel_Add")); // NOI18N
168: addButton.getAccessibleContext().setAccessibleDescription(
169: org.openide.util.NbBundle.getMessage(
170: ClasspathPanel.class, "ACSD_CPPanel_Add")); // NOI18N
171:
172: org.openide.awt.Mnemonics.setLocalizedText(removeButton,
173: NbBundle.getMessage(ClasspathPanel.class,
174: "LBL_CPPanel_Remove")); // NOI18N
175: removeButton.setEnabled(false);
176: removeButton
177: .addActionListener(new java.awt.event.ActionListener() {
178: public void actionPerformed(
179: java.awt.event.ActionEvent evt) {
180: removeButtonActionPerformed(evt);
181: }
182: });
183: gridBagConstraints = new java.awt.GridBagConstraints();
184: gridBagConstraints.gridx = 1;
185: gridBagConstraints.gridy = 2;
186: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
187: add(removeButton, gridBagConstraints);
188: removeButton.getAccessibleContext().setAccessibleName(
189: org.openide.util.NbBundle.getMessage(
190: ClasspathPanel.class, "ACSD_CPPanel_Remove")); // NOI18N
191: removeButton.getAccessibleContext().setAccessibleDescription(
192: org.openide.util.NbBundle.getMessage(
193: ClasspathPanel.class, "ACSD_CPPanel_Remove")); // NOI18N
194:
195: jLabel2.setLabelFor(infoPanel);
196: org.openide.awt.Mnemonics.setLocalizedText(jLabel2, NbBundle
197: .getMessage(ClasspathPanel.class,
198: "LBL_CPPanel_Detected_APIs")); // NOI18N
199: gridBagConstraints = new java.awt.GridBagConstraints();
200: gridBagConstraints.gridx = 0;
201: gridBagConstraints.gridy = 4;
202: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
203: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
204: add(jLabel2, gridBagConstraints);
205: jLabel2.getAccessibleContext().setAccessibleName(
206: org.openide.util.NbBundle.getMessage(
207: ClasspathPanel.class,
208: "ACSD_CPPanel_Detected_APIs")); // NOI18N
209: jLabel2.getAccessibleContext().setAccessibleDescription(
210: org.openide.util.NbBundle.getMessage(
211: ClasspathPanel.class,
212: "ACSD_CPPanel_Detected_APIs")); // NOI18N
213:
214: infoPanel.setBackground(javax.swing.UIManager.getDefaults()
215: .getColor("Panel.background"));
216: infoPanel.setEditable(false);
217: jScrollPane2.setViewportView(infoPanel);
218:
219: gridBagConstraints = new java.awt.GridBagConstraints();
220: gridBagConstraints.gridx = 0;
221: gridBagConstraints.gridy = 5;
222: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
223: gridBagConstraints.weighty = 1.0;
224: gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 12);
225: add(jScrollPane2, gridBagConstraints);
226: }// </editor-fold>//GEN-END:initComponents
227:
228: private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
229: final File[] values = browse(NbBundle.getMessage(
230: ClasspathPanel.class,
231: "TITLE_ClasspathPanel_SelectClasspath"));//NOI18N
232: if (values == null)
233: return;
234: for (int i = 0; i < values.length; i++) {
235: final String value = values[i].getAbsolutePath();
236: if (!classpathListModel.contains(value))
237: classpathListModel.addElement(value);
238: }
239: updateStatus();
240: }//GEN-LAST:event_addButtonActionPerformed
241:
242: private void removeButtonActionPerformed(
243: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
244: final Object[] values = classpathList.getSelectedValues();
245: if (values != null)
246: for (int i = 0; i < values.length; i++)
247: classpathListModel.removeElement(values[i]);
248: updateStatus();
249: }//GEN-LAST:event_removeButtonActionPerformed
250:
251: // Variables declaration - do not modify//GEN-BEGIN:variables
252: private javax.swing.JButton addButton;
253: private javax.swing.JList classpathList;
254: private javax.swing.JTextPane infoPanel;
255: private javax.swing.JLabel jLabel1;
256: private javax.swing.JLabel jLabel2;
257: private javax.swing.JScrollPane jScrollPane1;
258: private javax.swing.JScrollPane jScrollPane2;
259: private javax.swing.JButton removeButton;
260:
261: // End of variables declaration//GEN-END:variables
262:
263: public void setWizardPanel(final WizardPanel wizardPanel) {
264: this .wizardPanel = wizardPanel;
265: }
266:
267: public void readSettings(final WizardDescriptor wizardDescriptor) {
268: final J2MEPlatform platform = (J2MEPlatform) wizardDescriptor
269: .getProperty(DetectPanel.PLATFORM);
270: fileChooserValue = platform.getHomePath();
271: final J2MEPlatform.Device previous = platform.getDevices()[0];
272: final J2MEPlatform.J2MEProfile[] profiles = previous
273: .getProfiles();
274: if (profiles.length > 0) {
275: final List<String> classpath = (List<String>) wizardDescriptor
276: .getProperty(ClasspathPanel.PROP_CLASSPATH);
277: stringsToModel(classpath);
278: } else {
279: final ArrayList<String> list = new ArrayList<String>();
280: searchForClasspath(list, new File(platform.getHomePath()),
281: 6);
282: stringsToModel(list);
283: }
284: }
285:
286: public void storeSettings(final WizardDescriptor wizardDescriptor) {
287: final J2MEPlatform platform = (J2MEPlatform) wizardDescriptor
288: .getProperty(DetectPanel.PLATFORM);
289: final J2MEPlatform.Device previous = platform.getDevices()[0];
290: final J2MEPlatform.Device device = new J2MEPlatform.Device(
291: previous.getName(), null, null, profiles, null);
292: platform.setDevices(new J2MEPlatform.Device[] { device });
293: wizardDescriptor.putProperty(ClasspathPanel.PROP_CLASSPATH,
294: modelToStrings());
295: }
296:
297: public JComponent getComponent() {
298: return this ;
299: }
300:
301: public boolean isPanelValid() {
302: boolean prof = false;
303: boolean conf = false;
304:
305: for (int i = 0; i < profiles.length; i++) {
306: final J2MEPlatform.J2MEProfile profile = profiles[i];
307: if (J2MEPlatform.J2MEProfile.TYPE_PROFILE.equals(profile
308: .getType()))
309: prof = true;
310: else if (J2MEPlatform.J2MEProfile.TYPE_CONFIGURATION
311: .equals(profile.getType()))
312: conf = true;
313: }
314: if (!prof)
315: wizardPanel.setErrorMessage(ClasspathPanel.class,
316: "ERR_NoProfile");//NOI18N
317: else if (!conf)
318: wizardPanel.setErrorMessage(ClasspathPanel.class,
319: "ERR_NoConfiguration");//NOI18N
320: else
321: wizardPanel.setErrorMessage(ClasspathPanel.class, null);
322: return prof && conf;
323: }
324:
325: public boolean isFinishPanel() {
326: return true;
327: }
328:
329: private void updateStatus() {
330: final J2MEPlatform platform = (J2MEPlatform) wizardPanel
331: .getProperty(DetectPanel.PLATFORM);
332: final String string = stringsToString(Arrays
333: .asList(classpathListModel.toArray()));
334: final List<J2MEPlatform.J2MEProfile> profilesList = UEIEmulatorConfiguratorImpl
335: .analyzePath(new File(platform.getHomePath()), string,
336: string);
337: profiles = profilesList
338: .toArray(new J2MEPlatform.J2MEProfile[profilesList
339: .size()]);
340:
341: final StringBuffer recognizedProfiles = new StringBuffer();
342: final StringBuffer recognizedConfigurations = new StringBuffer();
343: final StringBuffer recognizedOptionals = new StringBuffer();
344: for (int i = 0; i < profiles.length; i++) {
345: final J2MEPlatform.J2MEProfile profile = profiles[i];
346: if (profile.isNameIsJarFileName())
347: continue;
348: if (J2MEPlatform.J2MEProfile.TYPE_PROFILE.equals(profile
349: .getType())) {
350: if (recognizedProfiles.length() > 0)
351: recognizedProfiles.append(", "); //NOI18N
352: recognizedProfiles.append(NbBundle.getMessage(
353: ClasspathPanel.class, "MSG_OutputTextBold",
354: profile.getName(), profile.getVersion()));//NOI18N
355: } else if (J2MEPlatform.J2MEProfile.TYPE_CONFIGURATION
356: .equals(profile.getType())) {
357: if (recognizedConfigurations.length() > 0)
358: recognizedConfigurations.append(", ");//NOI18N
359: recognizedConfigurations.append(NbBundle.getMessage(
360: ClasspathPanel.class, "MSG_OutputTextBold",
361: profile.getName(), profile.getVersion()));//NOI18N
362: } else if (J2MEPlatform.J2MEProfile.TYPE_OPTIONAL
363: .equals(profile.getType())) {
364: if (recognizedOptionals.length() > 0)
365: recognizedOptionals.append(", ");//NOI18N
366: recognizedOptionals.append(NbBundle.getMessage(
367: ClasspathPanel.class, "MSG_OutputText", profile
368: .getName(), profile.getVersion()));//NOI18N
369: }
370: }
371:
372: final StringBuffer sb = new StringBuffer(200);
373: sb.append("<html><font face=\"dialog\" size=\"-1\">");//NOI18N
374: boolean shown = false;
375: if (recognizedProfiles.length() > 0) {
376: sb.append(
377: NbBundle.getMessage(ClasspathPanel.class,
378: "LBL_CPPanel_Profiles")).append(
379: recognizedProfiles).append("<br>");//NOI18N
380: shown = true;
381: }
382: if (recognizedConfigurations.length() > 0) {
383: sb.append(
384: NbBundle.getMessage(ClasspathPanel.class,
385: "LBL_CPPanel_Configurations")).append(
386: recognizedConfigurations).append("<br>");//NOI18N
387: shown = true;
388: }
389: if (recognizedOptionals.length() > 0) {
390: sb.append(
391: NbBundle.getMessage(ClasspathPanel.class,
392: "LBL_CPPanle_Optional_APIs")).append(
393: recognizedOptionals).append("<br>");//NOI18N
394: shown = true;
395: }
396: sb.append(shown ? "<font size=\"-2\">"
397: + NbBundle.getMessage(ClasspathPanel.class,
398: "MSG_CPPanel_Recognized_Packages_Shown_Only")
399: : NbBundle.getMessage(ClasspathPanel.class,
400: "MSG_CPPanel_Details_Not_Available"));//NOI18N
401: infoPanel.setText(sb.toString());
402: infoPanel.setCaretPosition(0);
403:
404: wizardPanel.fireChanged();
405: }
406:
407: private void stringsToModel(final List<String> strings) {
408: classpathListModel.clear();
409: for (String str : strings)
410: classpathListModel.addElement(str);
411: updateStatus();
412: }
413:
414: private List<String> modelToStrings() {
415: final ArrayList<String> list = new ArrayList<String>();
416: for (int i = 0; i < classpathListModel.size(); i++)
417: list.add((String) classpathListModel.get(i));
418: return list;
419: }
420:
421: private String stringsToString(final List<Object> list) {
422: final StringBuffer sb = new StringBuffer();
423: for (int i = 0; i < list.size(); i++) {
424: if (i > 0)
425: sb.append(", ");//NOI18N
426: sb.append(list.get(i));
427: }
428: return sb.toString();
429: }
430:
431: private void searchForClasspath(final ArrayList<String> list,
432: final File directory, final int level) {
433: final File[] files = directory.listFiles();
434: if (files != null && files.length < 40 && list.size() < 100)
435: for (int i = 0; i < files.length; i++) {
436: final File file = files[i];
437: final String name = file.getName().toLowerCase();
438: if (level > 0 && file.isDirectory()) {
439: searchForClasspath(list, file, level - 1);
440: } else if (file.isFile()) {
441: if (name.endsWith(".jar") || name.endsWith(".zip"))//NOI18N
442: list.add(file.getAbsolutePath());
443: }
444: }
445: }
446:
447: private File[] browse(final String title) {
448: final String oldValue = fileChooserValue;
449: final JFileChooser chooser = new JFileChooser();
450: chooser.setMultiSelectionEnabled(true);
451: chooser
452: .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
453: chooser.setFileFilter(new FileFilter() {
454: public boolean accept(File f) {
455: return (f.exists() && f.canRead() && (f.isDirectory() || (f
456: .getName().endsWith(".zip") || f.getName()
457: .endsWith(".jar")))); //NOI18N
458: }
459:
460: public String getDescription() {
461: return NbBundle.getMessage(CommandLinesPanel.class,
462: "TXT_ZipFilter"); // NOI18N
463: }
464: });
465: if (oldValue != null)
466: chooser.setSelectedFile(new File(oldValue));
467: chooser.setDialogTitle(title);
468: if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
469: fileChooserValue = chooser.getSelectedFile()
470: .getAbsolutePath();
471: return chooser.getSelectedFiles();
472: }
473: return null;
474: }
475: }
|