1 19 24 25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers.common; 26 27 import java.util.ResourceBundle ; 28 29 import java.awt.Color ; 30 import java.awt.Component ; 31 import java.awt.Container ; 32 33 import java.beans.Customizer ; 34 import java.beans.PropertyChangeEvent ; 35 import java.beans.PropertyChangeListener ; 36 37 import javax.swing.JPanel ; 38 import javax.swing.ImageIcon ; 39 import javax.swing.UIManager ; 40 import org.openide.ErrorManager; 41 42 import org.openide.util.HelpCtx; 43 44 import org.netbeans.modules.j2ee.sun.share.configbean.Base; 45 import org.netbeans.modules.j2ee.sun.share.configbean.Utils; 46 import org.netbeans.modules.j2ee.sun.share.configbean.ErrorMessageDB; 47 import org.netbeans.modules.j2ee.sun.share.configbean.ValidationError; 48 49 50 68 public abstract class BaseCustomizer extends JPanel implements Customizer , 69 HelpCtx.Provider, CustomizerErrorPanel.ErrorClient { 70 71 73 protected static final ResourceBundle commonBundle = ResourceBundle.getBundle( 74 "org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.Bundle"); 76 78 private static final String errorGlyphPath = 79 "org/netbeans/modules/j2ee/sun/share/configbean/customizers/common/resources/errorGlyph.gif"; 81 84 public static ImageIcon panelErrorIcon; 87 88 static { 89 try { 92 panelErrorIcon = new ImageIcon (Utils.getResourceURL(errorGlyphPath, BaseCustomizer.class)); 93 } catch(NullPointerException ex) { 94 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 95 panelErrorIcon = null; 96 } 97 } 98 99 101 private static final String errorIconPath = 102 "org/netbeans/modules/j2ee/sun/share/configbean/customizers/common/resources/errorIcon.png"; private static final String warningIconPath = 104 "org/netbeans/modules/j2ee/sun/share/configbean/customizers/common/resources/warningIcon.png"; 106 public static ImageIcon errorMessageIcon; 107 public static ImageIcon warningMessageIcon; 108 109 static { 110 try { 113 errorMessageIcon = new ImageIcon (Utils.getResourceURL(errorIconPath, InputDialog.class)); 114 } catch(NullPointerException ex) { 115 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 116 errorMessageIcon = null; 117 } 118 try { 119 warningMessageIcon = new ImageIcon (Utils.getResourceURL(warningIconPath, InputDialog.class)); 120 } catch(NullPointerException ex) { 121 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 122 warningMessageIcon = null; 123 } 124 } 125 126 127 131 private static final Object colorMonitor = new Object (); 132 private static Color errorTextForegroundColor = null; 133 private static Color warningTextForegroundColor = null; 134 135 public static Color getErrorForegroundColor() { 136 Color result = null; 137 138 synchronized(colorMonitor) { 139 if(errorTextForegroundColor == null) { 140 errorTextForegroundColor = UIManager.getColor("nb.errorForeground"); 141 if(errorTextForegroundColor == null) { 142 errorTextForegroundColor = new Color (89, 79, 191); } 144 } 145 146 result = errorTextForegroundColor; 147 } 148 149 return result; 150 } 151 152 public static Color getWarningForegroundColor() { 153 Color result = null; 154 155 synchronized(colorMonitor) { 156 if(warningTextForegroundColor == null) { 157 warningTextForegroundColor = UIManager.getColor("Label.foreground"); 158 if(warningTextForegroundColor == null) { 159 warningTextForegroundColor = new Color (0,0,0); } 161 } 162 163 result = warningTextForegroundColor; 164 } 165 166 return result; 167 } 168 169 172 176 private Base theBaseBean; 177 178 179 private CustomizerTitlePanel titlePanel; 180 181 182 private CustomizerErrorPanel errorPanel; 183 184 185 186 public BaseCustomizer() { 187 titlePanel = new CustomizerTitlePanel(); 188 errorPanel = new CustomizerErrorPanel(this); 189 } 190 191 198 protected void addTitlePanel(String title) { 199 titlePanel.setCustomizerTitle(title); 200 add(titlePanel, titlePanel.getConstraints(), 0); 201 } 202 203 207 public CustomizerTitlePanel getTitlePanel() { 208 return titlePanel; 209 } 210 211 217 protected void addErrorPanel() { 218 add(errorPanel, errorPanel.getConstraints()); 219 } 220 221 225 public CustomizerErrorPanel getErrorPanel() { 226 return errorPanel; 227 } 228 229 230 233 public void setObject(Object bean) { 234 if(theBaseBean != bean) { 235 if(theBaseBean != null) { 236 removeListeners(); 238 } 239 240 if(setBean(bean)) { 241 assert (theBaseBean != null) : 242 "Derived class failed to call super.setBean() in their implementation"; 244 initFields(); 246 247 addListeners(); 249 250 validateBean(); 252 } 253 } 254 } 255 256 257 268 protected boolean setBean(Object bean) { 269 boolean result = false; 270 271 if(bean instanceof Base) { 272 theBaseBean = (Base) bean; 273 result = true; 274 } else { 275 theBaseBean = null; 276 } 277 278 return result; 279 } 280 281 Base getBean() { 283 return theBaseBean; 284 } 285 286 292 protected abstract void initFields(); 293 294 295 298 protected PropertyChangeListener validationListener = new PropertyChangeListener () { 299 public void propertyChange(PropertyChangeEvent evt) { 300 if(ErrorMessageDB.VALIDATION_STATE_CHANGED.equals(evt.getPropertyName())) { 301 validationStateChanged((Boolean ) evt.getNewValue()); 302 } else if(ErrorMessageDB.PARTITION_STATE_CHANGED.equals(evt.getPropertyName())) { 303 partitionStateChanged((ErrorMessageDB.PartitionState) evt.getOldValue(), 304 (ErrorMessageDB.PartitionState) evt.getNewValue()); 305 } 306 } 307 }; 308 309 310 313 protected void addListeners() { 314 ErrorMessageDB.getMessageDB(theBaseBean).addPropertyChangeListener(validationListener); 315 } 316 317 318 321 protected void removeListeners() { 322 ErrorMessageDB.getMessageDB(theBaseBean).removePropertyChangeListener(validationListener); 323 } 324 325 326 333 public void validationStateChanged(Boolean newState) { 334 } 335 336 337 348 public void partitionStateChanged(ErrorMessageDB.PartitionState oldState, 349 ErrorMessageDB.PartitionState newState) { 350 showErrors(); 351 } 352 353 354 358 public boolean validateBean() { 359 return theBaseBean.validateFields(false); 360 } 361 362 363 368 public boolean validateField(String fieldId) { 369 return theBaseBean.validateField(fieldId); 370 } 371 372 373 375 public void showErrors() { 376 errorPanel.showErrors(theBaseBean); 377 } 378 379 380 385 public abstract String getHelpId(); 386 387 388 391 public HelpCtx getHelpCtx() { 392 return new HelpCtx(getHelpId()); 393 } 394 395 403 public static int getComponentIndex(Container container, Component target) { 404 int result = -1; 405 406 Component [] components = container.getComponents(); 407 if(components != null) { 408 for(int i = 0; i < components.length; i++) { 409 if(components[i] == target) { 410 result = i; 411 break; 412 } 413 } 414 } 415 416 return result; 417 } 418 419 422 427 public Color getErrorMessageForegroundColor() { 428 return BaseCustomizer.getErrorForegroundColor(); 429 } 430 431 432 438 public ValidationError.Partition getPartition() { 439 return ValidationError.PARTITION_GLOBAL; 440 } 441 } 442 | Popular Tags |