KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > statushandlers > StatusHandlerDescriptorsMap


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.internal.statushandlers;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 /**
20  * Helper class supporting the prefix based status handling policy.
21  *
22  * @since 3.3
23  */

24 class StatusHandlerDescriptorsMap {
25
26     private final String JavaDoc ASTERISK = "*"; //$NON-NLS-1$
27

28     private HashMap JavaDoc map;
29
30     /**
31      * Creates a new instance of the class
32      */

33     public StatusHandlerDescriptorsMap() {
34         map = new HashMap JavaDoc();
35     }
36
37     /**
38      * Adds a new handler descriptor to the prefix tree
39      *
40      * @param handlerDescriptor
41      * the handler descriptor to add
42      */

43     public void addHandlerDescriptor(StatusHandlerDescriptor handlerDescriptor) {
44         add(this.map, handlerDescriptor.getPrefix(), handlerDescriptor);
45     }
46
47     /*
48      * Recursively searches the tree for the best place for the handler
49      * descriptor
50      */

51     private void add(Map JavaDoc map, String JavaDoc prefix,
52             StatusHandlerDescriptor handlerDescriptor) {
53         if (prefix == null) {
54             if (map.get(ASTERISK) == null) {
55                 map.put(ASTERISK, new ArrayList JavaDoc());
56             }
57
58             ((List JavaDoc) map.get(ASTERISK)).add(handlerDescriptor);
59         } else {
60             int delimIndex = prefix.indexOf("."); //$NON-NLS-1$
61

62             String JavaDoc pre = null;
63             String JavaDoc post = null;
64
65             if (delimIndex != -1) {
66                 pre = prefix.substring(0, delimIndex);
67
68                 if (delimIndex < prefix.length() - 1) {
69                     post = prefix.substring(delimIndex + 1);
70                 }
71             } else {
72                 pre = prefix;
73             }
74
75             if (map.get(pre) == null) {
76                 map.put(pre, new HashMap JavaDoc());
77             }
78
79             add((Map JavaDoc) map.get(pre), post, handlerDescriptor);
80         }
81     }
82
83     public void clear() {
84         map.clear();
85     }
86
87     /**
88      * Returns status handler descriptors whose prefixes are the most specific
89      * for given pluginId.
90      *
91      * @param pluginId
92      * @return handler descriptors list
93      */

94     public List JavaDoc getHandlerDescriptors(String JavaDoc pluginId) {
95         return get(pluginId, this.map);
96     }
97
98     /*
99      * Recursively searches the prefix tree for the most specific handler
100      * descriptor for the given pluginId.
101      */

102     private List JavaDoc get(String JavaDoc pluginId, Map JavaDoc map) {
103         if (pluginId == null) {
104             return getAsteriskList(map);
105         }
106
107         int delimIndex = pluginId.indexOf("."); //$NON-NLS-1$
108

109         String JavaDoc pre = null;
110         String JavaDoc post = null;
111
112         if (delimIndex != -1) {
113             pre = pluginId.substring(0, delimIndex);
114
115             if (delimIndex < pluginId.length() - 1) {
116                 post = pluginId.substring(delimIndex + 1);
117             }
118         } else {
119             pre = pluginId;
120         }
121
122         if (map.get(pre) == null) {
123             return getAsteriskList(map);
124         }
125
126         return get(post, (Map JavaDoc) map.get(pre));
127     }
128
129     private List JavaDoc getAsteriskList(Map JavaDoc map) {
130         Object JavaDoc list = map.get(ASTERISK);
131         if (list != null) {
132             return (List JavaDoc) list;
133         }
134
135         return null;
136     }
137 }
138
Popular Tags