]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/modulebrowser/ModuleNameTreeEntry.java
2461e5231e5535a504092acb15c0d132cb4b97c3
[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
6 import gnu.trove.map.hash.THashMap;
7
8 public class ModuleNameTreeEntry implements Comparable<ModuleNameTreeEntry> {
9     public static final String STANDARD_LIBRARY = "StandardLibrary";
10     public static final String HTTP_PREFIX = "http://";
11     
12     public final ModuleNameTreeEntry parent;
13     public final String fullName;
14     public final String name;
15     public final THashMap<String, ModuleNameTreeEntry> childMap = new THashMap<String, ModuleNameTreeEntry>();
16     public boolean isModule;
17     
18     public ModuleNameTreeEntry(ModuleNameTreeEntry parent, String fullName, String name) {
19         this.parent = parent;
20         this.fullName = fullName;
21         this.name = name;
22     }
23     
24     public void addModule(String moduleFullName) {
25         int startingPos = fullName.isEmpty() ? 0 : fullName.length()+1;
26         int p;
27         if(parent == null && moduleFullName.startsWith(HTTP_PREFIX))
28             p = moduleFullName.indexOf('/', HTTP_PREFIX.length());
29         else
30             p = moduleFullName.indexOf('/', startingPos);
31         if(p == -1) {
32             if(parent == null) {
33                 getOrCreateChildMapEntry(STANDARD_LIBRARY).addModule(name);
34             }
35             else {
36                 String name = moduleFullName.substring(startingPos);
37                 ModuleNameTreeEntry entry = getOrCreateChildMapEntry(name);
38                 entry.isModule = true;
39             }
40         }
41         else {
42             String name = moduleFullName.substring(startingPos, p);
43             ModuleNameTreeEntry entry = getOrCreateChildMapEntry(name);
44             entry.addModule(moduleFullName);
45         }
46     }
47     
48     private ModuleNameTreeEntry getOrCreateChildMapEntry(String name) {
49         ModuleNameTreeEntry entry = childMap.get(name);
50         if(entry == null) {
51             String newFullName;
52             if(parent == null) {
53                 if(name.equals(STANDARD_LIBRARY))
54                     newFullName = "";
55                 else
56                     newFullName = name;
57             }
58             else
59                 newFullName = fullName + "/" + name;
60             entry = new ModuleNameTreeEntry(this, newFullName, name);
61             childMap.put(name, entry);
62         }
63         return entry;
64     }
65     
66     public Object[] children() {
67         Object[] result = childMap.values().toArray();
68         Arrays.sort(result);
69         return result;
70     }
71
72     public void clearModuleFlags() {
73         isModule = false;
74         for(ModuleNameTreeEntry child : childMap.values())
75             child.clearModuleFlags();
76     }
77
78     @Override
79     public int compareTo(ModuleNameTreeEntry o) {
80         return name.compareTo(o.name);
81     }
82     
83     public boolean prune() {
84         Iterator<ModuleNameTreeEntry> it = childMap.values().iterator();
85         while(it.hasNext()) {
86             ModuleNameTreeEntry entry = it.next();
87             if(!entry.prune())
88                 it.remove();
89         }
90         return isModule || !childMap.isEmpty();
91     }
92 }