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;
043:
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.util.Arrays;
047: import java.util.HashMap;
048: import java.util.Map;
049: import javax.swing.JPanel;
050: import org.netbeans.api.java.platform.JavaPlatform;
051: import org.netbeans.api.java.platform.JavaPlatformManager;
052: import org.netbeans.api.java.platform.PlatformsCustomizer;
053: import org.netbeans.api.java.platform.Specification;
054: import org.netbeans.api.mobility.project.ui.customizer.ProjectProperties;
055: import org.netbeans.modules.j2me.cdc.platform.CDCDevice;
056: import org.netbeans.modules.j2me.cdc.platform.CDCPlatform;
057: import org.netbeans.modules.mobility.project.DefaultPropertiesDescriptor;
058: import org.netbeans.spi.mobility.project.ui.customizer.CustomizerPanel;
059: import org.netbeans.spi.mobility.project.ui.customizer.VisualPropertyGroup;
060: import org.netbeans.spi.mobility.project.ui.customizer.support.VisualPropertySupport;
061: import org.openide.util.NbBundle;
062:
063: /**
064: *
065: * @author Adam Sotona
066: */
067: public class CDCPlatformCustomizer extends JPanel implements
068: CustomizerPanel, VisualPropertyGroup, ActionListener {
069:
070: private static final String[] PROPERTY_NAMES = new String[] {
071: DefaultPropertiesDescriptor.PLATFORM_ACTIVE,
072: DefaultPropertiesDescriptor.PLATFORM_ACTIVE_DESCRIPTION,
073: DefaultPropertiesDescriptor.PLATFORM_DEVICE,
074: DefaultPropertiesDescriptor.PLATFORM_PROFILE,
075: DefaultPropertiesDescriptor.PLATFORM_BOOTCLASSPATH,
076: DefaultPropertiesDescriptor.PLATFORM_TYPE,
077: DefaultPropertiesDescriptor.JAVAC_SOURCE,
078: DefaultPropertiesDescriptor.JAVAC_TARGET,
079: CDCPropertiesDescriptor.PLATFORM_FAT_JAR };
080:
081: private Map<String, Object> props;
082: private VisualPropertySupport vps;
083: private String configuration;
084: private String platformNames[];
085: private HashMap<String, CDCPlatform> name2platform;
086: private HashMap<String, CDCDevice> name2device;
087: private HashMap<String, CDCDevice.CDCProfile> name2profile;
088: private boolean useDefault;
089:
090: /** Creates new form CDCPlatformCustomizer */
091: public CDCPlatformCustomizer() {
092: initComponents();
093: initAll();
094: }
095:
096: public void initValues(ProjectProperties props, String configuration) {
097: this .props = props;
098: this .vps = VisualPropertySupport.getDefault(props);
099: this .configuration = configuration;
100: }
101:
102: public void initGroupValues(boolean useDefault) {
103: jComboBoxTarget.removeActionListener(this );
104: if (platformNames.length > 0) {
105: this .useDefault = useDefault;
106: vps.register(jComboBoxTarget, platformNames,
107: DefaultPropertiesDescriptor.PLATFORM_ACTIVE,
108: useDefault);
109: jComboBoxTarget.addActionListener(this );
110: } else {
111: jComboBoxTarget.removeAllItems();
112: final String errorMessage = NbBundle.getMessage(
113: CDCPlatformCustomizer.class,
114: "ERR_CDCCust_NoPlatform"); //NOI18N
115: jComboBoxTarget.addItem(errorMessage);
116: jComboBoxTarget.setSelectedItem(errorMessage);
117: jComboBoxTarget.setEnabled(false);
118: }
119: initDevices((String) jComboBoxTarget.getSelectedItem());
120: enableLabels(!useDefault);
121: // props.put(VisualPropertySupport.translatePropertyName(configuration, DefaultPropertiesDescriptor.JAVAC_SOURCE, useDefault), "1.3"); //NOI18N
122: // props.put(VisualPropertySupport.translatePropertyName(configuration, DefaultPropertiesDescriptor.JAVAC_TARGET, useDefault), "1.3"); //NOI18N
123: }
124:
125: private synchronized void initAll() {
126: name2platform = new HashMap();
127: name2device = new HashMap();
128: name2profile = new HashMap();
129: // Read defined platforms and all configurations, profiles and optional packages
130: final JavaPlatform[] platforms = JavaPlatformManager
131: .getDefault().getPlatforms(
132: null,
133: new Specification(CDCPlatform.PLATFORM_CDC,
134: null));
135: for (int i = 0; i < platforms.length; i++) {
136: if (platforms[i] instanceof CDCPlatform) {
137: final CDCPlatform platform = (CDCPlatform) platforms[i];
138: name2platform.put(platform.getDisplayName(), platform);
139: }
140: }
141: platformNames = name2platform.keySet().toArray(
142: new String[name2platform.size()]);
143: Arrays.sort(platformNames);
144: }
145:
146: private synchronized void initDevices(final String platformName) {
147: jComboBoxDevice.removeActionListener(this );
148: final CDCPlatform platform = name2platform.get(platformName);
149: if (platform != null) {
150: props
151: .put(
152: VisualPropertySupport
153: .translatePropertyName(
154: configuration,
155: DefaultPropertiesDescriptor.PLATFORM_ACTIVE_DESCRIPTION,
156: useDefault), platform
157: .getDisplayName());
158: props.put(VisualPropertySupport.translatePropertyName(
159: configuration,
160: CDCPropertiesDescriptor.PLATFORM_FAT_JAR,
161: useDefault), Boolean.valueOf(platform.isFatJar()));
162: props.put(VisualPropertySupport.translatePropertyName(
163: configuration,
164: DefaultPropertiesDescriptor.JAVAC_SOURCE,
165: useDefault), platform.getClassVersion());
166: props.put(VisualPropertySupport.translatePropertyName(
167: configuration,
168: DefaultPropertiesDescriptor.JAVAC_TARGET,
169: useDefault), platform.getClassVersion());
170: props.put(VisualPropertySupport.translatePropertyName(
171: configuration,
172: DefaultPropertiesDescriptor.PLATFORM_TYPE,
173: useDefault), platform.getType());
174: final CDCDevice[] devices = platform.getDevices();
175: name2device = new HashMap<String, CDCDevice>();
176: for (int i = 0; i < devices.length; i++) {
177: name2device.put(devices[i].getName(), devices[i]);
178: }
179: String[] devNames = name2device.keySet().toArray(
180: new String[name2device.size()]);
181: Arrays.sort(devNames);
182: vps.register(jComboBoxDevice, devNames,
183: DefaultPropertiesDescriptor.PLATFORM_DEVICE,
184: useDefault);
185: initProfiles((String) jComboBoxDevice.getSelectedItem());
186: jComboBoxDevice.addActionListener(this );
187: } else {
188: jComboBoxDevice.removeAllItems();
189: jComboBoxDevice.setEnabled(false);
190: enableLabels(false);
191: }
192: }
193:
194: private synchronized void initProfiles(final String deviceName) {
195: jComboBoxProfile.removeActionListener(this );
196: final CDCDevice device = name2device.get(deviceName);
197: if (device != null) {
198: CDCDevice.CDCProfile[] profiles = device.getProfiles();
199: name2profile = new HashMap<String, CDCDevice.CDCProfile>();
200: for (int i = 0; i < profiles.length; i++) {
201: name2profile.put(profiles[i].getName(), profiles[i]);
202: }
203: String[] profNames = name2profile.keySet().toArray(
204: new String[name2profile.size()]);
205: Arrays.sort(profNames);
206: vps.register(jComboBoxProfile, profNames,
207: DefaultPropertiesDescriptor.PLATFORM_PROFILE,
208: useDefault);
209: jComboBoxProfile.addActionListener(this );
210: } else {
211: jComboBoxProfile.removeAllItems();
212: jComboBoxProfile.setEnabled(false);
213: }
214: saveClassPath();
215: }
216:
217: private void saveClassPath() {
218: CDCDevice.CDCProfile profile = name2profile
219: .get(jComboBoxProfile.getSelectedItem());
220: if (profile != null)
221: props.put(VisualPropertySupport.translatePropertyName(
222: configuration,
223: DefaultPropertiesDescriptor.PLATFORM_BOOTCLASSPATH,
224: useDefault), profile.getBootClassPath());
225: }
226:
227: public String[] getGroupPropertyNames() {
228: return PROPERTY_NAMES;
229: }
230:
231: private void enableLabels(final boolean enabled) {
232: jLabel1.setEnabled(enabled);
233: jLabel2.setEnabled(enabled);
234: jLabelTarget.setEnabled(enabled);
235: jButton1.setEnabled(enabled);
236: }
237:
238: public void actionPerformed(final ActionEvent e) {
239: if (jComboBoxTarget.equals(e.getSource())) {
240: initDevices((String) jComboBoxTarget.getSelectedItem());
241: } else if (jComboBoxDevice.equals(e.getSource())) {
242: initProfiles((String) jComboBoxDevice.getSelectedItem());
243: } else if (jComboBoxProfile.equals(e.getSource())) {
244: saveClassPath();
245: }
246: }
247:
248: /** This method is called from within the constructor to
249: * initialize the form.
250: * WARNING: Do NOT modify this code. The content of this method is
251: * always regenerated by the Form Editor.
252: */
253: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
254: private void initComponents() {
255: java.awt.GridBagConstraints gridBagConstraints;
256:
257: jLabelTarget = new javax.swing.JLabel();
258: jComboBoxTarget = new javax.swing.JComboBox();
259: jButton1 = new javax.swing.JButton();
260: jLabel1 = new javax.swing.JLabel();
261: jLabel2 = new javax.swing.JLabel();
262: jComboBoxDevice = new javax.swing.JComboBox();
263: jComboBoxProfile = new javax.swing.JComboBox();
264: jPanel1 = new javax.swing.JPanel();
265:
266: setLayout(new java.awt.GridBagLayout());
267:
268: jLabelTarget.setLabelFor(jComboBoxTarget);
269: org.openide.awt.Mnemonics.setLocalizedText(jLabelTarget,
270: org.openide.util.NbBundle.getMessage(
271: CDCPlatformCustomizer.class,
272: "CDCPlatformCustomizer.jLabelTarget.text")); // NOI18N
273: gridBagConstraints = new java.awt.GridBagConstraints();
274: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
275: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 12);
276: add(jLabelTarget, gridBagConstraints);
277: gridBagConstraints = new java.awt.GridBagConstraints();
278: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
279: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
280: gridBagConstraints.weightx = 1.0;
281: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
282: add(jComboBoxTarget, gridBagConstraints);
283:
284: org.openide.awt.Mnemonics.setLocalizedText(jButton1,
285: org.openide.util.NbBundle.getMessage(
286: CDCPlatformCustomizer.class,
287: "CDCPlatformCustomizer.jButton1.text")); // NOI18N
288: jButton1.addActionListener(new java.awt.event.ActionListener() {
289: public void actionPerformed(java.awt.event.ActionEvent evt) {
290: jButton1createNewPlatform(evt);
291: }
292: });
293: gridBagConstraints = new java.awt.GridBagConstraints();
294: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
295: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
296: gridBagConstraints.insets = new java.awt.Insets(0, 6, 5, 0);
297: add(jButton1, gridBagConstraints);
298:
299: jLabel1.setLabelFor(jComboBoxDevice);
300: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
301: org.openide.util.NbBundle.getMessage(
302: CDCPlatformCustomizer.class,
303: "CDCPlatformCustomizer.jLabel1.text")); // NOI18N
304: gridBagConstraints = new java.awt.GridBagConstraints();
305: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
306: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 12);
307: add(jLabel1, gridBagConstraints);
308:
309: jLabel2.setLabelFor(jComboBoxProfile);
310: org.openide.awt.Mnemonics.setLocalizedText(jLabel2,
311: org.openide.util.NbBundle.getMessage(
312: CDCPlatformCustomizer.class,
313: "CDCPlatformCustomizer.jLabel2.text")); // NOI18N
314: gridBagConstraints = new java.awt.GridBagConstraints();
315: gridBagConstraints.gridx = 0;
316: gridBagConstraints.gridy = 2;
317: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
318: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
319: add(jLabel2, gridBagConstraints);
320: gridBagConstraints = new java.awt.GridBagConstraints();
321: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
322: gridBagConstraints.weightx = 1.0;
323: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
324: add(jComboBoxDevice, gridBagConstraints);
325: gridBagConstraints = new java.awt.GridBagConstraints();
326: gridBagConstraints.gridx = 1;
327: gridBagConstraints.gridy = 2;
328: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
329: gridBagConstraints.weightx = 1.0;
330: add(jComboBoxProfile, gridBagConstraints);
331:
332: org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(
333: jPanel1);
334: jPanel1.setLayout(jPanel1Layout);
335: jPanel1Layout.setHorizontalGroup(jPanel1Layout
336: .createParallelGroup(
337: org.jdesktop.layout.GroupLayout.LEADING).add(0,
338: 400, Short.MAX_VALUE));
339: jPanel1Layout.setVerticalGroup(jPanel1Layout
340: .createParallelGroup(
341: org.jdesktop.layout.GroupLayout.LEADING).add(0,
342: 227, Short.MAX_VALUE));
343:
344: gridBagConstraints = new java.awt.GridBagConstraints();
345: gridBagConstraints.gridx = 0;
346: gridBagConstraints.gridy = 3;
347: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
348: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
349: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
350: gridBagConstraints.weightx = 1.0;
351: gridBagConstraints.weighty = 1.0;
352: add(jPanel1, gridBagConstraints);
353: }// </editor-fold>//GEN-END:initComponents
354:
355: private void jButton1createNewPlatform(
356: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1createNewPlatform
357: PlatformsCustomizer.showCustomizer(name2platform
358: .get(jComboBoxTarget.getSelectedItem()));
359: initAll();
360: initGroupValues(useDefault);
361: }//GEN-LAST:event_jButton1createNewPlatform
362:
363: // Variables declaration - do not modify//GEN-BEGIN:variables
364: private javax.swing.JButton jButton1;
365: private javax.swing.JComboBox jComboBoxDevice;
366: private javax.swing.JComboBox jComboBoxProfile;
367: private javax.swing.JComboBox jComboBoxTarget;
368: private javax.swing.JLabel jLabel1;
369: private javax.swing.JLabel jLabel2;
370: private javax.swing.JLabel jLabelTarget;
371: private javax.swing.JPanel jPanel1;
372: // End of variables declaration//GEN-END:variables
373:
374: }
|