]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/ModuleNameTreeEntry.java
4c3bf1a2c9df1495da8bc101922c957f6fea9003
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / modulebrowser / ModuleNameTreeEntry.java
1 package org.simantics.scl.ui.modulebrowser;
2
3 import java.util.Arrays;
4 import java.util.Iterator;
5 import java.util.Map.Entry;
6
7 import gnu.trove.map.hash.THashMap;
8
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;
15     
16     public ModuleNameTreeEntry(ModuleNameTreeEntry parent, String fullName, String name) {
17         this.parent = parent;
18         this.fullName = fullName;
19         this.name = name;
20     }
21     
22     public void addModule(String moduleFullName) {
23         int startingPos = fullName.isEmpty() ? 0 : fullName.length()+1;
24         int p = moduleFullName.indexOf('/', startingPos);
25         if(p == -1) {
26             String name = moduleFullName.substring(startingPos);
27             ModuleNameTreeEntry entry = getOrCreateChildMapEntry(name);
28             entry.isModule = true;
29         }
30         else {
31             String name = moduleFullName.substring(startingPos, p);
32             ModuleNameTreeEntry entry = getOrCreateChildMapEntry(name);
33             entry.addModule(moduleFullName);
34         }
35     }
36     
37     private ModuleNameTreeEntry getOrCreateChildMapEntry(String name) {
38         ModuleNameTreeEntry entry = childMap.get(name);
39         if(entry == null) {
40             String newFullName = fullName.isEmpty() ? name : fullName + "/" + name;
41             entry = new ModuleNameTreeEntry(this, newFullName, name);
42             childMap.put(name, entry);
43         }
44         return entry;
45     }
46     
47     public Object[] children() {
48         Object[] result = childMap.values().toArray();
49         Arrays.sort(result);
50         return result;
51     }
52
53     public void clearModuleFlags() {
54         isModule = false;
55         for(ModuleNameTreeEntry child : childMap.values())
56             child.clearModuleFlags();
57     }
58
59     @Override
60     public int compareTo(ModuleNameTreeEntry o) {
61         return name.compareTo(o.name);
62     }
63     
64     public boolean prune() {
65         Iterator<ModuleNameTreeEntry> it = childMap.values().iterator();
66         while(it.hasNext()) {
67             ModuleNameTreeEntry entry = it.next();
68             if(!entry.prune())
69                 it.remove();
70         }
71         return isModule || !childMap.isEmpty();
72     }
73 }