KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.registry;
12
13 import java.util.StringTokenizer JavaDoc;
14
15 import org.eclipse.core.commands.IHandler;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.core.runtime.IExtension;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.ui.IPageLayout;
23 import org.eclipse.ui.IPluginContribution;
24 import org.eclipse.ui.ISharedImages;
25 import org.eclipse.ui.IViewPart;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.handlers.IHandlerActivation;
28 import org.eclipse.ui.handlers.IHandlerService;
29 import org.eclipse.ui.internal.WorkbenchPlugin;
30 import org.eclipse.ui.plugin.AbstractUIPlugin;
31 import org.eclipse.ui.views.IViewDescriptor;
32
33 /**
34  * Capture the attributes of a view extension.
35  */

36 public class ViewDescriptor implements IViewDescriptor, IPluginContribution {
37     private String JavaDoc id;
38
39     private ImageDescriptor imageDescriptor;
40
41     private IConfigurationElement configElement;
42
43     private String JavaDoc[] categoryPath;
44
45     private float fastViewWidthRatio;
46
47     /**
48      * The activation token returned when activating the show view handler with
49      * the workbench.
50      */

51     private IHandlerActivation handlerActivation;
52     
53     /**
54      * Create a new <code>ViewDescriptor</code> for an extension.
55      *
56      * @param e the configuration element
57      * @throws CoreException thrown if there are errors in the configuration
58      */

59     public ViewDescriptor(IConfigurationElement e)
60             throws CoreException {
61         configElement = e;
62         loadFromExtension();
63     }
64
65     /* (non-Javadoc)
66      * @see org.eclipse.ui.internal.registry.IViewDescriptor#createView()
67      */

68     public IViewPart createView() throws CoreException {
69         Object JavaDoc extension = WorkbenchPlugin.createExtension(
70                 getConfigurationElement(),
71                 IWorkbenchRegistryConstants.ATT_CLASS);
72         return (IViewPart) extension;
73     }
74
75     /* (non-Javadoc)
76      * @see org.eclipse.ui.internal.registry.IViewDescriptor#getCategoryPath()
77      */

78     public String JavaDoc[] getCategoryPath() {
79         return categoryPath;
80     }
81
82     /**
83      * Return the configuration element for this descriptor.
84      *
85      * @return the configuration element
86      */

87     public IConfigurationElement getConfigurationElement() {
88         return configElement;
89     }
90
91     /* (non-Javadoc)
92      * @see org.eclipse.ui.internal.registry.IViewDescriptor#getDescription()
93      */

94     public String JavaDoc getDescription() {
95         return RegistryReader.getDescription(configElement);
96     }
97
98     /* (non-Javadoc)
99      * @see org.eclipse.ui.IWorkbenchPartDescriptor#getId()
100      */

101     public String JavaDoc getId() {
102         return id;
103     }
104
105     /* (non-Javadoc)
106      * @see org.eclipse.ui.IWorkbenchPartDescriptor#getImageDescriptor()
107      */

108     public ImageDescriptor getImageDescriptor() {
109         if (imageDescriptor != null) {
110             return imageDescriptor;
111         }
112         String JavaDoc iconName = configElement.getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
113         // If the icon attribute was omitted, use the default one
114
if (iconName == null) {
115             return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
116                     ISharedImages.IMG_DEF_VIEW);
117         }
118         IExtension extension = configElement.getDeclaringExtension();
119         String JavaDoc extendingPluginId = extension.getNamespace();
120         imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin(
121                 extendingPluginId, iconName);
122         // If the icon attribute was invalid, use the error icon
123
if (imageDescriptor == null) {
124             imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
125         }
126         
127         return imageDescriptor;
128     }
129
130     /* (non-Javadoc)
131      * @see org.eclipse.ui.IWorkbenchPartDescriptor#getLabel()
132      */

133     public String JavaDoc getLabel() {
134         return configElement.getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
135     }
136
137     /**
138      * Return the accelerator attribute.
139      *
140      * @return the accelerator attribute
141      */

142     public String JavaDoc getAccelerator() {
143         return configElement.getAttribute(IWorkbenchRegistryConstants.ATT_ACCELERATOR);
144     }
145
146     /* (non-Javadoc)
147      * @see org.eclipse.ui.internal.registry.IViewDescriptor#getFastViewWidthRatio()
148      */

149     public float getFastViewWidthRatio() {
150         configElement.getAttribute(IWorkbenchRegistryConstants.ATT_FAST_VIEW_WIDTH_RATIO); // check to ensure the element is still valid - exception thrown if it isn't
151
return fastViewWidthRatio;
152     }
153
154     /**
155      * load a view descriptor from the registry.
156      */

