KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > extensions > NavigatorViewerDescriptorManager


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 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.navigator.extensions;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
18 import org.eclipse.ui.navigator.MenuInsertionPoint;
19
20 /**
21  * <p>
22  * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
23  * part of a work in progress. There is a guarantee neither that this API will
24  * work nor that it will remain the same. Please do not use this API without
25  * consulting with the Platform/UI team.
26  * </p>
27  *
28  * @since 3.2
29  */

30 public class NavigatorViewerDescriptorManager {
31
32     private static final NavigatorViewerDescriptorManager INSTANCE = new NavigatorViewerDescriptorManager();
33
34     private final Map JavaDoc viewerDescriptors = new HashMap JavaDoc();
35
36     /**
37      * @return The intialized singleton instance of the viewer descriptor
38      * registry.
39      */

40     public static NavigatorViewerDescriptorManager getInstance() {
41         return INSTANCE;
42     }
43
44     protected NavigatorViewerDescriptorManager() {
45         new NavigatorViewerDescriptorRegistry().readRegistry();
46     }
47
48     /**
49      *
50      * @param aViewerId
51      * The viewer id for the viewer configuration
52      * @return The viewer descriptor for the given viewer id.
53      */

54     public NavigatorViewerDescriptor getNavigatorViewerDescriptor(
55             String JavaDoc aViewerId) {
56
57         NavigatorViewerDescriptor viewerDescriptor = (NavigatorViewerDescriptor) viewerDescriptors
58                 .get(aViewerId);
59         if (viewerDescriptor != null) {
60             return viewerDescriptor;
61         }
62
63         synchronized (viewerDescriptors) {
64             viewerDescriptor = (NavigatorViewerDescriptor) viewerDescriptors
65                     .get(aViewerId);
66             if (viewerDescriptor == null) {
67                 viewerDescriptor = new NavigatorViewerDescriptor(aViewerId);
68                 viewerDescriptors.put(viewerDescriptor.getViewerId(),
69                         viewerDescriptor);
70             }
71         }
72         return viewerDescriptor;
73
74     }
75
76     private class NavigatorViewerDescriptorRegistry extends RegistryReader
77             implements IViewerExtPtConstants {
78
79         protected NavigatorViewerDescriptorRegistry() {
80             super(NavigatorPlugin.PLUGIN_ID, TAG_VIEWER);
81         }
82
83         protected boolean readElement(IConfigurationElement element) {
84             if (TAG_VIEWER.equals(element.getName())) {
85                 String JavaDoc viewerId = element.getAttribute(ATT_VIEWER_ID);
86                 NavigatorViewerDescriptor descriptor = getNavigatorViewerDescriptor(viewerId);
87
88                 String JavaDoc attPopupMenuId = element.getAttribute(ATT_POPUP_MENU_ID);
89                 IConfigurationElement[] tagPopupMenu = element
90                         .getChildren(TAG_POPUP_MENU);
91                 if (tagPopupMenu.length == 0 && attPopupMenuId != null) {
92                     descriptor.setPopupMenuId(attPopupMenuId);
93                 } else {
94                     if (attPopupMenuId != null) {
95                         NavigatorPlugin
96                                 .logError(
97                                         0,
98                                         "A popupMenuId attribute and popupMenu element may NOT be concurrently specified. (see " + element.getNamespaceIdentifier() + ")", null); //$NON-NLS-1$ //$NON-NLS-2$
99
} else if (tagPopupMenu.length > 1) {
100                         NavigatorPlugin
101                                 .logError(
102                                         0,
103                                         "Only one \"popupMenu\" child of \"viewer\" may be specified. (see " + element.getNamespaceIdentifier() + ")", null); //$NON-NLS-1$ //$NON-NLS-2$
104
} else if(tagPopupMenu.length == 1) { // valid case
105

106                         String JavaDoc popupMenuId = tagPopupMenu[0]
107                                 .getAttribute(ATT_ID);
108                         String JavaDoc allowsPlatformContributions = tagPopupMenu[0]
109                                 .getAttribute(ATT_ALLOWS_PLATFORM_CONTRIBUTIONS);
110
111                         if (popupMenuId != null) {
112                             descriptor.setPopupMenuId(popupMenuId);
113                         }
114
115                         if (allowsPlatformContributions != null) {
116                             descriptor.setAllowsPlatformContributions(Boolean
117                                     .valueOf(allowsPlatformContributions)
118                                     .booleanValue());
119                         }
120
121                         IConfigurationElement[] insertionPointElements = tagPopupMenu[0]
122                                 .getChildren(TAG_INSERTION_POINT);
123                         MenuInsertionPoint[] insertionPoints = new MenuInsertionPoint[insertionPointElements.length];
124                         String JavaDoc name;
125                         String JavaDoc stringAttSeparator;
126
127                         boolean isSeparator;
128                         for (int indx = 0; indx < insertionPointElements.length; indx++) {
129                             name = insertionPointElements[indx]
130                                     .getAttribute(ATT_NAME);
131                             stringAttSeparator = insertionPointElements[indx]
132                                     .getAttribute(ATT_SEPARATOR);
133                             isSeparator = stringAttSeparator != null ? Boolean
134                                     .valueOf(stringAttSeparator).booleanValue()
135                                     : false;
136                             insertionPoints[indx] = new MenuInsertionPoint(name,
137                                     isSeparator);
138                         }
139                         descriptor.setCustomInsertionPoints(insertionPoints);
140                     }
141                 }
142
143                 IConfigurationElement[] options = element
144                         .getChildren(TAG_OPTIONS);
145                 if (options.length == 1) {
146                     IConfigurationElement[] properties = options[0]
147                             .getChildren(TAG_PROPERTY);
148                     String JavaDoc name;
149                     String JavaDoc value;
150                     for (int i = 0; i < properties.length; i++) {
151                         name = properties[i].getAttribute(ATT_NAME);
152                         if (name != null) {
153                             value = properties[i].getAttribute(ATT_VALUE);
154                             descriptor.setProperty(name, value);
155                         }
156                     }
157                 } else if (options.length > 1) {
158                     NavigatorPlugin
159                             .logError(
160                                     0,
161                                     "Only one \"options\" child of \"viewer\" may be specified. (see " + element.getNamespaceIdentifier() + ")", null); //$NON-NLS-1$ //$NON-NLS-2$
162

163                 }
164                 return true;
165             }
166             if (TAG_VIEWER_CONTENT_BINDING.equals(element.getName())) {
167                 String JavaDoc viewerId = element.getAttribute(ATT_VIEWER_ID);
168                 NavigatorViewerDescriptor descriptor = getNavigatorViewerDescriptor(viewerId);
169                 descriptor.consumeContentBinding(element);
170                 return true;
171             }
172             if (TAG_VIEWER_ACTION_BINDING.equals(element.getName())) {
173                 String JavaDoc viewerId = element.getAttribute(ATT_VIEWER_ID);
174                 NavigatorViewerDescriptor descriptor = getNavigatorViewerDescriptor(viewerId);
175                 descriptor.consumeActionBinding(element);
176                 return true;
177             } if (TAG_DRAG_ASSISTANT.equals(element.getName())) {
178                 String JavaDoc viewerId = element.getAttribute(ATT_VIEWER_ID);
179                 NavigatorViewerDescriptor descriptor = getNavigatorViewerDescriptor(viewerId);
180                 descriptor.addDragAssistant(new CommonDragAssistantDescriptor(element));
181                 return true;
182             }
183             return false;
184         }
185     }
186
187 }
188
Popular Tags