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 package de.surethingies.properties.ui;
21
22 import java.awt.Frame;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.util.Map;
26
27 import javax.swing.JCheckBox;
28 import javax.swing.JComponent;
29 import javax.swing.JTextArea;
30 import javax.swing.JTextField;
31
32 import de.surethingies.properties.ParameterFactory;
33 import de.surethingies.properties.TypedParameter;
34 import de.surethingies.properties.resources.Messages;
35 import de.surethingies.ui.ExceptionDialog;
36
37 public class PropertyEditorControl implements ActionListener {
38
39 private PropertyEditor dialog;
40
41 private Map<TypedParameter, JComponent> settingToInputfield;
42
43 public PropertyEditorControl(PropertyEditor dialog, Map<TypedParameter, JComponent> settingToInputfield) {
44 this.dialog = dialog;
45 this.settingToInputfield = settingToInputfield;
46 }
47
48 public void actionPerformed(ActionEvent e) {
49 if (e.getActionCommand().equals(Action.APPLY.toString())) {
50 try {
51 actionApply();
52 } catch (Exception e1) {
53 new ExceptionDialog((Frame) dialog.getOwner(), e1);
54 }
55 dialog.dispose();
56
57 } else if (e.getActionCommand().equals(Action.CANCEL.toString())) {
58 dialog.dispose();
59
60 } else {
61 throw new RuntimeException(Messages.getString("PropertyEditorControl.UnknownActionError") + e.getActionCommand());
62 }
63 }
64
65 private void actionApply() throws Exception {
66 for (TypedParameter param : settingToInputfield.keySet()) {
67 param.setValue(getText(settingToInputfield.get(param)));
68 }
69
70 ParameterFactory.instance().saveProperties();
71 }
72
73 private String getText(JComponent component) {
74 String result = "";
75
76 if (component instanceof JCheckBox) {
77 result = Boolean.toString(((JCheckBox) component).isSelected());
78
79 } else if (component instanceof JTextArea) {
80 result = ((JTextArea) component).getText();
81
82 } else if (component instanceof JTextField) {
83 result = ((JTextField) component).getText();
84
85 } else if (component instanceof JTextField) {
86 result = ((JTextField) component).getText();
87
88 } else {
89 throw new RuntimeException("Unknown Component " + component);
90 }
91
92 return result;
93 }
94
95 public enum Action {
96 APPLY(Messages.getString("PropertyEditorControl.Apply")), CANCEL(Messages.getString("PropertyEditorControl.Cancel"));
97
98 private String text;
99
100 private Action(String text) {
101 this.text = text;
102 }
103
104 public String getText() {
105 return text;
106 }
107 }
108 }