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 11 public class RadioEditorPane extends SwingPropertyEditor implements ItemListener { 12 String labelString = ""; 13 protected ButtonGroup buttonGroup = new ButtonGroup(); 14 protected ButtonModel lastSelected = null; 15 16 23 public void configureEditor(String propertyName, String template, String propertyBaseName, PropertyEditorManager newManager) { 24 configureBasic(propertyName, template, propertyBaseName, newManager); 25 26 SpringLayout layout = new SpringLayout(); 27 this.setLayout(layout); 28 29 String defaultLabel; 30 int dotIndex = editorTemplate.lastIndexOf("."); 31 if (dotIndex == -1) 32 defaultLabel = new String (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 42 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 55 List<String > allowedValues = manager.getPropertyAsList(editorTemplate + ".allowedValues", ""); 56 57 JComponent previous = null; 59 JComponent widest = null; 60 for(String allowedValue: allowedValues) { 62 String 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 92 public void itemStateChanged(ItemEvent e) { 93 if (e.getStateChange() == ItemEvent.SELECTED) { 94 JRadioButton button = (JRadioButton) e.getSource(); 95 String currentValue = button.getActionCommand(); 96 try { 97 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 115 public void setValue() throws PropertyValueVetoException { 116 validateProperty(); 117 ButtonModel selectedModel = buttonGroup.getSelection(); 118 String 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 135 public void validateProperty() throws PropertyValueVetoException { 136 ButtonModel selectedModel = buttonGroup.getSelection(); 137 String currentValue = ""; 138 if (selectedModel != null) { 139 currentValue = selectedModel.getActionCommand(); 140 } 141 if (! currentValue.equals(originalValue)) { 142 firePropertyChangingEvent(currentValue); 143 firePropertyChangedEvent(currentValue); 145 } 146 147 firePropertyCommittingEvent(currentValue); 148 } 149 150 154 public java.util.Properties getValue() { 155 java.util.Properties retProps = new java.util.Properties (); 156 157 ButtonModel selectedModel = buttonGroup.getSelection(); 158 String value = ""; 159 if (selectedModel != null) { 160 value = buttonGroup.getSelection().getActionCommand(); 161 } 162 retProps.setProperty(property, value); 163 164 return retProps; 165 } 166 167 171 public void resetDefaultValue() { 172 setSelectedValue(originalValue); 173 } 174 175 178 public void setSelectedValue(String newValue) { 179 180 } 181 182 186 public boolean isChanged() { 187 ButtonModel selectedModel = buttonGroup.getSelection(); 188 String currentValue = ""; 189 if (selectedModel != null) { 190 currentValue = buttonGroup.getSelection().getActionCommand(); 191 } 192 return (! currentValue.equals(originalValue)); 193 } 194 195 206 protected void updateEditorEnabled() { 207 Enumeration<AbstractButton> buttons = buttonGroup.getElements(); 208 while (buttons.hasMoreElements()) { 209 buttons.nextElement().setEnabled(isEditorEnabled()); 210 } 211 } 212 213 214 217 public PropertyEditorPane getPropertyEditorPane() { 218 return getPropertyEditorPane(this); 219 } 220 221 224 public String getDisplayValue() { 225 return labelString; 226 } 227 228 231 public boolean acceptDefaultFocus() { 232 if (isEditorEnabled()) { 233 Enumeration<AbstractButton> buttonEnum = buttonGroup.getElements(); 234 243 AbstractButton button = buttonEnum.nextElement(); 244 button.requestFocusInWindow(); 245 return true; 246 } else { 247 return false; 248 } 249 } 250 251 } 252 | Popular Tags |