1 11 package org.eclipse.ui.editors.text; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 import java.util.StringTokenizer ; 17 import java.util.Map.Entry; 18 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.widgets.Shell; 21 22 import org.eclipse.core.runtime.Assert; 23 import org.eclipse.core.runtime.IAdaptable; 24 import org.eclipse.core.runtime.NullProgressMonitor; 25 26 import org.eclipse.jface.action.Action; 27 import org.eclipse.jface.internal.text.html.HTMLTextPresenter; 28 import org.eclipse.jface.preference.IPreferenceStore; 29 30 import org.eclipse.jface.text.DefaultInformationControl; 31 import org.eclipse.jface.text.DefaultTextHover; 32 import org.eclipse.jface.text.IInformationControl; 33 import org.eclipse.jface.text.IInformationControlCreator; 34 import org.eclipse.jface.text.ITextHover; 35 import org.eclipse.jface.text.ITextHoverExtension; 36 import org.eclipse.jface.text.ITextViewerExtension2; 37 import org.eclipse.jface.text.IUndoManager; 38 import org.eclipse.jface.text.TextViewerUndoManager; 39 import org.eclipse.jface.text.hyperlink.DefaultHyperlinkPresenter; 40 import org.eclipse.jface.text.hyperlink.IHyperlinkDetector; 41 import org.eclipse.jface.text.hyperlink.IHyperlinkPresenter; 42 import org.eclipse.jface.text.quickassist.IQuickAssistAssistant; 43 import org.eclipse.jface.text.quickassist.QuickAssistAssistant; 44 import org.eclipse.jface.text.reconciler.IReconciler; 45 import org.eclipse.jface.text.reconciler.IReconcilingStrategy; 46 import org.eclipse.jface.text.reconciler.MonoReconciler; 47 import org.eclipse.jface.text.source.Annotation; 48 import org.eclipse.jface.text.source.DefaultAnnotationHover; 49 import org.eclipse.jface.text.source.IAnnotationHover; 50 import org.eclipse.jface.text.source.ISourceViewer; 51 import org.eclipse.jface.text.source.SourceViewerConfiguration; 52 53 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants; 54 import org.eclipse.ui.texteditor.AnnotationPreference; 55 import org.eclipse.ui.texteditor.HyperlinkDetectorRegistry; 56 import org.eclipse.ui.texteditor.spelling.SpellingCorrectionProcessor; 57 import org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy; 58 import org.eclipse.ui.texteditor.spelling.SpellingService; 59 60 61 66 public class TextSourceViewerConfiguration extends SourceViewerConfiguration { 67 68 74 protected IPreferenceStore fPreferenceStore; 75 76 79 public TextSourceViewerConfiguration() { 80 } 81 82 88 public TextSourceViewerConfiguration(IPreferenceStore preferenceStore) { 89 fPreferenceStore= preferenceStore; 90 } 91 92 95 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) { 96 return new DefaultAnnotationHover() { 97 protected boolean isIncluded(Annotation annotation) { 98 return isShowInVerticalRuler(annotation); 99 } 100 }; 101 } 102 103 107 protected boolean isShowInVerticalRuler(Annotation annotation) { 108 AnnotationPreference preference= getAnnotationPreference(annotation); 109 if (preference == null) 110 return true; 111 String key= preference.getVerticalRulerPreferenceKey(); 112 if (key != null && !fPreferenceStore.getBoolean(key)) 114 return false; 115 116 return true; 117 } 118 119 123 public IAnnotationHover getOverviewRulerAnnotationHover(ISourceViewer sourceViewer) { 124 return new DefaultAnnotationHover() { 125 protected boolean isIncluded(Annotation annotation) { 126 return isShowInOverviewRuler(annotation); 127 } 128 }; 129 } 130 131 135 protected boolean isShowInOverviewRuler(Annotation annotation) { 136 AnnotationPreference preference= getAnnotationPreference(annotation); 137 if (preference == null) 138 return true; 139 String key= preference.getOverviewRulerPreferenceKey(); 140 if (key == null || !fPreferenceStore.getBoolean(key)) 141 return false; 142 143 return true; 144 } 145 146 150 public int[] getConfiguredTextHoverStateMasks(ISourceViewer sourceViewer, String contentType) { 151 return new int[] { ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK }; 152 } 153 154 158 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) { 159 return new TextHover(sourceViewer); 160 } 161 162 166 protected boolean isShownInText(Annotation annotation) { 167 AnnotationPreference preference= getAnnotationPreference(annotation); 168 if (preference == null) 169 return false; 170 String key= preference.getTextPreferenceKey(); 171 if (key != null) { 172 if (!fPreferenceStore.getBoolean(key)) 173 return false; 174 } else { 175 key= preference.getHighlightPreferenceKey(); 176 if (key == null || !fPreferenceStore.getBoolean(key)) 177 return false; 178 } 179 return true; 180 } 181 182 189 private AnnotationPreference getAnnotationPreference(Annotation annotation) { 190 if (annotation == null || fPreferenceStore == null) 191 return null; 192 return EditorsUI.getAnnotationPreferenceLookup().getAnnotationPreference(annotation); 193 } 194 195 198 public int getTabWidth(ISourceViewer sourceViewer) { 199 if (fPreferenceStore == null) 200 return super.getTabWidth(sourceViewer); 201 return fPreferenceStore.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH); 202 } 203 204 208 public String [] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) { 209 String [] indentPrefixes= getIndentPrefixesForTab(getTabWidth(sourceViewer)); 210 if (indentPrefixes == null) 211 return null; 212 213 int length= indentPrefixes.length; 214 if (length > 2 && fPreferenceStore != null && fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SPACES_FOR_TABS)) { 215 String first= indentPrefixes[0]; 217 indentPrefixes[0]= indentPrefixes[length - 2]; 218 indentPrefixes[length - 2]= first; 219 } 220 221 return indentPrefixes; 222 } 223 224 228 public IHyperlinkDetector[] getHyperlinkDetectors(ISourceViewer sourceViewer) { 229 if (sourceViewer == null || fPreferenceStore == null) 230 return super.getHyperlinkDetectors(sourceViewer); 231 232 if (!fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINKS_ENABLED)) 233 return null; 234 235 return getRegisteredHyperlinkDetectors(sourceViewer); 236 } 237 238 246 protected final IHyperlinkDetector[] getRegisteredHyperlinkDetectors(ISourceViewer sourceViewer) { 247 HyperlinkDetectorRegistry registry= EditorsUI.getHyperlinkDetectorRegistry(); 248 249 Map targets= getHyperlinkDetectorTargets(sourceViewer); 250 Assert.isNotNull(targets); 251 252 IHyperlinkDetector[] result= null; 253 Iterator iter= targets.entrySet().iterator(); 254 while (iter.hasNext()) { 255 Entry target= (Entry)iter.next(); 256 String targetId= (String )target.getKey(); 257 IAdaptable context= (IAdaptable)target.getValue(); 258 result= merge(result, registry.createHyperlinkDetectors(targetId, context)); 259 } 260 return result; 261 } 262 263 275 protected Map getHyperlinkDetectorTargets(ISourceViewer sourceViewer) { 276 Map targets= new HashMap (); 277 targets.put("org.eclipse.ui.DefaultTextEditor", null); return targets; 279 } 280 281 285 public int getHyperlinkStateMask(ISourceViewer sourceViewer) { 286 if (fPreferenceStore == null) 287 return super.getHyperlinkStateMask(sourceViewer); 288 289 String modifiers= fPreferenceStore.getString(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINK_KEY_MODIFIER); 290 int modifierMask= computeStateMask(modifiers); 291 if (modifierMask == -1) { 292 modifierMask= fPreferenceStore.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINK_KEY_MODIFIER_MASK); 294 } 295 return modifierMask; 296 } 297 298 302 public IHyperlinkPresenter getHyperlinkPresenter(ISourceViewer sourceViewer) { 303 if (fPreferenceStore == null) 304 return super.getHyperlinkPresenter(sourceViewer); 305 306 return new DefaultHyperlinkPresenter(fPreferenceStore); 307 } 308 309 317 protected static final int findLocalizedModifier(String modifierName) { 318 if (modifierName == null) 319 return 0; 320 321 if (modifierName.equalsIgnoreCase(Action.findModifierString(SWT.CTRL))) 322 return SWT.CTRL; 323 if (modifierName.equalsIgnoreCase(Action.findModifierString(SWT.SHIFT))) 324 return SWT.SHIFT; 325 if (modifierName.equalsIgnoreCase(Action.findModifierString(SWT.ALT))) 326 return SWT.ALT; 327 if (modifierName.equalsIgnoreCase(Action.findModifierString(SWT.COMMAND))) 328 return SWT.COMMAND; 329 330 return 0; 331 } 332 333 340 protected static final int computeStateMask(String modifiers) { 341 if (modifiers == null) 342 return -1; 343 344 if (modifiers.length() == 0) 345 return SWT.NONE; 346 347 int stateMask= 0; 348 StringTokenizer modifierTokenizer= new StringTokenizer (modifiers, ",;.:+-* "); while (modifierTokenizer.hasMoreTokens()) { 350 int modifier= findLocalizedModifier(modifierTokenizer.nextToken()); 351 if (modifier == 0 || (stateMask & modifier) == modifier) 352 return -1; 353 stateMask= stateMask | modifier; 354 } 355 return stateMask; 356 } 357 358 362 public IUndoManager getUndoManager(ISourceViewer sourceViewer) { 363 if (fPreferenceStore == null || !fPreferenceStore.contains(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_UNDO_HISTORY_SIZE)) 364 return super.getUndoManager(sourceViewer); 365 366 int undoHistorySize= fPreferenceStore.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_UNDO_HISTORY_SIZE); 367 return new TextViewerUndoManager(undoHistorySize); 368 } 369 370 380 public IReconciler getReconciler(ISourceViewer sourceViewer) { 381 if (fPreferenceStore == null || !fPreferenceStore.getBoolean(SpellingService.PREFERENCE_SPELLING_ENABLED)) 382 return null; 383 384 SpellingService spellingService= EditorsUI.getSpellingService(); 385 if (spellingService.getActiveSpellingEngineDescriptor(fPreferenceStore) == null) 386 return null; 387 388 IReconcilingStrategy strategy= new SpellingReconcileStrategy(sourceViewer, spellingService); 389 MonoReconciler reconciler= new MonoReconciler(strategy, false); 390 reconciler.setIsIncrementalReconciler(false); 391 reconciler.setProgressMonitor(new NullProgressMonitor()); 392 reconciler.setDelay(500); 393 return reconciler; 394 } 395 396 400 public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer sourceViewer) { 401 if (fPreferenceStore == null || !fPreferenceStore.getBoolean(SpellingService.PREFERENCE_SPELLING_ENABLED)) 402 return null; 403 404 IQuickAssistAssistant assistant= new QuickAssistAssistant(); 405 assistant.setQuickAssistProcessor(new SpellingCorrectionProcessor()); 406 assistant.setInformationControlCreator(getQuickAssistAssistantInformationControlCreator()); 407 408 assistant.setProposalSelectorBackground(sourceViewer.getTextWidget().getDisplay().getSystemColor(SWT.COLOR_WHITE)); 410 assistant.setProposalSelectorForeground(sourceViewer.getTextWidget().getDisplay().getSystemColor(SWT.COLOR_BLACK)); 411 412 return assistant; 413 } 414 415 421 private IInformationControlCreator getQuickAssistAssistantInformationControlCreator() { 422 return new IInformationControlCreator() { 423 public IInformationControl createInformationControl(Shell parent) { 424 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true)); 425 } 426 }; 427 } 428 429 437 private IHyperlinkDetector[] merge(IHyperlinkDetector[] array1, IHyperlinkDetector[] array2) { 438 if (array1 == null && array2 == null) 439 return null; 440 else if (array1 == null) 441 return array2; 442 else if (array2 == null) 443 return array1; 444 else { 445 IHyperlinkDetector[] allHyperlinkDetectors; 446 int size= array1.length + array2.length; 447 allHyperlinkDetectors= new IHyperlinkDetector[size]; 448 System.arraycopy(array1, 0, allHyperlinkDetectors, 0, array1.length); 449 System.arraycopy(array2, 0, allHyperlinkDetectors, array1.length, array2.length); 450 return allHyperlinkDetectors; 451 } 452 } 453 454 460 private final class TextHover extends DefaultTextHover implements ITextHoverExtension { 461 public TextHover(ISourceViewer sourceViewer) { 462 super(sourceViewer); 463 } 464 465 protected boolean isIncluded(Annotation annotation) { 466 return isShownInText(annotation); 467 } 468 469 472 public IInformationControlCreator getHoverControlCreator() { 473 return new IInformationControlCreator() { 474 public IInformationControl createInformationControl(Shell parent) { 475 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true), EditorsUI.getTooltipAffordanceString()); 476 } 477 }; 478 } 479 } 480 481 } 482 | Popular Tags |