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