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 org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IConfigurationElement;
014: import org.eclipse.core.runtime.IStatus;
015: import org.eclipse.core.runtime.Status;
016: import org.eclipse.swt.graphics.Image;
017: import org.eclipse.ui.internal.views.properties.tabbed.TabbedPropertyViewPlugin;
018: import org.eclipse.ui.internal.views.properties.tabbed.TabbedPropertyViewStatusCodes;
019: import org.eclipse.ui.internal.views.properties.tabbed.l10n.TabbedPropertyMessages;
020: import org.eclipse.ui.plugin.AbstractUIPlugin;
021: import org.eclipse.ui.views.properties.tabbed.AbstractTabDescriptor;
022: import org.eclipse.ui.views.properties.tabbed.ISectionDescriptor;
023:
024: import com.ibm.icu.text.MessageFormat;
025:
026: /**
027: * Represents the default implementation of a tab descriptor on the tabbed
028: * property tabs extensions.
029: *
030: * @author Anthony Hunter
031: */
032: public class TabDescriptor extends AbstractTabDescriptor {
033:
034: private static final String ATT_ID = "id"; //$NON-NLS-1$
035:
036: private static final String ATT_LABEL = "label"; //$NON-NLS-1$
037:
038: private static final String ATT_IMAGE = "image"; //$NON-NLS-1$
039:
040: private static final String ATT_INDENTED = "indented"; //$NON-NLS-1$
041:
042: private static final String ATT_CATEGORY = "category"; //$NON-NLS-1$
043:
044: private static final String ATT_AFTER_TAB = "afterTab"; //$NON-NLS-1$
045:
046: private final static String TAB_ERROR = TabbedPropertyMessages.TabDescriptor_Tab_error;
047:
048: private String id;
049:
050: private String label;
051:
052: private Image image;
053:
054: private boolean selected;
055:
056: private boolean indented;
057:
058: private String category;
059:
060: private String afterTab;
061:
062: /**
063: * Constructor for TabDescriptor.
064: *
065: * @param configurationElement
066: * the configuration element for the tab descriptor.
067: */
068: public TabDescriptor(IConfigurationElement configurationElement) {
069: super ();
070: if (configurationElement != null) {
071: id = configurationElement.getAttribute(ATT_ID);
072: label = configurationElement.getAttribute(ATT_LABEL);
073: String imageString = configurationElement
074: .getAttribute(ATT_IMAGE);
075: if (imageString != null) {
076: image = AbstractUIPlugin.imageDescriptorFromPlugin(
077: configurationElement.getDeclaringExtension()
078: .getNamespaceIdentifier(), imageString)
079: .createImage();
080: }
081: String indentedString = configurationElement
082: .getAttribute(ATT_INDENTED);
083: indented = indentedString != null
084: && indentedString.equals("true"); //$NON-NLS-1$
085: category = configurationElement.getAttribute(ATT_CATEGORY);
086: afterTab = configurationElement.getAttribute(ATT_AFTER_TAB);
087: if (id == null || label == null || category == null) {
088: // the tab id, label and category are mandatory - log error
089: handleTabError(configurationElement, null);
090: }
091: }
092: selected = false;
093: }
094:
095: /**
096: * Get the unique identifier for the tab.
097: *
098: * @return the unique identifier for the tab.
099: */
100: public String getId() {
101: return id;
102: }
103:
104: /**
105: * Get the text label for the tab.
106: *
107: * @return the text label for the tab.
108: */
109: public String getLabel() {
110: return label;
111: }
112:
113: /**
114: * Get the identifier of the tab after which this tab should be displayed.
115: * When two or more tabs belong to the same category, they are sorted by the
116: * after tab values.
117: *
118: * @return the identifier of the tab.
119: */
120: public String getAfterTab() {
121: if (afterTab == null) {
122: return super .getAfterTab();
123: }
124: return afterTab;
125: }
126:
127: /**
128: * Get the category this tab belongs to.
129: *
130: * @return Get the category this tab belongs to.
131: */
132: public String getCategory() {
133: return category;
134: }
135:
136: /**
137: * Returns whether the given section was added to this tab. The section can
138: * be appended if its tab attribute matches the tab id. The afterSection
139: * attribute indicates the order in which the section should be appended.
140: *
141: * @param target
142: * the section descriptor to append.
143: */
144: protected boolean append(ISectionDescriptor target) {
145: if (!target.getTargetTab().equals(id)) {
146: return false;
147: }
148:
149: if (insertSectionDescriptor(target)) {
150: return true;
151: }
152:
153: getSectionDescriptors().add(target);
154: return true;
155: }
156:
157: /**
158: * Insert the section descriptor into the section descriptor list.
159: *
160: * @param target
161: * the section descriptor to insert.
162: * @return <code>true</code> if the target descriptor was added to the
163: * descriptors list.
164: */
165: private boolean insertSectionDescriptor(ISectionDescriptor target) {
166: if (target.getAfterSection().equals(TOP)) {
167: getSectionDescriptors().add(0, target);
168: return true;
169: }
170: for (int i = 0; i < getSectionDescriptors().size(); i++) {
171: ISectionDescriptor descriptor = (ISectionDescriptor) getSectionDescriptors()
172: .get(i);
173: if (target.getAfterSection().equals(descriptor.getId())) {
174: getSectionDescriptors().add(i + 1, target);
175: return true;
176: } else if (descriptor.getAfterSection().equals(
177: target.getId())) {
178: getSectionDescriptors().add(i, target);
179: return true;
180: }
181: }
182: return false;
183: }
184:
185: /**
186: * @see java.lang.Object#toString()
187: */
188: public String toString() {
189: return getId();
190: }
191:
192: /**
193: * Handle the tab error when an issue is found loading from the
194: * configuration element.
195: *
196: * @param configurationElement
197: * the configuration element
198: * @param exception
199: * an optional CoreException
200: */
201: private void handleTabError(
202: IConfigurationElement configurationElement,
203: CoreException exception) {
204: String pluginId = configurationElement.getDeclaringExtension()
205: .getNamespaceIdentifier();
206: String message = MessageFormat.format(TAB_ERROR,
207: new Object[] { pluginId });
208: IStatus status = new Status(IStatus.ERROR, pluginId,
209: TabbedPropertyViewStatusCodes.TAB_ERROR, message,
210: exception);
211: TabbedPropertyViewPlugin.getPlugin().getLog().log(status);
212: }
213:
214: /**
215: * Set the image for the tab.
216: *
217: * @param image
218: * the image for the tab.
219: */
220: protected void setImage(Image image) {
221: this .image = image;
222: }
223:
224: /**
225: * Set the indicator to determine if the tab should be displayed as
226: * indented.
227: *
228: * @param indented
229: * <code>true</code> if the tab should be displayed as
230: * indented.
231: */
232: protected void setIndented(boolean indented) {
233: this .indented = indented;
234: }
235:
236: /**
237: * Set the indicator to determine if the tab should be the selected tab in
238: * the list.
239: *
240: * @param selected
241: * <code>true</code> if the tab should be the selected tab in
242: * the list.
243: */
244: protected void setSelected(boolean selected) {
245: this .selected = selected;
246: }
247:
248: /**
249: * Set the text label for the tab.
250: *
251: * @param label
252: * the text label for the tab.
253: */
254: protected void setLabel(String label) {
255: this .label = label;
256: }
257:
258: /**
259: * Get the image for the tab.
260: *
261: * @return the image for the tab.
262: */
263: public Image getImage() {
264: return image;
265: }
266:
267: /**
268: * Determine if the tab is selected.
269: *
270: * @return <code>true</code> if the tab is selected.
271: */
272: public boolean isSelected() {
273: return selected;
274: }
275:
276: /**
277: * Determine if the tab should be displayed as indented.
278: *
279: * @return <code>true</code> if the tab should be displayed as indented.
280: */
281: public boolean isIndented() {
282: return indented;
283: }
284:
285: /**
286: * Get the text label for the tab.
287: *
288: * @return the text label for the tab.
289: */
290: public String getText() {
291: return label;
292: }
293: }
|