1 package org.simantics.scl.ui.modulebrowser;
3 import java.util.Arrays;
4 import java.util.Iterator;
5 import java.util.Map.Entry;
7 import gnu.trove.map.hash.THashMap;
9 public class ModuleNameTreeEntry implements Comparable<ModuleNameTreeEntry> {
10 public final ModuleNameTreeEntry parent;
11 public final String fullName;
12 public final String name;
13 public final THashMap<String, ModuleNameTreeEntry> childMap = new THashMap<String, ModuleNameTreeEntry>();
14 public boolean isModule;
16 public ModuleNameTreeEntry(ModuleNameTreeEntry parent, String fullName, String name) {
18 this.fullName = fullName;
22 public void addModule(String moduleFullName) {
23 int startingPos = fullName.isEmpty() ? 0 : fullName.length()+1;
24 int p = moduleFullName.indexOf('/', startingPos);
26 String name = moduleFullName.substring(startingPos);
27 ModuleNameTreeEntry entry = getOrCreateChildMapEntry(name);
28 entry.isModule = true;
31 String name = moduleFullName.substring(startingPos, p);
32 ModuleNameTreeEntry entry = getOrCreateChildMapEntry(name);
33 entry.addModule(moduleFullName);
37 private ModuleNameTreeEntry getOrCreateChildMapEntry(String name) {
38 ModuleNameTreeEntry entry = childMap.get(name);
40 String newFullName = fullName.isEmpty() ? name : fullName + "/" + name;
41 entry = new ModuleNameTreeEntry(this, newFullName, name);
42 childMap.put(name, entry);
47 public Object[] children() {
48 Object[] result = childMap.values().toArray();
53 public void clearModuleFlags() {
55 for(ModuleNameTreeEntry child : childMap.values())
56 child.clearModuleFlags();
60 public int compareTo(ModuleNameTreeEntry o) {
61 return name.compareTo(o.name);
64 public boolean prune() {
65 Iterator<ModuleNameTreeEntry> it = childMap.values().iterator();
67 ModuleNameTreeEntry entry = it.next();
71 return isModule || !childMap.isEmpty();