1 11 package org.eclipse.ui.internal.views.properties.tabbed.view; 12 13 import com.ibm.icu.text.MessageFormat; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 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 37 public class TabDescriptor 38 implements Cloneable , ITabItem { 39 40 private static final String ATT_ID = "id"; 42 private static final String ATT_LABEL = "label"; 44 private static final String ATT_IMAGE = "image"; 46 private static final String ATT_INDENTED = "indented"; 48 private static final String ATT_CATEGORY = "category"; 50 private static final String ATT_AFTER_TAB = "afterTab"; 52 private static final String TOP = "top"; 54 private final static String TAB_ERROR = TabbedPropertyMessages.TabDescriptor_Tab_error; 55 56 private String id; 57 58 private String label; 59 60 private Image image; 61 62 private boolean selected; 63 64 private boolean indented; 65 66 private String category; 67 68 private String afterTab; 69 70 private List sectionDescriptors; 71 72 78 public TabDescriptor(IConfigurationElement configurationElement) { 79 if (configurationElement != null) { 80 id = configurationElement.getAttribute(ATT_ID); 81 label = configurationElement.getAttribute(ATT_LABEL); 82 String imageString = configurationElement.getAttribute(ATT_IMAGE); 83 if (imageString != null) { 84 image = AbstractUIPlugin 85 .imageDescriptorFromPlugin( 86 configurationElement.getDeclaringExtension() 87 .getNamespace(), imageString).createImage(); 88 } 89 String indentedString = configurationElement 90 .getAttribute(ATT_INDENTED); 91 indented = indentedString != null && indentedString.equals("true"); category = configurationElement.getAttribute(ATT_CATEGORY); 93 afterTab = configurationElement.getAttribute(ATT_AFTER_TAB); 94 if (id == null || label == null || category == null) { 95 handleTabError(configurationElement, null); 97 } 98 } 99 if (getAfterTab() == null) { 100 afterTab = TOP; 101 } 102 sectionDescriptors = new ArrayList (5); 103 selected = false; 104 } 105 106 111 public String getId() { 112 return id; 113 } 114 115 120 public String getLabel() { 121 return label; 122 } 123 124 131 protected String getAfterTab() { 132 return afterTab; 133 } 134 135 140 protected String getCategory() { 141 return category; 142 } 143 144 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 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 197 public Tab createTab() { 198 List sections = new ArrayList (sectionDescriptors.size()); 199 for (Iterator 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 215 protected List getSectionDescriptors() { 216 return sectionDescriptors; 217 } 218 219 225 protected void setSectionDescriptors(List sectionDescriptors) { 226 this.sectionDescriptors = sectionDescriptors; 227 } 228 229 232 public String toString() { 233 return getId(); 234 } 235 236 245 private void handleTabError(IConfigurationElement configurationElement, 246 CoreException exception) { 247 String pluginId = configurationElement.getDeclaringExtension() 248 .getNamespace(); 249 String message = MessageFormat.format(TAB_ERROR, 250 new Object [] {pluginId}); 251 IStatus status = new Status(IStatus.ERROR, pluginId, 252 TabbedPropertyViewStatusCodes.TAB_ERROR, message, exception); 253 TabbedPropertyViewPlugin.getPlugin().getLog().log(status); 254 } 255 256 259 public boolean equals(Object 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 i = this.getSectionDescriptors().iterator(); 272 Iterator j = descriptor.getSectionDescriptors().iterator(); 273 274 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 296 public int hashCode() { 297 298 int hashCode = getCategory().hashCode(); 299 hashCode ^= getId().hashCode(); 300 Iterator 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 311 public Object clone() { 312 try { 313 return super.clone(); 314 } catch (CloneNotSupportedException 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 329 protected void setImage(Image image) { 330 this.image = image; 331 } 332 333 341 protected void setIndented(boolean indented) { 342 this.indented = indented; 343 } 344 345 353 protected void setSelected(boolean selected) { 354 this.selected = selected; 355 } 356 357 363 protected void setLabel(String label) { 364 this.label = label; 365 } 366 367 372 public Image getImage() { 373 return image; 374 } 375 376 381 public boolean isSelected() { 382 return selected; 383 } 384 385 390 public boolean isIndented() { 391 return indented; 392 } 393 394 399 public String getText() { 400 return label; 401 } 402 } 403 | Popular Tags |