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;
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
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();
}
}
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();
}
}
--- /dev/null
+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() {
+ }
+
+}
--- /dev/null
+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;
+ }
+}