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 static triptracker.core.Utils.getImageIcon;
23
24 import java.awt.BorderLayout;
25 import java.awt.CardLayout;
26 import java.awt.Container;
27 import java.awt.Dimension;
28 import java.awt.event.ActionListener;
29 import java.awt.event.KeyEvent;
30 import java.util.ArrayList;
31 import java.util.Calendar;
32 import java.util.List;
33
34 import javax.swing.BorderFactory;
35 import javax.swing.ImageIcon;
36 import javax.swing.JFrame;
37 import javax.swing.JLabel;
38 import javax.swing.JMenu;
39 import javax.swing.JMenuBar;
40 import javax.swing.JMenuItem;
41 import javax.swing.JOptionPane;
42 import javax.swing.JPanel;
43
44
45
46 import triptracker.client.gps.core.GPSClientModel;
47 import triptracker.client.ui.Form;
48 import triptracker.client.ui.FormManager;
49 import triptracker.core.Utils;
50
51 /***
52 * The main GUI class for the GPS-client
53 */
54 @SuppressWarnings("serial")
55 public class GPSGui extends JFrame implements FormManager {
56
57 private final CardLayout cardLayout;
58
59 private final JPanel mainPanel = new JPanel();
60
61 private final JLabel statusBar = new JLabel(" ");
62
63 private final Container rootPane;
64
65 protected JMenuItem logOnItem, logOffItem, exitItem, generalItem,
66 showRoutesItem;
67
68 private JMenuBar menuBar;
69
70 private JMenu menu;
71
72 private Form currentForm;
73
74 private List<Form> forms = new ArrayList<Form>();
75
76 private Form login, main, settings, routes;
77
78 /***
79 * Default constructor.
80 */
81 public GPSGui(GPSClientModel model) {
82 super("Trip Tracker: GPS Client v0.8.1");
83
84 rootPane = getContentPane();
85 rootPane.setLayout(new BorderLayout());
86
87
88 cardLayout = new CardLayout();
89 mainPanel.setLayout(cardLayout);
90
91 login = new LoginForm(this, "login", model);
92 main = new MainForm(this, model);
93 routes = new RoutesForm(this, "routes", model, main);
94 settings = new GPSSettingsForm(this, "settings", model);
95
96
97 rootPane.add(mainPanel, BorderLayout.CENTER);
98 rootPane.add(statusBar, BorderLayout.SOUTH);
99
100 setJMenuBar(createMenu());
101
102 statusBar.setBorder(BorderFactory.createBevelBorder(1));
103
104 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
105
106 setIconImage(getImageIcon("tt_icon_20x20.gif").getImage());
107 setSize(new Dimension(250, 250));
108 setLocation(400, 200);
109 setResizable(false);
110 pack();
111 }
112
113 /***
114 * Creates a menubar.
115 *
116 * @return a menubar
117 */
118 public JMenuBar createMenu() {
119
120 menuBar = new JMenuBar();
121
122
123 menu = new JMenu("File");
124 menu.setMnemonic(KeyEvent.VK_F);
125 menuBar.add(menu);
126
127
128 logOnItem = new JMenuItem("Log on", getImageIcon("menu_loginIcon.gif"));
129 menu.add(logOnItem);
130
131
132 ImageIcon logoffIcon = getImageIcon("menu_logoffIcon.gif");
133 logOffItem = new JMenuItem("Log off", logoffIcon);
134 menu.add(logOffItem);
135
136 menu.addSeparator();
137
138
139 generalItem = new JMenuItem("Settings", getImageIcon("menu_settingsIcon.gif"));
140 menu.add(generalItem);
141
142
143 showRoutesItem = new JMenuItem("Routes", getImageIcon("menu_routeIcon.gif"));
144 menu.add(showRoutesItem);
145
146 menu.addSeparator();
147
148
149 exitItem = new JMenuItem("Quit", getImageIcon("exitIcon.gif"));
150 menu.add(exitItem);
151
152 return menuBar;
153 }
154
155 /***
156 * Adds the menulisteners
157 *
158 * @param listener
159 */
160 public void addMenuListener(ActionListener listener) {
161 logOnItem.addActionListener(listener);
162 logOffItem.addActionListener(listener);
163 exitItem.addActionListener(listener);
164 generalItem.addActionListener(listener);
165 showRoutesItem.addActionListener(listener);
166 }
167
168 /***
169 * Registers a form
170 */
171 public void register(Form form) {
172 forms.add(form);
173 mainPanel.add(form.getFormComponent(), form.getName());
174
175 if (currentForm == null) {
176 currentForm = form;
177 }
178 }
179
180 /***
181 * Deregisters a form
182 */
183 public void deregister(Form form) {
184 if (currentForm == form) {
185 currentForm = null;
186 }
187
188 forms.remove(form);
189 }
190
191 /***
192 * Shows the given form
193 *
194 * @param form the form to be showed
195 */
196 public void showForm(Form form) {
197
198 currentForm = form;
199 currentForm.refreshView();
200 cardLayout.show(mainPanel, form.getName());
201 }
202
203 /***
204 * Shows dialog to log off, user have to confirm
205 */
206 public boolean showLogOffDialog() {
207 int confirm = JOptionPane.showConfirmDialog(this,
208 "Do you want to log off?", "Log off?",
209 JOptionPane.YES_NO_OPTION);
210 return (confirm == 0);
211 }
212
213 /***
214 * Shows a warning dialog
215 *
216 * @param heading the heading for the dialog
217 * @param warning the warning to show in the dialog
218 */
219 public void showErrorDialog(String heading, String warning) {
220 JOptionPane.showMessageDialog(null, warning, heading,
221 JOptionPane.ERROR_MESSAGE);
222 }
223
224 /***
225 * Shows a info dialog
226 *
227 * @param head the header
228 * @param info the info message
229 */
230 public void showInfoDialog(String head, String info) {
231 JOptionPane.showMessageDialog(null, info, head,
232 JOptionPane.INFORMATION_MESSAGE);
233 }
234
235 /***
236 * Set the text of the status bar.
237 *
238 * @param msg status message
239 */
240 public void setStatusMsg(String msg) {
241 statusBar.setText("(" + Utils.dateToString(
242 Calendar.getInstance().getTime(), "HH:mm:ss") + ") " + msg);
243 }
244
245 /***
246 * Shows the login form
247 *
248 */
249 public void showLoginForm() {
250 showForm(login);
251 }
252
253 /***
254 * Shows the settings form
255 *
256 */
257 public void showSettingsForm() {
258 showForm(settings);
259 }
260
261 /***
262 * Shows the main form
263 *
264 */
265 public void showMainForm() {
266 showForm(main);
267 }
268
269 /***
270 * Shows the route form
271 */
272 public void showRoutesForm() {
273 showForm(routes);
274 }
275
276 /***
277 * Enables/Disables the routeItem in the menubar
278 *
279 * @param enable true for enable, false for disable
280 */
281 public void enableRouteItem(boolean enable) {
282 showRoutesItem.setEnabled(enable);
283 }
284
285 /***
286 * Enables/Disables the logOffItem in the menubar
287 *
288 * @param enable true for enable, false for disable
289 */
290 public void enableLogOffItem(boolean enable) {
291 logOffItem.setEnabled(enable);
292 }
293
294 /***
295 * Enables/Disables the logOnItem in the menubar
296 *
297 * @param enable true for enable, false for disable
298 */
299 public void enableLogOnItem(boolean enable) {
300 logOnItem.setEnabled(enable);
301 }
302
303 /***
304 * Enables the menu if true
305 *
306 * @param b true for enable menu
307 */
308 public void enableMenu(boolean b) {
309 menu.setEnabled(b);
310 }
311
312 /***
313 * Gets the current form
314 *
315 * @return the current form
316 */
317 public Form getCurrentForm() {
318 return currentForm;
319 }
320
321 /***
322 * Gets the form registered as login form
323 *
324 * @return the form registered as login form
325 */
326 public Form getLoginForm() {
327 return login;
328 }
329
330 /***
331 * Generates and shows an error dialog
332 *
333 * @param heading heading for the error
334 * @param error the error itself
335 * @param icon icon for the error
336 */
337 public void showErrorDialog(String heading, String error, ImageIcon icon) {
338 JOptionPane.showMessageDialog(null, error, heading,
339 JOptionPane.INFORMATION_MESSAGE, icon);
340 }
341 }