001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 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.navigator.extensions;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.ui.internal.navigator.NavigatorPlugin;
017: import org.eclipse.ui.navigator.MenuInsertionPoint;
018:
019: /**
020: * <p>
021: * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
022: * part of a work in progress. There is a guarantee neither that this API will
023: * work nor that it will remain the same. Please do not use this API without
024: * consulting with the Platform/UI team.
025: * </p>
026: *
027: * @since 3.2
028: */
029: public class NavigatorViewerDescriptorManager {
030:
031: private static final NavigatorViewerDescriptorManager INSTANCE = new NavigatorViewerDescriptorManager();
032:
033: private final Map viewerDescriptors = new HashMap();
034:
035: /**
036: * @return The intialized singleton instance of the viewer descriptor
037: * registry.
038: */
039: public static NavigatorViewerDescriptorManager getInstance() {
040: return INSTANCE;
041: }
042:
043: protected NavigatorViewerDescriptorManager() {
044: new NavigatorViewerDescriptorRegistry().readRegistry();
045: }
046:
047: /**
048: *
049: * @param aViewerId
050: * The viewer id for the viewer configuration
051: * @return The viewer descriptor for the given viewer id.
052: */
053: public NavigatorViewerDescriptor getNavigatorViewerDescriptor(
054: String aViewerId) {
055:
056: NavigatorViewerDescriptor viewerDescriptor = (NavigatorViewerDescriptor) viewerDescriptors
057: .get(aViewerId);
058: if (viewerDescriptor != null) {
059: return viewerDescriptor;
060: }
061:
062: synchronized (viewerDescriptors) {
063: viewerDescriptor = (NavigatorViewerDescriptor) viewerDescriptors
064: .get(aViewerId);
065: if (viewerDescriptor == null) {
066: viewerDescriptor = new NavigatorViewerDescriptor(
067: aViewerId);
068: viewerDescriptors.put(viewerDescriptor.getViewerId(),
069: viewerDescriptor);
070: }
071: }
072: return viewerDescriptor;
073:
074: }
075:
076: private class NavigatorViewerDescriptorRegistry extends
077: RegistryReader implements IViewerExtPtConstants {
078:
079: protected NavigatorViewerDescriptorRegistry() {
080: super (NavigatorPlugin.PLUGIN_ID, TAG_VIEWER);
081: }
082:
083: protected boolean readElement(IConfigurationElement element) {
084: if (TAG_VIEWER.equals(element.getName())) {
085: String viewerId = element.getAttribute(ATT_VIEWER_ID);
086: NavigatorViewerDescriptor descriptor = getNavigatorViewerDescriptor(viewerId);
087:
088: String attPopupMenuId = element
089: .getAttribute(ATT_POPUP_MENU_ID);
090: IConfigurationElement[] tagPopupMenu = element
091: .getChildren(TAG_POPUP_MENU);
092: if (tagPopupMenu.length == 0 && attPopupMenuId != null) {
093: descriptor.setPopupMenuId(attPopupMenuId);
094: } else {
095: if (attPopupMenuId != null) {
096: NavigatorPlugin
097: .logError(
098: 0,
099: "A popupMenuId attribute and popupMenu element may NOT be concurrently specified. (see " + element.getNamespaceIdentifier() + ")", null); //$NON-NLS-1$ //$NON-NLS-2$
100: } else if (tagPopupMenu.length > 1) {
101: NavigatorPlugin
102: .logError(
103: 0,
104: "Only one \"popupMenu\" child of \"viewer\" may be specified. (see " + element.getNamespaceIdentifier() + ")", null); //$NON-NLS-1$ //$NON-NLS-2$
105: } else if (tagPopupMenu.length == 1) { // valid case
106:
107: String popupMenuId = tagPopupMenu[0]
108: .getAttribute(ATT_ID);
109: String allowsPlatformContributions = tagPopupMenu[0]
110: .getAttribute(ATT_ALLOWS_PLATFORM_CONTRIBUTIONS);
111:
112: if (popupMenuId != null) {
113: descriptor.setPopupMenuId(popupMenuId);
114: }
115:
116: if (allowsPlatformContributions != null) {
117: descriptor
118: .setAllowsPlatformContributions(Boolean
119: .valueOf(
120: allowsPlatformContributions)
121: .booleanValue());
122: }
123:
124: IConfigurationElement[] insertionPointElements = tagPopupMenu[0]
125: .getChildren(TAG_INSERTION_POINT);
126: MenuInsertionPoint[] insertionPoints = new MenuInsertionPoint[insertionPointElements.length];
127: String name;
128: String stringAttSeparator;
129:
130: boolean isSeparator;
131: for (int indx = 0; indx < insertionPointElements.length; indx++) {
132: name = insertionPointElements[indx]
133: .getAttribute(ATT_NAME);
134: stringAttSeparator = insertionPointElements[indx]
135: .getAttribute(ATT_SEPARATOR);
136: isSeparator = stringAttSeparator != null ? Boolean
137: .valueOf(stringAttSeparator)
138: .booleanValue()
139: : false;
140: insertionPoints[indx] = new MenuInsertionPoint(
141: name, isSeparator);
142: }
143: descriptor
144: .setCustomInsertionPoints(insertionPoints);
145: }
146: }
147:
148: IConfigurationElement[] options = element
149: .getChildren(TAG_OPTIONS);
150: if (options.length == 1) {
151: IConfigurationElement[] properties = options[0]
152: .getChildren(TAG_PROPERTY);
153: String name;
154: String value;
155: for (int i = 0; i < properties.length; i++) {
156: name = properties[i].getAttribute(ATT_NAME);
157: if (name != null) {
158: value = properties[i]
159: .getAttribute(ATT_VALUE);
160: descriptor.setProperty(name, value);
161: }
162: }
163: } else if (options.length > 1) {
164: NavigatorPlugin
165: .logError(
166: 0,
167: "Only one \"options\" child of \"viewer\" may be specified. (see " + element.getNamespaceIdentifier() + ")", null); //$NON-NLS-1$ //$NON-NLS-2$
168:
169: }
170: return true;
171: }
172: if (TAG_VIEWER_CONTENT_BINDING.equals(element.getName())) {
173: String viewerId = element.getAttribute(ATT_VIEWER_ID);
174: NavigatorViewerDescriptor descriptor = getNavigatorViewerDescriptor(viewerId);
175: descriptor.consumeContentBinding(element);
176: return true;
177: }
178: if (TAG_VIEWER_ACTION_BINDING.equals(element.getName())) {
179: String viewerId = element.getAttribute(ATT_VIEWER_ID);
180: NavigatorViewerDescriptor descriptor = getNavigatorViewerDescriptor(viewerId);
181: descriptor.consumeActionBinding(element);
182: return true;
183: }
184: if (TAG_DRAG_ASSISTANT.equals(element.getName())) {
185: String viewerId = element.getAttribute(ATT_VIEWER_ID);
186: NavigatorViewerDescriptor descriptor = getNavigatorViewerDescriptor(viewerId);
187: descriptor
188: .addDragAssistant(new CommonDragAssistantDescriptor(
189: element));
190: return true;
191: }
192: return false;
193: }
194: }
195:
196: }
|