View Javadoc

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.GridBagConstraints;
24  import java.awt.GridBagLayout;
25  import java.awt.Insets;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  import javax.swing.BorderFactory;
31  import javax.swing.JButton;
32  import javax.swing.JCheckBox;
33  import javax.swing.JComponent;
34  import javax.swing.JDialog;
35  import javax.swing.JLabel;
36  import javax.swing.JPanel;
37  import javax.swing.JScrollPane;
38  import javax.swing.JTextArea;
39  import javax.swing.JTextField;
40  import javax.swing.border.BevelBorder;
41  
42  import de.surethingies.properties.TypedParameter;
43  import de.surethingies.properties.ValueType;
44  
45  @SuppressWarnings("serial")
46  public class PropertyEditor extends JDialog {
47  
48      private PropertyEditorControl propertyEditorControl;
49  
50      private Map<TypedParameter, JComponent> settingToInputfield;
51  
52      public PropertyEditor(Frame owner, String displayName, List<TypedParameter> props) {
53          super(owner, displayName, true);
54  
55          settingToInputfield = new HashMap<TypedParameter, JComponent>();
56          propertyEditorControl = new PropertyEditorControl(this, settingToInputfield);
57  
58          createUI(props);
59          pack();
60          setLocationRelativeTo(owner);
61          setVisible(true);
62      }
63  
64      private void createUI(List<TypedParameter> props) {
65          JPanel panel = new JPanel();
66  
67          panel.setLayout(new GridBagLayout());
68          GridBagConstraints c = new GridBagConstraints();
69          c.insets = new Insets(5, 5, 5, 5);
70          c.gridy = 0;
71          c.gridx = 0;
72          c.weighty = 0.0;
73          c.weightx = 1.0;
74          c.anchor = GridBagConstraints.WEST;
75  
76          JLabel label;
77          JComponent input = null;
78          int gridy = 0;
79          for (TypedParameter key : props) {
80              if (key.getParameter().valueIsEditable()) {
81  
82                  label = new JLabel(key.getParameter().getDisplayName());
83  
84                  if (key.getParameter().valueType().equals(ValueType.STRING)) {
85                      input = new JTextField((String) key.getValue());
86  
87                  } else if (key.getParameter().valueType().equals(ValueType.TEXT)) {
88                      input = new JTextArea(5, 40);
89                      ((JTextArea) input).setText((String) key.getValue());
90  
91                  } else if (key.getParameter().valueType().equals(ValueType.BOOLEAN)) {
92                      input = new JCheckBox("", Boolean.parseBoolean(key.getValue()));
93  
94                  } else if (key.getParameter().valueType().equals(ValueType.STRINGLIST)) {
95                      input = new JTextField((String) key.getValue()); // TODO: Replace by EditComboBox
96  
97                  } else {
98                      throw new RuntimeException("Unknown ValueType " + key.getParameter().valueType());
99                  }
100                 input.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
101                 settingToInputfield.put(key, input);
102 
103                 if (key.getParameter().valueType().equals(ValueType.TEXT)) {
104                     input = new JScrollPane(input);
105                     ((JScrollPane) input).setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
106                 }
107 
108                 c.gridy = gridy++;
109 
110                 c.fill = GridBagConstraints.NONE;
111                 c.gridx = 0;
112                 c.weightx = 0.0;
113                 panel.add(label, c);
114 
115                 c.fill = GridBagConstraints.HORIZONTAL;
116                 c.gridx = 1;
117                 c.weightx = 1.0;
118                 panel.add(input, c);
119             }
120         }
121 
122         JButton cancel = new JButton(PropertyEditorControl.Action.CANCEL.getText());
123         cancel.setActionCommand(PropertyEditorControl.Action.CANCEL.toString());
124         cancel.addActionListener(propertyEditorControl);
125 
126         JButton apply = new JButton(PropertyEditorControl.Action.APPLY.getText());
127         apply.setActionCommand(PropertyEditorControl.Action.APPLY.toString());
128         apply.addActionListener(propertyEditorControl);
129 
130         c.gridy = gridy;
131         c.gridx = 0;
132         panel.add(cancel, c);
133 
134         c.gridy = gridy;
135         c.gridx = 1;
136         panel.add(apply, c);
137 
138         getContentPane().add(panel);
139     }
140 }