PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/eclipse-wtp-common-3.4.0/plugins/org.eclipse.wst.common.ui.properties/src/org/eclipse/wst/common/ui/properties/internal/view/TabDescriptor.java

#
Java | 404 lines | 204 code | 52 blank | 148 comment | 33 complexity | 168872a1fbf8abfceea3867d36f35337 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2001, 2004 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.wst.common.ui.properties.internal.view;
  12. import java.text.MessageFormat;
  13. import java.util.ArrayList;
  14. import java.util.Iterator;
  15. import java.util.List;
  16. import org.eclipse.core.runtime.CoreException;
  17. import org.eclipse.core.runtime.IConfigurationElement;
  18. import org.eclipse.core.runtime.IStatus;
  19. import org.eclipse.core.runtime.Status;
  20. import org.eclipse.swt.graphics.Image;
  21. import org.eclipse.ui.plugin.AbstractUIPlugin;
  22. import org.eclipse.wst.common.ui.properties.internal.CommonUIPropertiesPlugin;
  23. import org.eclipse.wst.common.ui.properties.internal.CommonUIPropertiesStatusCodes;
  24. import org.eclipse.wst.common.ui.properties.internal.l10n.Messages;
  25. import org.eclipse.wst.common.ui.properties.internal.provisional.IDynamicTabItem;
  26. import org.eclipse.wst.common.ui.properties.internal.provisional.ISection;
  27. import org.eclipse.wst.common.ui.properties.internal.provisional.ISectionDescriptor;
  28. /**
  29. * Represents the default implementation of a tab descriptor on the tabbed
  30. * property tabs extensions.
  31. *
  32. * @author Anthony Hunter <a
  33. * href="mailto:anthonyh@ca.ibm.com">anthonyh@ca.ibm.com </a>
  34. */
  35. public class TabDescriptor
  36. implements Cloneable, IDynamicTabItem {
  37. private static final String ATT_ID = "id"; //$NON-NLS-1$
  38. private static final String ATT_LABEL = "label"; //$NON-NLS-1$
  39. private static final String ATT_IMAGE = "image"; //$NON-NLS-1$
  40. private static final String ATT_INDENTED = "indented"; //$NON-NLS-1$
  41. private static final String ATT_CATEGORY = "category"; //$NON-NLS-1$
  42. private static final String ATT_AFTER_TAB = "afterTab"; //$NON-NLS-1$
  43. private static final String TOP = "top"; //$NON-NLS-1$
  44. private final static String TAB_ERROR = Messages.TabDescriptor_Tab_error; //$NON-NLS-1$
  45. private String id;
  46. private String label;
  47. private Image image;
  48. private boolean selected;
  49. private boolean indented;
  50. private String category;
  51. private String afterTab;
  52. private List sectionDescriptors;
  53. /**
  54. * Constructor for TabDescriptor.
  55. *
  56. * @param configurationElement
  57. * the configuration element for the tab descriptor.
  58. */
  59. public TabDescriptor(IConfigurationElement configurationElement) {
  60. if (configurationElement != null) {
  61. id = configurationElement.getAttribute(ATT_ID);
  62. label = configurationElement.getAttribute(ATT_LABEL);
  63. String imageString = configurationElement.getAttribute(ATT_IMAGE);
  64. if (imageString != null) {
  65. image = AbstractUIPlugin.imageDescriptorFromPlugin(
  66. configurationElement.getDeclaringExtension()
  67. .getNamespaceIdentifier(),
  68. imageString).createImage();
  69. }
  70. String indentedString = configurationElement
  71. .getAttribute(ATT_INDENTED);
  72. indented = indentedString != null && indentedString.equals("true"); //$NON-NLS-1$
  73. category = configurationElement.getAttribute(ATT_CATEGORY);
  74. afterTab = configurationElement.getAttribute(ATT_AFTER_TAB);
  75. if (id == null || label == null || category == null) {
  76. // the tab id, label and category are mandatory - log error
  77. handleTabError(configurationElement, null);
  78. }
  79. }
  80. if (getAfterTab() == null) {
  81. afterTab = TOP;
  82. }
  83. sectionDescriptors = new ArrayList(5);
  84. selected = false;
  85. }
  86. /**
  87. * Get the unique identifier for the tab.
  88. *
  89. * @return the unique identifier for the tab.
  90. */
  91. public String getId() {
  92. return id;
  93. }
  94. /**
  95. * Get the text label for the tab.
  96. *
  97. * @return the text label for the tab.
  98. */
  99. public String getLabel() {
  100. return label;
  101. }
  102. /**
  103. * Get the identifier of the tab after which this tab should be displayed.
  104. * When two or more tabs belong to the same category, they are sorted by the
  105. * after tab values.
  106. *
  107. * @return the identifier of the tab.
  108. */
  109. protected String getAfterTab() {
  110. return afterTab;
  111. }
  112. /**
  113. * Get the category this tab belongs to.
  114. *
  115. * @return Get the category this tab belongs to.
  116. */
  117. protected String getCategory() {
  118. return category;
  119. }
  120. /**
  121. * Returns whether the given section was added to this tab. The section can
  122. * be appended if its tab attribute matches the tab id. The afterSection
  123. * attribute indicates the order in which the section should be appended.
  124. *
  125. * @param target
  126. * the section descriptor to append.
  127. */
  128. protected boolean append(ISectionDescriptor target) {
  129. if (!target.getTargetTab().equals(id)) {
  130. return false;
  131. }
  132. if (insertSectionDescriptor(target)) {
  133. return true;
  134. }
  135. sectionDescriptors.add(target);
  136. return true;
  137. }
  138. /**
  139. * Insert the section descriptor into the section descriptor list.
  140. *
  141. * @param target
  142. * the section descriptor to insert.
  143. * @return <code>true</code> if the target descriptor was added to the
  144. * descriptors list.
  145. */
  146. private boolean insertSectionDescriptor(ISectionDescriptor target) {
  147. if (target.getAfterSection().equals(TOP)) {
  148. sectionDescriptors.add(0, target);
  149. return true;
  150. }
  151. for (int i = 0; i < sectionDescriptors.size(); i++) {
  152. ISectionDescriptor descriptor = (ISectionDescriptor) sectionDescriptors
  153. .get(i);
  154. if (target.getAfterSection().equals(descriptor.getId())) {
  155. sectionDescriptors.add(i + 1, target);
  156. return true;
  157. } else {
  158. if (descriptor.getAfterSection().equals(target.getId())) {
  159. sectionDescriptors.add(i, target);
  160. return true;
  161. }
  162. }
  163. }
  164. return false;
  165. }
  166. /**
  167. * Instantiate this tab's sections.
  168. */
  169. public Tab createTab() {
  170. List sections = new ArrayList(sectionDescriptors.size());
  171. for (Iterator iter = sectionDescriptors.iterator(); iter.hasNext();) {
  172. ISectionDescriptor descriptor = (ISectionDescriptor) iter.next();
  173. ISection section = descriptor.getSectionClass();
  174. sections.add(section);
  175. }
  176. Tab tab = new Tab();
  177. tab.setSections((ISection[]) sections.toArray(new ISection[sections
  178. .size()]));
  179. return tab;
  180. }
  181. /**
  182. * Get the list of section descriptors for the tab.
  183. *
  184. * @return the list of section descriptors for the tab.
  185. */
  186. protected List getSectionDescriptors() {
  187. return sectionDescriptors;
  188. }
  189. /**
  190. * Set the list of section descriptors for the tab.
  191. *
  192. * @param sectionDescriptors
  193. * the list of section descriptors for the tab.
  194. */
  195. protected void setSectionDescriptors(List sectionDescriptors) {
  196. this.sectionDescriptors = sectionDescriptors;
  197. }
  198. /**
  199. * @see java.lang.Object#toString()
  200. */
  201. public String toString() {
  202. return getId();
  203. }
  204. /**
  205. * Handle the tab error when an issue is found loading from the
  206. * configuration element.
  207. *
  208. * @param configurationElement
  209. * the configuration element
  210. * @param exception
  211. * an optional CoreException
  212. */
  213. private void handleTabError(IConfigurationElement configurationElement,
  214. CoreException exception) {
  215. String pluginId = configurationElement.getDeclaringExtension()
  216. .getNamespaceIdentifier();
  217. String message = MessageFormat.format(TAB_ERROR,
  218. new Object[] {pluginId});
  219. IStatus status = new Status(IStatus.ERROR, pluginId,
  220. CommonUIPropertiesStatusCodes.GENERAL_UI_FAILURE, message,
  221. exception);
  222. CommonUIPropertiesPlugin.getPlugin().getLog().log(status);
  223. }
  224. /**
  225. * @see java.lang.Object#equals(java.lang.Object)
  226. */
  227. public boolean equals(Object object) {
  228. if (this == object)
  229. return true;
  230. if (this.getClass() == object.getClass()) {
  231. TabDescriptor descriptor = (TabDescriptor) object;
  232. if (this.getCategory().equals(descriptor.getCategory())
  233. && this.getId().equals(descriptor.getId())
  234. && this.getSectionDescriptors().size() == descriptor
  235. .getSectionDescriptors().size()) {
  236. Iterator i = this.getSectionDescriptors().iterator();
  237. Iterator j = descriptor.getSectionDescriptors().iterator();
  238. // the order is importent here - so as long as the sizes of the
  239. // lists are the same and id of the section at the same
  240. // positions are the same - the lists are the same
  241. while (i.hasNext()) {
  242. ISectionDescriptor source = (ISectionDescriptor) i.next();
  243. ISectionDescriptor target = (ISectionDescriptor) j.next();
  244. if (!source.getId().equals(target.getId()))
  245. return false;
  246. }
  247. return true;
  248. }
  249. }
  250. return false;
  251. }
  252. /**
  253. * @see java.lang.Object#hashCode()
  254. */
  255. public int hashCode() {
  256. int hashCode = getCategory().hashCode();
  257. hashCode ^= getId().hashCode();
  258. Iterator i = this.getSectionDescriptors().iterator();
  259. while (i.hasNext()) {
  260. ISectionDescriptor section = (ISectionDescriptor) i.next();
  261. hashCode ^= section.getId().hashCode();
  262. }
  263. return hashCode;
  264. }
  265. /**
  266. * @see java.lang.Object#clone()
  267. */
  268. public Object clone() {
  269. try {
  270. return super.clone();
  271. } catch (CloneNotSupportedException exception) {
  272. IStatus status = new Status(IStatus.ERROR, CommonUIPropertiesPlugin
  273. .getPlugin().getBundle().getSymbolicName(),
  274. CommonUIPropertiesStatusCodes.GENERAL_UI_FAILURE, exception
  275. .getMessage(), exception);
  276. CommonUIPropertiesPlugin.getPlugin().getLog().log(status);
  277. }
  278. return null;
  279. }
  280. /**
  281. * Set the image for the tab.
  282. *
  283. * @param image
  284. * the image for the tab.
  285. */
  286. protected void setImage(Image image) {
  287. this.image = image;
  288. }
  289. /**
  290. * Set the indicator to determine if the tab should be displayed as
  291. * indented.
  292. *
  293. * @param indented
  294. * <code>true</code> if the tab should be displayed as
  295. * indented.
  296. */
  297. protected void setIndented(boolean indented) {
  298. this.indented = indented;
  299. }
  300. /**
  301. * Set the indicator to determine if the tab should be the selected tab in
  302. * the list.
  303. *
  304. * @param indented
  305. * <code>true</code> if the tab should be the selected tab in
  306. * the list.
  307. */
  308. protected void setSelected(boolean selected) {
  309. this.selected = selected;
  310. }
  311. /**
  312. * Set the text label for the tab.
  313. *
  314. * @param label
  315. * the text label for the tab.
  316. */
  317. protected void setLabel(String label) {
  318. this.label = label;
  319. }
  320. /**
  321. * Get the image for the tab.
  322. *
  323. * @return the image for the tab.
  324. */
  325. public Image getImage() {
  326. return image;
  327. }
  328. /**
  329. * Determine if the tab is selected.
  330. *
  331. * @return <code>true</code> if the tab is selected.
  332. */
  333. public boolean isSelected() {
  334. return selected;
  335. }
  336. /**
  337. * Determine if the tab should be displayed as indented.
  338. *
  339. * @return <code>true</code> if the tab should be displayed as indented.
  340. */
  341. public boolean isIndented() {
  342. return indented;
  343. }
  344. /**
  345. * Get the text label for the tab.
  346. *
  347. * @return the text label for the tab.
  348. */
  349. public String getText() {
  350. return label;
  351. }
  352. }