1 11 package org.eclipse.ui.internal.registry; 12 13 import java.io.File ; 14 import java.io.Serializable ; 15 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IConfigurationElement; 19 import org.eclipse.jface.resource.ImageDescriptor; 20 import org.eclipse.swt.graphics.Image; 21 import org.eclipse.swt.program.Program; 22 import org.eclipse.ui.IEditorActionBarContributor; 23 import org.eclipse.ui.IEditorDescriptor; 24 import org.eclipse.ui.IEditorMatchingStrategy; 25 import org.eclipse.ui.IEditorPart; 26 import org.eclipse.ui.IMemento; 27 import org.eclipse.ui.IPluginContribution; 28 import org.eclipse.ui.ISharedImages; 29 import org.eclipse.ui.internal.IWorkbenchConstants; 30 import org.eclipse.ui.internal.WorkbenchImages; 31 import org.eclipse.ui.internal.WorkbenchPlugin; 32 import org.eclipse.ui.internal.misc.ProgramImageDescriptor; 33 import org.eclipse.ui.internal.util.Util; 34 import org.eclipse.ui.plugin.AbstractUIPlugin; 35 36 39 public final class EditorDescriptor implements IEditorDescriptor, Serializable , 40 IPluginContribution { 41 42 46 private static final long serialVersionUID = 3905241225668998961L; 47 48 52 public static final int OPEN_INTERNAL = 0x01; 53 54 57 public static final int OPEN_INPLACE = 0x02; 58 59 62 public static final int OPEN_EXTERNAL = 0x04; 63 64 private String editorName; 65 66 private String imageFilename; 67 68 private transient ImageDescriptor imageDesc; 69 70 private boolean testImage = true; 71 72 private String className; 73 74 private String launcherName; 75 76 private String fileName; 77 78 private String id = Util.ZERO_LENGTH_STRING; 79 80 private boolean matchingStrategyChecked = false; 81 private IEditorMatchingStrategy matchingStrategy; 82 83 private Program program; 84 85 private String pluginIdentifier; 87 88 private int openMode = 0; 89 90 private transient IConfigurationElement configurationElement; 91 92 98 EditorDescriptor(String id2, IConfigurationElement element) { 99 setID(id2); 100 setConfigurationElement(element); 101 } 102 103 104 105 109 EditorDescriptor() { 110 super(); 111 } 112 113 114 115 121 public static EditorDescriptor createForProgram(String filename) { 122 if (filename == null) { 123 throw new IllegalArgumentException (); 124 } 125 EditorDescriptor editor = new EditorDescriptor(); 126 127 editor.setFileName(filename); 128 editor.setID(filename); 129 editor.setOpenMode(OPEN_EXTERNAL); 130 131 int start = filename.lastIndexOf(File.separator); 133 String name; 134 if (start != -1) { 135 name = filename.substring(start + 1); 136 } else { 137 name = filename; 138 } 139 int end = name.lastIndexOf('.'); 140 if (end != -1) { 141 name = name.substring(0, end); 142 } 143 editor.setName(name); 144 145 ImageDescriptor imageDescriptor = new ProgramImageDescriptor(filename, 147 0); 148 editor.setImageDescriptor(imageDescriptor); 149 150 return editor; 151 } 152 153 157 private static Program findProgram(String programName) { 158 159 Program[] programs = Program.getPrograms(); 160 for (int i = 0; i < programs.length; i++) { 161 if (programs[i].getName().equals(programName)) { 162 return programs[i]; 163 } 164 } 165 166 return null; 167 } 168 169 174 public IEditorActionBarContributor createActionBarContributor() { 175 if (configurationElement == null) { 179 return null; 180 } 181 182 String className = configurationElement 184 .getAttribute(IWorkbenchRegistryConstants.ATT_CONTRIBUTOR_CLASS); 185 if (className == null) { 186 return null; 187 } 188 189 IEditorActionBarContributor contributor = null; 191 try { 192 contributor = (IEditorActionBarContributor) WorkbenchPlugin 193 .createExtension(configurationElement, 194 IWorkbenchRegistryConstants.ATT_CONTRIBUTOR_CLASS); 195 } catch (CoreException e) { 196 WorkbenchPlugin.log("Unable to create editor contributor: " + id, e.getStatus()); 198 } 199 return contributor; 200 } 201 202 207 public String getClassName() { 208 if (configurationElement == null) { 209 return className; 210 } 211 return RegistryReader.getClassValue(configurationElement, 212 IWorkbenchRegistryConstants.ATT_CLASS); 213 } 214 215 220 public IConfigurationElement getConfigurationElement() { 221 return configurationElement; 222 } 223 224 230 public IEditorPart createEditor() throws CoreException { 231 Object extension = WorkbenchPlugin.createExtension(getConfigurationElement(), IWorkbenchRegistryConstants.ATT_CLASS); 232 return (IEditorPart)extension; 233 } 234 235 240 public String getFileName() { 241 if (program == null) { 242 if (configurationElement == null) { 243 return fileName; 244 } 245 return configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_COMMAND); 246 } 247 return program.getName(); 248 } 249 250 255 public String getId() { 256 if (program == null) { 257 if (configurationElement == null) { 258 return Util.safeString(id); 259 } 260 return Util.safeString(configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID)); 261 262 } 263 return Util.safeString(program.getName()); 264 } 265 266 271 public ImageDescriptor getImageDescriptor() { 272 if (testImage) { 273 testImage = false; 274 if (imageDesc == null) { 275 String imageFileName = getImageFilename(); 276 String command = getFileName(); 277 if (imageFileName != null && configurationElement != null) { 278 imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin( 279 configurationElement.getNamespace(), imageFileName); 280 } else if (command != null) { 281 imageDesc = WorkbenchImages.getImageDescriptorFromProgram( 282 command, 0); 283 } 284 } 285 verifyImage(); 286 } 287 288 return imageDesc; 289 } 290 291 297 private void verifyImage() { 298 if (imageDesc == null) { 299 imageDesc = WorkbenchImages 300 .getImageDescriptor(ISharedImages.IMG_OBJ_FILE); 301 } 302 else { 303 Image img = imageDesc.createImage(false); 304 if (img == null) { 305 imageDesc = WorkbenchImages 307 .getImageDescriptor(ISharedImages.IMG_OBJ_FILE); 308 } else { 309 img.dispose(); 310 } 311 } 312 } 313 314 319 public String getImageFilename() { 320 if (configurationElement == null) { 321 return imageFilename; 322 } 323 return configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_ICON); 324 } 325 326 331 public String getLabel() { 332 if (program == null) { 333 if (configurationElement == null) { 334 return editorName; 335 } 336 return configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_NAME); 337 } 338 return program.getName(); 339 } 340 341 346 public String getLauncher() { 347 if (configurationElement == null) { 348 return launcherName; 349 } 350 return configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_LAUNCHER); 351 } 352 353 358 public String getPluginID() { 359 if (configurationElement != null) { 360 return configurationElement.getNamespace(); 361 } 362 return pluginIdentifier; 363 } 364 365 369 public Program getProgram() { 370 return this.program; 371 } 372 373 376 public boolean isInternal() { 377 return getOpenMode() == OPEN_INTERNAL; 378 } 379 380 383 public boolean isOpenInPlace() { 384 return getOpenMode() == OPEN_INPLACE; 385 } 386 387 390 public boolean isOpenExternal() { 391 return getOpenMode() == OPEN_EXTERNAL; 392 } 393 394 399 protected boolean loadValues(IMemento memento) { 400 editorName = memento.getString(IWorkbenchConstants.TAG_LABEL); 401 imageFilename = memento.getString(IWorkbenchConstants.TAG_IMAGE); 402 className = memento.getString(IWorkbenchConstants.TAG_CLASS); 403 launcherName = memento.getString(IWorkbenchConstants.TAG_LAUNCHER); 404 fileName = memento.getString(IWorkbenchConstants.TAG_FILE); 405 id = Util.safeString(memento.getString(IWorkbenchConstants.TAG_ID)); 406 pluginIdentifier = memento.getString(IWorkbenchConstants.TAG_PLUGIN); 407 408 Integer openModeInt = memento 409 .getInteger(IWorkbenchConstants.TAG_OPEN_MODE); 410 if (openModeInt != null) { 411 openMode = openModeInt.intValue(); 412 } else { 413 boolean internal = new Boolean (memento 415 .getString(IWorkbenchConstants.TAG_INTERNAL)) 416 .booleanValue(); 417 boolean openInPlace = new Boolean (memento 418 .getString(IWorkbenchConstants.TAG_OPEN_IN_PLACE)) 419 .booleanValue(); 420 if (internal) { 421 openMode = OPEN_INTERNAL; 422 } else { 423 if (openInPlace) { 424 openMode = OPEN_INPLACE; 425 } else { 426 openMode = OPEN_EXTERNAL; 427 } 428 } 429 } 430 if (openMode != OPEN_EXTERNAL && openMode != OPEN_INTERNAL 431 && openMode != OPEN_INPLACE) { 432 WorkbenchPlugin 433 .log("Ignoring editor descriptor with invalid openMode: " + this); return false; 435 } 436 437 String programName = memento 438 .getString(IWorkbenchConstants.TAG_PROGRAM_NAME); 439 if (programName != null) { 440 this.program = findProgram(programName); 441 } 442 return true; 443 } 444 445 448 protected void saveValues(IMemento memento) { 449 memento.putString(IWorkbenchConstants.TAG_LABEL, getLabel()); 450 memento.putString(IWorkbenchConstants.TAG_IMAGE, getImageFilename()); 451 memento.putString(IWorkbenchConstants.TAG_CLASS, getClassName()); 452 memento.putString(IWorkbenchConstants.TAG_LAUNCHER, getLauncher()); 453 memento.putString(IWorkbenchConstants.TAG_FILE, getFileName()); 454 memento.putString(IWorkbenchConstants.TAG_ID, getId()); 455 memento.putString(IWorkbenchConstants.TAG_PLUGIN, getPluginId()); 456 457 memento.putInteger(IWorkbenchConstants.TAG_OPEN_MODE, getOpenMode()); 458 memento.putString(IWorkbenchConstants.TAG_INTERNAL, String 460 .valueOf(isInternal())); 461 memento.putString(IWorkbenchConstants.TAG_OPEN_IN_PLACE, String 462 .valueOf(isOpenInPlace())); 463 464 if (this.program != null) { 465 memento.putString(IWorkbenchConstants.TAG_PROGRAM_NAME, 466 this.program.getName()); 467 } 468 } 469 470 476 private int getOpenMode() { 477 if (configurationElement == null) { return openMode; 479 } 480 else if (getLauncher() != null) { 481 return EditorDescriptor.OPEN_EXTERNAL; 483 } else if (getFileName() != null) { 484 return EditorDescriptor.OPEN_EXTERNAL; 486 } else if (getPluginId() != null) { 487 return EditorDescriptor.OPEN_INTERNAL; 489 } 490 else { 491 return 0; } 493 } 494 495 498 void setClassName(String newClassName) { 499 className = newClassName; 500 } 501 502 505 void setConfigurationElement( 506 IConfigurationElement newConfigurationElement) { 507 configurationElement = newConfigurationElement; 508 } 509 510 513 void setFileName(String aFileName) { 514 fileName = aFileName; 515 } 516 517 522 void setID(String anID) { 523 Assert.isNotNull(anID); 524 id = anID; 525 } 526 527 530 void setImageDescriptor(ImageDescriptor desc) { 531 imageDesc = desc; 532 testImage = true; 533 } 534 535 538 void setImageFilename(String aFileName) { 539 imageFilename = aFileName; 540 } 541 542 547 void setLauncher(String newLauncher) { 548 launcherName = newLauncher; 549 } 550 551 554 void setName(String newName) { 555 editorName = newName; 556 } 557 558 565 public void setOpenMode(int mode) { 566 openMode = mode; 567 } 568 569 572 void setPluginIdentifier(String anID) { 573 pluginIdentifier = anID; 574 } 575 576 580 void setProgram(Program newProgram) { 581 582 this.program = newProgram; 583 if (editorName == null) { 584 setName(newProgram.getName()); 585 } 586 } 587 588 591 public String toString() { 592 return "EditorDescriptor(id=" + getId() + ", label=" + getLabel() + ")"; } 594 595 598 public String getLocalId() { 599 return getId(); 600 } 601 602 605 public String getPluginId() { 606 return getPluginID(); 607 } 608 609 612 public IEditorMatchingStrategy getEditorMatchingStrategy() { 613 if (matchingStrategy == null && !matchingStrategyChecked) { 614 matchingStrategyChecked = true; 615 if (program == null && configurationElement != null) { 616 if (configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_MATCHING_STRATEGY) != null) { 617 try { 618 matchingStrategy = (IEditorMatchingStrategy) WorkbenchPlugin.createExtension(configurationElement, IWorkbenchRegistryConstants.ATT_MATCHING_STRATEGY); 619 } catch (CoreException e) { 620 WorkbenchPlugin.log("Error creating editor management policy for editor id " + getId(), e); } 622 } 623 } 624 } 625 return matchingStrategy; 626 } 627 628 629 } 630 | Popular Tags |