KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > gui > propedit > RadioEditorPane


1 package net.suberic.util.gui.propedit;
2 import javax.swing.*;
3 import net.suberic.util.*;
4 import java.awt.event.*;
5 import java.util.*;
6
7 /**
8  * An EditorPane which allows a user to select from a set of choices.
9  *
10  */

11 public class RadioEditorPane extends SwingPropertyEditor implements ItemListener {
12   String JavaDoc labelString = "";
13   protected ButtonGroup buttonGroup = new ButtonGroup();
14   protected ButtonModel lastSelected = null;
15
16   /**
17    * @param propertyName The property to be edited.
18    * @param template The property that will define the layout of the
19    * editor.
20    * @param manager The PropertyEditorManager that will manage the
21    * changes.
22    */

23   public void configureEditor(String JavaDoc propertyName, String JavaDoc template, String JavaDoc propertyBaseName, PropertyEditorManager newManager) {
24     configureBasic(propertyName, template, propertyBaseName, newManager);
25
26     SpringLayout layout = new SpringLayout();
27     this.setLayout(layout);
28
29     String JavaDoc defaultLabel;
30     int dotIndex = editorTemplate.lastIndexOf(".");
31     if (dotIndex == -1)
32       defaultLabel = new String JavaDoc(editorTemplate);
33     else
34       defaultLabel = property.substring(dotIndex+1);
35
36     if (originalValue == null || originalValue.length() < 1) {
37       originalValue = manager.getProperty(property, manager.getProperty(editorTemplate, ""));
38     }
39
40     //JLabel mainLabel = new JLabel(manager.getProperty(editorTemplate + ".label", defaultLabel));
41

42     //this.add(mainLabel);
43
labelString = manager.getProperty(editorTemplate + ".label", defaultLabel);
44
45     if (manager.getProperty(editorTemplate + ".showBorder", "true").equalsIgnoreCase("false")) {
46       this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), labelString));
47     } else {
48       this.setBorder(BorderFactory.createTitledBorder(labelString));
49     }
50
51     //System.err.println("radioeditorpane: mainLabel = " + mainLabel.getText());
52
//layout.putConstraint(SpringLayout.WEST, mainLabel, 0, SpringLayout.WEST, this);
53
//layout.putConstraint(SpringLayout.NORTH, mainLabel, 0, SpringLayout.NORTH, this);
54

55     List<String JavaDoc> allowedValues = manager.getPropertyAsList(editorTemplate + ".allowedValues", "");
56
57     //JComponent previous = mainLabel;
58
JComponent previous = null;
59     JComponent widest = null;
60     //Spring widthSpring = layout.getConstraints(mainLabel).getWidth();
61
for(String JavaDoc allowedValue: allowedValues) {
62       String JavaDoc label = manager.getProperty(editorTemplate + ".listMapping." + allowedValue + ".label", allowedValue);
63       JRadioButton button = new JRadioButton(label);
64       button.setActionCommand(allowedValue);
65       button.addItemListener(this);
66
67       buttonGroup.add(button);
68       this.add(button);
69       if (allowedValue.equals(originalValue)) {
70         button.setSelected(true);
71       }
72       layout.putConstraint(SpringLayout.WEST, button, 15, SpringLayout.WEST, this);
73       if (previous != null)
74         layout.putConstraint(SpringLayout.NORTH, button, 0, SpringLayout.SOUTH, previous);
75       else
76         layout.putConstraint(SpringLayout.NORTH, button, 0, SpringLayout.NORTH, this);
77
78       if (widest == null || widest.getPreferredSize().width < button.getPreferredSize().width) {
79         widest = button;
80       }
81       previous = button;
82     }
83
84     layout.putConstraint(SpringLayout.SOUTH, this, 0, SpringLayout.SOUTH, previous);
85     layout.putConstraint(SpringLayout.EAST, this, Spring.constant(5, 5, Integer.MAX_VALUE), SpringLayout.EAST, widest);
86
87   }
88
89   /**
90    * Called when the button value changes.
91    */

92   public void itemStateChanged(ItemEvent e) {
93     if (e.getStateChange() == ItemEvent.SELECTED) {
94       JRadioButton button = (JRadioButton) e.getSource();
95       String JavaDoc currentValue = button.getActionCommand();
96       try {
97         //System.err.println("firing propertyChangedEvent for " + property);
98
firePropertyChangingEvent(currentValue);
99         firePropertyChangedEvent(currentValue);
100
101         lastSelected = button.getModel();
102       } catch (PropertyValueVetoException pvve) {
103         manager.getFactory().showError(this, "Error changing value " + labelString + " to " + currentValue + ": " + pvve.getReason());
104         if (lastSelected != null) {
105           lastSelected.setSelected(true);
106         }
107       }
108     }
109   }
110
111   /**
112    * This writes the currently configured value in the PropertyEditorUI
113    * to the source VariableBundle.
114    */

