1 /**
2 * Copyright (C) 2007 Joern Krueger surething@users.sourceforge.net
3 *
4 * This program is free software; you can redistribute
5 * it and/or modify it under the terms of the GNU General
6 * Public License version 2 as published by the Free Software
7 * Foundation.
8 *
9 * This program is distributed in the hope that it will be
10 * useful, but WITHOUT ANY WARRANTY; without even the implied
11 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 * PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place,
18 * Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21 package de.surething.lda.ui;
22
23 import java.awt.Cursor;
24 import java.awt.Dimension;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.Insets;
28 import java.awt.event.WindowAdapter;
29 import java.awt.event.WindowEvent;
30 import java.util.List;
31
32 import javax.swing.BorderFactory;
33 import javax.swing.JButton;
34 import javax.swing.JCheckBox;
35 import javax.swing.JComboBox;
36 import javax.swing.JFrame;
37 import javax.swing.JLabel;
38 import javax.swing.JPanel;
39 import javax.swing.border.BevelBorder;
40 import javax.swing.border.TitledBorder;
41
42 import de.surething.lda.actions.ActionTask;
43 import de.surething.lda.locations.Location;
44 import de.surething.lda.resources.IconFactory;
45 import de.surething.lda.resources.Messages;
46 import de.surethingies.properties.ParameterFactory;
47 import de.surethingies.ui.ExceptionDialog;
48
49 @SuppressWarnings("serial")
50 public class MainWindow extends JFrame {
51
52 protected JComboBox locations;
53
54 protected JComboBox actions;
55
56 private JCheckBox remember;
57
58 private MainWindowControl control;
59
60 public MainWindow() {
61 super("Location Dependent Action");
62 control = new MainWindowControl(this);
63 addMouseListener(control.getAdapter());
64
65 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
66 addWindowListener(new WindowAdapter() {
67 @Override
68 public void windowClosing(WindowEvent e) {
69 shutdown();
70 }
71 });
72
73 setupUI();
74 pack();
75 setLocationRelativeTo(null);
76 }
77
78 public void showUI() {
79 setVisible(true);
80 }
81
82 private void setupUI() {
83 JPanel panel = new JPanel();
84 panel.setBorder(new TitledBorder(Messages.getString("MainWindow.BorderTitle")));
85 panel.setLayout(new GridBagLayout());
86
87 JLabel location = new JLabel(Messages.getString("MainWindow.LocationLabel"));
88 JLabel action = new JLabel(Messages.getString("MainWindow.ActionLabel"));
89
90 locations = new JComboBox();
91 actions = new JComboBox();
92 remember = new JCheckBox(Messages.getString("MainWindow.RememberCheckbox"), true);
93 JButton execute = new JButton(MainWindowControl.Action.EXECUTE.getLabel());
94 execute.setActionCommand(MainWindowControl.Action.EXECUTE.toString());
95 execute.addActionListener(control);
96 execute.setIcon(IconFactory.instance().get(IconFactory.IconType.OK));
97 getRootPane().setDefaultButton(execute);
98
99 JLabel configLocations = new JLabel(IconFactory.instance().get(IconFactory.IconType.CONFIGURATION));
100 configLocations.setToolTipText(MainWindowControl.Action.CONFIG_LOCATIONS.getLabel());
101 configLocations.setName(MainWindowControl.Action.CONFIG_LOCATIONS.toString());
102 configLocations.addMouseListener(control.getAdapter());
103 configLocations.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
104
105 JLabel configActions = new JLabel(IconFactory.instance().get(IconFactory.IconType.CONFIGURATION));
106 configActions.setToolTipText(MainWindowControl.Action.CONFIG_ACTIONS.getLabel());
107 configActions.setName(MainWindowControl.Action.CONFIG_ACTIONS.toString());
108 configActions.addMouseListener(control.getAdapter());
109 configActions.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
110
111 GridBagConstraints c = new GridBagConstraints();
112
113 c.fill = GridBagConstraints.HORIZONTAL;
114 c.gridy = 0;
115 c.gridx = 0;
116 c.weightx = 0.0;
117 c.weighty = 0.0;
118 c.insets = new Insets(5, 5, 5, 5);
119 panel.add(location, c);
120
121 c.gridy = 0;
122 c.gridx = 1;
123 c.weightx = 1.0;
124 panel.add(locations, c);
125
126 c.gridy = 0;
127 c.gridx = 2;
128 c.weightx = 0.0;
129 panel.add(configLocations, c);
130
131 c.gridy = 1;
132 c.gridx = 0;
133 c.weightx = 0.0;
134 panel.add(action, c);
135
136 c.gridy = 1;
137 c.gridx = 1;
138 c.weightx = 1.0;
139 panel.add(actions, c);
140
141 c.gridy = 1;
142 c.gridx = 2;
143 c.weightx = 0.0;
144 panel.add(configActions, c);
145
146 c.gridy = 2;
147 c.gridx = 0;
148 c.weightx = 0.0;
149 panel.add(remember, c);
150
151 c.fill = GridBagConstraints.NONE;
152 c.gridy = 2;
153 c.gridx = 1;
154 c.weightx = 0.0;
155 c.weighty = 0.0;
156 c.anchor = GridBagConstraints.EAST;
157 panel.add(execute, c);
158
159 panel.setPreferredSize(new Dimension(320, 150));
160 getContentPane().setLayout(new GridBagLayout());
161
162 c.fill = GridBagConstraints.BOTH;
163 c.gridy = 0;
164 c.gridx = 0;
165 c.weightx = 1.0;
166 c.weighty = 1.0;
167 c.insets = new Insets(5, 5, 5, 5);
168 getContentPane().add(panel, c);
169 }
170
171 public void shutdown() {
172 System.out.println("Shutdown");
173 try {
174 ParameterFactory.instance().saveProperties();
175
176 } catch (Exception e) {
177 new ExceptionDialog(this, e);
178 }
179
180 System.exit(0);
181 }
182
183 public void sleep(boolean sleep) {
184 if (sleep) {
185 Cursor hourglassCursor = new Cursor(Cursor.WAIT_CURSOR);
186 setCursor(hourglassCursor);
187 } else {
188 Cursor hourglassCursor = new Cursor(Cursor.DEFAULT_CURSOR);
189 setCursor(hourglassCursor);
190 }
191 }
192
193 public void setActions(List<ActionTask> actions) {
194 for (ActionTask action : actions) {
195 this.actions.addItem(action);
196 }
197 }
198
199 public ActionTask getSelectedAction() {
200 return (ActionTask) actions.getSelectedItem();
201 }
202
203 public void setLocations(List<Location> locations) {
204 for (Location location : locations) {
205 this.locations.addItem(location);
206 }
207 }
208
209 public Location getSelectedLocation() {
210 return (Location) locations.getSelectedItem();
211 }
212 }