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: /*
043: * DeviceChooser.java
044: *
045: * Created on May 1, 2007, 12:09 PM
046: */
047:
048: package org.netbeans.modules.deployment.deviceanywhere;
049:
050: import java.awt.event.MouseEvent;
051: import java.awt.event.MouseListener;
052: import java.util.List;
053: import javax.swing.ListSelectionModel;
054: import javax.swing.SwingUtilities;
055: import javax.swing.event.ChangeEvent;
056: import javax.swing.event.ChangeListener;
057: import javax.swing.event.ListSelectionEvent;
058: import javax.swing.event.ListSelectionListener;
059: import org.netbeans.modules.deployment.deviceanywhere.service.ApplicationAPI;
060: import org.netbeans.modules.deployment.deviceanywhere.service.ApplicationAPIDeviceWrapper;
061: import org.netbeans.modules.deployment.deviceanywhere.service.ApplicationAPIGetLockedDevicesReturn;
062: import org.netbeans.modules.deployment.deviceanywhere.service.ApplicationAPIService;
063: import org.openide.ErrorManager;
064: import org.openide.awt.MouseUtils;
065: import org.openide.util.NbBundle;
066: import org.openide.util.RequestProcessor;
067:
068: /**
069: *
070: * @author suchys
071: */
072: public class DeviceChooser extends javax.swing.JPanel {
073:
074: protected ChangeListener changeListener;
075: private List<ApplicationAPIDeviceWrapper> devices;
076:
077: /** Creates new form DeviceChooser */
078: public DeviceChooser(String user, String password,
079: ApplicationAPIDeviceWrapper selectedDevice) {
080: initComponents();
081: initView(user, password, selectedDevice);
082: }
083:
084: private void initView(final String user, final String password,
085: final ApplicationAPIDeviceWrapper selectedDevice) {
086: deviceList
087: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
088: deviceList.setListData(getWarmupList());
089: deviceList
090: .addListSelectionListener(new ListSelectionListener() {
091: public void valueChanged(ListSelectionEvent evt) {
092: if (changeListener != null) {
093: changeListener
094: .stateChanged(new ChangeEvent(evt));
095: }
096: }
097: });
098: // support for double click to finish dialog with selected class
099: deviceList.addMouseListener(new MouseListener() {
100: public void mouseClicked(MouseEvent e) {
101: if (MouseUtils.isDoubleClick(e)) {
102: if (getSelectedDevice() != null) {
103: if (changeListener != null) {
104: changeListener
105: .stateChanged(new ChangeEvent(e));
106: }
107: }
108: }
109: }
110:
111: public void mousePressed(MouseEvent e) {
112: }
113:
114: public void mouseReleased(MouseEvent e) {
115: }
116:
117: public void mouseEntered(MouseEvent e) {
118: }
119:
120: public void mouseExited(MouseEvent e) {
121: }
122: });
123: //done
124: RequestProcessor.getDefault().post(new Runnable() {
125: public void run() {
126: try {
127: ApplicationAPIService service = new ApplicationAPIService();
128: ApplicationAPI port = service.getApplicationAPI();
129: final ApplicationAPIGetLockedDevicesReturn wrapper = port
130: .getLockedDevices(user, password);
131: final int returnCode = wrapper.getReturnCode();
132: if (returnCode != 0) {
133: SwingUtilities.invokeLater(new Runnable() {
134: public void run() {
135: String message = null;
136: if (returnCode == 1) {
137: message = NbBundle.getMessage(
138: DeviceChooser.class,
139: "MSG_InternalError"); // NOI18N
140: } else if (returnCode == 2) {
141: message = NbBundle.getMessage(
142: DeviceChooser.class,
143: "MSG_BadLogin"); // NOI18N
144: }
145: deviceList
146: .setListData(new String[] { message });
147: }
148: });
149: return;
150: }
151: devices = wrapper.getDeviceWrappers()
152: .getDeviceWrappers();
153: //only for testing
154: // devices = new ArrayList<ApplicationAPIDeviceWrapper>();
155: // ApplicationAPIDeviceWrapper dev = new ApplicationAPIDeviceWrapper();
156: // dev.setId(22);
157: // dev.setName("Kachny");
158: // devices.add(dev);
159: // dev = new ApplicationAPIDeviceWrapper();
160: // dev.setId(11);
161: // dev.setName("Kachnicky");
162: // devices.add(dev);
163: //
164: if (devices.isEmpty()) {
165: SwingUtilities.invokeLater(new Runnable() {
166: public void run() {
167: deviceList
168: .setListData(new String[] { NbBundle
169: .getMessage(
170: DeviceChooser.class,
171: "MSG_NoDevice") }); // NOI18N
172: }
173: });
174: return;
175: } else {
176: final String[] result = new String[devices
177: .size()];
178: int i = 0;
179: for (ApplicationAPIDeviceWrapper elem : devices) {
180: result[i++] = elem.getName();
181: }
182: //Arrays.sort (devices); //we must sort whole List<ApplicationAPIDeviceWrapper>
183: SwingUtilities.invokeLater(new Runnable() {
184: public void run() {
185: deviceList.setListData(result);
186: if (selectedDevice != null)
187: deviceList.setSelectedValue(
188: selectedDevice.getName(),
189: true);
190: else
191: deviceList.setSelectedIndex(0);
192: return;
193: }
194: });
195: }
196: } catch (Exception ex) {
197: SwingUtilities.invokeLater(new Runnable() {
198: public void run() {
199: deviceList
200: .setListData(new String[] { NbBundle
201: .getMessage(
202: DeviceChooser.class,
203: "MSG_ConnectionError") }); // NOI18N
204: }
205: });
206: if (ex instanceof ClassNotFoundException) {
207: ErrorManager.getDefault().notify(
208: ErrorManager.USER, ex);
209: } else {
210: ErrorManager.getDefault().notify(
211: ErrorManager.INFORMATIONAL, ex);
212: }
213: }
214: }
215: });
216:
217: // if (dialogSubtitle != null) {
218: // Mnemonics.setLocalizedText (jLabel1, dialogSubtitle);
219: // }
220: }
221:
222: /** This method is called from within the constructor to
223: * initialize the form.
224: * WARNING: Do NOT modify this code. The content of this method is
225: * always regenerated by the Form Editor.
226: */
227: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
228: private void initComponents() {
229:
230: jLabel1 = new javax.swing.JLabel();
231: jScrollPane1 = new javax.swing.JScrollPane();
232: deviceList = new javax.swing.JList();
233:
234: jLabel1.setLabelFor(deviceList);
235: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
236: org.openide.util.NbBundle.getMessage(
237: DeviceChooser.class, "LBL_AvailableDevices")); // NOI18N
238:
239: jScrollPane1.setViewportView(deviceList);
240:
241: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
242: this );
243: this .setLayout(layout);
244: layout
245: .setHorizontalGroup(layout
246: .createParallelGroup(
247: org.jdesktop.layout.GroupLayout.LEADING)
248: .add(
249: layout
250: .createSequentialGroup()
251: .addContainerGap()
252: .add(
253: layout
254: .createParallelGroup(
255: org.jdesktop.layout.GroupLayout.LEADING)
256: .add(
257: jScrollPane1,
258: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
259: 362,
260: Short.MAX_VALUE)
261: .add(jLabel1))
262: .addContainerGap()));
263: layout.setVerticalGroup(layout.createParallelGroup(
264: org.jdesktop.layout.GroupLayout.LEADING).add(
265: layout.createSequentialGroup().addContainerGap().add(
266: jLabel1).addPreferredGap(
267: org.jdesktop.layout.LayoutStyle.RELATED).add(
268: jScrollPane1,
269: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
270: 149, Short.MAX_VALUE).addContainerGap()));
271:
272: jLabel1.getAccessibleContext().setAccessibleName(
273: org.openide.util.NbBundle.getMessage(
274: DeviceChooser.class, "ACSN_AvailableDevices")); // NOI18N
275: jLabel1.getAccessibleContext().setAccessibleDescription(
276: org.openide.util.NbBundle.getMessage(
277: DeviceChooser.class, "ACSD_AvailableDevices")); // NOI18N
278: }// </editor-fold>//GEN-END:initComponents
279:
280: // Variables declaration - do not modify//GEN-BEGIN:variables
281: private javax.swing.JList deviceList;
282: private javax.swing.JLabel jLabel1;
283: private javax.swing.JScrollPane jScrollPane1;
284:
285: // End of variables declaration//GEN-END:variables
286:
287: private Object[] getWarmupList() {
288: return new Object[] { NbBundle.getMessage(DeviceChooser.class,
289: "MSG_LoadingDevices") }; // NOI18N
290: }
291:
292: public ApplicationAPIDeviceWrapper getSelectedDevice() {
293: if (isValidDeviceName(deviceList.getSelectedValue())) {
294: return devices.get(deviceList.getSelectedIndex());
295: } else {
296: return null;
297: }
298: }
299:
300: public List<ApplicationAPIDeviceWrapper> getLockedDevices() {
301: return devices;
302: }
303:
304: public synchronized void addChangeListener(ChangeListener l) {
305: changeListener = l;
306: }
307:
308: public synchronized void removeChangeListener(ChangeListener l) {
309: changeListener = null;
310: }
311:
312: private boolean isValidDeviceName(Object object) {
313: return devices != null && !devices.isEmpty();
314: }
315:
316: }
|