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.decorators;
011:
012: import org.eclipse.core.runtime.IAdaptable;
013: import org.eclipse.core.runtime.IConfigurationElement;
014: import org.eclipse.core.runtime.IExtension;
015: import org.eclipse.jface.resource.ImageDescriptor;
016: import org.eclipse.ui.internal.dialogs.DialogUtil;
017: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
018: import org.eclipse.ui.model.IWorkbenchAdapter;
019: import org.eclipse.ui.plugin.AbstractUIPlugin;
020:
021: /**
022: * Represent the description of an action within
023: * an action set. It does not create an action.
024: *
025: * [Issue: This class overlaps with ActionDescriptor
026: * and should be reviewed to determine if code
027: * reuse if possible.]
028: */
029: public class LightweightActionDescriptor implements IAdaptable,
030: IWorkbenchAdapter {
031: private static final Object[] NO_CHILDREN = new Object[0];
032:
033: private String id;
034:
035: private String label;
036:
037: private String description;
038:
039: private ImageDescriptor image;
040:
041: /**
042: * Create a new instance of <code>LightweightActionDescriptor</code>.
043: *
044: * @param actionElement the configuration element
045: */
046: public LightweightActionDescriptor(
047: IConfigurationElement actionElement) {
048: super ();
049:
050: this .id = actionElement
051: .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
052: this .label = actionElement
053: .getAttribute(IWorkbenchRegistryConstants.ATT_LABEL);
054: this .description = actionElement
055: .getAttribute(IWorkbenchRegistryConstants.TAG_DESCRIPTION);
056:
057: String iconName = actionElement
058: .getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
059: if (iconName != null) {
060: IExtension extension = actionElement
061: .getDeclaringExtension();
062: this .image = AbstractUIPlugin.imageDescriptorFromPlugin(
063: extension.getNamespace(), iconName);
064: }
065: }
066:
067: /**
068: * Returns an object which is an instance of the given class
069: * associated with this object. Returns <code>null</code> if
070: * no such object can be found.
071: */
072: public Object getAdapter(Class adapter) {
073: if (adapter == IWorkbenchAdapter.class) {
074: return this ;
075: }
076: return null;
077: }
078:
079: /**
080: * Returns the action's description.
081: *
082: * @return the description
083: */
084: public String getDescription() {
085: return description;
086: }
087:
088: /**
089: * Returns the action's id.
090: *
091: * @return the id
092: */
093: public String getId() {
094: return id;
095: }
096:
097: /**
098: * Returns the action's image descriptor.
099: *
100: * @return the image descriptor
101: */
102: public ImageDescriptor getImageDescriptor() {
103: return image;
104: }
105:
106: /* (non-Javadoc)
107: * @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(java.lang.Object)
108: */
109: public ImageDescriptor getImageDescriptor(Object o) {
110: if (o == this ) {
111: return getImageDescriptor();
112: }
113: return null;
114: }
115:
116: /**
117: * Returns the action's label.
118: *
119: * @return the label
120: */
121: public String getLabel() {
122: return label;
123: }
124:
125: /* (non-Javadoc)
126: * @see org.eclipse.ui.model.IWorkbenchAdapter#getLabel(java.lang.Object)
127: */
128: public String getLabel(Object o) {
129: if (o == this ) {
130: String text = getLabel();
131: int end = text.lastIndexOf('@');
132: if (end >= 0) {
133: text = text.substring(0, end);
134: }
135: return DialogUtil.removeAccel(text);
136: }
137: return o == null ? "" : o.toString();//$NON-NLS-1$
138: }
139:
140: /* (non-Javadoc)
141: * @see org.eclipse.ui.model.IWorkbenchAdapter#getChildren(java.lang.Object)
142: */
143: public Object[] getChildren(Object o) {
144: return NO_CHILDREN;
145: }
146:
147: /* (non-Javadoc)
148: * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
149: */
150: public Object getParent(Object o) {
151: return null;
152: }
153: }
|