1 11 package org.eclipse.jdt.ui.text; 12 13 import org.eclipse.core.runtime.Preferences; 14 15 import org.eclipse.jface.preference.IPreferenceStore; 16 import org.eclipse.jface.util.IPropertyChangeListener; 17 import org.eclipse.jface.util.PropertyChangeEvent; 18 19 import org.eclipse.jface.text.IDocument; 20 import org.eclipse.jface.text.IDocumentExtension3; 21 import org.eclipse.jface.text.IDocumentPartitioner; 22 import org.eclipse.jface.text.rules.FastPartitioner; 23 import org.eclipse.jface.text.rules.IPartitionTokenScanner; 24 import org.eclipse.jface.text.rules.RuleBasedScanner; 25 26 import org.eclipse.jdt.internal.ui.text.FastJavaPartitionScanner; 27 import org.eclipse.jdt.internal.ui.text.JavaColorManager; 28 import org.eclipse.jdt.internal.ui.text.JavaCommentScanner; 29 import org.eclipse.jdt.internal.ui.text.SingleTokenJavaScanner; 30 import org.eclipse.jdt.internal.ui.text.java.JavaCodeScanner; 31 import org.eclipse.jdt.internal.ui.text.javadoc.JavaDocScanner; 32 33 34 43 public class JavaTextTools { 44 45 49 private final static String [] LEGAL_CONTENT_TYPES= new String [] { 50 IJavaPartitions.JAVA_DOC, 51 IJavaPartitions.JAVA_MULTI_LINE_COMMENT, 52 IJavaPartitions.JAVA_SINGLE_LINE_COMMENT, 53 IJavaPartitions.JAVA_STRING, 54 IJavaPartitions.JAVA_CHARACTER 55 }; 56 57 60 private class PreferenceListener implements IPropertyChangeListener, Preferences.IPropertyChangeListener { 61 public void propertyChange(PropertyChangeEvent event) { 62 adaptToPreferenceChange(event); 63 } 64 public void propertyChange(Preferences.PropertyChangeEvent event) { 65 adaptToPreferenceChange(new PropertyChangeEvent(event.getSource(), event.getProperty(), event.getOldValue(), event.getNewValue())); 66 } 67 } 68 69 70 private JavaColorManager fColorManager; 71 72 private JavaCodeScanner fCodeScanner; 73 74 private JavaCommentScanner fMultilineCommentScanner; 75 76 private JavaCommentScanner fSinglelineCommentScanner; 77 78 private SingleTokenJavaScanner fStringScanner; 79 80 private JavaDocScanner fJavaDocScanner; 81 82 private IPreferenceStore fPreferenceStore; 83 87 private Preferences fCorePreferenceStore; 88 89 private PreferenceListener fPreferenceListener= new PreferenceListener(); 90 91 92 102 public JavaTextTools(IPreferenceStore store) { 103 this(store, null, true); 104 } 105 106 119 public JavaTextTools(IPreferenceStore store, boolean autoDisposeOnDisplayDispose) { 120 this(store, null, autoDisposeOnDisplayDispose); 121 } 122 123 135 public JavaTextTools(IPreferenceStore store, Preferences coreStore) { 136 this(store, coreStore, true); 137 } 138 139 155 public JavaTextTools(IPreferenceStore store, Preferences coreStore, boolean autoDisposeOnDisplayDispose) { 156 fPreferenceStore= store; 157 fPreferenceStore.addPropertyChangeListener(fPreferenceListener); 158 159 fCorePreferenceStore= coreStore; 160 if (fCorePreferenceStore != null) 161 fCorePreferenceStore.addPropertyChangeListener(fPreferenceListener); 162 163 fColorManager= new JavaColorManager(autoDisposeOnDisplayDispose); 164 fCodeScanner= new JavaCodeScanner(fColorManager, store); 165 fMultilineCommentScanner= new JavaCommentScanner(fColorManager, store, coreStore, IJavaColorConstants.JAVA_MULTI_LINE_COMMENT); 166 fSinglelineCommentScanner= new JavaCommentScanner(fColorManager, store, coreStore, IJavaColorConstants.JAVA_SINGLE_LINE_COMMENT); 167 fStringScanner= new SingleTokenJavaScanner(fColorManager, store, IJavaColorConstants.JAVA_STRING); 168 fJavaDocScanner= new JavaDocScanner(fColorManager, store, coreStore); 169 } 170 171 174 public void dispose() { 175 176 fCodeScanner= null; 177 fMultilineCommentScanner= null; 178 fSinglelineCommentScanner= null; 179 fStringScanner= null; 180 fJavaDocScanner= null; 181 182 if (fColorManager != null) { 183 fColorManager.dispose(); 184 fColorManager= null; 185 } 186 187 if (fPreferenceStore != null) { 188 fPreferenceStore.removePropertyChangeListener(fPreferenceListener); 189 fPreferenceStore= null; 190 191 if (fCorePreferenceStore != null) { 192 fCorePreferenceStore.removePropertyChangeListener(fPreferenceListener); 193 fCorePreferenceStore= null; 194 } 195 196 fPreferenceListener= null; 197 } 198 } 199 200 211 public IColorManager getColorManager() { 212 return fColorManager; 213 } 214 215 221 public RuleBasedScanner getCodeScanner() { 222 return fCodeScanner; 223 } 224 225 232 public RuleBasedScanner getMultilineCommentScanner() { 233 return fMultilineCommentScanner; 234 } 235 236 243 public RuleBasedScanner getSinglelineCommentScanner() { 244 return fSinglelineCommentScanner; 245 } 246 247 254 public RuleBasedScanner getStringScanner() { 255 return fStringScanner; 256 } 257 258 267 public RuleBasedScanner getJavaDocScanner() { 268 return fJavaDocScanner; 269 } 270 271 278 public IPartitionTokenScanner getPartitionScanner() { 279 return new FastJavaPartitionScanner(); 280 } 281 282 289 public IDocumentPartitioner createDocumentPartitioner() { 290 return new FastPartitioner(getPartitionScanner(), LEGAL_CONTENT_TYPES); 291 } 292 293 303 public String [] getPartitionManagingPositionCategories() { 304 return new String [] { org.eclipse.jface.text.rules.DefaultPartitioner.CONTENT_TYPES_CATEGORY }; 305 } 306 307 316 public boolean affectsBehavior(PropertyChangeEvent event) { 317 return fCodeScanner.affectsBehavior(event) || 318 fMultilineCommentScanner.affectsBehavior(event) || 319 fSinglelineCommentScanner.affectsBehavior(event) || 320 fStringScanner.affectsBehavior(event) || 321 fJavaDocScanner.affectsBehavior(event); 322 } 323 324 332 protected void adaptToPreferenceChange(PropertyChangeEvent event) { 333 if (fCodeScanner.affectsBehavior(event)) 334 fCodeScanner.adaptToPreferenceChange(event); 335 if (fMultilineCommentScanner.affectsBehavior(event)) 336 fMultilineCommentScanner.adaptToPreferenceChange(event); 337 if (fSinglelineCommentScanner.affectsBehavior(event)) 338 fSinglelineCommentScanner.adaptToPreferenceChange(event); 339 if (fStringScanner.affectsBehavior(event)) 340 fStringScanner.adaptToPreferenceChange(event); 341 if (fJavaDocScanner.affectsBehavior(event)) 342 fJavaDocScanner.adaptToPreferenceChange(event); 343 } 344 345 351 public void setupJavaDocumentPartitioner(IDocument document) { 352 setupJavaDocumentPartitioner(document, IDocumentExtension3.DEFAULT_PARTITIONING); 353 } 354 355 362 public void setupJavaDocumentPartitioner(IDocument document, String partitioning) { 363 IDocumentPartitioner partitioner= createDocumentPartitioner(); 364 if (document instanceof IDocumentExtension3) { 365 IDocumentExtension3 extension3= (IDocumentExtension3) document; 366 extension3.setDocumentPartitioner(partitioning, partitioner); 367 } else { 368 document.setDocumentPartitioner(partitioner); 369 } 370 partitioner.connect(document); 371 } 372 373 379 protected IPreferenceStore getPreferenceStore() { 380 return fPreferenceStore; 381 } 382 383 389 protected Preferences getCorePreferenceStore() { 390 return fCorePreferenceStore; 391 } 392 } 393 | Popular Tags |