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 org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IAdaptable;
014: import org.eclipse.core.runtime.IConfigurationElement;
015: import org.eclipse.core.runtime.IStatus;
016: import org.eclipse.core.runtime.Preferences;
017: import org.eclipse.core.runtime.Status;
018: import org.eclipse.jface.resource.ImageDescriptor;
019: import org.eclipse.ui.IPluginContribution;
020: import org.eclipse.ui.internal.PluginActionSet;
021: import org.eclipse.ui.internal.PluginActionSetReader;
022: import org.eclipse.ui.internal.WorkbenchPlugin;
023: import org.eclipse.ui.model.IWorkbenchAdapter;
024:
025: /**
026: * ActionSetDescriptor
027: */
028: public class ActionSetDescriptor implements IActionSetDescriptor,
029: IAdaptable, IWorkbenchAdapter, IPluginContribution {
030: private static final Object[] NO_CHILDREN = new Object[0];
031:
032: private static final String INITIALLY_HIDDEN_PREF_ID_PREFIX = "actionSet.initiallyHidden."; //$NON-NLS-1$
033:
034: private String id;
035:
036: private String pluginId;
037:
038: private String label;
039:
040: private boolean visible;
041:
042: private String description;
043:
044: private IConfigurationElement configElement;
045:
046: /**
047: * Create a descriptor from a configuration element.
048: *
049: * @param configElement the configuration element
050: * @throws CoreException thrown if there is an issue creating the descriptor
051: */
052: public ActionSetDescriptor(IConfigurationElement configElement)
053: throws CoreException {
054: super ();
055: this .configElement = configElement;
056: id = configElement
057: .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
058: pluginId = configElement.getNamespace();
059: label = configElement
060: .getAttribute(IWorkbenchRegistryConstants.ATT_LABEL);
061: description = configElement
062: .getAttribute(IWorkbenchRegistryConstants.TAG_DESCRIPTION);
063: String str = configElement
064: .getAttribute(IWorkbenchRegistryConstants.ATT_VISIBLE);
065: if (str != null && str.equals("true")) { //$NON-NLS-1$
066: visible = true;
067: }
068:
069: // Sanity check.
070: if (label == null) {
071: throw new CoreException(new Status(IStatus.ERROR,
072: WorkbenchPlugin.PI_WORKBENCH, 0,
073: "Invalid extension (missing label): " + id,//$NON-NLS-1$
074: null));
075: }
076: }
077:
078: /**
079: * Returns the action set for this descriptor.
080: *
081: * @return the action set
082: */
083: public IActionSet createActionSet() throws CoreException {
084: return new PluginActionSet(this );
085: }
086:
087: /**
088: * Returns an object which is an instance of the given class
089: * associated with this object. Returns <code>null</code> if
090: * no such object can be found.
091: */
092: public Object getAdapter(Class adapter) {
093: if (adapter == IWorkbenchAdapter.class) {
094: return this ;
095: }
096: return null;
097: }
098:
099: /**
100: * @see IWorkbenchAdapter#getChildren
101: */
102: public Object[] getChildren(Object o) {
103: if (o == this ) {
104: return (new PluginActionSetReader())
105: .readActionDescriptors(this );
106: }
107:
108: return NO_CHILDREN;
109: }
110:
111: /* (non-Javadoc)
112: * @see org.eclipse.ui.internal.registry.IActionSetDescriptor#getConfigurationElement()
113: */
114: public IConfigurationElement getConfigurationElement() {
115: return configElement;
116: }
117:
118: /**
119: * Returns this action set's description.
120: * This is the value of its <code>"description"</code> attribute.
121: *
122: * @return the description
123: */
124: public String getDescription() {
125: return description;
126: }
127:
128: /**
129: * Returns this action set's id.
130: * This is the value of its <code>"id"</code> attribute.
131: * <p>
132: *
133: * @return the action set id
134: */
135: public String getId() {
136: return id;
137: }
138:
139: /**
140: * Returns this action set's label.
141: * This is the value of its <code>"label"</code> attribute.
142: *
143: * @return the label
144: */
145: public String getLabel() {
146: return label;
147: }
148:
149: /**
150: * @see IWorkbenchAdapter#getLabel
151: */
152: public String getLabel(Object o) {
153: if (o == this ) {
154: return getLabel();
155: }
156: return "Unknown Label";//$NON-NLS-1$
157: }
158:
159: /**
160: * Returns whether this action set is initially visible.
161: */
162: public boolean isInitiallyVisible() {
163: if (id == null) {
164: return visible;
165: }
166: Preferences prefs = WorkbenchPlugin.getDefault()
167: .getPluginPreferences();
168: String prefId = INITIALLY_HIDDEN_PREF_ID_PREFIX + getId();
169: if (prefs.getBoolean(prefId)) {
170: return false;
171: }
172: return visible;
173: }
174:
175: /**
176: * Sets whether this action set is initially visible.
177: * If the action set identifier is undefined, then this is ignored.
178: *
179: * @since 3.0
180: */
181: public void setInitiallyVisible(boolean newValue) {
182: if (id == null) {
183: return;
184: }
185: Preferences prefs = WorkbenchPlugin.getDefault()
186: .getPluginPreferences();
187: String prefId = INITIALLY_HIDDEN_PREF_ID_PREFIX + getId();
188: prefs.setValue(prefId, !newValue);
189: }
190:
191: /* (non-Javadoc)
192: * @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(java.lang.Object)
193: */
194: public ImageDescriptor getImageDescriptor(Object object) {
195: return null;
196: }
197:
198: /* (non-Javadoc)
199: * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
200: */
201: public Object getParent(Object o) {
202: return null;
203: }
204:
205: /* (non-Javadoc)
206: * @see org.eclipse.ui.IPluginContribution#getLocalId()
207: */
208: public String getLocalId() {
209: return id;
210: }
211:
212: /* (non-Javadoc)
213: * @see org.eclipse.ui.IPluginContribution#getPluginId()
214: */
215: public String getPluginId() {
216: return pluginId;
217: }
218:
219: public boolean equals(Object arg0) {
220: if (!(arg0 instanceof ActionSetDescriptor)) {
221: return false;
222: }
223:
224: ActionSetDescriptor descr = (ActionSetDescriptor) arg0;
225:
226: return id.equals(descr.id) && descr.pluginId.equals(pluginId);
227: }
228:
229: public int hashCode() {
230: return id.hashCode() + pluginId.hashCode();
231: }
232: }
|