1 package net.suberic.util.gui.propedit; 2 import javax.swing.*; 3 import java.awt.Component ; 4 import java.awt.Container ; 5 import java.util.Vector ; 6 import java.util.ArrayList ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 import java.util.logging.Logger ; 10 11 33 public abstract class CompositeSwingPropertyEditor extends SwingPropertyEditor { 34 protected List <SwingPropertyEditor> editors; 35 protected Logger mLogger = Logger.getLogger("editors.debug"); 36 37 41 public void setValue() throws PropertyValueVetoException { 42 if (isEditorEnabled()) { 43 List <PropertyValueVetoException> exceptionList = new ArrayList <PropertyValueVetoException>(); 44 for (int i = 0; i < editors.size() ; i++) { 45 try { 46 editors.get(i).setValue(); 47 } catch (PropertyValueVetoException pvve) { 48 exceptionList.add(pvve); 49 } 50 } 51 if (exceptionList.size() > 0) { 52 StringBuilder builder = new StringBuilder (); 53 Iterator <PropertyValueVetoException> iter = exceptionList.iterator(); 54 while (iter.hasNext()) { 55 PropertyValueVetoException pvve = iter.next(); 56 builder.append(pvve.getMessage()); 57 if (iter.hasNext()) 58 builder.append("\r\n"); 59 } 60 throw new PropertyValueVetoException(builder.toString()); 61 } 62 } 63 } 64 65 public void validateProperty() throws PropertyValueVetoException { 66 67 if (isEditorEnabled()) { 68 List <PropertyValueVetoException> exceptionList = new ArrayList <PropertyValueVetoException>(); 69 for (int i = 0; i < editors.size() ; i++) { 70 try { 71 editors.get(i).validateProperty(); 72 } catch (PropertyValueVetoException pvve) { 73 exceptionList.add(pvve); 74 } 75 } 76 if (exceptionList.size() > 0) { 77 StringBuilder builder = new StringBuilder (); 78 Iterator <PropertyValueVetoException> iter = exceptionList.iterator(); 79 while (iter.hasNext()) { 80 PropertyValueVetoException pvve = iter.next(); 81 builder.append(pvve.getMessage()); 82 if (iter.hasNext()) 83 builder.append("\r\n"); 84 } 85 throw new PropertyValueVetoException(builder.toString()); 86 } 87 } 88 } 89 90 94 public void resetDefaultValue() throws PropertyValueVetoException { 95 if (isEditorEnabled()) { 96 for (int i = 0; i < editors.size() ; i++) { 97 editors.get(i).resetDefaultValue(); 98 } 99 } 100 } 101 102 106 public java.util.Properties getValue() { 107 java.util.Properties currentRetValue = new java.util.Properties (); 108 java.util.Iterator <SwingPropertyEditor> iter = editors.iterator(); 109 while (iter.hasNext()) { 110 currentRetValue.putAll(iter.next().getValue()); 111 } 112 113 return currentRetValue; 114 } 115 116 119 public String createSubProperty(String pSource) { 120 if (pSource.startsWith(".")) { 121 if (manager.getProperty(editorTemplate + ".addSubProperty", "").equalsIgnoreCase("false") || manager.getProperty(createSubTemplate(pSource) + ".appendProperty", "true").equalsIgnoreCase("false")) { 122 return property; 123 } else { 124 return property + pSource; 125 } 126 } else { 127 if (manager.getProperty(editorTemplate + ".propertyScoped", "").equalsIgnoreCase("true")) { 128 return property; 129 } else { 130 return pSource; 131 } 132 } 133 } 134 135 138 public String createSubTemplate(String pSource) { 139 if (pSource.startsWith(".")) { 140 return manager.getProperty(editorTemplate + ".templateBase", editorTemplate) + pSource; 141 } else { 142 return pSource; 143 } 144 } 145 146 149 public String createSubPropertyBase(String pSource) { 150 if (! pSource.startsWith(".") && ! manager.getProperty(editorTemplate + ".propertyScoped", "").equalsIgnoreCase("true")) { 151 return pSource; 152 } else { 153 return propertyBase; 154 } 155 } 156 157 160 protected void layoutGrid(Container parent, Component [] labelComponents, Component [] valueComponents, int initialX, int initialY, int xPad, int yPad, boolean nested) { 161 SpringLayout layout; 162 try { 163 layout = (SpringLayout)parent.getLayout(); 164 } catch (ClassCastException exc) { 165 System.err.println("The first argument to layoutGrid must use SpringLayout."); 166 return; 167 } 168 169 if (labelComponents == null || labelComponents.length < 1) { 170 System.err.println("Attempt to layoutGrid with no components."); 171 return; 172 } 173 174 Spring labelWidth = Spring.constant(0); 176 Spring valueWidth = Spring.constant(0); 177 Spring fullWidth = Spring.constant(0); 178 179 Spring labelValueXOffset = Spring.constant(initialX, initialX, 32000); 180 Spring xOffset = Spring.constant(initialX, initialX, initialX); 181 Spring fullXOffset = Spring.constant(initialX, initialX, 32000); 182 183 for (int i = 0; i < labelComponents.length; i++) { 184 if (valueComponents[i] != null) { 187 labelWidth = Spring.max(labelWidth, layout.getConstraints(labelComponents[i]).getWidth()); 188 valueWidth = Spring.max(valueWidth, layout.getConstraints(valueComponents[i]).getWidth()); 189 } else { 190 fullWidth = Spring.max(fullWidth, layout.getConstraints(labelComponents[i]).getWidth()); 192 } 193 } 194 195 if (fullWidth.getValue() <= labelWidth.getValue() + xPad + valueWidth.getValue()) { 197 fullWidth = Spring.sum(labelWidth, Spring.sum(Spring.constant(xPad), valueWidth)); 198 } else { 199 valueWidth = Spring.sum(fullWidth, Spring.minus(Spring.sum(Spring.constant(xPad), labelWidth))); 200 } 201 202 for (int i = 0; i < labelComponents.length; i++) { 203 if (valueComponents[i] != null) { 204 SpringLayout.Constraints constraints = layout.getConstraints(labelComponents[i]); 205 layout.putConstraint(SpringLayout.WEST, labelComponents[i], xOffset, SpringLayout.WEST, parent); 207 constraints.setWidth(labelWidth); 208 209 constraints = layout.getConstraints(valueComponents[i]); 210 layout.putConstraint(SpringLayout.WEST, valueComponents[i], xPad, SpringLayout.EAST, labelComponents[i]); 211 constraints.setWidth(valueWidth); 212 if (i == 0) { 213 layout.putConstraint(SpringLayout.EAST, parent, fullXOffset, SpringLayout.EAST, valueComponents[i]); 214 } 215 } else { 216 SpringLayout.Constraints constraints = layout.getConstraints(labelComponents[i]); 218 layout.putConstraint(SpringLayout.WEST, labelComponents[i], xOffset, SpringLayout.WEST, parent); 220 constraints.setWidth(fullWidth); 221 if (i == 0) { 222 layout.putConstraint(SpringLayout.EAST, parent, fullXOffset, SpringLayout.EAST, labelComponents[i]); 223 } 224 } 225 } 226 227 for (int i = 0; i < labelComponents.length; i++) { 229 Spring height = Spring.constant(0); 230 if (valueComponents[i] != null) { 231 height = Spring.max(layout.getConstraints(labelComponents[i]).getHeight(), layout.getConstraints(valueComponents[i]).getHeight()); 232 if (i == 0) { 233 layout.putConstraint(SpringLayout.NORTH, labelComponents[i], yPad, SpringLayout.NORTH, parent); 234 } else { 235 layout.putConstraint(SpringLayout.NORTH, labelComponents[i], yPad, SpringLayout.SOUTH, labelComponents[i - 1]); 236 } 237 layout.putConstraint(SpringLayout.NORTH, valueComponents[i], 0, SpringLayout.NORTH, labelComponents[i]); 238 layout.putConstraint(SpringLayout.SOUTH, valueComponents[i], 0, SpringLayout.SOUTH, labelComponents[i]); 239 240 layout.getConstraints(labelComponents[i]).setHeight(height); 241 layout.getConstraints(valueComponents[i]).setHeight(height); 242 } else { 243 if (i == 0) { 244 layout.putConstraint(SpringLayout.NORTH, labelComponents[i], yPad, SpringLayout.NORTH, parent); 245 } else { 246 layout.putConstraint(SpringLayout.NORTH, labelComponents[i], yPad, SpringLayout.SOUTH, labelComponents[i - 1]); 247 } 248 } 249 } 250 251 Spring southBoundary = Spring.constant(yPad, yPad, 32000); 252 if (nested) { 253 southBoundary = Spring.constant(yPad); 254 } 255 256 layout.putConstraint(SpringLayout.SOUTH, parent, southBoundary, SpringLayout.SOUTH, labelComponents[labelComponents.length - 1]); 257 } 260 261 264 public void addDisableMask(Object key) { 265 disableMaskSet.add(key); 266 updateEditorEnabled(); 267 } 268 269 272 public void removeDisableMask(Object key) { 273 disableMaskSet.remove(key); 274 updateEditorEnabled(); 275 } 276 277 280 protected void updateEditorEnabled() { 281 if (isEditorEnabled()) { 282 for(PropertyEditorUI editor: editors) { 283 editor.removeDisableMask(this); 284 } 285 } else { 286 for(PropertyEditorUI editor: editors) { 287 editor.addDisableMask(this); 288 } 289 } 290 } 291 292 295 public PropertyEditorPane getPropertyEditorPane() { 296 return getPropertyEditorPane(this); 297 } 298 299 302 public String getHelpID() { 303 String subProperty = manager.getProperty(editorTemplate + ".helpController", ""); 304 if (subProperty.length() == 0) 305 return getEditorTemplate(); 306 else { 307 String controllerProperty = createSubTemplate(subProperty); 308 Iterator <SwingPropertyEditor> iter = editors.iterator(); 309 while(iter.hasNext()) { 310 PropertyEditorUI ui = iter.next(); 311 if (ui.getEditorTemplate().equals(controllerProperty)) { 312 return ui.getHelpID(); 313 } 314 } 315 } 316 317 return getEditorTemplate(); 318 } 319 320 323 public String getDisplayValue() { 324 return getProperty(); 325 } 326 327 330 public void remove() { 331 manager.removePropertyEditorListeners(getProperty()); 332 for (PropertyEditorUI editor: editors) { 333 editor.remove(); 334 } 335 } 336 337 340 public boolean acceptDefaultFocus() { 341 if (editors != null) { 342 for (SwingPropertyEditor editor: editors) { 343 if (editor.acceptDefaultFocus()) { 344 return true; 345 } 346 } 347 return false; 348 } else { 349 return false; 350 } 351 } 352 353 354 } 355 356 357 358 | Popular Tags |