1 12 package org.eclipse.ui.internal.registry; 13 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IConfigurationElement; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.jface.resource.ImageDescriptor; 19 import org.eclipse.ui.IMemento; 20 import org.eclipse.ui.IPerspectiveDescriptor; 21 import org.eclipse.ui.IPerspectiveFactory; 22 import org.eclipse.ui.IPluginContribution; 23 import org.eclipse.ui.PlatformUI; 24 import org.eclipse.ui.internal.IWorkbenchConstants; 25 import org.eclipse.ui.internal.IWorkbenchGraphicConstants; 26 import org.eclipse.ui.internal.WorkbenchImages; 27 import org.eclipse.ui.internal.WorkbenchPlugin; 28 import org.eclipse.ui.plugin.AbstractUIPlugin; 29 30 45 public class PerspectiveDescriptor implements IPerspectiveDescriptor, 46 IPluginContribution { 47 private String id; 48 49 private String pluginId; 50 51 private String originalId; 52 53 private String label; 54 55 private String className; 56 57 private String description; 58 59 private boolean singleton; 60 61 private boolean fixed; 62 63 private ImageDescriptor image; 64 65 private IConfigurationElement configElement; 66 67 77 public PerspectiveDescriptor(String id, String label, 78 PerspectiveDescriptor originalDescriptor) { 79 this.id = id; 80 this.label = label; 81 if (originalDescriptor != null) { 82 this.originalId = originalDescriptor.getOriginalId(); 83 this.image = originalDescriptor.image; 84 85 this.pluginId = originalDescriptor.getPluginId(); 117 } 118 } 119 120 130 public PerspectiveDescriptor(String id, IConfigurationElement configElement) 131 throws CoreException { 132 this.configElement = configElement; 133 this.id = id; 134 if ((getId() == null) || (getLabel() == null) 136 || (getClassName() == null)) { 137 throw new CoreException( 138 new Status( 139 IStatus.ERROR, 140 WorkbenchPlugin.PI_WORKBENCH, 141 0, 142 "Invalid extension (missing label, id or class name): " + getId(), null)); 144 } 145 } 146 147 155 public IPerspectiveFactory createFactory() throws CoreException { 156 if (originalId != null) { 158 IPerspectiveDescriptor target = ((PerspectiveRegistry) WorkbenchPlugin 161 .getDefault().getPerspectiveRegistry()) 162 .findPerspectiveWithId(originalId); 163 164 return target == null ? null : ((PerspectiveDescriptor) target) 165 .createFactory(); 166 } 167 168 if (configElement != null) { 170 try { 171 return (IPerspectiveFactory) configElement 172 .createExecutableExtension(IWorkbenchRegistryConstants.ATT_CLASS); 173 } catch (CoreException e) { 174 } 176 } 177 178 return null; 179 } 180 181 184 public void deleteCustomDefinition() { 185 ((PerspectiveRegistry) WorkbenchPlugin.getDefault() 186 .getPerspectiveRegistry()).deleteCustomDefinition(this); 187 } 188 189 194 public String getDescription() { 195 return configElement == null ? description : RegistryReader 196 .getDescription(configElement); 197 } 198 199 204 public boolean getFixed() { 205 return configElement == null ? fixed : Boolean.valueOf( 206 configElement 207 .getAttribute(IWorkbenchRegistryConstants.ATT_FIXED)) 208 .booleanValue(); 209 } 210 211 216 public String getId() { 217 return id; 218 } 219 220 225 public ImageDescriptor getImageDescriptor() { 226 if (image == null) { 227 if (configElement != null) { 228 String icon = configElement 229 .getAttribute(IWorkbenchRegistryConstants.ATT_ICON); 230 if (icon != null) { 231 image = AbstractUIPlugin.imageDescriptorFromPlugin( 232 configElement.getNamespace(), icon); 233 } 234 if (image == null) { 235 image = WorkbenchImages 236 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_DEF_PERSPECTIVE); 237 } 238 } 239 } 240 return image; 241 } 242 243 248 public String getLabel() { 249 return configElement == null ? label : configElement 250 .getAttribute(IWorkbenchRegistryConstants.ATT_NAME); 251 } 252 253 258 public String getOriginalId() { 259 if (originalId == null) { 260 return getId(); 261 } 262 return originalId; 263 } 264 265 270 public boolean hasCustomDefinition() { 271 return ((PerspectiveRegistry) WorkbenchPlugin.getDefault() 272 .getPerspectiveRegistry()).hasCustomDefinition(this); 273 } 274 275 280 public boolean hasDefaultFlag() { 281 if (configElement == null) { 282 return false; 283 } 284 285 return Boolean.valueOf( 286 configElement 287 .getAttribute(IWorkbenchRegistryConstants.ATT_DEFAULT)) 288 .booleanValue(); 289 } 290 291 297 public boolean isPredefined() { 298 return getClassName() != null && configElement != null; 299 } 300 301 306 public boolean isSingleton() { 307 return configElement == null ? singleton 308 : configElement 309 .getAttributeAsIs(IWorkbenchRegistryConstants.ATT_SINGLETON) != null; 310 } 311 312 320 public IStatus restoreState(IMemento memento) { 321 IMemento childMem = memento 322 .getChild(IWorkbenchConstants.TAG_DESCRIPTOR); 323 if (childMem != null) { 324 id = childMem.getString(IWorkbenchConstants.TAG_ID); 325 originalId = childMem.getString(IWorkbenchConstants.TAG_DESCRIPTOR); 326 label = childMem.getString(IWorkbenchConstants.TAG_LABEL); 327 className = childMem.getString(IWorkbenchConstants.TAG_CLASS); 328 singleton = (childMem.getInteger(IWorkbenchConstants.TAG_SINGLETON) != null); 329 330 IPerspectiveDescriptor descriptor = WorkbenchPlugin.getDefault() 332 .getPerspectiveRegistry().findPerspectiveWithId( 333 getOriginalId()); 334 335 if (descriptor != null) { 336 image = descriptor.getImageDescriptor(); 338 } 339 } 340 return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); } 342 343 347 public void revertToPredefined() { 348 if (isPredefined()) { 349 deleteCustomDefinition(); 350 } 351 } 352 353 361 public IStatus saveState(IMemento memento) { 362 IMemento childMem = memento 363 .createChild(IWorkbenchConstants.TAG_DESCRIPTOR); 364 childMem.putString(IWorkbenchConstants.TAG_ID, getId()); 365 if (originalId != null) { 366 childMem.putString(IWorkbenchConstants.TAG_DESCRIPTOR, originalId); 367 } 368 childMem.putString(IWorkbenchConstants.TAG_LABEL, getLabel()); 369 childMem.putString(IWorkbenchConstants.TAG_CLASS, getClassName()); 370 if (singleton) { 371 childMem.putInteger(IWorkbenchConstants.TAG_SINGLETON, 1); 372 } 373 return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); } 375 376 383 public IConfigurationElement getConfigElement() { 384 return configElement; 385 } 386 387 392 public String getLocalId() { 393 return getId(); 394 } 395 396 401 public String getPluginId() { 402 return configElement == null ? pluginId : configElement.getNamespace(); 403 } 404 405 411 public String getClassName() { 412 return configElement == null ? className : RegistryReader 413 .getClassValue(configElement, 414 IWorkbenchRegistryConstants.ATT_CLASS); 415 } 416 } 417 | Popular Tags |