1 6 7 package org.netbeans.modules.options.colors; 8 9 import java.awt.Color ; 10 import java.awt.Component ; 11 import java.awt.event.ActionEvent ; 12 import java.awt.event.ActionListener ; 13 import java.beans.PropertyChangeEvent ; 14 import java.beans.PropertyChangeListener ; 15 import java.util.ArrayList ; 16 import java.util.Collection ; 17 import java.util.Collections ; 18 import java.util.HashMap ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Set ; 24 import java.util.Vector ; 25 import javax.swing.AbstractButton ; 26 import javax.swing.JComponent ; 27 import javax.swing.JLabel ; 28 import javax.swing.JPanel ; 29 import javax.swing.ListSelectionModel ; 30 import javax.swing.event.ListSelectionEvent ; 31 import javax.swing.event.ListSelectionListener ; 32 import javax.swing.text.AttributeSet ; 33 import javax.swing.text.SimpleAttributeSet ; 34 import javax.swing.text.StyleConstants ; 35 import org.netbeans.api.editor.settings.EditorStyleConstants; 36 import org.openide.awt.Mnemonics; 37 import org.openide.util.NbBundle; 38 39 43 public class AnnotationsPanel extends JPanel implements ActionListener , 44 PropertyChangeListener { 45 46 private ColorModel colorModel; 47 private boolean listen = false; 48 private String currentScheme; 49 private Map schemes = new HashMap (); 50 private Set toBeSaved = new HashSet (); 51 private boolean changed = false; 52 53 54 55 public AnnotationsPanel() { 56 initComponents(); 57 58 cbForeground.getAccessibleContext ().setAccessibleName (loc ("AN_Foreground_Chooser")); 60 cbForeground.getAccessibleContext ().setAccessibleDescription (loc ("AD_Foreground_Chooser")); 61 cbBackground.getAccessibleContext ().setAccessibleName (loc ("AN_Background_Chooser")); 62 cbBackground.getAccessibleContext ().setAccessibleDescription (loc ("AD_Background_Chooser")); 63 cbWaveUnderlined.getAccessibleContext ().setAccessibleName (loc ("AN_Wave_Underlined")); 64 cbWaveUnderlined.getAccessibleContext ().setAccessibleDescription (loc ("AD_Wave_Underlined")); 65 lCategories.getAccessibleContext ().setAccessibleName (loc ("AN_Categories")); 66 lCategories.getAccessibleContext ().setAccessibleDescription (loc ("AD_Categories")); 67 ColorComboBox.init (cbForeground); 68 ColorComboBox.init (cbBackground); 69 ColorComboBox.init (cbWaveUnderlined); 70 lCategories.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); 71 lCategories.setVisibleRowCount (3); 72 lCategories.addListSelectionListener (new ListSelectionListener () { 73 public void valueChanged (ListSelectionEvent e) { 74 if (!listen) return; 75 refreshUI (); 76 } 77 }); 78 lCategories.setCellRenderer (new CategoryRenderer ()); 79 cbForeground.addPropertyChangeListener (this); 80 ((JComponent )cbForeground.getEditor()).addPropertyChangeListener (this); 81 cbBackground.addPropertyChangeListener (this); 82 ((JComponent )cbBackground.getEditor()).addPropertyChangeListener (this); 83 cbWaveUnderlined.addPropertyChangeListener (this); 84 ((JComponent )cbWaveUnderlined.getEditor()).addPropertyChangeListener (this); 85 JLabel lCategory = new JLabel (); 86 loc (lCategory, "CTL_Category"); 87 lCategory.setLabelFor (lCategories); 88 } 89 90 95 private void initComponents() { 97 lCategory = new javax.swing.JLabel (); 98 cpCategories = new javax.swing.JScrollPane (); 99 lCategories = new javax.swing.JList (); 100 lForeground = new javax.swing.JLabel (); 101 lbackground = new javax.swing.JLabel (); 102 lWaveUnderlined = new javax.swing.JLabel (); 103 cbForeground = new javax.swing.JComboBox (); 104 cbBackground = new javax.swing.JComboBox (); 105 cbWaveUnderlined = new javax.swing.JComboBox (); 106 107 lCategory.setText("Category:"); 108 109 cpCategories.setViewportView(lCategories); 110 111 lForeground.setText("Foreground:"); 112 113 lbackground.setText("Background:"); 114 115 lWaveUnderlined.setText("Wave Underlined:"); 116 117 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 118 this.setLayout(layout); 119 layout.setHorizontalGroup( 120 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 121 .add(layout.createSequentialGroup() 122 .addContainerGap() 123 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 124 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() 125 .add(cpCategories, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE) 126 .add(20, 20, 20) 127 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 128 .add(lbackground) 129 .add(lWaveUnderlined) 130 .add(lForeground)) 131 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 132 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 133 .add(cbForeground, 0, 71, Short.MAX_VALUE) 134 .add(cbBackground, 0, 71, Short.MAX_VALUE) 135 .add(cbWaveUnderlined, 0, 71, Short.MAX_VALUE))) 136 .add(lCategory)) 137 .addContainerGap()) 138 ); 139 layout.setVerticalGroup( 140 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 141 .add(layout.createSequentialGroup() 142 .addContainerGap() 143 .add(lCategory) 144 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 145 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 146 .add(layout.createSequentialGroup() 147 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 148 .add(lForeground) 149 .add(cbForeground, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 150 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 151 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 152 .add(lbackground) 153 .add(cbBackground, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 154 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 155 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 156 .add(lWaveUnderlined) 157 .add(cbWaveUnderlined, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))) 158 .add(cpCategories, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)) 159 .addContainerGap()) 160 ); 161 } 163 164 private javax.swing.JComboBox cbBackground; 166 private javax.swing.JComboBox cbForeground; 167 private javax.swing.JComboBox cbWaveUnderlined; 168 private javax.swing.JScrollPane cpCategories; 169 private javax.swing.JList lCategories; 170 private javax.swing.JLabel lCategory; 171 private javax.swing.JLabel lForeground; 172 private javax.swing.JLabel lWaveUnderlined; 173 private javax.swing.JLabel lbackground; 174 176 177 public void actionPerformed (ActionEvent evt) { 178 if (!listen) return; 179 updateData (); 180 changed = true; 181 } 182 183 public void propertyChange (PropertyChangeEvent evt) { 184 if (!listen) return; 185 if (evt.getPropertyName () != ColorComboBox.PROP_COLOR) return; 186 updateData (); 187 } 188 189 void update (ColorModel colorModel) { 190 this.colorModel = colorModel; 191 listen = false; 192 currentScheme = colorModel.getCurrentProfile (); 193 lCategories.setListData (getAnnotations (currentScheme)); 194 if (lCategories.getModel ().getSize () > 0) 195 lCategories.setSelectedIndex (0); 196 refreshUI (); 197 listen = true; 198 changed = false; 199 } 200 201 void cancel () { 202 toBeSaved = new HashSet (); 203 schemes = new HashMap (); 204 changed = false; 205 } 206 207 void applyChanges () { 208 if (colorModel == null) return; 209 Iterator it = toBeSaved.iterator (); 210 while (it.hasNext ()) { 211 String scheme = (String ) it.next (); 212 colorModel.setAnnotations (scheme, getAnnotations (scheme)); 213 } 214 toBeSaved = new HashSet (); 215 schemes = new HashMap (); 216 } 217 218 boolean isChanged () { 219 return changed; 220 } 221 222 public void setCurrentProfile (String currentScheme) { 223 String oldScheme = this.currentScheme; 224 this.currentScheme = currentScheme; 225 Vector v = getAnnotations (currentScheme); 226 if (v == null) { 227 v = getAnnotations (oldScheme); 229 schemes.put (currentScheme, new Vector (v)); 230 toBeSaved.add (currentScheme); 231 v = getAnnotations (currentScheme); 232 } 233 lCategories.setListData (v); 234 if (lCategories.getModel ().getSize () > 0) 235 lCategories.setSelectedIndex (0); 236 refreshUI (); 237 } 238 239 void deleteProfile (String scheme) { 240 } 241 242 243 245 private static String loc (String key) { 246 return NbBundle.getMessage (SyntaxColoringPanel.class, key); 247 } 248 249 private static void loc (Component c, String key) { 250 if (c instanceof AbstractButton ) 251 Mnemonics.setLocalizedText ( 252 (AbstractButton ) c, 253 loc (key) 254 ); 255 else 256 Mnemonics.setLocalizedText ( 257 (JLabel ) c, 258 loc (key) 259 ); 260 } 261 262 private void updateData () { 263 Vector annotations = getAnnotations (currentScheme); 264 SimpleAttributeSet c = (SimpleAttributeSet ) annotations.get 265 (lCategories.getSelectedIndex ()); 266 Color color = ((ColorValue) cbBackground.getSelectedItem ()).color; 267 if (color != null) 268 c.addAttribute ( 269 StyleConstants.Background, 270 color 271 ); 272 else 273 c.removeAttribute (StyleConstants.Background); 274 color = ((ColorValue) cbForeground.getSelectedItem ()).color; 275 if (color != null) 276 c.addAttribute ( 277 StyleConstants.Foreground, 278 color 279 ); 280 else 281 c.removeAttribute (StyleConstants.Foreground); 282 color = ((ColorValue) cbWaveUnderlined.getSelectedItem ()).color; 283 if (color != null) 284 c.addAttribute ( 285 EditorStyleConstants.WaveUnderlineColor, 286 color 287 ); 288 else 289 c.removeAttribute (EditorStyleConstants.WaveUnderlineColor); 290 toBeSaved.add (currentScheme); 291 } 292 293 private void refreshUI () { 294 int index = lCategories.getSelectedIndex (); 295 if (index < 0) { 296 cbForeground.setEnabled (false); 298 cbBackground.setEnabled (false); 299 cbWaveUnderlined.setEnabled (false); 300 return; 301 } 302 cbForeground.setEnabled (true); 303 cbBackground.setEnabled (true); 304 cbWaveUnderlined.setEnabled (true); 305 306 listen = false; 307 308 AttributeSet defAs = getDefaultColoring(); 310 if (defAs != null) { 311 Color inheritedForeground = (Color ) defAs.getAttribute(StyleConstants.Foreground); 312 if (inheritedForeground == null) { 313 inheritedForeground = Color.black; 314 } 315 ColorComboBox.setInheritedColor(cbForeground, inheritedForeground); 316 317 Color inheritedBackground = (Color ) defAs.getAttribute(StyleConstants.Background); 318 if (inheritedBackground == null) { 319 inheritedBackground = Color.white; 320 } 321 ColorComboBox.setInheritedColor(cbBackground, inheritedBackground); 322 } 323 324 Vector annotations = getAnnotations (currentScheme); 326 AttributeSet c = (AttributeSet ) annotations.get (index); 327 ColorComboBox.setColor ( 328 cbForeground, 329 (Color ) c.getAttribute (StyleConstants.Foreground) 330 ); 331 ColorComboBox.setColor ( 332 cbBackground, 333 (Color ) c.getAttribute (StyleConstants.Background) 334 ); 335 ColorComboBox.setColor ( 336 cbWaveUnderlined, 337 (Color ) c.getAttribute (EditorStyleConstants.WaveUnderlineColor) 338 ); 339 listen = true; 340 } 341 342 private AttributeSet getDefaultColoring() { 343 Collection defaults = colorModel.getCategories(currentScheme, ColorModel.ALL_LANGUAGES); 344 345 for(Iterator i = defaults.iterator(); i.hasNext(); ) { 346 AttributeSet as = (AttributeSet ) i.next(); 347 String name = (String ) as.getAttribute(StyleConstants.NameAttribute); 348 if (name != null && "default".equals(name)) { return as; 350 } 351 } 352 353 return null; 354 } 355 356 private Vector getAnnotations (String scheme) { 357 if (!schemes.containsKey (scheme)) { 358 Collection c = colorModel.getAnnotations (currentScheme); 359 if (c == null) return null; 360 List l = new ArrayList (c); 361 Collections.sort (l, new CategoryComparator ()); 362 schemes.put (scheme, new Vector (l)); 363 } 364 return (Vector ) schemes.get (scheme); 365 } 366 } 367 | Popular Tags |