001: /*******************************************************************************
002: * Copyright (c) 2001, 2007 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.views.properties.tabbed.view;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.core.runtime.IStatus;
018: import org.eclipse.core.runtime.Status;
019: import org.eclipse.jface.viewers.IFilter;
020: import org.eclipse.ui.internal.views.properties.tabbed.TabbedPropertyViewPlugin;
021: import org.eclipse.ui.internal.views.properties.tabbed.TabbedPropertyViewStatusCodes;
022: import org.eclipse.ui.internal.views.properties.tabbed.l10n.TabbedPropertyMessages;
023: import org.eclipse.ui.views.properties.tabbed.AbstractSectionDescriptor;
024: import org.eclipse.ui.views.properties.tabbed.ISection;
025: import org.eclipse.ui.views.properties.tabbed.ITypeMapper;
026:
027: import com.ibm.icu.text.MessageFormat;
028:
029: /**
030: * Represents the default implementation of a section descriptor on the tabbed
031: * property sections extensions. This implementation assumes that we are
032: * interested in selected objects in an IStructuredSelection.
033: *
034: * @author Anthony Hunter
035: */
036: public class SectionDescriptor extends AbstractSectionDescriptor {
037:
038: private static final String ATT_ID = "id"; //$NON-NLS-1$
039:
040: private static final String ATT_TARGET_TAB = "tab"; //$NON-NLS-1$
041:
042: private static final String ATT_AFTER_SECTION = "afterSection"; //$NON-NLS-1$
043:
044: private static final String ATT_CLASS = "class"; //$NON-NLS-1$
045:
046: private static final String ATT_SECTION_FILTER = "filter"; //$NON-NLS-1$
047:
048: private static final String ATT_SECTION_ENABLES_FOR = "enablesFor"; //$NON-NLS-1$
049:
050: private static final String ATT_INPUT_TYPE = "type"; //$NON-NLS-1$
051:
052: private static final String ELEMENT_INPUT = "input"; //$NON-NLS-1$
053:
054: private String id;
055:
056: private String targetTab;
057:
058: private String afterSection;
059:
060: private ArrayList inputTypes;
061:
062: private IFilter filter;
063:
064: private int enablesFor = ENABLES_FOR_ANY;
065:
066: private IConfigurationElement configurationElement;
067:
068: /**
069: * Constructor for the section descriptor.
070: *
071: * @param configurationElement
072: * the configuration element for the section descriptor.
073: * @param typeMapper
074: * The type mapper.
075: */
076: protected SectionDescriptor(
077: IConfigurationElement configurationElement,
078: ITypeMapper typeMapper) {
079: super (typeMapper);
080: this .configurationElement = configurationElement;
081:
082: id = getConfigurationElement().getAttribute(ATT_ID);
083: targetTab = getConfigurationElement().getAttribute(
084: ATT_TARGET_TAB);
085: afterSection = getConfigurationElement().getAttribute(
086: ATT_AFTER_SECTION);
087: if (getConfigurationElement().getAttribute(
088: ATT_SECTION_ENABLES_FOR) != null) {
089: String enablesForStr = getConfigurationElement()
090: .getAttribute(ATT_SECTION_ENABLES_FOR);
091: int enablesForTest = Integer.parseInt(enablesForStr);
092: if (enablesForTest > 0) {
093: enablesFor = enablesForTest;
094: }
095: }
096:
097: if (id == null || targetTab == null) {
098: // the section id and tab are mandatory - log error
099: handleSectionError(null);
100: }
101: }
102:
103: /**
104: * Handle the section error when an issue is found loading from the
105: * configuration element.
106: *
107: * @param configurationElement
108: * the configuration element
109: * @param exception
110: * an optional CoreException
111: */
112: private void handleSectionError(CoreException exception) {
113: String pluginId = getConfigurationElement()
114: .getDeclaringExtension().getNamespaceIdentifier();
115: String message = TabbedPropertyMessages.SectionDescriptor_Section_error;
116: if (exception == null) {
117: message = MessageFormat
118: .format(
119: TabbedPropertyMessages.SectionDescriptor_Section_error,
120: new Object[] { pluginId });
121: } else {
122: message = MessageFormat
123: .format(
124: TabbedPropertyMessages.SectionDescriptor_class_not_found_error,
125: new Object[] { pluginId });
126: }
127: IStatus status = new Status(IStatus.ERROR, pluginId,
128: TabbedPropertyViewStatusCodes.SECTION_ERROR, message,
129: exception);
130: TabbedPropertyViewPlugin.getPlugin().getLog().log(status);
131: }
132:
133: /**
134: * @see org.eclipse.ui.views.properties.tabbed.ISectionDescriptor#getId()
135: */
136: public String getId() {
137: return id;
138: }
139:
140: /**
141: * @see org.eclipse.ui.views.properties.tabbed.ISectionDescriptor#getFilter()
142: */
143: public IFilter getFilter() {
144: if (filter == null) {
145: try {
146: if (getConfigurationElement().getAttribute(
147: ATT_SECTION_FILTER) != null) {
148: filter = (IFilter) configurationElement
149: .createExecutableExtension(ATT_SECTION_FILTER);
150: }
151: } catch (CoreException exception) {
152: handleSectionError(exception);
153: }
154: }
155: return filter;
156: }
157:
158: /**
159: * Retrieves the value for section enablement which is a precise number of
160: * items selected. For example: enablesFor=" 4" enables the action only when
161: * 4 items are selected. If not specified, enable for all selections.
162: *
163: * @return the value for section enablement.
164: */
165: public int getEnablesFor() {
166: return enablesFor;
167: }
168:
169: /**
170: * @see org.eclipse.ui.views.properties.tabbed.ISectionDescriptor#getTargetTab()
171: */
172: public String getTargetTab() {
173: return targetTab;
174: }
175:
176: /**
177: * @see org.eclipse.ui.views.properties.tabbed.ISectionDescriptor#getAfterSection()
178: */
179: public String getAfterSection() {
180: if (afterSection == null) {
181: return super .getAfterSection();
182: }
183: return afterSection;
184: }
185:
186: /**
187: * Creates an instance of a section described by this descriptor
188: *
189: * @see org.eclipse.ui.views.properties.tabbed.ISectionDescriptor#getSectionClass()
190: */
191: public ISection getSectionClass() {
192: ISection section = null;
193: try {
194: section = (ISection) getConfigurationElement()
195: .createExecutableExtension(ATT_CLASS);
196: } catch (CoreException exception) {
197: handleSectionError(exception);
198: }
199:
200: return section;
201: }
202:
203: /**
204: * Gets the input types that are valid for this section.
205: *
206: * @see org.eclipse.ui.views.properties.tabbed.ISectionDescriptor#getInputTypes()
207: */
208: public List getInputTypes() {
209: if (inputTypes == null) {
210: inputTypes = new ArrayList();
211: IConfigurationElement[] elements = getConfigurationElement()
212: .getChildren(ELEMENT_INPUT);
213: for (int i = 0; i < elements.length; i++) {
214: IConfigurationElement element = elements[i];
215: inputTypes.add(element.getAttribute(ATT_INPUT_TYPE));
216: }
217: }
218:
219: return inputTypes;
220: }
221:
222: /**
223: * @see java.lang.Object#toString()
224: */
225: public String toString() {
226: return getId();
227: }
228:
229: /**
230: * @return Returns the configurationElement.
231: */
232: private IConfigurationElement getConfigurationElement() {
233: return configurationElement;
234: }
235: }
|