1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package triptracker.client.gps.ui;
21
22 import java.awt.event.ActionListener;
23 import java.util.List;
24 import javax.swing.BorderFactory;
25 import javax.swing.JButton;
26 import javax.swing.JCheckBox;
27 import javax.swing.JComboBox;
28 import javax.swing.JComponent;
29 import javax.swing.JLabel;
30 import javax.swing.JPanel;
31 import triptracker.client.gps.core.GPSClientModel;
32 import triptracker.client.ui.Form;
33 import se.datadosen.component.RiverLayout;
34
35 public class GPSSettingsForm implements Form {
36 protected static int nameCount = 1;
37 protected final String name;
38
39 private GPSClientModel model;
40 private GPSGui view;
41
42 private final JPanel mainPanel, portPanel, portBorderPanel, checkPanel,
43 checkBorderPanel;
44 protected final JButton saveButton, cancelButton;
45 private JComboBox portBox;
46 private JCheckBox compressBox, autoTransferBox;
47
48 /***
49 * The constructor creates a JPanel for the login information, and registers
50 * as a form on the <code>view</code>'s FormManager.
51 *
52 * @param view parent form manager view
53 * @param model main GPS model
54 */
55 public GPSSettingsForm(GPSGui view, GPSClientModel model) {
56 this(view, GPSSettingsForm.class.getClass().getName() + "-"
57 + nameCount++, model);
58 }
59
60 /***
61 * The constructor creates a JPanel for the login information, and registers
62 * as a form on the <code>view</code>. The form name should be unique for
63 * the given form manager.
64 *
65 * @param view parent JComponent form manager
66 * @param name unique form name
67 * @param model GPS model
68 */
69 public GPSSettingsForm(GPSGui view, String name, GPSClientModel model) {
70 this.name = name;
71 this.model = model;
72 this.view = view;
73
74 mainPanel = new JPanel();
75 mainPanel.setLayout(new RiverLayout());
76
77 portPanel = new JPanel();
78 portPanel.setLayout(new RiverLayout());
79 portPanel.setBorder(BorderFactory.createTitledBorder(null,
80 "COM-port to communicate with the GPS-unit"));
81
82 checkBorderPanel = new JPanel();
83 checkBorderPanel.setBorder(
84 BorderFactory.createEmptyBorder(10, 10, 10, 10));
85
86 portBorderPanel = new JPanel();
87 portBorderPanel.setBorder(
88 BorderFactory.createEmptyBorder(10, 10, 10, 10));
89
90 checkPanel = new JPanel();
91 checkPanel.setLayout(new RiverLayout());
92 checkPanel.setBorder(BorderFactory.createTitledBorder(null,
93 "Buffer-related settings"));
94
95 portBox = new JComboBox();
96
97 saveButton = new JButton("Save");
98 cancelButton = new JButton("Cancel");
99
100 compressBox = new JCheckBox("Compress buffered");
101 compressBox.setSelected(false);
102
103 autoTransferBox = new JCheckBox("Auto transfer buffered");
104 compressBox.setSelected(false);
105
106 portPanel.add("center", new JLabel("Select port"));
107 portPanel.add("center", portBox);
108
109 portBorderPanel.add(portPanel);
110
111 checkPanel.add("center",compressBox);
112 checkPanel.add("br center",autoTransferBox);
113
114 checkBorderPanel.add(checkPanel);
115
116
117 compressBox.setEnabled(false);
118
119 mainPanel.add("center", new JLabel("TripTracker Settings"));
120 mainPanel.add("p center hfill", portBorderPanel);
121 mainPanel.add("p center hfill", checkBorderPanel);
122 mainPanel.add("p center", saveButton);
123 mainPanel.add("center", cancelButton);
124
125
126 view.register(this);
127 new GPSSettingsController(model, this);
128 }
129
130 /***
131 * Register a listener on the login button.
132 *
133 * @param listener
134 */
135 public void addBtnListener(ActionListener listener) {
136 saveButton.addActionListener(listener);
137 cancelButton.addActionListener(listener);
138 }
139
140 public JComponent getFormComponent() {
141 return mainPanel;
142 }
143
144 public String getName() {
145 return name;
146 }
147
148 public void showMain() {
149 view.showMainForm();
150 }
151
152 /*** {@inheritDoc} */
153 public void refreshView() {
154
155 portBox.removeAllItems();
156 List<String> ports = model.getComPorts();
157 for (String port : ports) {
158
159 System.out.println("Found Port " + port);
160 portBox.addItem(port);
161 }
162 }
163
164 /***
165 * Checks if the compressbox is selected
166 *
167 * @return true if selected
168 */
169 public boolean getCompression() {
170 return compressBox.isSelected();
171 }
172
173 /***
174 * Gets the selected port
175 *
176 * @return the selected port as String.
177 */
178 public String getPort() {
179 return (String) portBox.getSelectedItem();
180 }
181
182 /***
183 * Checks if the autotransfer box is selected.
184 *
185 * @return true if selected
186 */
187 public boolean getAutoTransfer() {
188 return autoTransferBox.isSelected();
189 }
190
191 /***
192 * Sets an status message.
193 *
194 * @param msg the status message
195 */
196 public void setStatusMsg(String msg) {
197 view.setStatusMsg(msg);
198
199 }
200
201 /***
202 * Show the route form
203 */
204 public void showRoutes() {
205 view.showRoutesForm();
206 }
207 }