Add outline page for SCLModuleEditor2 57/2657/3
authorjsimomaa <jani.simomaa@gmail.com>
Tue, 19 Feb 2019 09:48:26 +0000 (11:48 +0200)
committerJani Simomaa <jani.simomaa@semantum.fi>
Tue, 19 Feb 2019 17:05:37 +0000 (17:05 +0000)
gitlab #257

Change-Id: I95b26ee8056b16103e12f32711167257b324eacf

bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/SCLModuleEditor2.java
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/outline/SCLModuleOutlinePage.java

index 1b2096eb9c63932478c8e291bc442e7dd19986ba..9aa1f0dddc628a9c05f63934e7311d7158724c76 100644 (file)
@@ -26,10 +26,12 @@ 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;
 
@@ -46,12 +48,15 @@ public class SCLModuleEditor2 extends TextEditor {
     protected ResourceManager resourceManager;
     private DefaultCharacterPairMatcher matcher;
 
+       private SCLModuleOutlinePage outline;
+
     public SCLModuleEditor2() {
         super();
         resourceManager = new LocalResourceManager(JFaceResources.getResources());
         SCLSourceViewerConfigurationNew sourceViewerConfiguration = new SCLSourceViewerConfigurationNew(resourceManager);
         setDocumentProvider(new SCLModuleEditor2DocumentProvider(sourceViewerConfiguration));
         setSourceViewerConfiguration(sourceViewerConfiguration);
+        outline = new SCLModuleOutlinePage(this);
     }
     
     @Override
@@ -136,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.
      *
index d333f235ad2bde17f68b12f6ce00f771bb31396f..80e34c3746565f70cdf625cfcde5b19e5e2326a9 100644 (file)
 package org.simantics.scl.ui.outline;
 
-import org.eclipse.jface.viewers.ILabelProvider;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.resource.LocalResourceManager;
+import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider;
+import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
 import org.eclipse.jface.viewers.ILabelProviderListener;
 import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.ISelectionChangedListener;
 import org.eclipse.jface.viewers.ITreeContentProvider;
-import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.StyledString;
+import org.eclipse.jface.viewers.TreeSelection;
 import org.eclipse.jface.viewers.TreeViewer;
-import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Image;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Tree;
-import org.eclipse.ui.part.Page;
-import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
+import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
+import org.simantics.scl.compiler.elaboration.modules.SCLValue;
+import org.simantics.scl.compiler.environment.filter.AcceptAllNamespaceFilter;
+import org.simantics.scl.compiler.errors.Failable;
+import org.simantics.scl.compiler.errors.Locations;
+import org.simantics.scl.compiler.module.Module;
+import org.simantics.scl.compiler.module.repository.UpdateListener;
+import org.simantics.scl.compiler.source.ModuleSource;
+import org.simantics.scl.compiler.types.TCon;
+import org.simantics.scl.osgi.SCLOsgi;
+import org.simantics.scl.ui.Activator;
 import org.simantics.scl.ui.editor2.SCLModuleEditor2;
+import org.simantics.scl.ui.editor2.SCLModuleEditorInput;
 
-public class SCLModuleOutlinePage extends Page implements IContentOutlinePage {
+public class SCLModuleOutlinePage extends ContentOutlinePage {
 
-    private SCLOutlineViewer outlineViewer;
+    private TreeViewer outlineViewer;
     private SCLModuleEditor2 moduleEditor;
-    
+    private UpdateListener updateListener;
+    private LocalResourceManager resourceManager;
+    private Image publicImage;
+    private Image privateImage;
+    private Image typeImage;
+
     public SCLModuleOutlinePage(SCLModuleEditor2 moduleEditor) {
         this.moduleEditor = moduleEditor;
-    }
-
-    @Override
-    public void addSelectionChangedListener(ISelectionChangedListener listener) {
-        if (outlineViewer != null)
-            outlineViewer.addSelectionChangedListener(listener);
-    }
-
-    @Override
-    public ISelection getSelection() {
-        if (outlineViewer != null)
-            outlineViewer.getSelection();
-        return StructuredSelection.EMPTY;
-    }
-
-    @Override
-    public void removeSelectionChangedListener(ISelectionChangedListener listener) {
-        if (outlineViewer != null)
-            outlineViewer.removeSelectionChangedListener(listener);
-    }
-
-    @Override
-    public void setSelection(ISelection selection) {
-        if (outlineViewer != null)
-            outlineViewer.setSelection(selection);
+        resourceManager = new LocalResourceManager(JFaceResources.getResources());
+        publicImage = resourceManager
+                .createImage(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/public_co.gif"));
+        privateImage = resourceManager
+                .createImage(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/private_co.gif"));
+        typeImage = resourceManager
+                .createImage(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/typedef_obj.gif"));
     }
 
     @Override
     public void createControl(Composite parent) {
-        Tree tree = new Tree(parent, SWT.MULTI);
-        outlineViewer = new SCLOutlineViewer(tree);
-        
+        super.createControl(parent);
+        outlineViewer = getTreeViewer();
+
         SCLModuleOutlineProvider provider = new SCLModuleOutlineProvider();
+        DelegatingStyledCellLabelProvider labelProvider = new DelegatingStyledCellLabelProvider(provider);
         outlineViewer.setContentProvider(provider);
-        outlineViewer.setLabelProvider(provider);
-        
-        outlineViewer.setInput(moduleEditor.getEditorInput());
+        outlineViewer.setLabelProvider(labelProvider);
+        outlineViewer.addSelectionChangedListener(this);
+
+        SCLModuleEditorInput input = (SCLModuleEditorInput) moduleEditor.getEditorInput();
+        ModuleSource moduleSource = input.getAdapter(ModuleSource.class);
+        updateListener = new UpdateListener() {
+
+            @Override
+            public void notifyAboutUpdate() {
+                parent.getDisplay().asyncExec(() -> {
+                    if (!outlineViewer.getControl().isDisposed()) {
+                        outlineViewer.refresh();
+                    }
+                });
+            }
+        };
+        Failable<Module> module = SCLOsgi.MODULE_REPOSITORY.getModule(moduleSource.getModuleName(), updateListener);
+        Module result = module.getResult();
+        outlineViewer.setInput(result);
     }
 
     @Override
-    public Control getControl() {
-        if (outlineViewer != null)
-            outlineViewer.getControl();
-        return null;
+    public void selectionChanged(SelectionChangedEvent event) {
+        super.selectionChanged(event);
+        ISelection selection = event.getSelection();
+        TreeSelection tselection = (TreeSelection) selection;
+        if (tselection.getFirstElement() instanceof SCLValue) {
+            SCLValue value = (SCLValue) tselection.getFirstElement();
+            long location = value.definitionLocation;
+            int begin = Locations.beginOf(location);
+            int end = Locations.endOf(location);
+            moduleEditor.selectAndReveal(begin, end - begin);
+        } else {
+//            TCon type = (TCon) tselection.getFirstElement();
+//            type.loction; // this is missing? 
+        }
     }
 
     @Override
-    public void setFocus() {
-        if (outlineViewer != null)
-            outlineViewer.getControl().setFocus();
+    public void dispose() {
+        resourceManager.dispose();
+        super.dispose();
     }
 
-    protected class SCLOutlineViewer extends TreeViewer {
-
-        public SCLOutlineViewer(Tree tree) {
-            super(tree);
-            setAutoExpandLevel(ALL_LEVELS);
-            setUseHashlookup(true);
-        }
-
-        
-    }
-    
-    protected class SCLModuleOutlineProvider implements ITreeContentProvider, ILabelProvider {
+    protected class SCLModuleOutlineProvider implements ITreeContentProvider, IStyledLabelProvider {
 
         @Override
         public Object[] getElements(Object inputElement) {
-            return null;
+            Module result = (Module) inputElement;
+            List<SCLValue> values = new ArrayList<>();
+            result.findValuesForPrefix("", AcceptAllNamespaceFilter.INSTANCE, value -> {
+                values.add(value);
+            });
+            values.sort(new Comparator<SCLValue>() {
+
+                @Override
+                public int compare(SCLValue o1, SCLValue o2) {
+                    int isPrivate = Boolean.compare(o1.isPrivate(), o2.isPrivate());
+                    if (isPrivate != 0)
+                        return isPrivate;
+                    else
+                        return o1.getName().name.compareTo(o2.getName().name);
+                }
+            });
+            List<TCon> types = new ArrayList<>();
+            result.findTypesForPrefix("", AcceptAllNamespaceFilter.INSTANCE, type -> {
+                types.add(type);
+            });
+            types.sort(new Comparator<TCon>() {
+
+                @Override
+                public int compare(TCon o1, TCon o2) {
+                    return o1.name.compareTo(o2.name);
+                }
+            });
+            List<Object> results = new ArrayList<>();
+            results.addAll(types);
+            results.addAll(values);
+            return results.toArray();
         }
 
         @Override
@@ -110,7 +158,7 @@ public class SCLModuleOutlinePage extends Page implements IContentOutlinePage {
 
         @Override
         public void addListener(ILabelProviderListener listener) {
-            
+
         }
 
         @Override
@@ -120,23 +168,41 @@ public class SCLModuleOutlinePage extends Page implements IContentOutlinePage {
 
         @Override
         public void removeListener(ILabelProviderListener listener) {
-            
+
         }
 
         @Override
         public Image getImage(Object element) {
-            return null;
+            if (element instanceof SCLValue) {
+                SCLValue value = (SCLValue) element;
+                if (value.isPrivate()) {
+                    return privateImage;
+                } else {
+                    return publicImage;
+                }
+            } else if (element instanceof TCon) {
+                return typeImage;
+            } else {
+                return null;
+            }
         }
 
-        @Override
-        public String getText(Object element) {
-            return null;
-        }
-        
         @Override
         public void dispose() {
             ITreeContentProvider.super.dispose();
         }
 
+        @Override
+        public StyledString getStyledText(Object element) {
+            if (element instanceof SCLValue) {
+                SCLValue value = (SCLValue) element;
+                StyledString ss = new StyledString(value.getName().name);
+                ss.append(" :: " + value.getType().toString(), StyledString.COUNTER_STYLER);
+                return ss;
+            } else {
+                TCon type = (TCon) element;
+                return new StyledString(type.name);
+            }
+        }
     }
 }