]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
(refs #7264) Better updating of SCL module tree 83/583/2
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 31 May 2017 21:00:28 +0000 (00:00 +0300)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Thu, 1 Jun 2017 06:18:10 +0000 (09:18 +0300)
Change-Id: I0f8c7f1314335b301bcfe84422d9e5eb4df36593

bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/ModuleNameTreeEntry.java
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/SCLModuleTree.java
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/SCLModuleTreeContentProvider.java [new file with mode: 0644]
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/SCLModuleTreeLabelProvider.java [new file with mode: 0644]

index 6f657b35134859abc3b6285c468e906047fae908..4c3bf1a2c9df1495da8bc101922c957f6fea9003 100644 (file)
@@ -1,8 +1,8 @@
 package org.simantics.scl.ui.modulebrowser;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.Map.Entry;
 
 import gnu.trove.map.hash.THashMap;
 
@@ -44,10 +44,16 @@ public class ModuleNameTreeEntry implements Comparable<ModuleNameTreeEntry> {
         return entry;
     }
     
-    public Collection<ModuleNameTreeEntry> children() {
-        ArrayList<ModuleNameTreeEntry> children = new ArrayList<ModuleNameTreeEntry>(childMap.values());
-        Collections.sort(children);
-        return children;
+    public Object[] children() {
+        Object[] result = childMap.values().toArray();
+        Arrays.sort(result);
+        return result;
+    }
+
+    public void clearModuleFlags() {
+        isModule = false;
+        for(ModuleNameTreeEntry child : childMap.values())
+            child.clearModuleFlags();
     }
 
     @Override
@@ -55,8 +61,13 @@ public class ModuleNameTreeEntry implements Comparable<ModuleNameTreeEntry> {
         return name.compareTo(o.name);
     }
     
-    @Override
-    public String toString() {
-        return name;
+    public boolean prune() {
+        Iterator<ModuleNameTreeEntry> it = childMap.values().iterator();
+        while(it.hasNext()) {
+            ModuleNameTreeEntry entry = it.next();
+            if(!entry.prune())
+                it.remove();
+        }
+        return isModule || !childMap.isEmpty();
     }
 }
index acaffd0f565103cd2301e006794f84304961a574..8355c6dd3a6a642703ebd91b952bb76838593fe6 100644 (file)
@@ -1,75 +1,28 @@
 package org.simantics.scl.ui.modulebrowser;
 
-import java.util.Collection;
-
-import org.eclipse.jface.viewers.ITreeContentProvider;
 import org.eclipse.jface.viewers.LabelProvider;
 import org.eclipse.jface.viewers.TreeViewer;
-import org.eclipse.jface.viewers.Viewer;
 import org.eclipse.swt.widgets.Composite;
 import org.simantics.scl.compiler.module.repository.ModuleRepository;
 
 public class SCLModuleTree extends TreeViewer {
-
-    private final ModuleRepository repository;
     
-    
-    private ITreeContentProvider contentProvider = new ITreeContentProvider() {
-        ModuleNameTreeEntry rootEntry;
-        
-        @Override
-        public void dispose() {
-        }
-
-        @Override
-        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
-            if(newInput != null)
-                this.rootEntry = createModuleTreeEntry((Collection<String>)newInput);
-        }
-
-        @Override
-        public Object[] getElements(Object inputElement) {
-            if(rootEntry == null)
-                return new Object[0];
-            return rootEntry.children().toArray();
-        }
-
-        @Override
-        public Object[] getChildren(Object parentElement) {
-            return ((ModuleNameTreeEntry)parentElement).children().toArray();
-        }
-
-        @Override
-        public Object getParent(Object element) {
-            return ((ModuleNameTreeEntry)element).parent;
-        }
-
-        @Override
-        public boolean hasChildren(Object element) {
-            return !((ModuleNameTreeEntry)element).childMap.isEmpty();
-        }
-    };
-    
-    private LabelProvider labelProvider = new LabelProvider();
+    private LabelProvider labelProvider = new SCLModuleTreeLabelProvider();
+    private SCLModuleTreeContentProvider contentProvider;
     
     public SCLModuleTree(Composite parent, int style, ModuleRepository repository) {
         super(parent, style);
-        this.repository = repository;
+        this.contentProvider = new SCLModuleTreeContentProvider();
+        setUseHashlookup(true);
         setLabelProvider(labelProvider);
         setContentProvider(contentProvider);
         setAutoExpandLevel(1);
-        recalculateInput();
-    }
-
-    private static ModuleNameTreeEntry createModuleTreeEntry(Collection<String> names) {
-        ModuleNameTreeEntry root = new ModuleNameTreeEntry(null, "", "");
-        for(String name : names)
-            root.addModule(name);
-        return root;
+        setInput(repository);
     }
 
     public void recalculateInput() {
-        setInput(repository.getSourceRepository().getModuleNames());
+        contentProvider.update();
+        refresh();
     }
 
 }
diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/SCLModuleTreeContentProvider.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/SCLModuleTreeContentProvider.java
new file mode 100644 (file)
index 0000000..d73639c
--- /dev/null
@@ -0,0 +1,54 @@
+package org.simantics.scl.ui.modulebrowser;
+
+import org.eclipse.jface.viewers.ITreeContentProvider;
+import org.eclipse.jface.viewers.Viewer;
+import org.simantics.scl.compiler.module.repository.ModuleRepository;
+
+public class SCLModuleTreeContentProvider implements ITreeContentProvider {
+    private ModuleRepository repository;
+    private final ModuleNameTreeEntry rootEntry = new ModuleNameTreeEntry(null, "", "");
+    
+    public void update() {
+        rootEntry.clearModuleFlags();
+        if(repository != null) {
+            for(String moduleName : repository.getSourceRepository().getModuleNames()) {
+                rootEntry.addModule(moduleName);
+            }
+        }
+        rootEntry.prune();
+    }
+
+    @Override
+    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+        this.repository = (ModuleRepository)newInput;
+        update();
+    }
+
+    @Override
+    public Object[] getElements(Object inputElement) {
+        return rootEntry.children();
+    }
+
+    @Override
+    public Object[] getChildren(Object parentElement) {
+        ModuleNameTreeEntry entry = (ModuleNameTreeEntry)parentElement;
+        return entry.children();
+    }
+
+    @Override
+    public Object getParent(Object element) {
+        ModuleNameTreeEntry entry = (ModuleNameTreeEntry)element;
+        return entry.parent;
+    }
+
+    @Override
+    public boolean hasChildren(Object element) {
+        ModuleNameTreeEntry entry = (ModuleNameTreeEntry)element;
+        return !entry.childMap.isEmpty();
+    }
+
+    @Override
+    public void dispose() {
+    }
+
+}
diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/SCLModuleTreeLabelProvider.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/SCLModuleTreeLabelProvider.java
new file mode 100644 (file)
index 0000000..057cd68
--- /dev/null
@@ -0,0 +1,11 @@
+package org.simantics.scl.ui.modulebrowser;
+
+import org.eclipse.jface.viewers.LabelProvider;
+
+public class SCLModuleTreeLabelProvider extends LabelProvider {
+    @Override
+    public String getText(Object element) {
+        ModuleNameTreeEntry entry = (ModuleNameTreeEntry)element;
+        return entry.name;
+    }
+}