KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > views > properties > tabbed > view > SectionDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2001, 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.views.properties.tabbed.view;
12
13 import com.ibm.icu.text.MessageFormat;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jface.viewers.IFilter;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.ui.IWorkbenchPart;
24 import org.eclipse.ui.internal.views.properties.tabbed.TabbedPropertyViewPlugin;
25 import org.eclipse.ui.internal.views.properties.tabbed.TabbedPropertyViewStatusCodes;
26 import org.eclipse.ui.internal.views.properties.tabbed.l10n.TabbedPropertyMessages;
27 import org.eclipse.ui.views.properties.tabbed.ISection;
28 import org.eclipse.ui.views.properties.tabbed.ISectionDescriptor;
29 import org.eclipse.ui.views.properties.tabbed.ITypeMapper;
30
31 /**
32  * Represents the default implementation of a section descriptor on the tabbed
33  * property sections extensions. This implementation assumes that we are
34  * interested in selected objects in an IStructuredSelection.
35  *
36  * @author Anthony Hunter
37  */

38 public class SectionDescriptor
39     implements ISectionDescriptor {
40
41     private final static String JavaDoc SECTION_ERROR = TabbedPropertyMessages.SectionDescriptor_Section_error;
42
43     private static final String JavaDoc ATT_ID = "id"; //$NON-NLS-1$
44

45     private static final String JavaDoc ATT_TARGET_TAB = "tab"; //$NON-NLS-1$
46

47     private static final String JavaDoc ATT_AFTER_SECTION = "afterSection"; //$NON-NLS-1$
48

49     private static final String JavaDoc ATT_CLASS = "class"; //$NON-NLS-1$
50

51     private static final String JavaDoc ATT_SECTION_FILTER = "filter"; //$NON-NLS-1$
52

53     private static final String JavaDoc ATT_SECTION_ENABLES_FOR = "enablesFor"; //$NON-NLS-1$
54

55     private static final String JavaDoc ATT_INPUT_TYPE = "type"; //$NON-NLS-1$
56

57     private static final String JavaDoc ELEMENT_INPUT = "input"; //$NON-NLS-1$
58

59     private static final String JavaDoc TOP = "top"; //$NON-NLS-1$
60

61     private String JavaDoc id;
62
63     private String JavaDoc targetTab;
64
65     private String JavaDoc afterSection;
66
67     private ArrayList JavaDoc inputTypes;
68
69     private TabbedPropertyRegistryClassSectionFilter classFilter;
70
71     private IFilter filter;
72
73     private int enablesFor = ENABLES_FOR_ANY;
74
75     private IConfigurationElement configurationElement;
76
77     /**
78      * Constructor for the section descriptor.
79      *
80      * @param configurationElement
81      * the configuration element for the section descriptor.
82      * @param typeMapper
83      * The type mapper.
84      */

85     protected SectionDescriptor(IConfigurationElement configurationElement,
86             ITypeMapper typeMapper) {
87         this.configurationElement = configurationElement;
88
89         classFilter = new TabbedPropertyRegistryClassSectionFilter(typeMapper);
90         id = getConfigurationElement().getAttribute(ATT_ID);
91         targetTab = getConfigurationElement().getAttribute(ATT_TARGET_TAB);
92         afterSection = getConfigurationElement()
93             .getAttribute(ATT_AFTER_SECTION);
94         if (getConfigurationElement().getAttribute(ATT_SECTION_ENABLES_FOR) != null) {
95             String JavaDoc enablesForStr = getConfigurationElement().getAttribute(
96                 ATT_SECTION_ENABLES_FOR);
97             int enablesForTest = Integer.parseInt(enablesForStr);
98             if (enablesForTest > 0) {
99                 enablesFor = enablesForTest;
100             }
101         }
102
103         if (id == null || targetTab == null) {
104             // the section id and tab are mandatory - log error
105
handleSectionError(null);
106         }
107         if (getAfterSection() == null) {
108             afterSection = TOP;
109         }
110     }
111
112     /**
113      * Handle the section error when an issue is found loading from the
114      * configuration element.
115      *
116      * @param configurationElement
117      * the configuration element
118      * @param exception
119      * an optional CoreException
120      */

121     private void handleSectionError(CoreException exception) {
122         String JavaDoc pluginId = getConfigurationElement().getDeclaringExtension()
123             .getNamespace();
124         String JavaDoc message = MessageFormat.format(SECTION_ERROR,
125             new Object JavaDoc[] {pluginId});
126         IStatus status = new Status(IStatus.ERROR, pluginId,
127             TabbedPropertyViewStatusCodes.SECTION_ERROR, message, exception);
128         TabbedPropertyViewPlugin.getPlugin().getLog().log(status);
129     }
130
131     /**
132      * @see org.eclipse.ui.views.properties.tabbed.ISectionDescriptor#getId()
133      */

134     public String JavaDoc getId() {
135         return id;
136     }
137
138     /**
139      * @see org.eclipse.ui.views.properties.tabbed.ISectionDescriptor#getFilter()
140      */

141     public IFilter getFilter() {
142         if (filter == null) {
143             try {
144                 if (getConfigurationElement().getAttribute(ATT_SECTION_FILTER) != null) {
145                     filter = (IFilter) configurationElement
146                         .createExecutableExtension(ATT_SECTION_FILTER);
147                 }
148             } catch (CoreException exception) {
149                 handleSectionError(exception);
150             }
151         }
152         return filter;
153     }
154
155     /**
156      * Retrieves the value for section enablement which is a precise number of
157      * items selected. For example: enablesFor=" 4" enables the action only when
158      * 4 items are selected. If not specified, enable for all selections.
159      *
160      * @return the value for section enablement.
161      */

162     public int getEnablesFor() {
163         return enablesFor;
164     }
165
166     /**
167      * @see org.eclipse.ui.views.properties.tabbed.ISectionDescriptor#getTargetTab()
168      */

169     public String JavaDoc getTargetTab() {
170         return targetTab;
171     }
172
173     /**
174      * @see org.eclipse.ui.views.properties.tabbed.ISectionDescriptor#appliesTo(org.eclipse.ui.IWorkbenchPart,
175      * org.eclipse.jface.viewers.ISelection)
176      */

177     public boolean appliesTo(IWorkbenchPart part, ISelection selection) {
178         return classFilter.appliesToSelection(this, selection);
179     }
180
181     /**
182      * @see org.eclipse.ui.views.properties.tabbed.ISectionDescriptor#getAfterSection()
183      */

184     public String JavaDoc getAfterSection() {
185         return afterSection;
186     }
187
188     /**
189      * Creates an instance of a section described by this descriptor
190      *
191      * @see org.eclipse.ui.views.properties.tabbed.ISectionDescriptor#getSectionClass()
192      */

193     public ISection getSectionClass() {
194         ISection section = null;
195         try {
196             section = (ISection) getConfigurationElement()
197                 .createExecutableExtension(ATT_CLASS);
198         } catch (CoreException exception) {
199             handleSectionError(exception);
200         }
201
202         return section;
203     }
204
205     /**
206      * Gets the input types that are valid for this section.
207      *
208      * @see org.eclipse.ui.views.properties.tabbed.ISectionDescriptor#getInputTypes()
209      */

210     public List JavaDoc getInputTypes() {
211         if (inputTypes == null) {
212             inputTypes = new ArrayList JavaDoc();
213             IConfigurationElement[] elements = getConfigurationElement()
214                 .getChildren(ELEMENT_INPUT);
215             for (int i = 0; i < elements.length; i++) {
216                 IConfigurationElement element = elements[i];
217                 inputTypes.add(element.getAttribute(ATT_INPUT_TYPE));
218             }
219         }
220
221         return inputTypes;
222     }
223
224     /**
225      * @see java.lang.Object#toString()
226      */

227     public String JavaDoc toString() {
228         return getId();
229     }
230
231     /**
232      * @return Returns the configurationElement.
233      */

234     private IConfigurationElement getConfigurationElement() {
235         return configurationElement;
236     }
237 }
238
Popular Tags