1 19 20 package org.netbeans.modules.editor.options; 21 22 import java.awt.*; 23 import java.beans.*; 24 import java.util.Set ; 25 import java.util.Iterator ; 26 import java.util.HashMap ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.util.ArrayList ; 29 import java.util.Arrays ; 30 import java.util.Collections ; 31 import java.util.List ; 32 import java.util.Vector ; 33 import javax.swing.SwingUtilities ; 34 35 import org.openide.explorer.propertysheet.PropertyPanel; 36 import org.openide.explorer.propertysheet.PropertyModel; 37 38 import org.netbeans.editor.Coloring; 39 import org.netbeans.editor.LocaleSupport; 40 import org.netbeans.editor.SettingsNames; 41 import org.openide.util.Lookup; 42 import org.openide.util.NbBundle; 43 44 52 public class ColoringArrayEditorPanel extends javax.swing.JPanel { 53 54 55 PropertyModel coloringModel; 56 57 58 int actValueIndex; 59 int newValueIndex; 61 62 63 64 private String typeName; 65 66 67 private String names[]; 68 69 70 72 73 private HashMap value; 74 75 76 public ColoringArrayEditorPanel() { 77 typeName = BaseOptions.BASE; 78 79 value = new HashMap (); 80 names = new String [] { SettingsNames.DEFAULT_COLORING }; 81 82 actValueIndex = 0; 83 value.put(names[0], new Coloring( Font.decode( null ), Color.red, Color.blue ) ); 84 85 initComponents (); 86 87 getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_CAEP_Panel")); syntaxLabel.setDisplayedMnemonic (getBundleString("CAEP_SyntaxLabel_Mnemonic").charAt (0)); syntaxList.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_CAEP_Syntax")); 91 coloringModel = new PropertyModelSupport( ColoringBean.class, ColoringEditor.class); 92 coloringModel.addPropertyChangeListener( new PropertyChangeListener() { 93 public void propertyChange( PropertyChangeEvent evt ) { 94 try { 95 Coloring newColoring = ((ColoringBean)coloringModel.getValue()).coloring; 96 if( ! newColoring.equals( value.get( names[actValueIndex] ) ) ) { 97 value = (HashMap )value.clone(); 100 value.put( names[actValueIndex], newColoring ); 101 actValueIndex = newValueIndex; 103 ColoringArrayEditorPanel.this.firePropertyChange( "value", null, null ); } 105 } catch( InvocationTargetException e ) { 106 if( Boolean.getBoolean( "org.netbeans.exceptions" ) ) e.printStackTrace(); } 108 } 109 }); 110 111 112 syntaxList.setSelectedIndex( actValueIndex ); 113 PropertyPanel editorPanel = new PropertyPanel( coloringModel, PropertyPanel.PREF_CUSTOM_EDITOR ); 115 detailPanel.add( editorPanel, BorderLayout.CENTER ); 116 } 117 118 private String getBundleString(String s) { 119 return NbBundle.getMessage(ColoringArrayEditorPanel.class, s); 120 } 121 122 public HashMap getValue() { 123 return value; 124 } 125 126 public void setValue( HashMap map ) { 127 if( map == null ) return; 128 129 int oldIndex = actValueIndex; 130 131 value = map; 132 133 try { 135 ClassLoader l = (ClassLoader )Lookup.getDefault().lookup(ClassLoader .class); 136 Class kitClass = Class.forName( (String )map.get( null ), true, l ); 137 typeName = OptionSupport.getTypeName( kitClass ); 138 } catch( ClassNotFoundException e ) { 139 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e); 140 return; 141 } 142 143 Set keySet = map.keySet(); 144 HashMap tempMap = new HashMap (keySet.size() - 1); 145 String [] names = new String [keySet.size() - 1]; 146 147 Iterator iter = keySet.iterator(); 148 String defaultName = null; 149 while (iter.hasNext()){ 150 String name = (String ) iter.next(); 151 if (name == null) continue; 152 String visualName = LocaleSupport.getString( "NAME_coloring_" + name ); if( visualName == null ) 154 visualName = LocaleSupport.getString("NAME_coloring_" + BaseOptions.BASE + "-" + name, name ); if (name == SettingsNames.DEFAULT_COLORING) defaultName = visualName; 156 tempMap.put(visualName, name); 157 } 158 159 List visualNamesList = new ArrayList (tempMap.keySet()); 160 161 Collections.sort(visualNamesList); 162 163 if (defaultName!=null){ 164 boolean removed = visualNamesList.remove(defaultName); 165 if (removed){ 166 visualNamesList.add(0, defaultName); 167 } 168 } 169 170 for (int i = 0; i<visualNamesList.size(); i++){ 171 names[i] = (String )tempMap.get(visualNamesList.get(i)); 172 } 173 174 this.names = names; 175 176 syntaxList.setListData(new Vector (visualNamesList)); 177 if( oldIndex < visualNamesList.size() ) actValueIndex = oldIndex; 178 else actValueIndex = 0; 179 syntaxList.setSelectedIndex( actValueIndex ); 180 } 181 182 void setEditorValue( int index ) { 183 if( index < 0 ) return; 184 185 String example = LocaleSupport.getString( "EXAMPLE_coloring_" + names[index] ); if( example == null ) 187 example = LocaleSupport.getString("EXAMPLE_coloring_" + BaseOptions.BASE + "-" + names[index], names[index] ); 189 Coloring c = (Coloring)value.get(names[index]); 190 ColoringBean bean = 191 new ColoringBean( (Coloring)value.get(names[index]), example, (Coloring)value.get(names[0]), index == 0 ); 192 193 try { 194 coloringModel.setValue( bean ); 195 } catch( java.lang.reflect.InvocationTargetException e ) { 196 if( Boolean.getBoolean( "org.netbeans.exceptions" ) ) e.printStackTrace(); } 198 } 199 200 201 202 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 204 205 detailPanel = new javax.swing.JPanel (); 206 masterPanel = new javax.swing.JPanel (); 207 syntaxLabel = new javax.swing.JLabel (); 208 syntaxScroll = new javax.swing.JScrollPane (); 209 syntaxList = new javax.swing.JList (); 210 211 setLayout(new java.awt.GridBagLayout ()); 212 213 detailPanel.setLayout(new java.awt.GridLayout (1, 1)); 214 215 gridBagConstraints = new java.awt.GridBagConstraints (); 216 gridBagConstraints.gridx = 1; 217 gridBagConstraints.gridy = 0; 218 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 219 gridBagConstraints.weightx = 1.0; 220 gridBagConstraints.weighty = 1.0; 221 gridBagConstraints.insets = new java.awt.Insets (12, 0, 11, 11); 222 add(detailPanel, gridBagConstraints); 223 224 masterPanel.setLayout(new java.awt.GridBagLayout ()); 225 226 syntaxLabel.setLabelFor(syntaxList); 227 syntaxLabel.setText(getBundleString("CAEP_SyntaxLabel")); 228 gridBagConstraints = new java.awt.GridBagConstraints (); 229 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 230 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 231 masterPanel.add(syntaxLabel, gridBagConstraints); 232 233 syntaxList.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 234 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 235 syntaxListValueChanged(evt); 236 } 237 }); 238 239 syntaxScroll.setViewportView(syntaxList); 240 241 gridBagConstraints = new java.awt.GridBagConstraints (); 242 gridBagConstraints.gridx = 0; 243 gridBagConstraints.gridy = 1; 244 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 245 gridBagConstraints.weightx = 1.0; 246 gridBagConstraints.weighty = 1.0; 247 masterPanel.add(syntaxScroll, gridBagConstraints); 248 249 gridBagConstraints = new java.awt.GridBagConstraints (); 250 gridBagConstraints.gridx = 0; 251 gridBagConstraints.gridy = 0; 252 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 253 gridBagConstraints.weightx = 1.0; 254 gridBagConstraints.weighty = 1.0; 255 gridBagConstraints.insets = new java.awt.Insets (12, 12, 11, 12); 256 add(masterPanel, gridBagConstraints); 257 258 } 260 private void syntaxListValueChanged (javax.swing.event.ListSelectionEvent evt) { if( syntaxList.getSelectedIndex() < 0 ) 263 return; 264 if( actValueIndex != syntaxList.getSelectedIndex()) { 265 newValueIndex = syntaxList.getSelectedIndex(); 266 SwingUtilities.invokeLater(new Runnable () { 267 public void run() { 268 actValueIndex = newValueIndex; 269 setEditorValue( actValueIndex ); 270 } 271 }); 272 }else{ 273 actValueIndex = syntaxList.getSelectedIndex(); 274 setEditorValue( actValueIndex ); 275 } 276 } 278 279 private javax.swing.JPanel masterPanel; 281 private javax.swing.JLabel syntaxLabel; 282 private javax.swing.JScrollPane syntaxScroll; 283 private javax.swing.JList syntaxList; 284 private javax.swing.JPanel detailPanel; 285 287 private class PropertyModelSupport implements PropertyModel { 288 289 290 private PropertyChangeSupport support; 291 292 Class type; 293 Class editor; 294 Object value; 295 296 public PropertyModelSupport( Class propertyType, Class propertyEditor ) { 297 support = new PropertyChangeSupport(this); 298 this.type = propertyType; 299 this.editor = propertyEditor; 300 } 301 302 public Class getPropertyType() { 303 return type; 304 } 305 306 public Class getPropertyEditorClass() { 307 return editor; 308 } 309 310 public Object getValue() { 311 return value; 312 } 313 314 public void setValue(Object v) { 315 if( v != null && (! v.equals( value ) ) ) { 316 value = v; 317 support.firePropertyChange( PROP_VALUE, null, null ); 318 } 319 } 320 321 322 324 public void addPropertyChangeListener(PropertyChangeListener l) { 325 support.addPropertyChangeListener(l); 326 } 327 328 330 public void removePropertyChangeListener(PropertyChangeListener l) { 331 support.removePropertyChangeListener(l); 332 } 333 334 } 335 } 336 | Popular Tags |