KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.util.gui.propedit;
2 import javax.swing.*;
3 import javax.swing.event.*;
4 import java.awt.FlowLayout JavaDoc;
5 import java.awt.event.ItemListener JavaDoc;
6 import java.awt.event.ItemEvent JavaDoc;
7 import net.suberic.util.*;
8
9 /**
10  * This is a Swing implemenation of a boolean PropertyEditorUI.
11  */

12 public class BooleanEditorPane extends SwingPropertyEditor {
13   JCheckBox inputField;
14   String JavaDoc label;
15   boolean originalBoolean = false;
16
17   /**
18    * @param propertyName The property to be edited.
19    * @param template The property that will define the layout of the
20    * editor.
21    * @param manager The PropertyEditorManager that will manage the
22    * changes.
23    */

24   public void configureEditor(String JavaDoc propertyName, String JavaDoc template, String JavaDoc propertyBaseName, PropertyEditorManager newManager) {
25     configureBasic(propertyName, template, propertyBaseName, newManager);
26     debug = newManager.getProperty("editors.debug", "false").equalsIgnoreCase("true");
27
28     //this.setLayout(new FlowLayout(FlowLayout.LEFT));
29
SpringLayout layout = new SpringLayout();
30     this.setLayout(layout);
31
32     originalBoolean = originalValue.equalsIgnoreCase("true");
33
34     String JavaDoc defaultLabel;
35     int dotIndex = property.lastIndexOf(".");
36     if (dotIndex == -1)
37       defaultLabel = new String JavaDoc(property);
38     else
39       defaultLabel = property.substring(dotIndex+1);
40
41     label = manager.getProperty(editorTemplate + ".label", defaultLabel);
42     inputField = new JCheckBox(label);
43
44     inputField.setSelected(originalBoolean);
45
46     inputField.addItemListener(new ItemListener JavaDoc() {
47         public void itemStateChanged(ItemEvent JavaDoc e) {
48           String JavaDoc newValue = null;
49           if (e.getStateChange() == ItemEvent.SELECTED) {
50             newValue = "true";
51           } else if (e.getStateChange() == ItemEvent.DESELECTED) {
52             newValue = "false";
53           }
54
55           try {
56             if (newValue != null) {
57               firePropertyChangingEvent(newValue);
58               firePropertyChangedEvent(newValue);
59             }
60           } catch (PropertyValueVetoException pvve) {
61             manager.getFactory().showError(inputField, "Error changing value " + label + " to " + newValue+ ": " + pvve.getReason());
62             inputField.setSelected(! inputField.isSelected());
63           }
64         }
65       });
66
67     inputField.getInsets().set(0,0,0,0);
68     inputField.setMargin(new java.awt.Insets JavaDoc(0,0,0,5));
69
70     this.add(inputField);
71
72     //inputField.setBackground(java.awt.Color.RED);
73
//this.setBackground(java.awt.Color.BLUE);
74

75     layout.putConstraint(SpringLayout.WEST, inputField, 0, SpringLayout.WEST, this);
76     layout.putConstraint(SpringLayout.NORTH, inputField, 0, SpringLayout.NORTH, this);
77     layout.putConstraint(SpringLayout.SOUTH, this, 0, SpringLayout.SOUTH, inputField);
78     layout.putConstraint(SpringLayout.EAST, this, Spring.constant(0, 0, Integer.MAX_VALUE), SpringLayout.EAST, inputField);
79     //layout.putConstraint(SpringLayout.BASELINE, label, 0, SpringLayout.SOUTH, inputField);
80

81     this.getInsets().set(0,0,0,0);
82
83     //this.setBackground(java.awt.Color.BLACK);
84

85     this.setMaximumSize(new java.awt.Dimension JavaDoc(Integer.MAX_VALUE, this.getPreferredSize().height));
86     manager.registerPropertyEditor(property, this);
87     updateEditorEnabled();
88   }
89
90   /**
91    * as defined in net.suberic.util.gui.PropertyEditorUI
92    */

93   public void setValue() throws PropertyValueVetoException {
94     if (isEditorEnabled()) {
95       validateProperty();
96       if (inputField.isSelected() != originalBoolean || manager.getProperty(property, "unset").equals("unset")) {
97         String JavaDoc newValue;
98         if (inputField.isSelected())
99           newValue = "true";
100         else
101           newValue = "false";
102
103         manager.setProperty(property, newValue);
104
105         originalBoolean = inputField.isSelected();
106       }
107
108     }
109   }
110
111   /**
112    * as defined in net.suberic.util.gui.PropertyEditorUI
113    */

114   public void validateProperty() throws PropertyValueVetoException {
115     if (isEditorEnabled()) {
116       String JavaDoc newValue;
117       if (inputField.isSelected())
118         newValue = "true";
119       else
120         newValue = "false";
121
122       firePropertyCommittingEvent(newValue);
123     }
124   }
125
126   /**
127    * Returns the current values of the edited properties as a
128    * java.util.Properties object.
129    */

130   public java.util.Properties JavaDoc getValue() {
131     java.util.Properties JavaDoc retProps = new java.util.Properties JavaDoc();
132
133     if (! isEditorEnabled()) {
134       return retProps;
135     } else {
136       if (inputField.isSelected())
137         retProps.setProperty(property, "true");
138       else
139         retProps.setProperty(property, "false");
140     }
141     return retProps;
142   }
143
144   /**
145    * This resets the editor to the original (or latest set, if setValue()
146    * has been called) value of the edited property.
147    */

148   public void resetDefaultValue() {
149     // this will be handled by the listener on the inputField, so we don't
150
// have to send any events here.
151
inputField.setSelected(originalBoolean);
152   }
153
154   /**
155    * Run when the PropertyEditor may have changed enabled states.
156    */

157   protected void updateEditorEnabled() {
158     //System.err.println("setting enabled for " + getProperty() + " to " + newValue);
159
if (inputField != null) {
160       //System.err.println("setting enabled for " + getProperty() + "; setting enabled on input field to " + newValue);
161
inputField.setEnabled(isEditorEnabled());
162     }
163   }
164
165   /**
166    * Gets the parent PropertyEditorPane for the given component.
167    */

168   public PropertyEditorPane getPropertyEditorPane() {
169     return getPropertyEditorPane(this);
170   }
171
172  /**
173    * Returns the display value for this property.
174    */

175   public String JavaDoc getDisplayValue() {
176     return label;
177   }
178
179   /**
180    * Accepts or rejects the initial focus for this component.
181    */

182   public boolean acceptDefaultFocus() {
183     if (isEditorEnabled() && inputField.isRequestFocusEnabled()) {
184       return inputField.requestFocusInWindow();
185     } else {
186       return false;
187     }
188   }
189
190 }
191
192
Popular Tags