Source code: jflight/awt/SerialPortSetupDialog.java
1 /*
2 Project name: JFlight
3 Hosted at: www.sourceforge.net
4 Homepage: jflight.sourceforge.net
5 Licence: GNU General Public Licence
6 Filename: SerialPortSetupDialog.java
7 Package: jflight.awt
8 */
9
10 package jflight.awt;
11
12 import java.awt.*;
13 import java.awt.event.*;
14 import java.util.Enumeration;
15 import java.util.ResourceBundle;
16 import javax.comm.*;
17 import jflight.tools.SerialParameters;
18
19 /**
20 Dialog for setup of serial ports.
21
22 @since JDK1.1.x
23 @author Guido Scholz
24
25 CVS-section:
26 @file_version $Revision: 1.7 $
27 */
28
29 public class SerialPortSetupDialog extends Dialog {
30 public final static int CONFIG_VARIO = 1;
31 public final static int CONFIG_GPS = 2;
32
33 private boolean changed = false;
34 private ResourceBundle strRes = null;
35
36 Panel BasisPanel = new Panel();
37 BorderLayout BasisPanelBorderLayout = new BorderLayout();
38 FlowLayout TopFlowLayout = new FlowLayout();
39 Panel TopPanel = new Panel();
40 Panel TopBorderPanel = new Panel();
41 Panel RightGridPanel = new Panel();
42 BorderLayout RightGridPanelBorderLayout = new BorderLayout();
43 Panel LeftGridPanel = new Panel();
44 BorderLayout LeftGridPanelBorderLayout = new BorderLayout();
45 Label flowInLabel = new Label();
46 GridLayout LabelPanelGridLayout = new GridLayout();
47 Label dataBitsLabel = new Label();
48 Label portLabel = new Label();
49 Label parityLabel = new Label();
50 Panel LeftLabelPanel = new Panel();
51 GridLayout ChoisePanelGridLayout = new GridLayout();
52 Choice flowInChoice = new Choice();
53 Choice dataBitsChoice = new Choice();
54 Choice parityChoice = new Choice();
55 Choice portChoice = new Choice();
56 Panel LeftChoisePanel = new Panel();
57 Label baudLabel = new Label();
58 Panel RightLabelPanel = new Panel();
59 Label stopBitsLabel = new Label();
60 Label flowOutLabel = new Label();
61 GridLayout RightLabelPanelGridLayout = new GridLayout();
62 Choice stopBitsChoice = new Choice();
63 GridLayout RightChoisePanelGridLayout = new GridLayout();
64 Choice baudChoice = new Choice();
65 Choice flowOutChoice = new Choice();
66 Panel RightChoisePanel = new Panel();
67 BorderLayout TopBorderLayout = new BorderLayout();
68 Button CancelButton = new Button();
69 Button OKButton = new Button();
70 Button DefaultButton = new Button();
71 GridLayout ButtonGridLayout = new GridLayout();
72 Panel ButtomPanel = new Panel();
73 FlowLayout ButtomFlowLayout = new FlowLayout();
74 Panel ButtonPanel = new Panel();
75
76 public SerialPortSetupDialog(Frame owner) {
77 super(owner, true);
78 enableEvents(AWTEvent.WINDOW_EVENT_MASK);
79 strRes = ((JFlightFrame) owner).strRes;
80 // enable JBuilder designer:
81 //if (strRes == null) {
82 // strRes = ResourceBundle.getBundle("jflight.main.StringRes");
83 //}
84 try {
85 jbInit();
86 //setVisible(true);
87 }
88 catch(Exception e) {
89 e.printStackTrace();
90 }
91 pack();
92 }
93
94 private void jbInit() throws Exception {
95 //setResizable(false);
96 BasisPanel.setLayout(BasisPanelBorderLayout);
97 BasisPanelBorderLayout.setHgap(5);
98 BasisPanelBorderLayout.setVgap(5);
99 TopPanel.setLayout(TopFlowLayout);
100 TopBorderPanel.setLayout(TopBorderLayout);
101 RightGridPanel.setLayout(RightGridPanelBorderLayout);
102 LeftGridPanel.setLayout(LeftGridPanelBorderLayout);
103 flowInLabel.setText(strRes.getString("FlowControlIn")+":");
104 LabelPanelGridLayout.setRows(1);
105 LabelPanelGridLayout.setVgap(5);
106 LabelPanelGridLayout.setColumns(1);
107 LabelPanelGridLayout.setHgap(5);
108 dataBitsLabel.setText(strRes.getString("DataBits")+":");
109 portLabel.setText(strRes.getString("Port")+":");
110 parityLabel.setText(strRes.getString("Parity")+":");
111 LeftLabelPanel.setLayout(LabelPanelGridLayout);
112 ChoisePanelGridLayout.setHgap(5);
113 ChoisePanelGridLayout.setRows(1);
114 ChoisePanelGridLayout.setColumns(1);
115 ChoisePanelGridLayout.setVgap(5);
116 LeftChoisePanel.setLayout(ChoisePanelGridLayout);
117 baudLabel.setText(strRes.getString("BaudRate")+":");
118 RightLabelPanel.setLayout(RightLabelPanelGridLayout);
119 stopBitsLabel.setText(strRes.getString("StopBits")+":");
120 flowOutLabel.setText(strRes.getString("FlowControlOut")+":");
121 RightLabelPanelGridLayout.setHgap(5);
122 RightLabelPanelGridLayout.setRows(4);
123 RightLabelPanelGridLayout.setColumns(1);
124 RightLabelPanelGridLayout.setVgap(5);
125 RightChoisePanelGridLayout.setHgap(5);
126 RightChoisePanelGridLayout.setRows(4);
127 RightChoisePanelGridLayout.setColumns(1);
128 RightChoisePanelGridLayout.setVgap(5);
129 RightChoisePanel.setLayout(RightChoisePanelGridLayout);
130 TopBorderLayout.setHgap(20);
131 TopBorderLayout.setVgap(10);
132 CancelButton.setLabel(strRes.getString("Cancel"));
133 CancelButton.addActionListener(new java.awt.event.ActionListener() {
134 public void actionPerformed(ActionEvent e) {
135 CancelButton_actionPerformed(e);
136 }
137 });
138 OKButton.setLabel(strRes.getString("OK"));
139 OKButton.setActionCommand("OK");
140 OKButton.addActionListener(new java.awt.event.ActionListener() {
141 public void actionPerformed(ActionEvent e) {
142 OKButton_actionPerformed(e);
143 }
144 });
145 DefaultButton.setLabel(strRes.getString("Default"));
146 DefaultButton.addActionListener(new java.awt.event.ActionListener() {
147 public void actionPerformed(ActionEvent e) {
148 DefaultButton_actionPerformed(e);
149 }
150 });
151 ButtonGridLayout.setColumns(2);
152 ButtonGridLayout.setHgap(5);
153 ButtonGridLayout.setVgap(5);
154 ButtomPanel.setLayout(ButtomFlowLayout);
155 ButtonPanel.setLayout(ButtonGridLayout);
156 this.add(BasisPanel, null);
157
158 BasisPanel.add(TopPanel, BorderLayout.CENTER);
159 TopPanel.add(TopBorderPanel, null);
160 TopBorderPanel.add(LeftGridPanel, BorderLayout.WEST);
161 LeftGridPanel.add(LeftLabelPanel, BorderLayout.WEST);
162 LeftLabelPanel.add(portLabel, null);
163 //LeftLabelPanel.add(flowInLabel, null);
164 //LeftLabelPanel.add(dataBitsLabel, null);
165 //LeftLabelPanel.add(parityLabel, null);
166 LeftGridPanel.add(LeftChoisePanel, BorderLayout.CENTER);
167 LeftChoisePanel.add(portChoice, null);
168 //LeftChoisePanel.add(flowInChoice, null);
169 //LeftChoisePanel.add(dataBitsChoice, null);
170 //LeftChoisePanel.add(parityChoice, null);
171 TopBorderPanel.add(RightGridPanel, BorderLayout.CENTER);
172 RightGridPanel.add(RightLabelPanel, BorderLayout.WEST);
173 //RightLabelPanel.add(baudLabel, null);
174 //RightLabelPanel.add(flowOutLabel, null);
175 //RightLabelPanel.add(stopBitsLabel, null);
176 RightGridPanel.add(RightChoisePanel, BorderLayout.CENTER);
177 //RightChoisePanel.add(baudChoice, null);
178 //RightChoisePanel.add(flowOutChoice, null);
179 //RightChoisePanel.add(stopBitsChoice, null);
180 BasisPanel.add(ButtomPanel, BorderLayout.SOUTH);
181 ButtomPanel.add(ButtonPanel, null);
182 ButtonPanel.add(OKButton, null);
183 ButtonPanel.add(CancelButton, null);
184 ButtonPanel.add(DefaultButton, null);
185
186
187 flowInChoice.addItem("None");
188 flowInChoice.addItem("Xon/Xoff In");
189 flowInChoice.addItem("RTS/CTS In");
190 dataBitsChoice.addItem("5");
191 dataBitsChoice.addItem("6");
192 dataBitsChoice.addItem("7");
193 dataBitsChoice.addItem("8");
194 parityChoice.addItem("None");
195 parityChoice.addItem("Even");
196 parityChoice.addItem("Odd");
197 parityChoice.select("None");
198 stopBitsChoice.addItem("1");
199 stopBitsChoice.addItem("1.5");
200 stopBitsChoice.addItem("2");
201 baudChoice.addItem("2400");
202 baudChoice.addItem("4800");
203 baudChoice.addItem("9600");
204 baudChoice.addItem("19200");
205 baudChoice.addItem("38400");
206 baudChoice.addItem("57600");
207 baudChoice.addItem("115200");
208 flowOutChoice.addItem("None");
209 flowOutChoice.addItem("Xon/Xoff Out");
210 flowOutChoice.addItem("RTS/CTS Out");
211 // Setup the port list
212 listPortChoises();
213 }
214
215 protected void processWindowEvent(WindowEvent e) {
216 if (e.getID() == WindowEvent.WINDOW_CLOSING) {
217 cancel();
218 }
219 super.processWindowEvent(e);
220 }
221
222 void cancel() {
223 setVisible(false);
224 }
225
226 private void listPortChoises() {
227 CommPortIdentifier portId;
228 Enumeration en = CommPortIdentifier.getPortIdentifiers();
229 // iterate through the ports.
230 while (en.hasMoreElements()) {
231 portId = (CommPortIdentifier) en.nextElement();
232 if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
233 portChoice.addItem(portId.getName());
234 }
235 }
236 }
237
238 public void setParameters(SerialParameters serialParams, int mode) {
239 if (mode == CONFIG_GPS){
240 setTitle(strRes.getString("GPSSerialPortSetup"));
241 }
242 else {
243 setTitle(strRes.getString("VarioSerialPortSetup"));
244 }
245 portChoice.select(serialParams.getPortName());
246 baudChoice.select(serialParams.getBaudRateString());
247 flowInChoice.select(serialParams.getFlowControlInString());
248 flowOutChoice.select(serialParams.getFlowControlOutString());
249 dataBitsChoice.select(serialParams.getDatabitsString());
250 stopBitsChoice.select(serialParams.getStopbitsString());
251 parityChoice.select(serialParams.getParityString());
252 }
253
254 public void getParameters(SerialParameters serialParams) {
255 serialParams.setPortName(portChoice.getSelectedItem());
256 serialParams.setBaudRate(baudChoice.getSelectedItem());
257 serialParams.setFlowControlIn(flowInChoice.getSelectedItem());
258 serialParams.setFlowControlOut(flowOutChoice.getSelectedItem());
259 serialParams.setDatabits(dataBitsChoice.getSelectedItem());
260 serialParams.setStopbits(stopBitsChoice.getSelectedItem());
261 serialParams.setParity(parityChoice.getSelectedItem());
262 }
263
264 public boolean hasChanged() {
265 return changed;
266 }
267
268 void OKButton_actionPerformed(ActionEvent e) {
269 changed = true;
270 cancel();
271 }
272
273 void CancelButton_actionPerformed(ActionEvent e) {
274 changed = false;
275 cancel();
276 }
277
278 void DefaultButton_actionPerformed(ActionEvent e) {
279 portChoice.select(0);
280 baudChoice.select(2);
281 flowInChoice.select(0);
282 flowOutChoice.select(0);
283 dataBitsChoice.select(3);
284 stopBitsChoice.select(0);
285 parityChoice.select(0);
286 }
287
288 }