1 11 package org.eclipse.ui.internal.part; 12 13 import java.text.MessageFormat ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.Platform; 19 import org.eclipse.jface.resource.ImageDescriptor; 20 import org.eclipse.jface.resource.JFaceResources; 21 import org.eclipse.jface.resource.ResourceManager; 22 import org.eclipse.jface.util.SafeRunnable; 23 import org.eclipse.swt.graphics.Image; 24 import org.eclipse.ui.IEditorInput; 25 import org.eclipse.ui.IPropertyListener; 26 import org.eclipse.ui.ISharedImages; 27 import org.eclipse.ui.IWorkbenchPart; 28 import org.eclipse.ui.IWorkbenchPartConstants; 29 import org.eclipse.ui.PlatformUI; 30 import org.eclipse.ui.internal.WorkbenchMessages; 31 import org.eclipse.ui.internal.components.framework.IDisposable; 32 import org.eclipse.ui.internal.part.components.services.IDirtyHandler; 33 import org.eclipse.ui.internal.part.components.services.IInputHandler; 34 import org.eclipse.ui.internal.part.components.services.INameable; 35 import org.eclipse.ui.internal.part.components.services.IPartDescriptor; 36 import org.eclipse.ui.internal.util.Util; 37 38 45 public class PartPropertyProvider implements IPartPropertyProvider, INameable, IDirtyHandler, IDisposable, IInputHandler { 46 47 private INameable nameable; 49 private IDirtyHandler dirty; 50 private List listeners = new ArrayList (); 51 52 private String tooltip = ""; private String partName; 54 private String contentDescription = ""; private String title = ""; private boolean isDirty = false; 57 private ImageDescriptor titleImage; 58 private Image image = null; 59 private IEditorInput input; 60 private ResourceManager mgr; 61 62 private final static class ListenerRec { 63 public ListenerRec(IPropertyListener l, IWorkbenchPart p) { 64 this.l = l; 65 this.part = p; 66 } 67 68 public boolean equals(Object other) { 69 if (!(other instanceof ListenerRec)) return false; 70 71 ListenerRec lr = (ListenerRec)other; 72 return (lr.l == l) && (lr.part == part); 73 } 74 75 public IPropertyListener l; 76 public IWorkbenchPart part; 77 }; 78 79 public PartPropertyProvider(ResourceManager manager, INameable parentNameable, 80 IDirtyHandler dirtyListener, IPartDescriptor descriptor, IEditorInput initialInput) { 81 this.nameable = parentNameable; 82 this.dirty = dirtyListener; 83 this.partName = descriptor.getLabel(); 84 this.titleImage = descriptor.getImage(); 85 this.input = initialInput; 86 this.mgr = manager; 87 } 88 89 private ResourceManager getManager() { 90 if (mgr == null) { 91 mgr = JFaceResources.getResources(); 92 } 93 return mgr; 94 } 95 96 99 public void addPropertyListener(IWorkbenchPart part, IPropertyListener l) { 100 listeners.add(new ListenerRec(l, part)); 101 } 102 103 public void removePropertyListener(IWorkbenchPart part, IPropertyListener l) { 104 listeners.remove(new ListenerRec(l, part)); 105 } 106 107 110 public String getTitle() { 111 return title; 112 } 113 114 117 public String getTitleToolTip() { 118 return tooltip; 119 } 120 121 124 public Image getTitleImage() { 125 if (image == null) { 126 if (titleImage == null) { 127 return PlatformUI.getWorkbench().getSharedImages().getImage( 128 ISharedImages.IMG_DEF_VIEW); 129 } 130 131 image = getManager().createImageWithDefault(titleImage); 132 } 133 134 return image; 135 } 136 137 140 public String getPartName() { 141 return partName; 142 } 143 144 147 public String getContentDescription() { 148 return contentDescription; 149 } 150 151 154 public IEditorInput getEditorInput() { 155 return input; 156 } 157 158 161 public boolean isDirty() { 162 return isDirty; 163 } 164 165 168 public void dispose() { 169 listeners.clear(); 170 171 if (image != null) { 172 getManager().destroy(titleImage); 173 } 174 } 175 176 179 public void setContentDescription(String contentDescription) { 180 if (this.contentDescription.equals(contentDescription)) { 181 return; 182 } 183 184 this.contentDescription = contentDescription; 185 186 if (nameable != null) { 187 nameable.setContentDescription(contentDescription); 188 } 189 190 firePropertyChange(IWorkbenchPartConstants.PROP_CONTENT_DESCRIPTION); 191 } 192 193 public void setDirty(boolean isDirty) { 194 if (isDirty == this.isDirty) { 195 return; 196 } 197 198 this.isDirty = isDirty; 199 200 if (dirty != null) { 201 dirty.setDirty(isDirty); 202 } 203 204 firePropertyChange(IWorkbenchPartConstants.PROP_DIRTY); 205 } 206 207 210 public void setImage(ImageDescriptor theImage) { 211 ImageDescriptor oldImageDescriptor = this.titleImage; 212 Image oldImage = image; 213 214 if (oldImageDescriptor == theImage) { 215 return; 216 } 217 218 this.titleImage = theImage; 219 this.image = null; 220 221 if (nameable != null) { 222 nameable.setImage(theImage); 223 } 224 225 firePropertyChange(IWorkbenchPartConstants.PROP_TITLE); 226 227 if (oldImage != null) { 228 getManager().destroy(oldImageDescriptor); 229 } 230 } 231 232 235 public void setName(String newName) { 236 if (this.partName.equals(newName)) { 237 return; 238 } 239 240 this.partName = newName; 241 242 if (nameable != null) { 243 nameable.setName(newName); 244 } 245 246 firePropertyChange(IWorkbenchPartConstants.PROP_PART_NAME); 247 248 setDefaultTitle(); 249 } 250 251 254 public void setTooltip(String toolTip) { 255 if (this.tooltip.equals(toolTip)) { 256 return; 257 } 258 259 this.tooltip = toolTip; 260 261 if (nameable != null) { 262 nameable.setTooltip(toolTip); 263 } 264 265 firePropertyChange(IWorkbenchPartConstants.PROP_TITLE); 266 } 267 268 271 public void setEditorInput(IEditorInput input) { 272 if (this.input.equals(input)) { 273 return; 274 } 275 276 this.input = input; 277 278 firePropertyChange(IWorkbenchPartConstants.PROP_INPUT); 279 } 280 281 private void setTitle(String newTitle) { 282 if (newTitle.equals(this.title)) { 283 return; 284 } 285 286 this.title = newTitle; 287 firePropertyChange(IWorkbenchPartConstants.PROP_TITLE); 288 } 289 290 void setDefaultTitle() { 291 String description = getContentDescription(); 292 String name = getPartName(); 293 String newTitle = name; 294 295 if (!Util.equals(description, "")) { newTitle = MessageFormat 297 .format( 298 WorkbenchMessages.WorkbenchPart_AutoTitleFormat, new String [] { name, description }); 299 } 300 301 setTitle(newTitle); 302 } 303 304 309 protected void firePropertyChange(final int propertyId) { 310 for (Iterator iter = listeners.iterator(); iter.hasNext();) { 311 final ListenerRec rec = (ListenerRec) iter.next(); 312 313 Platform.run(new SafeRunnable() { 314 public void run() { 315 rec.l.propertyChanged(rec.part, propertyId); 316 } 317 }); 318 } 319 } 320 } 321 | Popular Tags |