KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > decorators > LightweightActionDescriptor


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.decorators;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.core.runtime.IConfigurationElement;
15 import org.eclipse.core.runtime.IExtension;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.ui.internal.dialogs.DialogUtil;
18 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
19 import org.eclipse.ui.model.IWorkbenchAdapter;
20 import org.eclipse.ui.plugin.AbstractUIPlugin;
21
22 /**
23  * Represent the description of an action within
24  * an action set. It does not create an action.
25  *
26  * [Issue: This class overlaps with ActionDescriptor
27  * and should be reviewed to determine if code
28  * reuse if possible.]
29  */

30 public class LightweightActionDescriptor implements IAdaptable,
31         IWorkbenchAdapter {
32     private static final Object JavaDoc[] NO_CHILDREN = new Object JavaDoc[0];
33
34     private String JavaDoc id;
35
36     private String JavaDoc label;
37
38     private String JavaDoc description;
39
40     private ImageDescriptor image;
41
42     /**
43      * Create a new instance of <code>LightweightActionDescriptor</code>.
44      *
45      * @param actionElement the configuration element
46      */

47     public LightweightActionDescriptor(IConfigurationElement actionElement) {
48         super();
49
50         this.id = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
51         this.label = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL);
52         this.description = actionElement
53                 .getAttribute(IWorkbenchRegistryConstants.TAG_DESCRIPTION);
54
55         String JavaDoc iconName = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
56         if (iconName != null) {
57             IExtension extension = actionElement.getDeclaringExtension();
58             this.image = AbstractUIPlugin.imageDescriptorFromPlugin(extension
59                     .getNamespace(), iconName);
60         }
61     }
62
63     /**
64      * Returns an object which is an instance of the given class
65      * associated with this object. Returns <code>null</code> if
66      * no such object can be found.
67      */

68     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
69         if (adapter == IWorkbenchAdapter.class) {
70             return this;
71         }
72         return null;
73     }
74
75     /**
76      * Returns the action's description.
77      *
78      * @return the description
79      */

80     public String JavaDoc getDescription() {
81         return description;
82     }
83
84     /**
85      * Returns the action's id.
86      *
87      * @return the id
88      */

89     public String JavaDoc getId() {
90         return id;
91     }
92
93     /**
94      * Returns the action's image descriptor.
95      *
96      * @return the image descriptor
97      */

98     public ImageDescriptor getImageDescriptor() {
99         return image;
100     }
101
102     /* (non-Javadoc)
103      * @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(java.lang.Object)
104      */

105     public ImageDescriptor getImageDescriptor(Object JavaDoc o) {
106         if (o == this) {
107             return getImageDescriptor();
108         }
109         return null;
110     }
111
112     /**
113      * Returns the action's label.
114      *
115      * @return the label
116      */

117     public String JavaDoc getLabel() {
118         return label;
119     }
120
121     /* (non-Javadoc)
122      * @see org.eclipse.ui.model.IWorkbenchAdapter#getLabel(java.lang.Object)
123      */

124     public String JavaDoc getLabel(Object JavaDoc o) {
125         if (o == this) {
126             String JavaDoc text = getLabel();
127             int end = text.lastIndexOf('@');
128             if (end >= 0) {
129                 text = text.substring(0, end);
130             }
131             return DialogUtil.removeAccel(text);
132         }
133         return o == null ? "" : o.toString();//$NON-NLS-1$
134
}
135
136     /* (non-Javadoc)
137      * @see org.eclipse.ui.model.IWorkbenchAdapter#getChildren(java.lang.Object)
138      */

139     public Object JavaDoc[] getChildren(Object JavaDoc o) {
140         return NO_CHILDREN;
141     }
142
143     /* (non-Javadoc)
144      * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
145      */

146     public Object JavaDoc getParent(Object JavaDoc o) {
147         return null;
148     }
149 }
150
Popular Tags