1 /********************************************************************************
2 * InternetCafe is a software solution that helps the management of Cybercafes
3 * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4 * Copyright (C) 2006 Guido Angelo Ingenito
5
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *******************************************************************************/
20 package ui.panel;
21
22 import java.awt.BorderLayout;
23 import java.awt.Color;
24 import java.awt.GridLayout;
25
26 import javax.swing.JCheckBox;
27 import javax.swing.JComboBox;
28 import javax.swing.JPanel;
29 import javax.swing.JTextField;
30 import javax.swing.border.EtchedBorder;
31 import javax.swing.border.TitledBorder;
32
33 import ui.Messages;
34 import base.Control;
35 import base.service.Service;
36 import base.service.ServiceRateType;
37
38 @SuppressWarnings("serial")
39 public class ServicePanel extends JPanel {
40
41
42
43
44
45 private JPanel namePanel;
46
47 private JTextField nameTextField;
48
49 private JPanel descriptionPanel;
50
51 private JTextField descriptionTextField;
52
53 private JPanel rateTypePanel;
54
55 private JComboBox rateTypeComboBox;
56
57 private JPanel baseRatePanel;
58
59 private JTextField baseRateTextField;
60
61 private JPanel minDurationInMinutePanel;
62
63 private JTextField minDurationInMinuteTextField;
64
65 private JPanel maxDurationInMinutePanel;
66
67 private JTextField maxDurationInMinuteTextField;
68
69 private JPanel careAboutMinDurationPanel;
70
71 private JCheckBox careAboutMinDurationCheckBox;
72
73 private JPanel careAboutMaxDurationPanel;
74
75 private JCheckBox careAboutMaxDurationCheckBox;
76
77 private Service service;
78
79 public String getServiceName() {
80 return this.getNameTextField().getText();
81 }
82
83 public String getServiceDescription() {
84 return this.getDescriptionTextField().getText();
85 }
86
87 public String getServiceRateType() {
88 return (String) getRateTypeComboBox().getSelectedItem();
89 }
90
91 public double getServiceBaseRate() {
92 double result = -1.0;
93 if (Control.isValidDoubleString(this.getBaseRateTextField().getText()))
94 result = new Double(this.getBaseRateTextField().getText());
95 return result;
96 }
97
98 public int getServiceMinDurationInMinute() {
99 int result = -1;
100 if (Control.isValidIntegerOnlyString(this
101 .getMinDurationInMinuteTextField().getText()))
102 result = new Integer(this.getMinDurationInMinuteTextField()
103 .getText());
104 return result;
105 }
106
107 public boolean getServiceCareAboutMinDuration() {
108 return getCareAboutMinDurationCheckBox().isSelected();
109 }
110
111 public int getServiceMaxDurationInMinute() {
112 int result = -1;
113 if (Control.isValidIntegerOnlyString(this
114 .getMaxDurationInMinuteTextField().getText()))
115 result = new Integer(this.getMaxDurationInMinuteTextField()
116 .getText());
117 return result;
118 }
119
120 public boolean getServiceCareAboutMaxDuration() {
121 return getCareAboutMaxDurationCheckBox().isSelected();
122 }
123
124 public ServicePanel(Service service) {
125 this.service = service;
126 initialize();
127
128 }
129
130 protected void initialize() {
131 this.setLayout(new GridLayout(6, 1));
132 this.add(getNamePanel(), null);
133 this.add(getDescriptionPanel(), null);
134 this.add(getRateTypePanel(), null);
135 this.add(getBaseRatePanel(), null);
136 this.add(getMinDurationInMinutePanel(), null);
137 this.add(getMaxDurationInMinutePanel(), null);
138 }
139
140 /***
141 * @return Returns the baseRatePanel.
142 */
143 protected JPanel getBaseRatePanel() {
144 if (baseRatePanel == null) {
145 baseRatePanel = new JPanel();
146 TitledBorder titledBorder = new TitledBorder(Messages.getString("common.baserate"));
147 baseRatePanel.setBorder(titledBorder);
148 baseRatePanel.setLayout(new BorderLayout());
149 baseRatePanel.add(getBaseRateTextField(), BorderLayout.CENTER);
150 }
151 return baseRatePanel;
152 }
153
154 /***
155 * @return Returns the baseRateTextField.
156 */
157 protected JTextField getBaseRateTextField() {
158 if (baseRateTextField == null) {
159 baseRateTextField = new JTextField();
160 baseRateTextField.setText(service != null ? ""
161 + service.getBaseRate() : Messages.getString("service.default.baserate"));
162 }
163 return baseRateTextField;
164 }
165
166 /***
167 * @return Returns the descriptionPanel.
168 */
169 protected JPanel getDescriptionPanel() {
170 if (descriptionPanel == null) {
171 descriptionPanel = new JPanel();
172 TitledBorder titledBorder = new TitledBorder(Messages.getString("common.description"));
173 descriptionPanel.setBorder(titledBorder);
174 descriptionPanel.setLayout(new BorderLayout());
175 descriptionPanel
176 .add(getDescriptionTextField(), BorderLayout.CENTER);
177 }
178 return descriptionPanel;
179 }
180
181 /***
182 * @return Returns the descriptionTextField.
183 */
184 protected JTextField getDescriptionTextField() {
185 if (descriptionTextField == null) {
186 descriptionTextField = new JTextField();
187 descriptionTextField.setText(service != null ? service
188 .getDescription() : "");
189 }
190 return descriptionTextField;
191 }
192
193 /***
194 * @return Returns the maxDurationInMinutePanel.
195 */
196 protected JPanel getMaxDurationInMinutePanel() {
197 if (maxDurationInMinutePanel == null) {
198 maxDurationInMinutePanel = new JPanel();
199 TitledBorder titledBorder = new TitledBorder(Messages.getString("common.maxdurationinminute"));
200 maxDurationInMinutePanel.setBorder(titledBorder);
201 maxDurationInMinutePanel.setLayout(new BorderLayout());
202 maxDurationInMinutePanel.add(getMaxDurationInMinuteTextField(),
203 BorderLayout.CENTER);
204 maxDurationInMinutePanel.add(getCareAboutMaxDurationPanel(),
205 BorderLayout.EAST);
206 }
207 return maxDurationInMinutePanel;
208 }
209
210 /***
211 * @return Returns the maxDurationInMinuteTextField.
212 */
213 protected JTextField getMaxDurationInMinuteTextField() {
214 if (maxDurationInMinuteTextField == null) {
215 maxDurationInMinuteTextField = new JTextField();
216 maxDurationInMinuteTextField.setText(service != null ? ""
217 + service.getMaxDurationInMinute() : Messages.getString("service.default.maxdurationinminute"));
218 }
219 return maxDurationInMinuteTextField;
220 }
221
222 /***
223 * @return Returns the careAboutMaxDurationPanel.
224 */
225 protected JPanel getCareAboutMaxDurationPanel() {
226 if (careAboutMaxDurationPanel == null) {
227 careAboutMaxDurationPanel = new JPanel();
228 careAboutMaxDurationPanel.setBorder(new EtchedBorder());
229 careAboutMaxDurationPanel.setLayout(new BorderLayout());
230 careAboutMaxDurationPanel.add(getCareAboutMaxDurationCheckBox(),
231 BorderLayout.EAST);
232 }
233 return careAboutMaxDurationPanel;
234 }
235
236 /***
237 * @return Returns the careAboutMaxDurationCheckBox.
238 */
239 protected JCheckBox getCareAboutMaxDurationCheckBox() {
240 if (careAboutMaxDurationCheckBox == null) {
241 careAboutMaxDurationCheckBox = new JCheckBox();
242 careAboutMaxDurationCheckBox
243 .setToolTipText(Messages.getString("panel.servicepanel.message1"));
244 careAboutMaxDurationCheckBox.setSelected(service != null ? service
245 .careAboutMaxDuration() : false);
246 }
247 return careAboutMaxDurationCheckBox;
248 }
249
250 /***
251 * @return Returns the minDurationInMinutePanel.
252 */
253 protected JPanel getMinDurationInMinutePanel() {
254 if (minDurationInMinutePanel == null) {
255 minDurationInMinutePanel = new JPanel();
256 TitledBorder titledBorder = new TitledBorder(Messages.getString("common.mindurationinminute"));
257 minDurationInMinutePanel.setBorder(titledBorder);
258 minDurationInMinutePanel.setLayout(new BorderLayout());
259 minDurationInMinutePanel.add(getMinDurationInMinuteTextField(),
260 BorderLayout.CENTER);
261 minDurationInMinutePanel.add(getCareAboutMinDurationPanel(),
262 BorderLayout.EAST);
263 }
264 return minDurationInMinutePanel;
265 }
266
267 /***
268 * @return Returns the minDurationInMinuteTextField.
269 */
270 protected JTextField getMinDurationInMinuteTextField() {
271 if (minDurationInMinuteTextField == null) {
272 minDurationInMinuteTextField = new JTextField();
273 minDurationInMinuteTextField.setText(service != null ? ""
274 + service.getMinDurationInMinute() : Messages.getString("service.default.mindurationinminute"));
275 }
276 return minDurationInMinuteTextField;
277 }
278
279 /***
280 * @return Returns the careAboutMinDurationPanel.
281 */
282 protected JPanel getCareAboutMinDurationPanel() {
283 if (careAboutMinDurationPanel == null) {
284 careAboutMinDurationPanel = new JPanel();
285 careAboutMinDurationPanel.setBorder(new EtchedBorder());
286 careAboutMinDurationPanel.setLayout(new BorderLayout());
287 careAboutMinDurationPanel.add(getCareAboutMinDurationCheckBox(),
288 BorderLayout.EAST);
289 }
290 return careAboutMinDurationPanel;
291 }
292
293 /***
294 * @return Returns the careAboutMinDurationCheckBox.
295 */
296 protected JCheckBox getCareAboutMinDurationCheckBox() {
297 if (careAboutMinDurationCheckBox == null) {
298 careAboutMinDurationCheckBox = new JCheckBox();
299 careAboutMinDurationCheckBox
300 .setToolTipText(Messages.getString("panel.servicepanel.message2"));
301 careAboutMinDurationCheckBox.setSelected(service != null ? service
302 .careAboutMinDuration() : true);
303 }
304 return careAboutMinDurationCheckBox;
305 }
306
307 /***
308 * @return Returns the namePanel.
309 */
310 protected JPanel getNamePanel() {
311 if (namePanel == null) {
312 namePanel = new JPanel();
313 TitledBorder titledBorder = new TitledBorder(Messages.getString("common.name"));
314 namePanel.setBorder(titledBorder);
315 namePanel.setLayout(new BorderLayout());
316 namePanel.add(getNameTextField(), BorderLayout.CENTER);
317 }
318 return namePanel;
319 }
320
321 /***
322 * @return Returns the nameTextField.
323 */
324 protected JTextField getNameTextField() {
325 if (nameTextField == null) {
326 nameTextField = new JTextField();
327 nameTextField.setText(service != null ? service.getName() : "");
328 }
329 return nameTextField;
330 }
331
332 /***
333 * @return Returns the rateTypeComboBox.
334 */
335 protected JComboBox getRateTypeComboBox() {
336 if (rateTypeComboBox == null) {
337 rateTypeComboBox = new JComboBox();
338 for (int i = 0; i < ServiceRateType.RATE_TYPE.length; i++)
339 rateTypeComboBox.addItem(ServiceRateType.RATE_TYPE[i]);
340 rateTypeComboBox.setSelectedItem(service != null ? service
341 .getRateType()
342 : ServiceRateType.RATE_TYPE[ServiceRateType.UNDEFINED]);
343 }
344 return rateTypeComboBox;
345 }
346
347 /***
348 * @return Returns the rateTypePanel.
349 */
350 protected JPanel getRateTypePanel() {
351 if (rateTypePanel == null) {
352 rateTypePanel = new JPanel();
353 TitledBorder titledBorder = new TitledBorder(Messages.getString("common.ratetype"));
354 rateTypePanel.setBorder(titledBorder);
355 rateTypePanel.setLayout(new BorderLayout());
356 rateTypePanel.add(getRateTypeComboBox(), BorderLayout.CENTER);
357 }
358 return rateTypePanel;
359 }
360
361 /***
362 * @return Returns the service.
363 */
364 public Service getService() {
365 return service;
366 }
367
368 /***
369 * Resets to the default values all the fields contained in this panel.
370 */
371 public void clearServiceData() {
372 this.getBaseRateTextField().setText(Messages.getString("service.default.baserate"));
373 this.getDescriptionTextField().setText("");
374 this.getRateTypeComboBox().setSelectedItem(
375 ServiceRateType.RATE_TYPE[ServiceRateType.UNDEFINED]);
376 this.getNameTextField().setText("");
377 this.getMinDurationInMinuteTextField().setText(Messages.getString("service.default.mindurationinminute"));
378 this.getMaxDurationInMinuteTextField().setText(Messages.getString("service.default.maxdurationinminute"));
379
380 }
381
382 public void setNameError(boolean isError) {
383 if (isError)
384 ((TitledBorder) this.getNamePanel().getBorder())
385 .setTitleColor(Color.RED);
386 else
387 ((TitledBorder) this.getNamePanel().getBorder())
388 .setTitleColor(new TitledBorder("").getTitleColor());
389 this.getNamePanel().updateUI();
390 }
391
392 public void setBaseRateError(boolean isError) {
393 if (isError)
394 ((TitledBorder) this.getBaseRatePanel().getBorder())
395 .setTitleColor(Color.RED);
396 else
397 ((TitledBorder) this.getBaseRatePanel().getBorder())
398 .setTitleColor(new TitledBorder("").getTitleColor());
399 this.getBaseRatePanel().updateUI();
400 }
401
402 public void setMinMaxDurationError(boolean isError) {
403 if (isError) {
404 ((TitledBorder) this.getMinDurationInMinutePanel().getBorder())
405 .setTitleColor(Color.RED);
406 ((TitledBorder) this.getMaxDurationInMinutePanel().getBorder())
407 .setTitleColor(Color.RED);
408 } else {
409 ((TitledBorder) this.getMinDurationInMinutePanel().getBorder())
410 .setTitleColor(new TitledBorder("").getTitleColor());
411 ((TitledBorder) this.getMaxDurationInMinutePanel().getBorder())
412 .setTitleColor(new TitledBorder("").getTitleColor());
413 }
414 this.getMinDurationInMinutePanel().updateUI();
415 this.getMaxDurationInMinutePanel().updateUI();
416 }
417
418 }