]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/SCLModuleEditor2.java
Add outline page for SCLModuleEditor2
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / editor2 / SCLModuleEditor2.java
index 05015bb0b36dfeba8cee5afa2b44d3e11bd8d527..9aa1f0dddc628a9c05f63934e7311d7158724c76 100644 (file)
@@ -10,6 +10,7 @@ import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.link.LinkedModeModel;
 import org.eclipse.jface.text.link.LinkedPosition;
+import org.eclipse.jface.text.source.DefaultCharacterPairMatcher;
 import org.eclipse.jface.text.source.ISourceViewer;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.ST;
@@ -23,17 +24,31 @@ import org.eclipse.ui.contexts.IContextService;
 import org.eclipse.ui.editors.text.TextEditor;
 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
 import org.eclipse.ui.texteditor.IUpdate;
+import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;
 import org.eclipse.ui.texteditor.TextNavigationAction;
+import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
 import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
 import org.simantics.scl.ui.editor.completion.SCLTextEditorEnvironment;
 import org.simantics.scl.ui.editor2.iterator.DocumentCharacterIterator;
 import org.simantics.scl.ui.editor2.iterator.JavaWordIterator;
+import org.simantics.scl.ui.outline.SCLModuleOutlinePage;
 
 import com.ibm.icu.text.BreakIterator;
 
 public class SCLModuleEditor2 extends TextEditor {
+    
+    private static final char[] CHARS = new char[] { '(', ')', '{', '}', '[', ']', '<', '>' };
+    
+    private static final String MATCHING_BRACKETS = "matchingBrackets"; //$NON-NLS-1$
+    private static final String MATCHING_BRACKETS_COLOR = "matchingBracketsColor"; //$NON-NLS-1$
+    private static final String HIGHLIGHT_BRACKET_AT_CARET_LOCATION = "highlightBracketAtCaretLocation"; //$NON-NLS-1$
+    private static final String ENCLOSING_BRACKETS = "enclosingBrackets"; //$NON-NLS-1$
+    
     private boolean disposed = false;
     protected ResourceManager resourceManager;
+    private DefaultCharacterPairMatcher matcher;
+
+       private SCLModuleOutlinePage outline;
 
     public SCLModuleEditor2() {
         super();
@@ -41,6 +56,7 @@ public class SCLModuleEditor2 extends TextEditor {
         SCLSourceViewerConfigurationNew sourceViewerConfiguration = new SCLSourceViewerConfigurationNew(resourceManager);
         setDocumentProvider(new SCLModuleEditor2DocumentProvider(sourceViewerConfiguration));
         setSourceViewerConfiguration(sourceViewerConfiguration);
+        outline = new SCLModuleOutlinePage(this);
     }
     
     @Override
@@ -52,12 +68,16 @@ public class SCLModuleEditor2 extends TextEditor {
     public void init(IEditorSite site, IEditorInput input)
             throws PartInitException {
         super.init(site, input);
+        getPreferenceStore().setValue(MATCHING_BRACKETS, true);
+        getPreferenceStore().setValue(MATCHING_BRACKETS_COLOR, "192,192,192"); //$NON-NLS-1$
+        getPreferenceStore().setValue(HIGHLIGHT_BRACKET_AT_CARET_LOCATION, true);
+        getPreferenceStore().setValue(ENCLOSING_BRACKETS, true);
     }
 
     @Override
     public void createPartControl(Composite parent) {
         super.createPartControl(parent);
-        getEditorSite().getService(IContextService.class).activateContext("org.simantics.scl.ui.editor");
+        getEditorSite().getService(IContextService.class).activateContext("org.simantics.scl.ui.editor"); //$NON-NLS-1$
         updatePartName();
     }
 
@@ -92,12 +112,21 @@ public class SCLModuleEditor2 extends TextEditor {
     protected void updatePartName() {
         setPartName(getEditorInput().getName());
     }
+
+    @Override
+    protected void configureSourceViewerDecorationSupport(SourceViewerDecorationSupport support) {
+        matcher = new DefaultCharacterPairMatcher(CHARS);
+        support.setCharacterPairMatcher(matcher);
+        support.setMatchingCharacterPainterPreferenceKeys(MATCHING_BRACKETS, MATCHING_BRACKETS_COLOR, HIGHLIGHT_BRACKET_AT_CARET_LOCATION, ENCLOSING_BRACKETS);
+        super.configureSourceViewerDecorationSupport(support);
+    }
     
     @Override
     public void dispose() {
         disposed = true;
         super.dispose();
         resourceManager.dispose();
+        if (matcher != null) matcher.dispose();
     }
 
     public boolean isDisposed() {
@@ -112,7 +141,16 @@ public class SCLModuleEditor2 extends TextEditor {
     public IDocument getDocument() {
         return getSourceViewer().getDocument();
     }
-    
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public <T> T getAdapter(Class<T> adapter) {
+        if (IContentOutlinePage.class.equals(adapter)) {
+            return (T) outline;
+        }
+        return super.getAdapter(adapter);
+    }
+
     /**
      * Text navigation action to navigate to the next sub-word.
      *