115   public void setValue() throws PropertyValueVetoException {
116     validateProperty();
117     ButtonModel selectedModel = buttonGroup.getSelection();
118     String JavaDoc currentValue = "";
119     if (selectedModel != null) {
120       currentValue = selectedModel.getActionCommand();
121     }
122
123     firePropertyCommittingEvent(currentValue);
124
125     if (isEditorEnabled() && isChanged()) {
126       manager.setProperty(property, currentValue);
127       originalValue = currentValue;
128     }
129     lastSelected = selectedModel;
130   }
131
132   /**
133    * This checks that the currently configured value is valid.
134    */

135   public void validateProperty() throws PropertyValueVetoException {
136     ButtonModel selectedModel = buttonGroup.getSelection();
137     String JavaDoc currentValue = "";
138     if (selectedModel != null) {
139       currentValue = selectedModel.getActionCommand();
140     }
141     if (! currentValue.equals(originalValue)) {
142       firePropertyChangingEvent(currentValue);
143       //System.err.println("firing propertyChangedEvent for " + property);
144
firePropertyChangedEvent(currentValue);
145     }
146
147     firePropertyCommittingEvent(currentValue);
148   }
149
150   /**
151    * Returns the current values of the edited properties as a
152    * java.util.Properties object.
153    */

154   public java.util.Properties JavaDoc getValue() {
155     java.util.Properties JavaDoc retProps = new java.util.Properties JavaDoc();
156
157     ButtonModel selectedModel = buttonGroup.getSelection();
158     String JavaDoc value = "";
159     if (selectedModel != null) {
160       value = buttonGroup.getSelection().getActionCommand();
161     }
162     retProps.setProperty(property, value);
163
164     return retProps;
165   }
166
167   /**
168    * This resets the editor to the original (or latest set, if setValue()
169    * has been called) value of the edited property.
170    */

171   public void resetDefaultValue() {
172     setSelectedValue(originalValue);
173   }
174
175   /**
176    * Selects the given value.
177    */

178   public void setSelectedValue(String JavaDoc newValue) {
179
180   }
181
182   /**
183    * Returns whether or not the current list selection has changed from
184    * the last save.
185    */

186   public boolean isChanged() {
187     ButtonModel selectedModel = buttonGroup.getSelection();
188     String JavaDoc currentValue = "";
189     if (selectedModel != null) {
190       currentValue = buttonGroup.getSelection().getActionCommand();
191     }
192     return (! currentValue.equals(originalValue));
193   }
194
195   /**
196    * Run when the PropertyEditor may have changed enabled states.
197    *
198    * This is a default implementation of updateEnabledState. If the
199    * labelComponent and valueComponent attributes are set, it will call
200    * setEnabled on those.
201    *
202    * Subclasses which do not use the default labelComponent and
203    * valueComponent attributes, or which require additional functionality,
204    * should override this method.
205    */

206   protected void updateEditorEnabled() {
207     Enumeration<AbstractButton> buttons = buttonGroup.getElements();
208     while (buttons.hasMoreElements()) {
209       buttons.nextElement().setEnabled(isEditorEnabled());
210     }
211   }
212
213
214   /**
215    * Gets the parent PropertyEditorPane for the given component.
216    */

217   public PropertyEditorPane getPropertyEditorPane() {
218     return getPropertyEditorPane(this);
219   }
220
221   /**
222    * Returns the display value for this property.
223    */

224   public String JavaDoc getDisplayValue() {
225     return labelString;
226   }
227
228   /**
229    * Accepts or rejects the initial focus for this component.
230    */

231   public boolean acceptDefaultFocus() {
232     if (isEditorEnabled()) {
233       Enumeration<AbstractButton> buttonEnum = buttonGroup.getElements();
234       // for some reason this returns false on dialogs.
235
/*
236         while (buttonEnum.hasMoreElements()) {
237         AbstractButton button = buttonEnum.nextElement();
238         if (button.requestFocusInWindow())
239         return true;
240         }
241         return false;
242       */

243       AbstractButton button = buttonEnum.nextElement();
244       button.requestFocusInWindow();
245       return true;
246     } else {
247       return false;
248     }
249   }
250
251 }
252
Popular Tags