157     private void loadFromExtension() throws CoreException {
158         id = configElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
159   
160         String JavaDoc category = configElement.getAttribute(IWorkbenchRegistryConstants.TAG_CATEGORY);
161
162         // Sanity check.
163
if ((configElement.getAttribute(IWorkbenchRegistryConstants.ATT_NAME) == null)
164                 || (RegistryReader.getClassValue(configElement,
165                         IWorkbenchRegistryConstants.ATT_CLASS) == null)) {
166             throw new CoreException(new Status(IStatus.ERROR, configElement
167                     .getNamespace(), 0,
168                     "Invalid extension (missing label or class name): " + id, //$NON-NLS-1$
169
null));
170         }
171         
172         if (category != null) {
173             StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(category, "/"); //$NON-NLS-1$
174
categoryPath = new String JavaDoc[stok.countTokens()];
175             // Parse the path tokens and store them
176
for (int i = 0; stok.hasMoreTokens(); i++) {
177                 categoryPath[i] = stok.nextToken();
178             }
179         }
180         
181         String JavaDoc ratio = configElement.getAttribute(IWorkbenchRegistryConstants.ATT_FAST_VIEW_WIDTH_RATIO);
182         if (ratio != null) {
183             try {
184                 fastViewWidthRatio = new Float JavaDoc(ratio).floatValue();
185                 if (fastViewWidthRatio > IPageLayout.RATIO_MAX) {
186                     fastViewWidthRatio = IPageLayout.RATIO_MAX;
187                 }
188                 if (fastViewWidthRatio < IPageLayout.RATIO_MIN) {
189                     fastViewWidthRatio = IPageLayout.RATIO_MIN;
190                 }
191             } catch (NumberFormatException JavaDoc e) {
192                 fastViewWidthRatio = IPageLayout.DEFAULT_FASTVIEW_RATIO;
193             }
194         } else {
195             fastViewWidthRatio = IPageLayout.DEFAULT_FASTVIEW_RATIO;
196         }
197     }
198
199     /**
200      * Returns a string representation of this descriptor. For debugging
201      * purposes only.
202      */

203     public String JavaDoc toString() {
204         return "View(" + getId() + ")"; //$NON-NLS-2$//$NON-NLS-1$
205
}
206
207     /*
208      * (non-Javadoc)
209      *
210      * @see org.eclipse.ui.activities.support.IPluginContribution#getPluginId()
211      */

212     public String JavaDoc getPluginId() {
213         String JavaDoc pluginId = configElement.getNamespace();
214         return pluginId == null ? "" : pluginId; //$NON-NLS-1$
215
}
216
217     /*
218      * (non-Javadoc)
219      *
220      * @see org.eclipse.ui.activities.support.IPluginContribution#getLocalId()
221      */

222     public String JavaDoc getLocalId() {
223         return getId() == null ? "" : getId(); //$NON-NLS-1$
224
}
225
226     /* (non-Javadoc)
227      * @see org.eclipse.ui.internal.registry.IViewDescriptor#getAllowMultiple()
228      */

229     public boolean getAllowMultiple() {
230         String JavaDoc string = configElement.getAttribute(IWorkbenchRegistryConstants.ATT_ALLOW_MULTIPLE);
231         return string == null ? false : Boolean.valueOf(string).booleanValue();
232     }
233
234     /* (non-Javadoc)
235      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
236      */

237     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
238         if (adapter.equals(IConfigurationElement.class)) {
239             return getConfigurationElement();
240         }
241         return null;
242     }
243
244     /**
245      * Activates a show view handler for this descriptor. This handler can later
246      * be deactivated by calling {@link ViewDescriptor#deactivateHandler()}.
247      * This method will only activate the handler if it is not currently active.
248      *
249      * @since 3.1
250      */

251     public final void activateHandler() {
252         if (handlerActivation == null) {
253             final IHandler handler = new ShowViewHandler(getId());
254             final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
255             handlerActivation = handlerService
256                     .activateHandler(getId(), handler);
257         }
258     }
259     
260     /**
261      * Deactivates the show view handler for this descriptor. This handler was
262      * previously activated by calling {@link ViewDescriptor#activateHandler()}.
263      * This method will only deactivative the handler if it is currently active.
264      *
265      * @since 3.1
266      */

267     public final void deactivateHandler() {
268         if (handlerActivation != null) {
269             final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
270             handlerService.deactivateHandler(handlerActivation);
271             handlerActivation = null;
272         }
273     }
274 }
275
Popular Tags