Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
157 kaklik 1
/*
2
    AVRcamVIEW: A PC application to test out the functionallity of the
3
     AVRcam real-time image processing engine.
4
    Copyright (C) 2004    Brent A. Taylor
5
 
6
    This program is free software; you can redistribute it and/or
7
    modify it under the terms of the GNU General Public
8
    License as published by the Free Software Foundation; either
9
    version 2 of the License, or (at your option) any later version.
10
 
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
    General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public
17
    License along with this program; if not, write to the Free Software
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
20
   For more information on the AVRcamVIEW, please contact:
21
 
22
   taylorba@comcast.net
23
 
24
   or go to www.jrobot.net for more details regarding the system.
25
*/
26
 
27
package avr.swing;
28
 
29
import javax.comm.*;
30
 
31
import java.awt.*;
32
import javax.swing.*;
33
import javax.swing.border.*;
34
 
35
import avr.connection.*;
36
 
37
public class JSerialPanel extends JPanel {
38
 
39
   public static void main(String[] args) throws Exception {
40
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
41
      int option = new JSerialPanel().showDialog(null, null);
42
      System.out.println(option);
43
   }
44
 
45
   private static final Integer[] BAUD_RATES = {
46
                                              new Integer(115200),
47
                                              new Integer(57600),
48
                                              new Integer(38400),
49
                                              new Integer(19200),
50
                                              new Integer(9600),
51
                                              new Integer(4800)
52
   };
53
 
54
   private static final Integer[] DATA_BITS = {
55
                                              new Integer(8),
56
                                              new Integer(7),
57
                                              new Integer(6),
58
                                              new Integer(5)
59
   };
60
 
61
   private static final String[] PARITY = {
62
                                          "None",
63
                                          "Odd",
64
                                          "Even",
65
                                          "Mark",
66
                                          "Space"
67
   };
68
 
69
   private static final Number[] STOP_BITS = {
70
                                             new Integer(1),
71
                                             new Double(1.5),
72
                                             new Integer(2)
73
   };
74
 
75
   public static final String[] FLOW_CONTROL = {
76
                                               "None",
77
                                               "Hardware",
78
                                               "Xon / Xoff"
79
   };
80
 
81
 
82
   private static final int UNKNOWN_OPTION = 0x00;
83
   public static final int OK_OPTION = 0x01;
84
   public static final int CANCEL_OPTION = 0x02;
85
 
86
   private JComboBox baudRateCB;
87
   private JComboBox dataBitsCB;
88
   private JComboBox stopBitsCB;
89
   private JComboBox parityCB;
90
   private JComboBox flowControlCB;
91
 
92
   private JDialog dialog;
93
   private int option;
94
 
95
   public JSerialPanel() {
96
      super(new GridLayout(5, 2, 10, 10));
97
      setBorder(new EmptyBorder(5, 5, 5, 5));
98
 
99
      baudRateCB = new JComboBox(BAUD_RATES);
100
      dataBitsCB = new JComboBox(DATA_BITS);
101
      stopBitsCB = new JComboBox(STOP_BITS);
102
      parityCB = new JComboBox(PARITY);
103
      flowControlCB = new JComboBox(FLOW_CONTROL);
104
 
105
      add(new JLabel("Baud Rate:", JLabel.RIGHT));
106
      add(baudRateCB);
107
      add(new JLabel("Data Bits:", JLabel.RIGHT));
108
      add(dataBitsCB);
109
      add(new JLabel("Stop Bits:", JLabel.RIGHT));
110
      add(stopBitsCB);
111
      add(new JLabel("Parity:", JLabel.RIGHT));
112
      add(parityCB);
113
      add(new JLabel("Flow Control:", JLabel.RIGHT));
114
      add(flowControlCB);
115
   }
116
 
117
   public void setSerialParameters(SerialParams params) {
118
      if(params != null) {
119
         baudRateCB.setSelectedItem(new Integer(params.getBaudRate()));
120
         dataBitsCB.setSelectedItem(new Integer(params.getDataBits()));
121
         parityCB.setSelectedIndex(params.getParity());
122
 
123
         switch(params.getStopBits()) {
124
            case 1:
125
               stopBitsCB.setSelectedIndex(0);
126
               break;
127
            case 2:
128
               stopBitsCB.setSelectedIndex(2);
129
               break;
130
            case 3:
131
               stopBitsCB.setSelectedIndex(1);
132
               break;
133
         }
134
 
135
         if(params.getFlowControl() == SerialPort.FLOWCONTROL_NONE) {
136
            flowControlCB.setSelectedIndex(0);
137
         } else if(params.getFlowControl() == (SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT)) {
138
            flowControlCB.setSelectedIndex(1);
139
         } else if(params.getFlowControl() == (SerialPort.FLOWCONTROL_XONXOFF_IN | SerialPort.FLOWCONTROL_XONXOFF_OUT)) {
140
            flowControlCB.setSelectedIndex(2);
141
         }
142
      }
143
   }
144
 
145
   public SerialParams getSerialParameters() {
146
      int baudRate = ((Integer)baudRateCB.getSelectedItem()).intValue();
147
      int dataBits = ((Integer)dataBitsCB.getSelectedItem()).intValue();
148
      int parity = parityCB.getSelectedIndex();
149
      int stopBits = 0;
150
      int flowControl = 0;
151
 
152
      switch(stopBitsCB.getSelectedIndex()) {
153
         case 0:
154
            stopBits = 1;
155
            break;
156
         case 1:
157
            stopBits = 3;
158
            break;
159
         case 2:
160
            stopBits = 2;
161
            break;
162
      }
163
 
164
      switch(flowControlCB.getSelectedIndex()) {
165
         case 0:
166
            flowControl = SerialPort.FLOWCONTROL_NONE;
167
            break;
168
         case 1:
169
            flowControl = SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT;
170
            break;
171
         case 2:
172
            flowControl = SerialPort.FLOWCONTROL_XONXOFF_IN | SerialPort.FLOWCONTROL_XONXOFF_OUT;
173
            break;
174
      }
175
 
176
      return new SerialParams(baudRate, dataBits, stopBits, parity, flowControl);
177
   }
178
 
179
   public void ok() {
180
      option = OK_OPTION;
181
      dialog.setVisible(false);
182
   }
183
 
184
   public void cancel() {
185
      option = CANCEL_OPTION;
186
      dialog.setVisible(false);
187
   }
188
 
189
   public int showDialog(Frame owner, SerialParams params) {
190
 
191
      if(dialog == null) {
192
         dialog = new JDialog(owner, "Serial Port Parameters", true);
193
 
194
         dialog.getContentPane().add(this, BorderLayout.CENTER);
195
         dialog.getContentPane().add(createButtonPanel(), BorderLayout.SOUTH);
196
         dialog.pack();
197
         dialog.setResizable(false);
198
      }
199
 
200
      option = UNKNOWN_OPTION;
201
 
202
      setSerialParameters(params);
203
      dialog.setLocationRelativeTo(owner);
204
      dialog.setVisible(true);
205
 
206
      return option;
207
 
208
   }
209
 
210
   private JComponent createButtonPanel() {
211
 
212
      JPanel buttonP = new JPanel(new FlowLayout(FlowLayout.RIGHT));
213
 
214
      buttonP.setBorder(new EtchedBorder());
215
 
216
      buttonP.add(new JButton(new ProxyAction(this, "ok", "OK", 'o')));
217
      buttonP.add(new JButton(new ProxyAction(this, "cancel", "Cancel", 'c')));
218
 
219
      return buttonP;
220
   }
221
 
222
}