From 94a2993bfc6477c4beeb8f7009c210865457db38 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Thu, 1 Jun 2017 00:00:28 +0300 Subject: [PATCH] (refs #7264) Better updating of SCL module tree Change-Id: I0f8c7f1314335b301bcfe84422d9e5eb4df36593 --- .../ui/modulebrowser/ModuleNameTreeEntry.java | 31 +++++++--- .../scl/ui/modulebrowser/SCLModuleTree.java | 61 +++---------------- .../SCLModuleTreeContentProvider.java | 54 ++++++++++++++++ .../SCLModuleTreeLabelProvider.java | 11 ++++ 4 files changed, 93 insertions(+), 64 deletions(-) create mode 100644 bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/SCLModuleTreeContentProvider.java create mode 100644 bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/SCLModuleTreeLabelProvider.java diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/ModuleNameTreeEntry.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/ModuleNameTreeEntry.java index 6f657b351..4c3bf1a2c 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/ModuleNameTreeEntry.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/ModuleNameTreeEntry.java @@ -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 { return entry; } - public Collection children() { - ArrayList children = new ArrayList(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 { return name.compareTo(o.name); } - @Override - public String toString() { - return name; + public boolean prune() { + Iterator it = childMap.values().iterator(); + while(it.hasNext()) { + ModuleNameTreeEntry entry = it.next(); + if(!entry.prune()) + it.remove(); + } + return isModule || !childMap.isEmpty(); } } diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/SCLModuleTree.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/SCLModuleTree.java index acaffd0f5..8355c6dd3 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/SCLModuleTree.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/SCLModuleTree.java @@ -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)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 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 index 000000000..d73639c70 --- /dev/null +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/SCLModuleTreeContentProvider.java @@ -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 index 000000000..057cd68f1 --- /dev/null +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/SCLModuleTreeLabelProvider.java @@ -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; + } +} -- 2.47.1