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.j2me.cdc.project.ui.wizards;
043:
044: import java.awt.event.ItemEvent;
045: import java.awt.event.ItemListener;
046: import java.util.HashSet;
047: import java.util.Set;
048: import java.util.StringTokenizer;
049: import java.util.TreeSet;
050: import java.util.Vector;
051:
052: import javax.swing.AbstractListModel;
053: import javax.swing.ComboBoxModel;
054: import javax.swing.DefaultComboBoxModel;
055:
056: import org.netbeans.api.java.platform.JavaPlatform;
057: import org.netbeans.api.java.platform.JavaPlatformManager;
058: import org.netbeans.api.java.platform.Specification;
059: import org.netbeans.modules.j2me.cdc.platform.CDCDevice;
060: import org.netbeans.modules.j2me.cdc.platform.CDCPlatform;
061: import org.openide.WizardDescriptor;
062: import org.openide.util.NbBundle;
063:
064: /**
065: *
066: * @author suchys
067: */
068: final class PanelConfigurePlatformVisual extends javax.swing.JPanel {
069:
070: protected PanelConfigurePlatform panel;
071: private PlatformComboBoxModel model;
072: private CDCDevice[] devices;
073: private String platformType;
074:
075: /** Creates new form PanelConfigurePlatformVisual */
076: public PanelConfigurePlatformVisual(
077: final PanelConfigurePlatform panel, String platformType) {
078: this .panel = panel;
079: this .platformType = platformType;
080: setName(NbBundle.getMessage(PanelConfigurePlatformVisual.class,
081: "LAB_SelectPlatform"));
082: initComponents();
083: }
084:
085: protected void selectPlatformPanel() {
086: String platformType = ((PlatformKey) model.getSelectedItem())
087: .getPlatform().getType();
088: }
089:
090: protected void selectDevice() {
091: CDCPlatform platform = ((PlatformKey) model.getSelectedItem())
092: .getPlatform();
093: devices = platform.getDevices();
094: Vector<String> devcs = new Vector<String>();
095: for (CDCDevice device : devices) {
096: devcs.add(device.getName());
097: }
098: jComboBoxDevice.setModel(new DefaultComboBoxModel(devcs));
099: }
100:
101: protected void selectProfile() {
102: String name = null;
103: Object o = jComboBoxDevice.getSelectedItem();
104: for (CDCDevice device : devices) {
105: if (device.getName().equals(o)) {
106: CDCDevice.CDCProfile[] profiles = device.getProfiles();
107: Vector<String> profs = new Vector<String>();
108: for (int j = 0; j < profiles.length; j++) {
109: String s = profiles[j].getName();
110: profs.add(s);
111: if (profiles[j].isDefault())
112: name = s;
113: }
114: jComboProfile.setModel(new DefaultComboBoxModel(profs));
115: jComboProfile.setSelectedItem(name);
116: }
117: }
118: }
119:
120: /** This method is called from within the constructor to
121: * initialize the form.
122: * WARNING: Do NOT modify this code. The content of this method is
123: * always regenerated by the Form Editor.
124: */
125: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
126: private void initComponents() {
127: java.awt.GridBagConstraints gridBagConstraints;
128:
129: jLabel1 = new javax.swing.JLabel();
130: jLabel2 = new javax.swing.JLabel();
131: jLabel3 = new javax.swing.JLabel();
132: jComboPlatform = new javax.swing.JComboBox();
133: jComboBoxDevice = new javax.swing.JComboBox();
134: jComboProfile = new javax.swing.JComboBox();
135: jSeparator1 = new javax.swing.JSeparator();
136: containerPanel = new javax.swing.JPanel();
137:
138: setLayout(new java.awt.GridBagLayout());
139:
140: jLabel1.setLabelFor(jComboPlatform);
141: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
142: org.openide.util.NbBundle.getMessage(
143: PanelConfigurePlatformVisual.class,
144: "LBL_WizardsPlatform_Platform")); // NOI18N
145: gridBagConstraints = new java.awt.GridBagConstraints();
146: gridBagConstraints.gridx = 0;
147: gridBagConstraints.gridy = 0;
148: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
149: gridBagConstraints.insets = new java.awt.Insets(0, 0, 3, 0);
150: add(jLabel1, gridBagConstraints);
151: jLabel1.getAccessibleContext().setAccessibleName(
152: org.openide.util.NbBundle.getMessage(
153: PanelConfigurePlatformVisual.class,
154: "ACSN_WizardsPlatform_Platform")); // NOI18N
155: jLabel1.getAccessibleContext().setAccessibleDescription(
156: org.openide.util.NbBundle.getMessage(
157: PanelConfigurePlatformVisual.class,
158: "ACSD_WizardsPlatform_Platform")); // NOI18N
159:
160: jLabel2.setLabelFor(jComboBoxDevice);
161: org.openide.awt.Mnemonics.setLocalizedText(jLabel2,
162: org.openide.util.NbBundle.getMessage(
163: PanelConfigurePlatformVisual.class,
164: "LBL_WizardsPlatform_Device")); // NOI18N
165: gridBagConstraints = new java.awt.GridBagConstraints();
166: gridBagConstraints.gridx = 0;
167: gridBagConstraints.gridy = 1;
168: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
169: gridBagConstraints.insets = new java.awt.Insets(0, 0, 3, 0);
170: add(jLabel2, gridBagConstraints);
171: jLabel2.getAccessibleContext().setAccessibleName(
172: org.openide.util.NbBundle.getMessage(
173: PanelConfigurePlatformVisual.class,
174: "ACSN_WizardsPlatform_Device")); // NOI18N
175: jLabel2.getAccessibleContext().setAccessibleDescription(
176: org.openide.util.NbBundle.getMessage(
177: PanelConfigurePlatformVisual.class,
178: "ACSD_WizardsPlatform_Device")); // NOI18N
179:
180: jLabel3.setLabelFor(jComboProfile);
181: org.openide.awt.Mnemonics.setLocalizedText(jLabel3,
182: org.openide.util.NbBundle.getMessage(
183: PanelConfigurePlatformVisual.class,
184: "LBL_WizardsPlatform_Profile")); // NOI18N
185: gridBagConstraints = new java.awt.GridBagConstraints();
186: gridBagConstraints.gridx = 0;
187: gridBagConstraints.gridy = 2;
188: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
189: gridBagConstraints.insets = new java.awt.Insets(0, 0, 3, 0);
190: add(jLabel3, gridBagConstraints);
191: jLabel3.getAccessibleContext().setAccessibleName(
192: org.openide.util.NbBundle.getMessage(
193: PanelConfigurePlatformVisual.class,
194: "ACSN_WizardsPlatform_Profile")); // NOI18N
195: jLabel3.getAccessibleContext().setAccessibleDescription(
196: org.openide.util.NbBundle.getMessage(
197: PanelConfigurePlatformVisual.class,
198: "ACSD_WizardsPlatform_Profile")); // NOI18N
199:
200: jComboPlatform
201: .setModel(new javax.swing.DefaultComboBoxModel(
202: new String[] { "Item 1", "Item 2", "Item 3",
203: "Item 4" }));
204: gridBagConstraints = new java.awt.GridBagConstraints();
205: gridBagConstraints.gridx = 1;
206: gridBagConstraints.gridy = 0;
207: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
208: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
209: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
210: gridBagConstraints.weightx = 1.0;
211: gridBagConstraints.insets = new java.awt.Insets(0, 6, 5, 0);
212: add(jComboPlatform, gridBagConstraints);
213:
214: jComboBoxDevice.setModel(new javax.swing.DefaultComboBoxModel(
215: new String[] { "Default" }));
216: gridBagConstraints = new java.awt.GridBagConstraints();
217: gridBagConstraints.gridx = 1;
218: gridBagConstraints.gridy = 1;
219: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
220: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
221: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
222: gridBagConstraints.weightx = 1.0;
223: gridBagConstraints.insets = new java.awt.Insets(0, 6, 5, 0);
224: add(jComboBoxDevice, gridBagConstraints);
225:
226: jComboProfile.setModel(new javax.swing.DefaultComboBoxModel(
227: new String[] { "Default" }));
228: gridBagConstraints = new java.awt.GridBagConstraints();
229: gridBagConstraints.gridx = 1;
230: gridBagConstraints.gridy = 2;
231: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
232: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
233: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
234: gridBagConstraints.weightx = 1.0;
235: gridBagConstraints.insets = new java.awt.Insets(0, 6, 5, 0);
236: add(jComboProfile, gridBagConstraints);
237: gridBagConstraints = new java.awt.GridBagConstraints();
238: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
239: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
240: gridBagConstraints.weightx = 1.0;
241: add(jSeparator1, gridBagConstraints);
242:
243: containerPanel.setLayout(new java.awt.BorderLayout());
244: gridBagConstraints = new java.awt.GridBagConstraints();
245: gridBagConstraints.gridx = 0;
246: gridBagConstraints.gridy = 5;
247: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
248: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
249: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
250: gridBagConstraints.weightx = 1.0;
251: gridBagConstraints.weighty = 1.0;
252: add(containerPanel, gridBagConstraints);
253:
254: getAccessibleContext().setAccessibleName(
255: org.openide.util.NbBundle.getMessage(
256: PanelConfigurePlatformVisual.class,
257: "ACSN_WizardsPlatform")); // NOI18N
258: getAccessibleContext().setAccessibleDescription(
259: org.openide.util.NbBundle.getMessage(
260: PanelConfigurePlatformVisual.class,
261: "ACSD_WizardsPlatform")); // NOI18N
262: }// </editor-fold>//GEN-END:initComponents
263:
264: void read(WizardDescriptor wizardDescriptor) {
265:
266: jComboPlatform.setModel(model = new PlatformComboBoxModel(
267: platformType));//todo
268: selectPlatformPanel();
269: selectDevice();
270: selectProfile();
271: jComboPlatform.addItemListener(new ItemListener() {
272: public void itemStateChanged(ItemEvent e) {
273: selectPlatformPanel();
274: selectDevice();
275: selectProfile();
276: panel.fireChangeEvent();
277:
278: }
279: });
280:
281: jComboBoxDevice.addItemListener(new ItemListener() {
282: public void itemStateChanged(ItemEvent e) {
283: selectProfile();
284: panel.fireChangeEvent();
285: }
286: });
287:
288: panel.fireChangeEvent();
289:
290: String s = (String) wizardDescriptor
291: .getProperty("activePlatform");
292:
293: if (s != null) {
294: for (int i = 0; i < jComboPlatform.getItemCount(); i++) {
295: if (((PlatformKey) jComboPlatform.getItemAt(i))
296: .getDisplayName().equals(s))
297: jComboPlatform.setSelectedIndex(i);
298: }
299: }
300: s = (String) wizardDescriptor.getProperty("activeDevice");
301: if (s != null)
302: jComboBoxDevice.setSelectedItem(s);
303: s = (String) wizardDescriptor.getProperty("activeProfile");
304: if (s != null)
305: jComboProfile.setSelectedItem(s);
306: }
307:
308: void store(WizardDescriptor d) {
309: d.putProperty("activePlatform", ((PlatformKey) jComboPlatform
310: .getSelectedItem()).getDisplayName());
311: d
312: .putProperty("activeDevice", jComboBoxDevice
313: .getSelectedItem());
314: d.putProperty("activeProfile", jComboProfile.getSelectedItem());
315: }
316:
317: boolean valid(WizardDescriptor wizardDescriptor) {
318: wizardDescriptor.putProperty("WizardPanel_errorMessage", " "); //NOI18N
319: return true;
320: }
321:
322: // Variables declaration - do not modify//GEN-BEGIN:variables
323: private javax.swing.JPanel containerPanel;
324: private javax.swing.JComboBox jComboBoxDevice;
325: private javax.swing.JComboBox jComboPlatform;
326: private javax.swing.JComboBox jComboProfile;
327: private javax.swing.JLabel jLabel1;
328: private javax.swing.JLabel jLabel2;
329: private javax.swing.JLabel jLabel3;
330: private javax.swing.JSeparator jSeparator1;
331:
332: // End of variables declaration//GEN-END:variables
333:
334: private static class PlatformKey implements Comparable {
335:
336: private String name;
337: private JavaPlatform platform;
338:
339: public PlatformKey(JavaPlatform platform) {
340: //assert platform != null;
341: this .platform = platform;
342: }
343:
344: public int compareTo(Object o) {
345: return this .getDisplayName().compareTo(
346: ((PlatformKey) o).getDisplayName());
347: }
348:
349: public boolean equals(Object other) {
350: if (other instanceof PlatformKey) {
351: PlatformKey otherKey = (PlatformKey) other;
352: return (this .platform == null ? otherKey.platform == null
353: : this .platform.equals(otherKey.platform))
354: && otherKey.getDisplayName().equals(
355: this .getDisplayName());
356: }
357: return false;
358: }
359:
360: public int hashCode() {
361: return getDisplayName().hashCode();
362: }
363:
364: public String toString() {
365: return getDisplayName();
366: }
367:
368: public synchronized String getDisplayName() {
369: if (this .name == null) {
370: this .name = this .platform.getDisplayName();
371: }
372: return this .name;
373: }
374:
375: public synchronized CDCPlatform getPlatform() {
376: return (CDCPlatform) this .platform;
377: }
378: }
379:
380: private static class PlatformComboBoxModel extends
381: AbstractListModel implements ComboBoxModel {
382:
383: private JavaPlatformManager pm;
384: private PlatformKey[] platformNamesCache;
385: private PlatformKey selectedPlatform;
386: private String platformType;
387:
388: public PlatformComboBoxModel(String platformType) {
389: this .pm = JavaPlatformManager.getDefault();
390: this .platformType = platformType;
391: }
392:
393: public int getSize() {
394: PlatformKey[] platformNames = getPlatformNames();
395: return platformNames.length;
396: }
397:
398: public Object getElementAt(int index) {
399: PlatformKey[] platformNames = getPlatformNames();
400: assert index >= 0 && index < platformNames.length;
401: return platformNames[index];
402: }
403:
404: public Object getSelectedItem() {
405: this .getPlatformNames(); //Force setting of selectedPlatform if it is not alredy done
406: return this .selectedPlatform;
407: }
408:
409: public void setSelectedItem(Object obj) {
410: this .selectedPlatform = (PlatformKey) obj;
411: this .fireContentsChanged(this , -1, -1);
412: }
413:
414: private synchronized PlatformKey[] getPlatformNames() {
415: Set<String> accepted = null;
416: if (platformType != null) {
417: accepted = new HashSet<String>();
418: StringTokenizer st = new StringTokenizer(platformType,
419: ",");
420: while (st.hasMoreTokens()) {
421: accepted.add(st.nextToken());
422: }
423: }
424:
425: if (this .platformNamesCache == null) {
426: JavaPlatform[] platforms = pm.getPlatforms(null,
427: new Specification(CDCPlatform.PLATFORM_CDC,
428: null)); //NOI18N
429: Set<PlatformKey> orderedNames = new TreeSet<PlatformKey>();
430: for (JavaPlatform platform : platforms) {
431: if (platform.getInstallFolders().size() > 0
432: && (accepted == null || accepted
433: .contains(((CDCPlatform) platform)
434: .getType()))) {
435: PlatformKey pk = new PlatformKey(platform);
436: orderedNames.add(pk);
437: if (this .selectedPlatform == null) {
438: this .selectedPlatform = pk;
439: }
440: }
441: }
442: this .platformNamesCache = orderedNames
443: .toArray(new PlatformKey[orderedNames.size()]);
444: }
445: return this.platformNamesCache;
446: }
447: }
448: }
|