1 11 package org.eclipse.ui.internal.registry; 12 13 import com.ibm.icu.text.Collator; 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.Collections ; 17 import java.util.Comparator ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.StringTokenizer ; 21 22 import org.eclipse.ui.internal.WorkbenchPlugin; 23 24 28 public abstract class CategorizedPageRegistryReader extends RegistryReader { 29 30 public static final String ATT_CATEGORY = "category"; 32 static final String PREFERENCE_SEPARATOR = "/"; 34 List topLevelNodes; 35 36 private static final Comparator comparer = new Comparator () { 37 private Collator collator = Collator.getInstance(); 38 39 public int compare(Object arg0, Object arg1) { 40 String s1 = ((CategoryNode) arg0).getFlatCategory(); 41 String s2 = ((CategoryNode) arg1).getFlatCategory(); 42 return collator.compare(s1, s2); 43 } 44 }; 45 46 50 abstract class CategoryNode { 51 54 private final CategorizedPageRegistryReader reader; 55 56 58 private String flatCategory; 59 60 63 public CategoryNode(CategorizedPageRegistryReader reader) { 64 this.reader = reader; 65 } 66 67 70 public String getFlatCategory() { 71 if (flatCategory == null) { 72 initialize(); 73 if (flatCategory == null) { 74 flatCategory = getLabelText(); 75 } 76 } 77 return flatCategory; 78 } 79 80 84 abstract String getLabelText(); 85 86 90 private void initialize() { 91 String category = reader.getCategory(getNode()); 92 if (category == null) { 93 return; 94 } 95 96 StringBuffer sb = new StringBuffer (); 97 StringTokenizer stok = new StringTokenizer (category, PREFERENCE_SEPARATOR); 98 Object immediateParent = null; 99 while (stok.hasMoreTokens()) { 100 String pathID = stok.nextToken(); 101 immediateParent = this.reader.findNode(pathID); 102 if (immediateParent == null) { 103 return; 104 } 105 if (sb.length() > 0) { 106 sb.append(PREFERENCE_SEPARATOR); 107 } 108 sb.append(getLabelText(immediateParent)); 109 } 110 111 if (sb.length() > 0) { 112 sb.append(PREFERENCE_SEPARATOR); 113 } 114 sb.append(getLabelText()); 115 flatCategory = sb.toString(); 116 } 117 118 123 abstract String getLabelText(Object element); 124 125 129 abstract Object getNode(); 130 } 131 132 135 public CategorizedPageRegistryReader() { 136 super(); 137 } 138 139 142 void processNodes() { 143 topLevelNodes = new ArrayList (); 144 146 StringTokenizer tokenizer; 148 String currentToken; 149 150 Object favorite = null; 152 String favoriteId = getFavoriteNodeId(); 153 if (favoriteId != null) { 154 favorite = findNode(favoriteId); 155 } 156 if (favorite != null) { 157 topLevelNodes.add(favorite); 158 } 159 160 Object [] sortedNodes = sortByCategories(getNodes()); 163 for (int i = 0; i < sortedNodes.length; i++) { 164 CategoryNode categoryNode = (CategoryNode) sortedNodes[i]; 166 Object node = categoryNode.getNode(); 167 if (node == favorite) { 168 continue; 170 } 171 String category = getCategory(node); 172 if (category == null) { 173 topLevelNodes.add(node); 174 continue; 175 } 176 tokenizer = new StringTokenizer (category, PREFERENCE_SEPARATOR); 178 Object parent = null; 179 while (tokenizer.hasMoreElements()) { 180 currentToken = tokenizer.nextToken(); 181 Object child = null; 182 if (parent == null) { 183 child = findNode(currentToken); 184 } else { 185 child = findNode(parent, currentToken); 186 } 187 if (child == null) { 188 parent = null; 189 break; 190 } else { 191 parent = child; 192 } 193 } 194 if (parent != null) { 195 add(parent, node); 196 } else { 197 WorkbenchPlugin 199 .log("Invalid preference page path: " + categoryNode.getFlatCategory()); topLevelNodes.add(node); 201 } 202 } 203 } 204 205 211 abstract String getCategory(Object node); 212 213 218 abstract void add(Object parent, Object node); 219 220 224 abstract Collection getNodes(); 225 226 231 abstract String getFavoriteNodeId(); 232 233 239 Object [] sortByCategories(Collection nodesToCategorize) { 240 List nodes = new ArrayList (); 242 243 Iterator nodesIterator = nodesToCategorize.iterator(); 244 while (nodesIterator.hasNext()) { 245 nodes.add(createCategoryNode(this, nodesIterator.next())); 246 } 247 248 Collections.sort(nodes, comparer); 249 return nodes.toArray(); 250 } 251 252 259 abstract CategoryNode createCategoryNode(CategorizedPageRegistryReader reader, Object object); 260 261 267 abstract Object findNode(String id); 268 269 276 abstract Object findNode(Object parent, String currentToken); 277 278 } 279 | Popular Tags |