KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.ui.internal.views.properties.tabbed.TabbedPropertyViewPlugin;
24 import org.eclipse.ui.internal.views.properties.tabbed.TabbedPropertyViewStatusCodes;
25 import org.eclipse.ui.internal.views.properties.tabbed.l10n.TabbedPropertyMessages;
26 import org.eclipse.ui.plugin.AbstractUIPlugin;
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.ITabItem;
30
31 /**
32  * Represents the default implementation of a tab descriptor on the tabbed
33  * property tabs extensions.
34  *
35  * @author Anthony Hunter
36  */

37 public class TabDescriptor
38     implements Cloneable JavaDoc, ITabItem {
39
40     private static final String JavaDoc ATT_ID = "id"; //$NON-NLS-1$
41

42     private static final String JavaDoc ATT_LABEL = "label"; //$NON-NLS-1$
43

44     private static final String JavaDoc ATT_IMAGE = "image"; //$NON-NLS-1$
45

46     private static final String JavaDoc ATT_INDENTED = "indented"; //$NON-NLS-1$
47

48     private static final String JavaDoc ATT_CATEGORY = "category"; //$NON-NLS-1$
49

50     private static final String JavaDoc ATT_AFTER_TAB = "afterTab"; //$NON-NLS-1$
51

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

54     private final static String JavaDoc TAB_ERROR = TabbedPropertyMessages.TabDescriptor_Tab_error;
55
56     private String JavaDoc id;
57
58     private String JavaDoc label;
59
60     private Image image;
61
62     private boolean selected;
63
64     private boolean indented;
65
66     private String JavaDoc category;
67
68     private String JavaDoc afterTab;
69
70     private List JavaDoc sectionDescriptors;
71
72     /**
73      * Constructor for TabDescriptor.
74      *
75      * @param configurationElement
76      * the configuration element for the tab descriptor.
77      */

78     public TabDescriptor(IConfigurationElement configurationElement) {
79         if (configurationElement != null) {
80             id = configurationElement.getAttribute(ATT_ID);
81             label = configurationElement.getAttribute(ATT_LABEL);
82             String JavaDoc imageString = configurationElement.getAttribute(ATT_IMAGE);
83             if (imageString != null) {
84                 image = AbstractUIPlugin
85                     .imageDescriptorFromPlugin(
86                         configurationElement.getDeclaringExtension()
87                             .getNamespace(), imageString).createImage();
88             }
89             String JavaDoc indentedString = configurationElement
90                 .getAttribute(ATT_INDENTED);
91             indented = indentedString != null && indentedString.equals("true"); //$NON-NLS-1$
92
category = configurationElement.getAttribute(ATT_CATEGORY);
93             afterTab = configurationElement.getAttribute(ATT_AFTER_TAB);
94             if (id == null || label == null || category == null) {
95                 // the tab id, label and category are mandatory - log error
96
handleTabError(configurationElement, null);
97             }
98         }
99         if (getAfterTab() == null) {
100             afterTab = TOP;
101         }
102         sectionDescriptors = new ArrayList JavaDoc(5);
103         selected = false;
104     }
105
106     /**
107      * Get the unique identifier for the tab.
108      *
109      * @return the unique identifier for the tab.
110      */

111     public String JavaDoc getId() {
112         return id;
113     }
114
115     /**
116      * Get the text label for the tab.
117      *
118      * @return the text label for the tab.
119      */

120     public String JavaDoc getLabel() {
121         return label;
122     }
123
124     /**
125      * Get the identifier of the tab after which this tab should be displayed.
126      * When two or more tabs belong to the same category, they are sorted by the
127      * after tab values.
128      *
129      * @return the identifier of the tab.
130      */

131     protected String JavaDoc getAfterTab() {
132         return afterTab;
133     }
134
135     /**
136      * Get the category this tab belongs to.
137      *
138      * @return Get the category this tab belongs to.
139      */

140     protected String JavaDoc getCategory() {
141         return category;
142     }
143
144     /**
145      * Returns whether the given section was added to this tab. The section can
146      * be appended if its tab attribute matches the tab id. The afterSection
147      * attribute indicates the order in which the section should be appended.
148      *
149      * @param target
150      * the section descriptor to append.
151      */

152     protected boolean append(ISectionDescriptor target) {
153         if (!target.getTargetTab().equals(id)) {
154             return false;
155         }
156
157         if (insertSectionDescriptor(target)) {
158             return true;
159         }
160
161         sectionDescriptors.add(target);
162         return true;
163     }
164
165     /**
166      * Insert the section descriptor into the section descriptor list.
167      *
168      * @param target
169      * the section descriptor to insert.
170      * @return <code>true</code> if the target descriptor was added to the
171      * descriptors list.
172      */

173     private boolean insertSectionDescriptor(ISectionDescriptor target) {
174         if (target.getAfterSection().equals(TOP)) {
175             sectionDescriptors.add(0, target);
176             return true;
177         }
178         for (int i = 0; i < sectionDescriptors.size(); i++) {
179             ISectionDescriptor descriptor = (ISectionDescriptor) sectionDescriptors
180                 .get(i);
181             if (target.getAfterSection().equals(descriptor.getId())) {
182                 sectionDescriptors.add(i + 1, target);
183                 return true;
184             } else {
185                 if (descriptor.getAfterSection().equals(target.getId())) {
186                     sectionDescriptors.add(i, target);
187                     return true;
188                 }
189             }
190         }
191         return false;
192     }
193
194     /**
195      * Instantiate this tab's sections.
196      */

197     public Tab createTab() {
198         List JavaDoc sections = new ArrayList JavaDoc(sectionDescriptors.size());
199         for (Iterator JavaDoc iter = sectionDescriptors.iterator(); iter.hasNext();) {
200             ISectionDescriptor descriptor = (ISectionDescriptor) iter.next();
201             ISection section = descriptor.getSectionClass();
202             sections.add(section);
203         }
204         Tab tab = new Tab();
205         tab.setSections((ISection[]) sections.toArray(new ISection[sections
206             .size()]));
207         return tab;
208     }
209
210     /**
211      * Get the list of section descriptors for the tab.
212      *
213      * @return the list of section descriptors for the tab.
214      */

215     protected List JavaDoc getSectionDescriptors() {
216         return sectionDescriptors;
217     }
218
219     /**
220      * Set the list of section descriptors for the tab.
221      *
222      * @param sectionDescriptors
223      * the list of section descriptors for the tab.
224      */

225     protected void setSectionDescriptors(List JavaDoc sectionDescriptors) {
226         this.sectionDescriptors = sectionDescriptors;
227     }
228
229     /**
230      * @see java.lang.Object#toString()
231      */

232     public String JavaDoc toString() {
233         return getId();
234     }
235
236     /**
237      * Handle the tab error when an issue is found loading from the
238      * configuration element.
239      *
240      * @param configurationElement
241      * the configuration element
242      * @param exception
243      * an optional CoreException
244      */

245     private void handleTabError(IConfigurationElement configurationElement,
246             CoreException exception) {
247         String JavaDoc pluginId = configurationElement.getDeclaringExtension()
248             .getNamespace();
249         String JavaDoc message = MessageFormat.format(TAB_ERROR,
250             new Object JavaDoc[] {pluginId});
251         IStatus status = new Status(IStatus.ERROR, pluginId,
252             TabbedPropertyViewStatusCodes.TAB_ERROR, message, exception);
253         TabbedPropertyViewPlugin.getPlugin().getLog().log(status);
254     }
255
256     /**
257      * @see java.lang.Object#equals(java.lang.Object)
258      */

259     public boolean equals(Object JavaDoc object) {
260         if (this == object) {
261             return true;
262         }
263
264         if (this.getClass() == object.getClass()) {
265             TabDescriptor descriptor = (TabDescriptor) object;
266             if (this.getCategory().equals(descriptor.getCategory())
267                 && this.getId().equals(descriptor.getId())
268                 && this.getSectionDescriptors().size() == descriptor
269                     .getSectionDescriptors().size()) {
270
271                 Iterator JavaDoc i = this.getSectionDescriptors().iterator();
272                 Iterator JavaDoc j = descriptor.getSectionDescriptors().iterator();
273
274                 // the order is importent here - so as long as the sizes of the
275
// lists are the same and id of the section at the same
276
// positions are the same - the lists are the same
277
while (i.hasNext()) {
278                     ISectionDescriptor source = (ISectionDescriptor) i.next();
279                     ISectionDescriptor target = (ISectionDescriptor) j.next();
280                     if (!source.getId().equals(target.getId())) {
281                         return false;
282                     }
283                 }
284
285                 return true;
286             }
287
288         }
289
290         return false;
291     }
292
293     /**
294      * @see java.lang.Object#hashCode()
295      */

296     public int hashCode() {
297
298         int hashCode = getCategory().hashCode();
299         hashCode ^= getId().hashCode();
300         Iterator JavaDoc i = this.getSectionDescriptors().iterator();
301         while (i.hasNext()) {
302             ISectionDescriptor section = (ISectionDescriptor) i.next();
303             hashCode ^= section.getId().hashCode();
304         }
305         return hashCode;
306     }
307
308     /**
309      * @see java.lang.Object#clone()
310      */

311     public Object JavaDoc clone() {
312         try {
313             return super.clone();
314         } catch (CloneNotSupportedException JavaDoc exception) {
315             IStatus status = new Status(IStatus.ERROR, TabbedPropertyViewPlugin
316                 .getPlugin().getBundle().getSymbolicName(), 666, exception
317                 .getMessage(), exception);
318             TabbedPropertyViewPlugin.getPlugin().getLog().log(status);
319         }
320         return null;
321     }
322
323     /**
324      * Set the image for the tab.
325      *
326      * @param image
327      * the image for the tab.
328      */

329     protected void setImage(Image image) {
330         this.image = image;
331     }
332
333     /**
334      * Set the indicator to determine if the tab should be displayed as
335      * indented.
336      *
337      * @param indented
338      * <code>true</code> if the tab should be displayed as
339      * indented.
340      */

341     protected void setIndented(boolean indented) {
342         this.indented = indented;
343     }
344
345     /**
346      * Set the indicator to determine if the tab should be the selected tab in
347      * the list.
348      *
349      * @param selected
350      * <code>true</code> if the tab should be the selected tab in
351      * the list.
352      */

353     protected void setSelected(boolean selected) {
354         this.selected = selected;
355     }
356
357     /**
358      * Set the text label for the tab.
359      *
360      * @param label
361      * the text label for the tab.
362      */

363     protected void setLabel(String JavaDoc label) {
364         this.label = label;
365     }
366
367     /**
368      * Get the image for the tab.
369      *
370      * @return the image for the tab.
371      */

372     public Image getImage() {
373         return image;
374     }
375
376     /**
377      * Determine if the tab is selected.
378      *
379      * @return <code>true</code> if the tab is selected.
380      */

381     public boolean isSelected() {
382         return selected;
383     }
384
385     /**
386      * Determine if the tab should be displayed as indented.
387      *
388      * @return <code>true</code> if the tab should be displayed as indented.
389      */

390     public boolean isIndented() {
391         return indented;
392     }
393
394     /**
395      * Get the text label for the tab.
396      *
397      * @return the text label for the tab.
398      */

399     public String JavaDoc getText() {
400         return label;
401     }
402 }
403
Popular Tags