KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > registry > CategorizedPageRegistryReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.registry;
12
13 import com.ibm.icu.text.Collator;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21
22 import org.eclipse.ui.internal.WorkbenchPlugin;
23
24 /**
25  * The CategorizedPageRegistryReader is the abstract super class
26  * of registry readers for page that have categorization.
27  */

28 public abstract class CategorizedPageRegistryReader extends RegistryReader {
29
30     public static final String JavaDoc ATT_CATEGORY = "category"; //$NON-NLS-1$
31

32     static final String JavaDoc PREFERENCE_SEPARATOR = "/"; //$NON-NLS-1$
33

34     List JavaDoc topLevelNodes;
35
36     private static final Comparator JavaDoc comparer = new Comparator JavaDoc() {
37         private Collator collator = Collator.getInstance();
38
39         public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
40             String JavaDoc s1 = ((CategoryNode) arg0).getFlatCategory();
41             String JavaDoc s2 = ((CategoryNode) arg1).getFlatCategory();
42             return collator.compare(s1, s2);
43         }
44     };
45
46     /**
47      * Internal class used to sort all the preference page nodes
48      * based on the category.
49      */

50     abstract class CategoryNode {
51         /**
52          * Comment for <code>reader</code>
53          */

54         private final CategorizedPageRegistryReader reader;
55
56         //private WorkbenchPreferenceNode node;
57

58         private String JavaDoc flatCategory;
59
60         /**
61          * Default constructor
62          */

63         public CategoryNode(CategorizedPageRegistryReader reader) {
64             this.reader = reader;
65         }
66
67         /**
68          * Return the flatten category
69          */

70         public String JavaDoc getFlatCategory() {
71             if (flatCategory == null) {
72                 initialize();
73                 if (flatCategory == null) {
74                     flatCategory = getLabelText();
75                 }
76             }
77             return flatCategory;
78         }
79
80         /**
81          * Get the label text for this node.
82          * @return String
83          */

84         abstract String JavaDoc getLabelText();
85
86         /*
87          * Initialize the flat category to include the parents'
88          * category names and the current node's label
89          */

90         private void initialize() {
91             String JavaDoc category = reader.getCategory(getNode());
92             if (category == null) {
93                 return;
94             }
95
96             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
97             StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(category, PREFERENCE_SEPARATOR);
98             Object JavaDoc immediateParent = null;
99             while (stok.hasMoreTokens()) {
100                 String JavaDoc 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         /**
119          * Return the label text for the passed element.
120          * @param element
121          * @return String
122          */

123         abstract String JavaDoc getLabelText(Object JavaDoc element);
124
125         /**
126          * Get the node the receiver represents.
127          * @return Object
128          */

129         abstract Object JavaDoc getNode();
130     }
131
132     /**
133      * Create a new instance of the receiver.
134      */

135     public CategorizedPageRegistryReader() {
136         super();
137     }
138
139     /**
140      * Process the preference page nodes.
141      */

142     void processNodes() {
143         topLevelNodes = new ArrayList JavaDoc();
144         //root nodes (which contain subnodes)
145

146         //Add root nodes to the contributions vector
147
StringTokenizer JavaDoc tokenizer;
148         String JavaDoc currentToken;
149
150         // Make the advisor's favorite the first category
151
Object JavaDoc favorite = null;
152         String JavaDoc favoriteId = getFavoriteNodeId();
153         if (favoriteId != null) {
154             favorite = findNode(favoriteId);
155         }
156         if (favorite != null) {
157             topLevelNodes.add(favorite);
158         }
159
160         // Sort nodes based on flattened display path composed of
161
// actual labels of nodes referenced in category attribute.
162
Object JavaDoc[] sortedNodes = sortByCategories(getNodes());
163         for (int i = 0; i < sortedNodes.length; i++) {
164             //Iterate through all the nodes
165
CategoryNode categoryNode = (CategoryNode) sortedNodes[i];
166             Object JavaDoc node = categoryNode.getNode();
167             if (node == favorite) {
168                 // skip it - favorite already at the top of the list
169
continue;
170             }
171             String JavaDoc category = getCategory(node);
172             if (category == null) {
173                 topLevelNodes.add(node);
174                 continue;
175             }
176             // has category
177
tokenizer = new StringTokenizer JavaDoc(category, PREFERENCE_SEPARATOR);
178             Object JavaDoc parent = null;
179             while (tokenizer.hasMoreElements()) {
180                 currentToken = tokenizer.nextToken();
181                 Object JavaDoc 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                 //Could not find the parent - log
198
WorkbenchPlugin
199                         .log("Invalid preference page path: " + categoryNode.getFlatCategory()); //$NON-NLS-1$
200
topLevelNodes.add(node);
201             }
202         }
203     }
204
205     /**
206      * Get the category for the node if there is one. If there
207      * isn't return <code>null</code>.
208      * @param node
209      * @return String or <code>null</code>.
210      */

211     abstract String JavaDoc getCategory(Object JavaDoc node);
212
213     /**
214      * Add the node to the parent.
215      * @param parent
216      * @param node
217      */

218     abstract void add(Object JavaDoc parent, Object JavaDoc node);
219
220     /**
221      * Get the nodes for the receiver.
222      * @return Collection of Object
223      */

224     abstract Collection JavaDoc getNodes();
225
226     /**
227      * Return the id of the favorite node or <code>null</code>
228      * if there isn't one.
229      * @return String
230      */

231     abstract String JavaDoc getFavoriteNodeId();
232
233     /**
234      * Sort the nodes based on full category + name. Category used for sorting
235      * is created by substituting node IDs with labels of the referenced
236      * nodes. workbench node is excluded from sorting because it always
237      * appears first in the dialog.
238      */

239     Object JavaDoc[] sortByCategories(Collection JavaDoc nodesToCategorize) {
240         //sort by categories
241
List JavaDoc nodes = new ArrayList JavaDoc();
242
243         Iterator JavaDoc 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     /**
253      * Create a node for categorization from the reader
254      * and the supplied object.
255      * @param reader
256      * @param object
257      * @return CategoryNode
258      */

259     abstract CategoryNode createCategoryNode(CategorizedPageRegistryReader reader, Object JavaDoc object);
260
261     /**
262      * Searches for the top-level node with the given id.
263      * @param id
264      * @return Object of the type being categorized or
265      * <code>null</code>
266      */

267     abstract Object JavaDoc findNode(String JavaDoc id);
268
269     /**
270      * Find the node with the given parent with the id
271      * of currentToken.
272      * @param parent
273      * @param currentToken
274      * @return
275      */

276     abstract Object JavaDoc findNode(Object JavaDoc parent, String JavaDoc currentToken);
277
278 }
279
Popular Tags