1 11 12 package org.eclipse.jdt.internal.ui.javaeditor; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.core.runtime.Assert; 20 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.custom.BidiSegmentEvent; 23 import org.eclipse.swt.custom.BidiSegmentListener; 24 import org.eclipse.swt.custom.StyleRange; 25 import org.eclipse.swt.custom.StyledText; 26 import org.eclipse.swt.graphics.Color; 27 import org.eclipse.swt.graphics.Point; 28 import org.eclipse.swt.graphics.RGB; 29 import org.eclipse.swt.widgets.Composite; 30 import org.eclipse.swt.widgets.Display; 31 32 import org.eclipse.jface.preference.IPreferenceStore; 33 import org.eclipse.jface.preference.PreferenceConverter; 34 import org.eclipse.jface.util.IPropertyChangeListener; 35 import org.eclipse.jface.util.PropertyChangeEvent; 36 37 import org.eclipse.jface.text.BadLocationException; 38 import org.eclipse.jface.text.IDocument; 39 import org.eclipse.jface.text.IRegion; 40 import org.eclipse.jface.text.ITextPresentationListener; 41 import org.eclipse.jface.text.ITypedRegion; 42 import org.eclipse.jface.text.Region; 43 import org.eclipse.jface.text.TextUtilities; 44 import org.eclipse.jface.text.formatter.FormattingContextProperties; 45 import org.eclipse.jface.text.formatter.IFormattingContext; 46 import org.eclipse.jface.text.information.IInformationPresenter; 47 import org.eclipse.jface.text.reconciler.IReconciler; 48 import org.eclipse.jface.text.source.IOverviewRuler; 49 import org.eclipse.jface.text.source.IVerticalRuler; 50 import org.eclipse.jface.text.source.SourceViewerConfiguration; 51 import org.eclipse.jface.text.source.projection.ProjectionViewer; 52 53 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants; 54 import org.eclipse.ui.texteditor.AbstractTextEditor; 55 56 import org.eclipse.jdt.core.JavaCore; 57 58 import org.eclipse.jdt.ui.PreferenceConstants; 59 import org.eclipse.jdt.ui.text.IJavaPartitions; 60 import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration; 61 62 import org.eclipse.jdt.internal.ui.text.SmartBackspaceManager; 63 import org.eclipse.jdt.internal.ui.text.comment.CommentFormattingContext; 64 65 66 67 public class JavaSourceViewer extends ProjectionViewer implements IPropertyChangeListener { 68 69 72 public static final int SHOW_OUTLINE= 51; 73 74 77 public static final int OPEN_STRUCTURE= 52; 78 79 82 public static final int SHOW_HIERARCHY= 53; 83 84 private IInformationPresenter fOutlinePresenter; 85 private IInformationPresenter fStructurePresenter; 86 private IInformationPresenter fHierarchyPresenter; 87 88 92 private Color fForegroundColor; 93 97 private Color fBackgroundColor; 98 102 private Color fSelectionForegroundColor; 103 107 private Color fSelectionBackgroundColor; 108 113 private IPreferenceStore fPreferenceStore; 114 119 private boolean fIsConfigured; 120 125 private SmartBackspaceManager fBackspaceManager; 126 127 135 private boolean fIsSetVisibleDocumentDelayed= false; 136 137 public JavaSourceViewer(Composite parent, IVerticalRuler verticalRuler, IOverviewRuler overviewRuler, boolean showAnnotationsOverview, int styles, IPreferenceStore store) { 138 super(parent, verticalRuler, overviewRuler, showAnnotationsOverview, styles); 139 setPreferenceStore(store); 140 } 141 142 146 public IFormattingContext createFormattingContext() { 147 148 IFormattingContext context= new CommentFormattingContext(); 151 Map map= new HashMap (JavaCore.getOptions()); 152 context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, map); 153 154 return context; 155 } 156 157 160 public void doOperation(int operation) { 161 if (getTextWidget() == null) 162 return; 163 164 switch (operation) { 165 case SHOW_OUTLINE: 166 if (fOutlinePresenter != null) 167 fOutlinePresenter.showInformation(); 168 return; 169 case OPEN_STRUCTURE: 170 if (fStructurePresenter != null) 171 fStructurePresenter.showInformation(); 172 return; 173 case SHOW_HIERARCHY: 174 if (fHierarchyPresenter != null) 175 fHierarchyPresenter.showInformation(); 176 return; 177 } 178 179 super.doOperation(operation); 180 } 181 182 185 public boolean canDoOperation(int operation) { 186 if (operation == SHOW_OUTLINE) 187 return fOutlinePresenter != null; 188 if (operation == OPEN_STRUCTURE) 189 return fStructurePresenter != null; 190 if (operation == SHOW_HIERARCHY) 191 return fHierarchyPresenter != null; 192 193 return super.canDoOperation(operation); 194 } 195 196 199 public void configure(SourceViewerConfiguration configuration) { 200 201 206 StyledText textWidget= getTextWidget(); 207 if (textWidget != null && !textWidget.isDisposed()) { 208 Color foregroundColor= textWidget.getForeground(); 209 if (foregroundColor != null && foregroundColor.isDisposed()) 210 textWidget.setForeground(null); 211 Color backgroundColor= textWidget.getBackground(); 212 if (backgroundColor != null && backgroundColor.isDisposed()) 213 textWidget.setBackground(null); 214 } 215 216 super.configure(configuration); 217 if (configuration instanceof JavaSourceViewerConfiguration) { 218 JavaSourceViewerConfiguration javaSVCconfiguration= (JavaSourceViewerConfiguration)configuration; 219 fOutlinePresenter= javaSVCconfiguration.getOutlinePresenter(this, false); 220 if (fOutlinePresenter != null) 221 fOutlinePresenter.install(this); 222 223 fStructurePresenter= javaSVCconfiguration.getOutlinePresenter(this, true); 224 if (fStructurePresenter != null) 225 fStructurePresenter.install(this); 226 227 fHierarchyPresenter= javaSVCconfiguration.getHierarchyPresenter(this, true); 228 if (fHierarchyPresenter != null) 229 fHierarchyPresenter.install(this); 230 231 } 232 233 if (fPreferenceStore != null) { 234 fPreferenceStore.addPropertyChangeListener(this); 235 initializeViewerColors(); 236 } 237 238 fIsConfigured= true; 239 } 240 241 242 protected void initializeViewerColors() { 243 if (fPreferenceStore != null) { 244 245 StyledText styledText= getTextWidget(); 246 247 Color color= fPreferenceStore.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT) 249 ? null 250 : createColor(fPreferenceStore, AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND, styledText.getDisplay()); 251 styledText.setForeground(color); 252 253 if (fForegroundColor != null) 254 fForegroundColor.dispose(); 255 256 fForegroundColor= color; 257 258 color= fPreferenceStore.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT) 260 ? null 261 : createColor(fPreferenceStore, AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND, styledText.getDisplay()); 262 styledText.setBackground(color); 263 264 if (fBackgroundColor != null) 265 fBackgroundColor.dispose(); 266 267 fBackgroundColor= color; 268 269 color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR) 271 ? null 272 : createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR, styledText.getDisplay()); 273 styledText.setSelectionForeground(color); 274 275 if (fSelectionForegroundColor != null) 276 fSelectionForegroundColor.dispose(); 277 278 fSelectionForegroundColor= color; 279 280 color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR) 282 ? null 283 : createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR, styledText.getDisplay()); 284 styledText.setSelectionBackground(color); 285 286 if (fSelectionBackgroundColor != null) 287 fSelectionBackgroundColor.dispose(); 288 289 fSelectionBackgroundColor= color; 290 } 291 } 292 293 303 private Color createColor(IPreferenceStore store, String key, Display display) { 304 305 RGB rgb= null; 306 307 if (store.contains(key)) { 308 309 if (store.isDefault(key)) 310 rgb= PreferenceConverter.getDefaultColor(store, key); 311 else 312 rgb= PreferenceConverter.getColor(store, key); 313 314 if (rgb != null) 315 return new Color(display, rgb); 316 } 317 318 return null; 319 } 320 321 325 public void unconfigure() { 326 if (fOutlinePresenter != null) { 327 fOutlinePresenter.uninstall(); 328 fOutlinePresenter= null; 329 } 330 if (fStructurePresenter != null) { 331 fStructurePresenter.uninstall(); 332 fStructurePresenter= null; 333 } 334 if (fHierarchyPresenter != null) { 335 fHierarchyPresenter.uninstall(); 336 fHierarchyPresenter= null; 337 } 338 if (fForegroundColor != null) { 339 fForegroundColor.dispose(); 340 fForegroundColor= null; 341 } 342 if (fBackgroundColor != null) { 343 fBackgroundColor.dispose(); 344 fBackgroundColor= null; 345 } 346 347 if (fPreferenceStore != null) 348 fPreferenceStore.removePropertyChangeListener(this); 349 350 super.unconfigure(); 351 352 fIsConfigured= false; 353 } 354 355 358 public Point rememberSelection() { 359 return super.rememberSelection(); 360 } 361 362 365 public void restoreSelection() { 366 super.restoreSelection(); 367 } 368 369 372 public void propertyChange(PropertyChangeEvent event) { 373 String property = event.getProperty(); 374 if (AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND.equals(property) 375 || AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT.equals(property) 376 || AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND.equals(property) 377 || AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property) 378 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR.equals(property) 379 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR.equals(property) 380 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR.equals(property) 381 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR.equals(property)) 382 { 383 initializeViewerColors(); 384 } 385 } 386 387 394 public void setPreferenceStore(IPreferenceStore store) { 395 if (fIsConfigured && fPreferenceStore != null) 396 fPreferenceStore.removePropertyChangeListener(this); 397 398 fPreferenceStore= store; 399 400 if (fIsConfigured && fPreferenceStore != null) { 401 fPreferenceStore.addPropertyChangeListener(this); 402 initializeViewerColors(); 403 } 404 } 405 406 410 public void resetVisibleRegion() { 411 super.resetVisibleRegion(); 412 if (fPreferenceStore != null && fPreferenceStore.getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED) && !isProjectionMode()) 414 enableProjection(); 415 } 416 417 420 protected void createControl(Composite parent, int styles) { 421 422 if ((styles & SWT.RIGHT_TO_LEFT) == 0 && (styles & SWT.LEFT_TO_RIGHT) == 0) 424 styles |= SWT.LEFT_TO_RIGHT; 425 426 super.createControl(parent, styles); 427 428 fBackspaceManager= new SmartBackspaceManager(); 429 fBackspaceManager.install(this); 430 431 StyledText text= getTextWidget(); 432 text.addBidiSegmentListener(new BidiSegmentListener() { 433 public void lineGetSegments(BidiSegmentEvent event) { 434 if (redraws()) 435 event.segments= getBidiLineSegments(event.lineOffset, event.lineText); 436 } 437 }); 438 } 439 440 447 public SmartBackspaceManager getBackspaceManager() { 448 return fBackspaceManager; 449 } 450 451 454 protected void handleDispose() { 455 if (fBackspaceManager != null) { 456 fBackspaceManager.uninstall(); 457 fBackspaceManager= null; 458 } 459 460 super.handleDispose(); 461 } 462 463 472 public void prependTextPresentationListener(ITextPresentationListener listener) { 473 474 Assert.isNotNull(listener); 475 476 if (fTextPresentationListeners == null) 477 fTextPresentationListeners= new ArrayList (); 478 479 fTextPresentationListeners.remove(listener); 480 fTextPresentationListeners.add(0, listener); 481 } 482 483 489 void setReconciler(IReconciler reconciler) { 490 fReconciler= reconciler; 491 } 492 493 499 IReconciler getReconciler() { 500 return fReconciler; 501 } 502 503 511 protected int[] getBidiLineSegments(int widgetLineOffset, String line) { 512 if (line != null && line.length() > 0) { 513 int lineOffset= widgetOffset2ModelOffset(widgetLineOffset); 514 try { 515 return getBidiLineSegments(getDocument(), lineOffset); 516 } catch (BadLocationException x) { 517 return null; } 519 } 520 return null; 521 } 522 523 533 protected static int[] getBidiLineSegments(IDocument document, int lineOffset) throws BadLocationException { 534 535 if (document == null) 536 return null; 537 538 IRegion line= document.getLineInformationOfOffset(lineOffset); 539 ITypedRegion[] linePartitioning= TextUtilities.computePartitioning(document, IJavaPartitions.JAVA_PARTITIONING, lineOffset, line.getLength(), false); 540 541 List segmentation= new ArrayList (); 542 for (int i= 0; i < linePartitioning.length; i++) { 543 if (IJavaPartitions.JAVA_STRING.equals(linePartitioning[i].getType())) 544 segmentation.add(linePartitioning[i]); 545 } 546 547 548 if (segmentation.size() == 0) 549 return null; 550 551 int size= segmentation.size(); 552 int[] segments= new int[size * 2 + 1]; 553 554 int j= 0; 555 for (int i= 0; i < size; i++) { 556 ITypedRegion segment= (ITypedRegion) segmentation.get(i); 557 558 if (i == 0) 559 segments[j++]= 0; 560 561 int offset= segment.getOffset() - lineOffset; 562 if (offset > segments[j - 1]) 563 segments[j++]= offset; 564 565 if (offset + segment.getLength() >= line.getLength()) 566 break; 567 568 segments[j++]= offset + segment.getLength(); 569 } 570 571 if (j < segments.length) { 572 int[] result= new int[j]; 573 System.arraycopy(segments, 0, result, 0, j); 574 segments= result; 575 } 576 577 return segments; 578 } 579 580 591 void prepareDelayedProjection() { 592 Assert.isTrue(!fIsSetVisibleDocumentDelayed); 593 fIsSetVisibleDocumentDelayed= true; 594 } 595 596 605 protected void setVisibleDocument(IDocument document) { 606 if (fIsSetVisibleDocumentDelayed) { 607 fIsSetVisibleDocumentDelayed= false; 608 IDocument previous= getVisibleDocument(); 609 enableProjection(); IDocument current= getVisibleDocument(); 611 if (current != null && current != previous) 613 return; 614 } 615 616 super.setVisibleDocument(document); 617 } 618 619 627 protected StyleRange modelStyleRange2WidgetStyleRange(StyleRange range) { 628 IRegion region= modelRange2WidgetRange(new Region(range.start, range.length)); 629 if (region != null) { 630 range.start= region.getOffset(); 632 range.length= region.getLength(); 633 return range; 634 } 635 return null; 636 } 637 } 638 | Popular Tags |