001: /*******************************************************************************
002: * Copyright (c) 2000, 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.registry;
011:
012: import java.util.StringTokenizer;
013:
014: import org.eclipse.core.commands.IHandler;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.core.runtime.IExtension;
018: import org.eclipse.core.runtime.IStatus;
019: import org.eclipse.core.runtime.Status;
020: import org.eclipse.jface.resource.ImageDescriptor;
021: import org.eclipse.ui.IPageLayout;
022: import org.eclipse.ui.IPluginContribution;
023: import org.eclipse.ui.ISharedImages;
024: import org.eclipse.ui.IViewPart;
025: import org.eclipse.ui.PlatformUI;
026: import org.eclipse.ui.handlers.IHandlerActivation;
027: import org.eclipse.ui.handlers.IHandlerService;
028: import org.eclipse.ui.internal.WorkbenchPlugin;
029: import org.eclipse.ui.plugin.AbstractUIPlugin;
030: import org.eclipse.ui.views.IViewDescriptor;
031:
032: /**
033: * Capture the attributes of a view extension.
034: */
035: public class ViewDescriptor implements IViewDescriptor,
036: IPluginContribution {
037: private String id;
038:
039: private ImageDescriptor imageDescriptor;
040:
041: private IConfigurationElement configElement;
042:
043: private String[] categoryPath;
044:
045: private float fastViewWidthRatio;
046:
047: /**
048: * The activation token returned when activating the show view handler with
049: * the workbench.
050: */
051: private IHandlerActivation handlerActivation;
052:
053: /**
054: * Create a new <code>ViewDescriptor</code> for an extension.
055: *
056: * @param e the configuration element
057: * @throws CoreException thrown if there are errors in the configuration
058: */
059: public ViewDescriptor(IConfigurationElement e) throws CoreException {
060: configElement = e;
061: loadFromExtension();
062: }
063:
064: /* (non-Javadoc)
065: * @see org.eclipse.ui.internal.registry.IViewDescriptor#createView()
066: */
067: public IViewPart createView() throws CoreException {
068: Object extension = WorkbenchPlugin.createExtension(
069: getConfigurationElement(),
070: IWorkbenchRegistryConstants.ATT_CLASS);
071: return (IViewPart) extension;
072: }
073:
074: /* (non-Javadoc)
075: * @see org.eclipse.ui.internal.registry.IViewDescriptor#getCategoryPath()
076: */
077: public String[] getCategoryPath() {
078: return categoryPath;
079: }
080:
081: /**
082: * Return the configuration element for this descriptor.
083: *
084: * @return the configuration element
085: */
086: public IConfigurationElement getConfigurationElement() {
087: return configElement;
088: }
089:
090: /* (non-Javadoc)
091: * @see org.eclipse.ui.internal.registry.IViewDescriptor#getDescription()
092: */
093: public String getDescription() {
094: return RegistryReader.getDescription(configElement);
095: }
096:
097: /* (non-Javadoc)
098: * @see org.eclipse.ui.IWorkbenchPartDescriptor#getId()
099: */
100: public String getId() {
101: return id;
102: }
103:
104: /* (non-Javadoc)
105: * @see org.eclipse.ui.IWorkbenchPartDescriptor#getImageDescriptor()
106: */
107: public ImageDescriptor getImageDescriptor() {
108: if (imageDescriptor != null) {
109: return imageDescriptor;
110: }
111: String iconName = configElement
112: .getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
113: // If the icon attribute was omitted, use the default one
114: if (iconName == null) {
115: return PlatformUI.getWorkbench().getSharedImages()
116: .getImageDescriptor(ISharedImages.IMG_DEF_VIEW);
117: }
118: IExtension extension = configElement.getDeclaringExtension();
119: String 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
125: .getMissingImageDescriptor();
126: }
127:
128: return imageDescriptor;
129: }
130:
131: /* (non-Javadoc)
132: * @see org.eclipse.ui.IWorkbenchPartDescriptor#getLabel()
133: */
134: public String getLabel() {
135: return configElement
136: .getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
137: }
138:
139: /**
140: * Return the accelerator attribute.
141: *
142: * @return the accelerator attribute
143: */
144: public String getAccelerator() {
145: return configElement
146: .getAttribute(IWorkbenchRegistryConstants.ATT_ACCELERATOR);
147: }
148:
149: /* (non-Javadoc)
150: * @see org.eclipse.ui.internal.registry.IViewDescriptor#getFastViewWidthRatio()
151: */
152: public float getFastViewWidthRatio() {
153: configElement
154: .getAttribute(IWorkbenchRegistryConstants.ATT_FAST_VIEW_WIDTH_RATIO); // check to ensure the element is still valid - exception thrown if it isn't
155: return fastViewWidthRatio;
156: }
157:
158: /**
159: * load a view descriptor from the registry.
160: */
161: private void loadFromExtension() throws CoreException {
162: id = configElement
163: .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
164:
165: String category = configElement
166: .getAttribute(IWorkbenchRegistryConstants.TAG_CATEGORY);
167:
168: // Sanity check.
169: if ((configElement
170: .getAttribute(IWorkbenchRegistryConstants.ATT_NAME) == null)
171: || (RegistryReader.getClassValue(configElement,
172: IWorkbenchRegistryConstants.ATT_CLASS) == null)) {
173: throw new CoreException(
174: new Status(
175: IStatus.ERROR,
176: configElement.getNamespace(),
177: 0,
178: "Invalid extension (missing label or class name): " + id, //$NON-NLS-1$
179: null));
180: }
181:
182: if (category != null) {
183: StringTokenizer stok = new StringTokenizer(category, "/"); //$NON-NLS-1$
184: categoryPath = new String[stok.countTokens()];
185: // Parse the path tokens and store them
186: for (int i = 0; stok.hasMoreTokens(); i++) {
187: categoryPath[i] = stok.nextToken();
188: }
189: }
190:
191: String ratio = configElement
192: .getAttribute(IWorkbenchRegistryConstants.ATT_FAST_VIEW_WIDTH_RATIO);
193: if (ratio != null) {
194: try {
195: fastViewWidthRatio = new Float(ratio).floatValue();
196: if (fastViewWidthRatio > IPageLayout.RATIO_MAX) {
197: fastViewWidthRatio = IPageLayout.RATIO_MAX;
198: }
199: if (fastViewWidthRatio < IPageLayout.RATIO_MIN) {
200: fastViewWidthRatio = IPageLayout.RATIO_MIN;
201: }
202: } catch (NumberFormatException e) {
203: fastViewWidthRatio = IPageLayout.DEFAULT_FASTVIEW_RATIO;
204: }
205: } else {
206: fastViewWidthRatio = IPageLayout.DEFAULT_FASTVIEW_RATIO;
207: }
208: }
209:
210: /**
211: * Returns a string representation of this descriptor. For debugging
212: * purposes only.
213: */
214: public String toString() {
215: return "View(" + getId() + ")"; //$NON-NLS-2$//$NON-NLS-1$
216: }
217:
218: /*
219: * (non-Javadoc)
220: *
221: * @see org.eclipse.ui.activities.support.IPluginContribution#getPluginId()
222: */
223: public String getPluginId() {
224: String pluginId = configElement.getNamespace();
225: return pluginId == null ? "" : pluginId; //$NON-NLS-1$
226: }
227:
228: /*
229: * (non-Javadoc)
230: *
231: * @see org.eclipse.ui.activities.support.IPluginContribution#getLocalId()
232: */
233: public String getLocalId() {
234: return getId() == null ? "" : getId(); //$NON-NLS-1$
235: }
236:
237: /* (non-Javadoc)
238: * @see org.eclipse.ui.internal.registry.IViewDescriptor#getAllowMultiple()
239: */
240: public boolean getAllowMultiple() {
241: String string = configElement
242: .getAttribute(IWorkbenchRegistryConstants.ATT_ALLOW_MULTIPLE);
243: return string == null ? false : Boolean.valueOf(string)
244: .booleanValue();
245: }
246:
247: /* (non-Javadoc)
248: * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
249: */
250: public Object getAdapter(Class adapter) {
251: if (adapter.equals(IConfigurationElement.class)) {
252: return getConfigurationElement();
253: }
254: return null;
255: }
256:
257: /**
258: * Activates a show view handler for this descriptor. This handler can later
259: * be deactivated by calling {@link ViewDescriptor#deactivateHandler()}.
260: * This method will only activate the handler if it is not currently active.
261: *
262: * @since 3.1
263: */
264: public final void activateHandler() {
265: if (handlerActivation == null) {
266: final IHandler handler = new ShowViewHandler(getId());
267: final IHandlerService handlerService = (IHandlerService) PlatformUI
268: .getWorkbench().getService(IHandlerService.class);
269: handlerActivation = handlerService.activateHandler(getId(),
270: handler);
271: }
272: }
273:
274: /**
275: * Deactivates the show view handler for this descriptor. This handler was
276: * previously activated by calling {@link ViewDescriptor#activateHandler()}.
277: * This method will only deactivative the handler if it is currently active.
278: *
279: * @since 3.1
280: */
281: public final void deactivateHandler() {
282: if (handlerActivation != null) {
283: final IHandlerService handlerService = (IHandlerService) PlatformUI
284: .getWorkbench().getService(IHandlerService.class);
285: handlerService.deactivateHandler(handlerActivation);
286: handlerActivation = null;
287: }
288: }
289: }
|