package org.simantics.scl.ui.modulebrowser; import java.util.Arrays; import java.util.Iterator; import gnu.trove.map.hash.THashMap; public class ModuleNameTreeEntry implements Comparable { public static final String STANDARD_LIBRARY = "StandardLibrary"; public static final String HTTP_PREFIX = "http://"; public final ModuleNameTreeEntry parent; public final String fullName; public final String name; public final THashMap childMap = new THashMap(); public boolean isModule; public ModuleNameTreeEntry(ModuleNameTreeEntry parent, String fullName, String name) { this.parent = parent; this.fullName = fullName; this.name = name; } public void addModule(String moduleFullName) { int startingPos = fullName.isEmpty() ? 0 : fullName.length()+1; int p; if(parent == null && moduleFullName.startsWith(HTTP_PREFIX)) p = moduleFullName.indexOf('/', HTTP_PREFIX.length()); else p = moduleFullName.indexOf('/', startingPos); if(p == -1) { if(parent == null) { getOrCreateChildMapEntry(STANDARD_LIBRARY).addModule(name); } else { String name = moduleFullName.substring(startingPos); ModuleNameTreeEntry entry = getOrCreateChildMapEntry(name); entry.isModule = true; } } else { String name = moduleFullName.substring(startingPos, p); ModuleNameTreeEntry entry = getOrCreateChildMapEntry(name); entry.addModule(moduleFullName); } } private ModuleNameTreeEntry getOrCreateChildMapEntry(String name) { ModuleNameTreeEntry entry = childMap.get(name); if(entry == null) { String newFullName; if(parent == null) { if(name.equals(STANDARD_LIBRARY)) newFullName = ""; else newFullName = name; } else newFullName = fullName + "/" + name; entry = new ModuleNameTreeEntry(this, newFullName, name); childMap.put(name, entry); } return entry; } 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 public int compareTo(ModuleNameTreeEntry o) { return name.compareTo(o.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(); } }