1 /*
2 * TelescopeDialog.java
3 *
4 * Created on April 21, 2003, 2:45 PM
5 */
6
7 package com.raben.telescope.comm;
8 import java.util.Iterator;
9 import java.util.Properties;
10 import java.io.File;
11 import java.io.FileOutputStream;
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 import javax.swing.JComboBox;
15
16
17 /***
18 * TelescopeDialog
19 * @author Vern Raben
20 * @version $Revision: 1.1.1.1 $ $Date: 2003/06/13 04:50:58 $
21 */
22 public class ConfigureDialog extends javax.swing.JDialog {
23
24 /***
25 * Creates new form TelescopeDialog
26 * @param parent Reference to parent frame
27 * @param modal Set to true if dialog is to be modal
28 */
29 public ConfigureDialog(java.awt.Frame parent, boolean modal) {
30 super(parent, modal);
31 initComponents();
32 initConnectionCombos();
33 initTelescopeModelCombo();
34 loadProperties();
35 }
36
37 /*** This method is called from within the constructor to
38 * initialize the form.
39 * WARNING: Do NOT modify this code. The content of this method is
40 * always regenerated by the Form Editor.
41 */
42 private void initComponents() {//GEN-BEGIN:initComponents
43 connectionPanel = new javax.swing.JPanel();
44 jPanel7 = new javax.swing.JPanel();
45 jLabel1 = new javax.swing.JLabel();
46 modelComboBox = new javax.swing.JComboBox();
47 jPanel71 = new javax.swing.JPanel();
48 jLabel11 = new javax.swing.JLabel();
49 commPortCombo = new javax.swing.JComboBox();
50 jLabel7 = new javax.swing.JLabel();
51 flowCombo = new javax.swing.JComboBox();
52 jLabel9 = new javax.swing.JLabel();
53 parityCombo = new javax.swing.JComboBox();
54 jPanel711 = new javax.swing.JPanel();
55 jLabel111 = new javax.swing.JLabel();
56 baudRateCombo = new javax.swing.JComboBox();
57 jLabel8 = new javax.swing.JLabel();
58 stopBitsCombo = new javax.swing.JComboBox();
59 jLabel10 = new javax.swing.JLabel();
60 dataBitsCombo = new javax.swing.JComboBox();
61 jPanel8 = new javax.swing.JPanel();
62 saveButton = new javax.swing.JButton();
63 cancelButton = new javax.swing.JButton();
64
65 getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));
66
67 setTitle(java.util.ResourceBundle.getBundle("resources/comm/Telescope").getString("TELESCOPE_SETUP"));
68 addWindowListener(new java.awt.event.WindowAdapter() {
69 public void windowClosing(java.awt.event.WindowEvent evt) {
70 closeDialog(evt);
71 }
72 });
73
74 connectionPanel.setLayout(new javax.swing.BoxLayout(connectionPanel, javax.swing.BoxLayout.Y_AXIS));
75
76 connectionPanel.setBorder(new javax.swing.border.TitledBorder(java.util.ResourceBundle.getBundle("resources/comm/Telescope").getString("CONFIGURE_TELESCOPE")));
77 jPanel7.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
78
79 jLabel1.setText(java.util.ResourceBundle.getBundle("resources/comm/Telescope").getString("MODEL"));
80 jPanel7.add(jLabel1);
81
82 modelComboBox.addActionListener(new java.awt.event.ActionListener() {
83 public void actionPerformed(java.awt.event.ActionEvent evt) {
84 modelComboBoxActionPerformed(evt);
85 }
86 });
87
88 jPanel7.add(modelComboBox);
89
90 connectionPanel.add(jPanel7);
91
92 jPanel71.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
93
94 jLabel11.setText(java.util.ResourceBundle.getBundle("resources/comm/Telescope").getString("PORT"));
95 jPanel71.add(jLabel11);
96
97 commPortCombo.addActionListener(new java.awt.event.ActionListener() {
98 public void actionPerformed(java.awt.event.ActionEvent evt) {
99 commPortComboActionPerformed(evt);
100 }
101 });
102
103 jPanel71.add(commPortCombo);
104
105 jLabel7.setText(java.util.ResourceBundle.getBundle("resources/comm/Telescope").getString("FLOW_CONTROL"));
106 jPanel71.add(jLabel7);
107
108 flowCombo.setEnabled(false);
109 flowCombo.addActionListener(new java.awt.event.ActionListener() {
110 public void actionPerformed(java.awt.event.ActionEvent evt) {
111 flowComboActionPerformed(evt);
112 }
113 });
114
115 jPanel71.add(flowCombo);
116
117 jLabel9.setText(java.util.ResourceBundle.getBundle("resources/comm/Telescope").getString("PARITY"));
118 jPanel71.add(jLabel9);
119
120 parityCombo.setEnabled(false);
121 parityCombo.addActionListener(new java.awt.event.ActionListener() {
122 public void actionPerformed(java.awt.event.ActionEvent evt) {
123 parityComboActionPerformed(evt);
124 }
125 });
126
127 jPanel71.add(parityCombo);
128
129 connectionPanel.add(jPanel71);
130
131 jPanel711.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
132
133 jLabel111.setText(java.util.ResourceBundle.getBundle("resources/comm/Telescope").getString("BAUD_RATE"));
134 jPanel711.add(jLabel111);
135
136 baudRateCombo.setEnabled(false);
137 baudRateCombo.addActionListener(new java.awt.event.ActionListener() {
138 public void actionPerformed(java.awt.event.ActionEvent evt) {
139 baudRateComboActionPerformed(evt);
140 }
141 });
142
143 jPanel711.add(baudRateCombo);
144
145 jLabel8.setText(java.util.ResourceBundle.getBundle("resources/comm/Telescope").getString("STOPBITS"));
146 jPanel711.add(jLabel8);
147
148 stopBitsCombo.setEnabled(false);
149 stopBitsCombo.addActionListener(new java.awt.event.ActionListener() {
150 public void actionPerformed(java.awt.event.ActionEvent evt) {
151 stopBitsComboActionPerformed(evt);
152 }
153 });
154
155 jPanel711.add(stopBitsCombo);
156
157 jLabel10.setText(java.util.ResourceBundle.getBundle("resources/comm/Telescope").getString("DATA_BITS"));
158 jPanel711.add(jLabel10);
159
160 dataBitsCombo.setEnabled(false);
161 dataBitsCombo.addActionListener(new java.awt.event.ActionListener() {
162 public void actionPerformed(java.awt.event.ActionEvent evt) {
163 dataBitsComboActionPerformed(evt);
164 }
165 });
166
167 jPanel711.add(dataBitsCombo);
168
169 connectionPanel.add(jPanel711);
170
171 getContentPane().add(connectionPanel);
172
173 jPanel8.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
174
175 saveButton.setText(java.util.ResourceBundle.getBundle("resources/comm/Telescope").getString("SAVE"));
176 saveButton.addActionListener(new java.awt.event.ActionListener() {
177 public void actionPerformed(java.awt.event.ActionEvent evt) {
178 saveButtonActionPerformed(evt);
179 }
180 });
181
182 jPanel8.add(saveButton);
183
184 cancelButton.setText(java.util.ResourceBundle.getBundle("resources/comm/Telescope").getString("CANCEL"));
185 cancelButton.addActionListener(new java.awt.event.ActionListener() {
186 public void actionPerformed(java.awt.event.ActionEvent evt) {
187 cancelButtonActionPerformed(evt);
188 }
189 });
190
191 jPanel8.add(cancelButton);
192
193 getContentPane().add(jPanel8);
194
195 pack();
196 }//GEN-END:initComponents
197 /***
198 * Handle event when user changes number of databits
199 * @param evt Not used
200 */
201 private void dataBitsComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dataBitsComboActionPerformed
202 String data = (String) dataBitsCombo.getSelectedItem();
203 telescopeProperties.setProperty(ScopeKey.DATABITS, data);
204 }//GEN-LAST:event_dataBitsComboActionPerformed
205
206 /***
207 * Handle event that occurs when user changes number of stopbits
208 * @param evt Not used
209 */
210 private void stopBitsComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopBitsComboActionPerformed
211 String stop = (String) stopBitsCombo.getSelectedItem();
212 telescopeProperties.setProperty(ScopeKey.STOPBITS, stop);
213 }//GEN-LAST:event_stopBitsComboActionPerformed
214
215 /***
216 * Handle event that occurs when user changes baud rate
217 * @param evt Not used
218 */
219 private void baudRateComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_baudRateComboActionPerformed
220 String baud = (String) baudRateCombo.getSelectedItem();
221 telescopeProperties.setProperty(ScopeKey.BAUD, baud);
222 }//GEN-LAST:event_baudRateComboActionPerformed
223
224 /***
225 * Handle event that occurs when parity is changed
226 * @param evt Not used
227 */
228 private void parityComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_parityComboActionPerformed
229 String parity = (String) parityCombo.getSelectedItem();
230 telescopeProperties.setProperty(ScopeKey.PARITY, parity);
231 }//GEN-LAST:event_parityComboActionPerformed
232
233 /***
234 * Handle event that occurs when flow control is changed
235 * @param evt Not used
236 */
237 private void flowComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_flowComboActionPerformed
238 String flow = (String) flowCombo.getSelectedItem();
239 telescopeProperties.setProperty(ScopeKey.FLOW_CONTROL, flow);
240 }//GEN-LAST:event_flowComboActionPerformed
241
242 /***
243 * Handle event that occurs when comm port is changed
244 * @param evt Not used
245 */
246 private void commPortComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_commPortComboActionPerformed
247 String portName = (String) commPortCombo.getSelectedItem();
248 telescopeProperties.setProperty(ScopeKey.PORT, portName);
249 }//GEN-LAST:event_commPortComboActionPerformed
250
251 /***
252 * Handle event that occurs when save button is pressed
253 * @param evt Not used
254 */
255 private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
256
257 try {
258 telescopeProperties.store(new FileOutputStream(propFile),
259 java.util.ResourceBundle.getBundle(
260 "com/raben/telescope/Telescope").getString(
261 "PROPFILE_HEADER"));
262 firePropertyChange("com.raben.telescope", null,
263 telescopeProperties);
264 } catch (IOException e) {
265 System.out.println(e);
266 }
267
268 closeDialog(null);
269
270 }//GEN-LAST:event_saveButtonActionPerformed
271
272
273 /***
274 * Handle event that occurs when cancel is pressed
275 * @param evt Not used
276 */
277 private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
278 closeDialog(null);
279 }//GEN-LAST:event_cancelButtonActionPerformed
280
281 /***
282 * Handle ModelComboBox selected event
283 * @param evt Action event (not used)
284 */
285 private void modelComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modelComboBoxActionPerformed
286 String modelName = (String) modelComboBox.getSelectedItem();
287 telescopeProperties.setProperty(ScopeKey.MODEL, modelName);
288
289 int j = modelName.indexOf("Meade");
290
291 if (j != -1) {
292 baudRateCombo.setEnabled(true);
293 } else {
294 baudRateCombo.setEnabled(false);
295 }
296
297 }//GEN-LAST:event_modelComboBoxActionPerformed
298
299 /***
300 * Closes the dialog
301 * @param evt Not used
302 */
303 private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
304 setVisible(false);
305 dispose();
306
307 if (isStandalone()) {
308 System.out.println("ConfigureDialog exit");
309 System.exit(0);
310 }
311
312 }//GEN-LAST:event_closeDialog
313
314 /***
315 * Called when user executes dialog as standalone program
316 * @param args the command line arguments
317 */
318 public static void main(String args[]) {
319 ConfigureDialog configureDialog = new ConfigureDialog(
320 new javax.swing.JFrame(), true);
321 configureDialog.setStandalone(true);
322 configureDialog.show();
323 }
324
325 /***
326 * Add names of supported telescope models to combo box
327 */
328 private void initTelescopeModelCombo() {
329 Iterator iter = TelescopeModel.VALUES.iterator();
330
331 while (iter.hasNext()) {
332 TelescopeModel model = (TelescopeModel) iter.next();
333 modelComboBox.addItem(model.toString());
334 }
335 }
336
337 /***
338 * Initialize communication combos
339 */
340 private void initConnectionCombos() {
341 initCommPortCombo();
342 initBaudRateCombo();
343 initDataBitsCombo();
344 initStopBitsCombo();
345 initParityCombo();
346 initFlowControlCombo();
347 }
348
349 /***
350 * Set baud rate combo to initial values
351 */
352 private void initBaudRateCombo() {
353 baudRateCombo.addItem("2400");
354 baudRateCombo.addItem("4800");
355 baudRateCombo.addItem("9600");
356 baudRateCombo.addItem("19200");
357 baudRateCombo.addItem("38400");
358 baudRateCombo.addItem("57600");
359 baudRateCombo.addItem("115200");
360 baudRateCombo.setSelectedItem("9600");
361 }
362
363 /***
364 * Set databits combo to initial values
365 */
366 private void initDataBitsCombo() {
367 dataBitsCombo.addItem("5");
368 dataBitsCombo.addItem("6");
369 dataBitsCombo.addItem("7");
370 dataBitsCombo.addItem("8");
371 dataBitsCombo.setSelectedItem("8");
372 }
373
374 /*** Set stopbits combo to initial values */
375 private void initStopBitsCombo() {
376 stopBitsCombo.addItem("1");
377 stopBitsCombo.addItem("1.5");
378 stopBitsCombo.addItem("2");
379 stopBitsCombo.setSelectedItem("1");
380 }
381
382 /*** Set parity combo to initial values */
383 private void initParityCombo() {
384 parityCombo.addItem("None");
385 parityCombo.addItem("Even");
386 parityCombo.addItem("Mark");
387 parityCombo.addItem("Space");
388 parityCombo.setSelectedItem("None");
389 }
390
391 /*** Set flow control combo to initial values */
392 private void initFlowControlCombo() {
393 flowCombo.addItem("None");
394 flowCombo.addItem("Xon/Xoff");
395 flowCombo.addItem("Hardware");
396 }
397
398
399 /***
400 * Get list of serial comm ports and add them to combo box
401 */
402 private void initCommPortCombo() {
403
404 // Get array of comm ports
405 String[] ports = SerialConnection.getAvailablePorts();
406
407 if (ports != null) {
408 System.out.println("ports.length=" + ports.length);
409 for (int i = 0; i < ports.length; i++) {
410 commPortCombo.addItem(ports[i]);
411 }
412 }
413
414 commPortCombo.setSelectedIndex(0);
415 }
416
417 /***
418 * Return true if started from ConfigureDialog.main
419 * @return True if started from main
420 */
421 private boolean isStandalone() {
422 return this.standalone;
423 }
424
425 /***
426 * Set standlone to true if started from main
427 * @param standalone Set to true if started from ConfigureDialog
428 */
429 private void setStandalone(boolean standalone) {
430 this.standalone = standalone;
431 }
432
433 /***
434 * Load properties file if it exists
435 */
436 private void loadProperties() {
437 String homeDir = System.getProperties().getProperty("user.home");
438 propFile = homeDir + File.separator + "telescope.properties";
439 File fil = new File(propFile);
440
441 try {
442 if (fil.exists()) {
443 telescopeProperties.load(new FileInputStream(propFile));
444 setCombos();
445 }
446 } catch (IOException e) {
447 System.out.println(e);
448 }
449
450 }
451
452 /*** Set combo boxes from properties */
453 private void setCombos() {
454 setComboFromProperty(ScopeKey.PORT, commPortCombo);
455 setComboFromProperty(ScopeKey.BAUD, baudRateCombo);
456 setComboFromProperty(ScopeKey.PARITY, parityCombo);
457 setComboFromProperty(ScopeKey.DATABITS, dataBitsCombo);
458 setComboFromProperty(ScopeKey.STOPBITS, stopBitsCombo);
459 setComboFromProperty(ScopeKey.FLOW_CONTROL, flowCombo);
460 setComboFromProperty(ScopeKey.MODEL, modelComboBox);
461 }
462
463 /***
464 * Set combo box from property
465 * @param key Name of the property
466 * @param box The combo box to change
467 */
468 private void setComboFromProperty(String key, JComboBox box) {
469 String val = telescopeProperties.getProperty(key);
470
471 if (val != null) {
472 box.setSelectedItem(val);
473 }
474 }
475
476 // Variables declaration - do not modify//GEN-BEGIN:variables
477 private javax.swing.JPanel jPanel8;
478 private javax.swing.JPanel jPanel7;
479 private javax.swing.JLabel jLabel111;
480 private javax.swing.JComboBox modelComboBox;
481 private javax.swing.JComboBox parityCombo;
482 private javax.swing.JComboBox commPortCombo;
483 private javax.swing.JComboBox baudRateCombo;
484 private javax.swing.JComboBox dataBitsCombo;
485 private javax.swing.JPanel jPanel711;
486 private javax.swing.JPanel jPanel71;
487 private javax.swing.JButton saveButton;
488 private javax.swing.JLabel jLabel9;
489 private javax.swing.JLabel jLabel8;
490 private javax.swing.JLabel jLabel7;
491 private javax.swing.JButton cancelButton;
492 private javax.swing.JLabel jLabel11;
493 private javax.swing.JLabel jLabel10;
494 private javax.swing.JLabel jLabel1;
495 private javax.swing.JComboBox stopBitsCombo;
496 private javax.swing.JPanel connectionPanel;
497 private javax.swing.JComboBox flowCombo;
498 // End of variables declaration//GEN-END:variables
499
500 /*** Telescope properties */
501 private Properties telescopeProperties = new Properties();
502
503 /*** Property file name */
504 private String propFile = "";
505
506 /*** Flag indicating that application started from its on main */
507 private boolean standalone = false;
508 }
This page was automatically generated by Maven