1 19 20 package org.netbeans.modules.editor.options; 21 22 import java.awt.*; 23 import java.awt.event.*; 24 import java.beans.*; 25 import java.lang.reflect.InvocationTargetException ; 26 import javax.swing.*; 27 import javax.swing.border.*; 28 import javax.accessibility.*; 29 30 import org.openide.explorer.propertysheet.PropertyPanel; 31 import org.openide.explorer.propertysheet.PropertyModel; 32 import org.netbeans.editor.Coloring; 33 import org.netbeans.editor.Settings; 34 import org.netbeans.editor.SettingsDefaults; 35 import java.awt.Dimension ; 36 import java.beans.FeatureDescriptor ; 37 import org.openide.explorer.propertysheet.ExPropertyModel; 38 39 46 public class ColoringEditorPanel extends javax.swing.JPanel { 47 48 public static final String PROP_COLORING = "Coloring"; 50 51 52 private ColoringBean value; 53 54 57 private PropWithDefaultPanel fontPanel; 58 private PropWithDefaultPanel fgColorPanel; 59 private PropWithDefaultPanel bgColorPanel; 60 61 63 private ColoringPreview preview; 64 65 66 public ColoringEditorPanel() { 67 68 value = new ColoringBean(SettingsDefaults.defaultColoring, "null", SettingsDefaults.defaultColoring, true ); 70 71 initComponents (); 72 73 GridBagConstraints gridBagConstraints1 = new java.awt.GridBagConstraints (); 74 gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; 75 gridBagConstraints1.insets = new java.awt.Insets (0, 0, 0, 0); 76 gridBagConstraints1.weightx = 1.0; 77 Class fontEditorClass = PropertyEditorManager.findEditor(Font.class).getClass(); 79 Class colorEditorClass = PropertyEditorManager.findEditor(Color.class).getClass(); 80 fontPanel = new PropWithDefaultPanel( Font.class, fontEditorClass, 81 getBundleString("CEP_FontTitle"), getBundleString("ACSD_CEP_Font"), getBundleString("CEP_FontTrans"), getBundleString("CEP_FontTrans_Mnemonic").charAt(0), getBundleString("ACSD_CEP_FontTrans")); fontPanel.addPropertyChangeListener( new PropertyChangeListener() { 87 public void propertyChange( PropertyChangeEvent evt ) { 88 if( PropWithDefaultPanel.PROP_VALUE.equals( evt.getPropertyName() ) ) { 89 Font newValue = (Font)fontPanel.getValue(); 90 setValueImpl( Coloring.changeFont( value.coloring, newValue ) ); 91 } 92 } 93 } ); 94 add( fontPanel, gridBagConstraints1 ); 95 96 gridBagConstraints1 = new java.awt.GridBagConstraints (); 97 gridBagConstraints1.gridx = 0; 98 gridBagConstraints1.gridy = 1; 99 gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; 100 gridBagConstraints1.insets = new java.awt.Insets (12, 0, 0, 0); 101 gridBagConstraints1.weightx = 1.0; 102 fgColorPanel = new PropWithDefaultPanel( Color.class, colorEditorClass, 103 getBundleString("CEP_FgTitle"), getBundleString("ACSD_CEP_Fg"), getBundleString("CEP_FgTrans"), getBundleString("CEP_FgTrans_Mnemonic").charAt(0), getBundleString("ACSD_CEP_FgTrans")); fgColorPanel.addPropertyChangeListener( new PropertyChangeListener() { 109 public void propertyChange( PropertyChangeEvent evt ) { 110 if( PropWithDefaultPanel.PROP_VALUE.equals( evt.getPropertyName() ) ) { 111 Color newValue = (Color)fgColorPanel.getValue(); 112 setValueImpl( Coloring.changeForeColor( value.coloring, newValue) ); 113 } 114 } 115 } ); 116 add( fgColorPanel, gridBagConstraints1 ); 117 118 gridBagConstraints1 = new java.awt.GridBagConstraints (); 119 gridBagConstraints1.gridx = 0; 120 gridBagConstraints1.gridy = 2; 121 gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; 122 gridBagConstraints1.insets = new java.awt.Insets (12, 0, 0, 0); 123 gridBagConstraints1.weightx = 1.0; 124 bgColorPanel = new PropWithDefaultPanel( Color.class, colorEditorClass, 125 getBundleString("CEP_BgTitle"), getBundleString("ACSD_CEP_Bg"), getBundleString("CEP_BgTrans"), getBundleString("CEP_BgTrans_Mnemonic").charAt(0), getBundleString("ACSD_CEP_BgTrans")); 131 bgColorPanel.addPropertyChangeListener( new PropertyChangeListener() { 132 public void propertyChange( PropertyChangeEvent evt ) { 133 if( PropWithDefaultPanel.PROP_VALUE.equals( evt.getPropertyName() ) ) { 134 Color newValue = (Color)bgColorPanel.getValue(); 135 setValueImpl( Coloring.changeBackColor( value.coloring, newValue) ); 136 } 137 } 138 } ); 139 add( bgColorPanel, gridBagConstraints1 ); 140 141 gridBagConstraints1 = new java.awt.GridBagConstraints (); 142 gridBagConstraints1.gridx = 0; 143 gridBagConstraints1.gridy = 3; 144 gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; 145 gridBagConstraints1.insets = new java.awt.Insets (12, 0, 0, 0); 146 gridBagConstraints1.weightx = 1.0; 147 gridBagConstraints1.weighty = 1.0; 148 JPanel previewPanel = new JPanel( new BorderLayout () ); 149 previewPanel.setBorder( new CompoundBorder( 150 new TitledBorder( getBundleString("CEP_PreviewTitle" ) ), new EmptyBorder( new Insets( 9, 12, 11, 11) ) 152 ) ); 153 154 preview = new ColoringPreview(); 155 preview.getAccessibleContext().setAccessibleName(getBundleString("ACSN_CEP_Preview")); 156 preview.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_CEP_Preview")); 157 previewPanel.add( preview ); 158 add( previewPanel, gridBagConstraints1 ); 159 160 updateEditors(); 161 162 Dimension small = getPreferredSize(); 163 small.width *= 2; 164 small.height *= 1.4; 165 setPreferredSize( small ); 166 } 167 168 private String getBundleString(String s) { 169 return org.openide.util.NbBundle.getMessage(ColoringEditorPanel.class, s); 170 } 171 172 175 public ColoringBean getValue() { 176 return value; 177 } 178 179 183 public void setValue( ColoringBean value ) { 184 if( (value == null) || (value.coloring == null) ) { 185 return; 186 } 187 188 if( this.value != value ) { 189 this.value = value; 190 updateEditors(); 191 preview.setValue( value ); 192 193 firePropertyChange( "value", null, null ); } 195 } 196 197 198 private void setValueImpl( Coloring newColoring ) { 199 200 value = value.changeColoring( newColoring ); 201 202 preview.setValue( value ); 203 205 firePropertyChange( "value", null, null ); } 207 208 209 private void updateEditors() { 210 if( value == null ) { 211 return; 212 } 213 214 fontPanel.setValue( value.coloring.getFont() ); 215 fontPanel.setDefaultValue( value.defaultColoring.getFont() ); 216 fontPanel.setDefault( value.isDefault ); 217 218 fgColorPanel.setValue( value.coloring.getForeColor() ); 219 fgColorPanel.setDefaultValue( value.defaultColoring.getForeColor() ); 220 fgColorPanel.setDefault( value.isDefault ); 221 222 bgColorPanel.setValue( value.coloring.getBackColor() ); 223 bgColorPanel.setDefaultValue( value.defaultColoring.getBackColor() ); 224 bgColorPanel.setDefault( value.isDefault ); 225 } 226 227 228 private void initComponents() { 230 setLayout(new java.awt.GridBagLayout ()); 231 232 } 234 235 238 239 241 private class ColoringPreview extends javax.swing.JComponent implements Accessible { 242 ColoringBean value; 243 244 void setValue( ColoringBean c ) { 245 value = c; 246 this.repaint(); 247 } 248 249 public void paint( java.awt.Graphics g ) { 250 if (value != null) { 251 Coloring coloring = value.coloring.apply( value.defaultColoring ); 252 253 java.awt.Rectangle box = this.getBounds(); 254 255 g.setColor(coloring.getBackColor()); 257 g.fillRect(0, 0 , box.width - 1, box.height - 1); 258 259 g.setColor(coloring.getForeColor()); 261 g.setFont(coloring.getFont()); 262 FontMetrics fm = g.getFontMetrics(); 263 int x = Math.max((box.width - fm.stringWidth(value.example)) / 2, 0); 264 int y = Math.max((box.height - fm.getHeight()) / 2 + fm.getAscent(), 0); 265 g.drawString(value.example, x, y); 266 } 267 } 268 269 public AccessibleContext getAccessibleContext () { 270 if (accessibleContext == null) { 271 accessibleContext = new AccessibleJComponent() { 272 public AccessibleRole getAccessibleRole() { 273 return AccessibleRole.PANEL; 274 } 275 }; 276 } 277 return accessibleContext; 278 } 279 280 } 281 282 private static class PropWithDefaultPanel extends JPanel { 284 285 public static final String PROP_VALUE = "RealValue"; 287 Object value; 288 Object defaultValue; 289 boolean isDefault; 290 291 PropertyModel model; 292 JCheckBox defaultCheckBox; 293 294 public PropWithDefaultPanel( Class propertyClass, Class propertyEditorClass, String title, String description, 295 String checkBoxTitle, char checkBoxMnemonic, String checkBoxDescription) { 296 297 setLayout( new java.awt.BorderLayout ()); 298 setBorder( new CompoundBorder( new TitledBorder( title ), 299 new EmptyBorder( new Insets( 8, 12, 6, 11) ) ) ); 300 301 model = new PropertyModelSupport( propertyClass, propertyEditorClass, title ); 302 model.addPropertyChangeListener( new PropertyChangeListener() { 303 public void propertyChange( PropertyChangeEvent evt ) { 304 if( PropertyModelSupport.PROP_MOD_VALUE.equals( evt.getPropertyName() ) ) { 305 306 Object newValue = null; 307 try { 308 newValue = model.getValue(); 309 } catch( InvocationTargetException e ) { 310 if( Boolean.getBoolean( "org.netbeans.exceptions" ) ) e.printStackTrace(); } 312 313 if( value == null && newValue.equals( defaultValue ) ) { return; } 316 317 value = newValue; 318 defaultCheckBox.setSelected( false ); firePropertyChange( PROP_VALUE, null, null ); 320 repaint(); } 322 } 323 } ); 324 PropertyPanel pp = new PropertyPanel (model, 0); 325 pp.setMinimumSize(new Dimension (50,22)); 326 pp.getAccessibleContext().setAccessibleName(title); 327 pp.getAccessibleContext().setAccessibleDescription(description); 328 add (pp, BorderLayout.CENTER); 329 330 331 defaultCheckBox = new JCheckBox(); 332 defaultCheckBox.setText(checkBoxTitle); 333 defaultCheckBox.setMnemonic(checkBoxMnemonic); 334 defaultCheckBox.getAccessibleContext().setAccessibleDescription(checkBoxDescription); 335 defaultCheckBox.addActionListener( new ActionListener() { 336 public void actionPerformed( ActionEvent evt ) { 337 if( !defaultCheckBox.isSelected() ) { value = defaultValue; 339 modelSetValue( defaultValue ); 340 firePropertyChange( PROP_VALUE, null, null ); 341 342 } else { value = null; 344 modelSetValue( defaultValue ); 345 firePropertyChange( PROP_VALUE, null, null ); 346 } 347 } 348 } ); 349 add( defaultCheckBox, BorderLayout.SOUTH ); 350 } 351 352 public void firePropertyChange( String s, Object old, Object newVal ) { 353 super.firePropertyChange( s, old, newVal ); 354 } 355 356 357 public void setValue( Object value ) { 358 this.value = value; 359 if( value == null ) { 360 modelSetValue( defaultValue ); 361 defaultCheckBox.setSelected( true ); 362 } else { 363 modelSetValue( value ); 364 defaultCheckBox.setSelected( false ); 365 } 366 } 367 368 Object getValue() { 369 return value; 370 } 371 372 public void setDefaultValue( Object def ) { 373 defaultValue = def; 375 if( value == null ) modelSetValue( defaultValue ); 376 } 377 378 public void setDefault( boolean isDefault ) { 379 this.isDefault = isDefault; 380 } 381 382 public void addNotify() { 383 super.addNotify(); 384 if (isDefault) { 385 defaultCheckBox.setEnabled(false); 386 defaultCheckBox.setSelected(false); 387 } 388 } 389 390 private void modelSetValue( Object val ) { 391 try { 392 model.setValue( val ); 393 } catch( InvocationTargetException e ) { 394 if( Boolean.getBoolean( "org.netbeans.exceptions" ) ) e.printStackTrace(); } 396 repaint(); } 398 399 private class PropertyModelSupport implements ExPropertyModel { 400 401 public static final String PROP_MOD_VALUE = "value"; 403 404 private PropertyChangeSupport support; 405 406 Class type; 407 Class editor; 408 Object value; 409 String displayName; 410 411 public PropertyModelSupport( Class propertyType, Class propertyEditor, String displayName ) { 412 support = new PropertyChangeSupport(this); 413 this.type = propertyType; 414 this.editor = propertyEditor; 415 this.displayName = displayName; 416 } 417 418 public Class getPropertyType() { 419 return type; 420 } 421 422 public Class getPropertyEditorClass() { 423 return editor; 424 } 425 426 public Object getValue() { 427 return value; 428 } 429 430 public void setValue(Object v) { 431 if( v != null && (!v.equals( value )) ) { 433 value = v; 435 support.firePropertyChange( PROP_MOD_VALUE, null, null ); 436 } 437 } 438 439 440 442 public void addPropertyChangeListener(PropertyChangeListener l) { 443 support.addPropertyChangeListener(l); 444 } 445 446 448 public void removePropertyChangeListener(PropertyChangeListener l) { 449 support.removePropertyChangeListener(l); 450 } 451 452 public Object [] getBeans() { 453 return new Object [0]; 454 } 455 456 public FeatureDescriptor getFeatureDescriptor() { 457 FeatureDescriptor desc = new FeatureDescriptor (); 458 desc.setDisplayName(displayName); 459 if (editor == PropertyEditorManager.findEditor(Font.class).getClass()){ 460 desc.setValue("canEditAsText", Boolean.FALSE); } 463 return desc; 464 } 465 466 } 467 468 } 469 470 } 471 | Popular Tags |