001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.statushandlers;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.List;
015: import java.util.Map;
016:
017: /**
018: * Helper class supporting the prefix based status handling policy.
019: *
020: * @since 3.3
021: */
022: class StatusHandlerDescriptorsMap {
023:
024: private final String ASTERISK = "*"; //$NON-NLS-1$
025:
026: private HashMap map;
027:
028: /**
029: * Creates a new instance of the class
030: */
031: public StatusHandlerDescriptorsMap() {
032: map = new HashMap();
033: }
034:
035: /**
036: * Adds a new handler descriptor to the prefix tree
037: *
038: * @param handlerDescriptor
039: * the handler descriptor to add
040: */
041: public void addHandlerDescriptor(
042: StatusHandlerDescriptor handlerDescriptor) {
043: add(this .map, handlerDescriptor.getPrefix(), handlerDescriptor);
044: }
045:
046: /*
047: * Recursively searches the tree for the best place for the handler
048: * descriptor
049: */
050: private void add(Map map, String prefix,
051: StatusHandlerDescriptor handlerDescriptor) {
052: if (prefix == null) {
053: if (map.get(ASTERISK) == null) {
054: map.put(ASTERISK, new ArrayList());
055: }
056:
057: ((List) map.get(ASTERISK)).add(handlerDescriptor);
058: } else {
059: int delimIndex = prefix.indexOf("."); //$NON-NLS-1$
060:
061: String pre = null;
062: String post = null;
063:
064: if (delimIndex != -1) {
065: pre = prefix.substring(0, delimIndex);
066:
067: if (delimIndex < prefix.length() - 1) {
068: post = prefix.substring(delimIndex + 1);
069: }
070: } else {
071: pre = prefix;
072: }
073:
074: if (map.get(pre) == null) {
075: map.put(pre, new HashMap());
076: }
077:
078: add((Map) map.get(pre), post, handlerDescriptor);
079: }
080: }
081:
082: public void clear() {
083: map.clear();
084: }
085:
086: /**
087: * Returns status handler descriptors whose prefixes are the most specific
088: * for given pluginId.
089: *
090: * @param pluginId
091: * @return handler descriptors list
092: */
093: public List getHandlerDescriptors(String pluginId) {
094: return get(pluginId, this .map);
095: }
096:
097: /*
098: * Recursively searches the prefix tree for the most specific handler
099: * descriptor for the given pluginId.
100: */
101: private List get(String pluginId, Map map) {
102: if (pluginId == null) {
103: return getAsteriskList(map);
104: }
105:
106: int delimIndex = pluginId.indexOf("."); //$NON-NLS-1$
107:
108: String pre = null;
109: String post = null;
110:
111: if (delimIndex != -1) {
112: pre = pluginId.substring(0, delimIndex);
113:
114: if (delimIndex < pluginId.length() - 1) {
115: post = pluginId.substring(delimIndex + 1);
116: }
117: } else {
118: pre = pluginId;
119: }
120:
121: if (map.get(pre) == null) {
122: return getAsteriskList(map);
123: }
124:
125: return get(post, (Map) map.get(pre));
126: }
127:
128: private List getAsteriskList(Map map) {
129: Object list = map.get(ASTERISK);
130: if (list != null) {
131: return (List) list;
132: }
133:
134: return null;
135: }
136: }
|