1 11 package org.eclipse.ui.part; 12 13 import java.util.Collections ; 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 import org.eclipse.core.commands.common.EventManager; 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.IConfigurationElement; 20 import org.eclipse.core.runtime.IExecutableExtension; 21 import org.eclipse.core.runtime.ListenerList; 22 import org.eclipse.core.runtime.Platform; 23 import org.eclipse.jface.resource.ImageDescriptor; 24 import org.eclipse.jface.resource.JFaceResources; 25 import org.eclipse.jface.util.IPropertyChangeListener; 26 import org.eclipse.jface.util.PropertyChangeEvent; 27 import org.eclipse.jface.window.Window; 28 import org.eclipse.swt.graphics.Image; 29 import org.eclipse.swt.widgets.Composite; 30 import org.eclipse.ui.IPropertyListener; 31 import org.eclipse.ui.ISharedImages; 32 import org.eclipse.ui.IWorkbenchPart; 33 import org.eclipse.ui.IWorkbenchPart3; 34 import org.eclipse.ui.IWorkbenchPartConstants; 35 import org.eclipse.ui.IWorkbenchPartSite; 36 import org.eclipse.ui.PlatformUI; 37 import org.eclipse.ui.internal.WorkbenchMessages; 38 import org.eclipse.ui.internal.WorkbenchPlugin; 39 import org.eclipse.ui.internal.util.Util; 40 import org.eclipse.ui.plugin.AbstractUIPlugin; 41 42 import com.ibm.icu.text.MessageFormat; 43 44 55 public abstract class WorkbenchPart extends EventManager implements 56 IWorkbenchPart3, IExecutableExtension, IWorkbenchPartOrientation { 57 private String title = ""; 59 private ImageDescriptor imageDescriptor; 60 61 private Image titleImage; 62 63 private String toolTip = ""; 65 private IConfigurationElement configElement; 66 67 private IWorkbenchPartSite partSite; 68 69 private String partName = ""; 71 private String contentDescription = ""; 73 private ListenerList partChangeListeners = new ListenerList(); 74 75 78 protected WorkbenchPart() { 79 super(); 80 } 81 82 85 public void addPropertyListener(IPropertyListener l) { 86 addListenerObject(l); 87 } 88 89 99 public abstract void createPartControl(Composite parent); 100 101 106 public void dispose() { 107 if (imageDescriptor != null) { 108 JFaceResources.getResources().destroyImage(imageDescriptor); 109 } 110 111 clearListeners(); 115 partChangeListeners.clear(); 116 } 117 118 123 protected void firePropertyChange(final int propertyId) { 124 Object [] array = getListeners(); 125 for (int nX = 0; nX < array.length; nX++) { 126 final IPropertyListener l = (IPropertyListener) array[nX]; 127 try { 128 l.propertyChanged(WorkbenchPart.this, propertyId); 129 } catch (RuntimeException e) { 130 WorkbenchPlugin.log(e); 131 } 132 } 133 } 134 135 142 public Object getAdapter(Class adapter) { 143 144 149 150 return Platform.getAdapterManager().getAdapter(this, adapter); 151 } 152 153 159 protected IConfigurationElement getConfigurationElement() { 160 return configElement; 161 } 162 163 168 protected Image getDefaultImage() { 169 return PlatformUI.getWorkbench().getSharedImages().getImage( 170 ISharedImages.IMG_DEF_VIEW); 171 } 172 173 176 public IWorkbenchPartSite getSite() { 177 return partSite; 178 } 179 180 187 public String getTitle() { 188 return title; 189 } 190 191 194 public Image getTitleImage() { 195 if (titleImage != null) { 196 return titleImage; 197 } 198 return getDefaultImage(); 199 } 200 201 206 public String getTitleToolTip() { 207 return toolTip; 208 } 209 210 213 public void removePropertyListener(IPropertyListener l) { 214 removeListenerObject(l); 215 } 216 217 226 public abstract void setFocus(); 227 228 239 public void setInitializationData(IConfigurationElement cfig, 240 String propertyName, Object data) { 241 242 configElement = cfig; 244 245 partName = Util.safeString(cfig.getAttribute("name")); title = partName; 248 249 String strIcon = cfig.getAttribute("icon"); if (strIcon == null) { 252 return; 253 } 254 255 imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin( 256 configElement.getNamespace(), strIcon); 257 258 if (imageDescriptor == null) { 259 return; 260 } 261 262 titleImage = JFaceResources.getResources().createImageWithDefault(imageDescriptor); 263 } 264 265 273 protected void setSite(IWorkbenchPartSite site) { 274 checkSite(site); 275 this.partSite = site; 276 } 277 278 285 protected void checkSite(IWorkbenchPartSite site) { 286 } 288 289 300 protected void setTitle(String title) { 301 title = Util.safeString(title); 302 303 if (Util.equals(this.title, title)) { 305 return; 306 } 307 this.title = title; 308 firePropertyChange(IWorkbenchPart.PROP_TITLE); 309 } 310 311 316 protected void setTitleImage(Image titleImage) { 317 Assert.isTrue(titleImage == null || !titleImage.isDisposed()); 318 if (this.titleImage == titleImage) { 320 return; 321 } 322 this.titleImage = titleImage; 323 firePropertyChange(IWorkbenchPart.PROP_TITLE); 324 if (imageDescriptor != null) { 325 JFaceResources.getResources().destroyImage(imageDescriptor); 326 imageDescriptor = null; 327 } 328 } 329 330 336 protected void setTitleToolTip(String toolTip) { 337 toolTip = Util.safeString(toolTip); 338 if (Util.equals(this.toolTip, toolTip)) { 340 return; 341 } 342 this.toolTip = toolTip; 343 firePropertyChange(IWorkbenchPart.PROP_TITLE); 344 } 345 346 354 public void showBusy(boolean busy) { 355 } 357 358 365 public String getPartName() { 366 return partName; 367 } 368 369 383 protected void setPartName(String partName) { 384 385 internalSetPartName(partName); 386 387 setDefaultTitle(); 388 } 389 390 void setDefaultTitle() { 391 String description = getContentDescription(); 392 String name = getPartName(); 393 String newTitle = name; 394 395 if (!Util.equals(description, "")) { newTitle = MessageFormat 397 .format( 398 WorkbenchMessages.WorkbenchPart_AutoTitleFormat, new String [] { name, description }); 399 } 400 401 setTitle(newTitle); 402 } 403 404 411 public String getContentDescription() { 412 return contentDescription; 413 } 414 415 433 protected void setContentDescription(String description) { 434 internalSetContentDescription(description); 435 436 setDefaultTitle(); 437 } 438 439 void internalSetContentDescription(String description) { 440 Assert.isNotNull(description); 441 442 if (Util.equals(contentDescription, description)) { 444 return; 445 } 446 this.contentDescription = description; 447 448 firePropertyChange(IWorkbenchPartConstants.PROP_CONTENT_DESCRIPTION); 449 } 450 451 void internalSetPartName(String partName) { 452 partName = Util.safeString(partName); 453 454 Assert.isNotNull(partName); 455 456 if (Util.equals(this.partName, partName)) { 458 return; 459 } 460 this.partName = partName; 461 462 firePropertyChange(IWorkbenchPartConstants.PROP_PART_NAME); 463 464 } 465 466 467 470 public int getOrientation(){ 471 return Window.getDefaultOrientation(); 473 } 474 475 478 public void addPartPropertyListener(IPropertyChangeListener listener) { 479 partChangeListeners.add(listener); 480 } 481 482 485 public void removePartPropertyListener(IPropertyChangeListener listener) { 486 partChangeListeners.remove(listener); 487 } 488 489 492 protected void firePartPropertyChanged(String key, String oldValue, String newValue) { 493 final PropertyChangeEvent event = new PropertyChangeEvent(this, key, oldValue, newValue); 494 Object [] l = partChangeListeners.getListeners(); 495 for (int i = 0; i < l.length; i++) { 496 try { 497 ((IPropertyChangeListener)l[i]).propertyChange(event); 498 } catch (RuntimeException e) { 499 WorkbenchPlugin.log(e); 500 } 501 } 502 } 503 504 private Map partProperties = new HashMap (); 505 506 509 public void setPartProperty(String key, String value) { 510 String oldValue = (String ) partProperties.get(key); 511 if (value==null) { 512 partProperties.remove(key); 513 } else { 514 partProperties.put(key, value); 515 } 516 firePartPropertyChanged(key, oldValue, value); 517 } 518 519 522 public String getPartProperty(String key) { 523 return (String )partProperties.get(key); 524 } 525 526 529 public Map getPartProperties() { 530 return Collections.unmodifiableMap(partProperties); 531 } 532 } 533 | Popular Tags |