1 package net.suberic.util.gui.propedit; 2 import javax.swing.*; 3 import javax.swing.event.*; 4 import java.awt.FlowLayout ; 5 import java.awt.event.ItemListener ; 6 import java.awt.event.ItemEvent ; 7 import net.suberic.util.*; 8 9 12 public class BooleanEditorPane extends SwingPropertyEditor { 13 JCheckBox inputField; 14 String label; 15 boolean originalBoolean = false; 16 17 24 public void configureEditor(String propertyName, String template, String propertyBaseName, PropertyEditorManager newManager) { 25 configureBasic(propertyName, template, propertyBaseName, newManager); 26 debug = newManager.getProperty("editors.debug", "false").equalsIgnoreCase("true"); 27 28 SpringLayout layout = new SpringLayout(); 30 this.setLayout(layout); 31 32 originalBoolean = originalValue.equalsIgnoreCase("true"); 33 34 String defaultLabel; 35 int dotIndex = property.lastIndexOf("."); 36 if (dotIndex == -1) 37 defaultLabel = new String (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 () { 47 public void itemStateChanged(ItemEvent e) { 48 String 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 (0,0,0,5)); 69 70 this.add(inputField); 71 72 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 81 this.getInsets().set(0,0,0,0); 82 83 85 this.setMaximumSize(new java.awt.Dimension (Integer.MAX_VALUE, this.getPreferredSize().height)); 86 manager.registerPropertyEditor(property, this); 87 updateEditorEnabled(); 88 } 89 90 93 public void setValue() throws PropertyValueVetoException { 94 if (isEditorEnabled()) { 95 validateProperty(); 96 if (inputField.isSelected() != originalBoolean || manager.getProperty(property, "unset").equals("unset")) { 97 String 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 114 public void validateProperty() throws PropertyValueVetoException { 115 if (isEditorEnabled()) { 116 String newValue; 117 if (inputField.isSelected()) 118 newValue = "true"; 119 else 120 newValue = "false"; 121 122 firePropertyCommittingEvent(newValue); 123 } 124 } 125 126 130 public java.util.Properties getValue() { 131 java.util.Properties retProps = new java.util.Properties (); 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 148 public void resetDefaultValue() { 149 inputField.setSelected(originalBoolean); 152 } 153 154 157 protected void updateEditorEnabled() { 158 if (inputField != null) { 160 inputField.setEnabled(isEditorEnabled()); 162 } 163 } 164 165 168 public PropertyEditorPane getPropertyEditorPane() { 169 return getPropertyEditorPane(this); 170 } 171 172 175 public String getDisplayValue() { 176 return label; 177 } 178 179 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 |