1 11 package org.eclipse.ui.internal.presentations; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.ListenerList; 17 import org.eclipse.jface.util.IPropertyChangeListener; 18 import org.eclipse.jface.util.PropertyChangeEvent; 19 import org.eclipse.swt.graphics.Image; 20 import org.eclipse.swt.graphics.Point; 21 import org.eclipse.swt.graphics.Rectangle; 22 import org.eclipse.swt.widgets.Composite; 23 import org.eclipse.swt.widgets.Control; 24 import org.eclipse.ui.IPropertyListener; 25 import org.eclipse.ui.ISharedImages; 26 import org.eclipse.ui.PlatformUI; 27 import org.eclipse.ui.internal.PartPane; 28 import org.eclipse.ui.internal.WorkbenchPartReference; 29 import org.eclipse.ui.internal.dnd.SwtUtil; 30 import org.eclipse.ui.presentations.IPartMenu; 31 import org.eclipse.ui.presentations.IPresentablePart; 32 33 40 public class PresentablePart implements IPresentablePart { 41 42 private PartPane part; 43 44 48 private List listeners = new ArrayList (); 49 50 private IPropertyListener lazyPropertyListenerProxy; 52 53 private ListenerList partPropertyChangeListeners = new ListenerList(); 54 55 private IPropertyChangeListener lazyPartPropertyChangeListener; 56 57 private IPartMenu viewMenu; 59 60 private boolean enableInputs = true; 63 64 private boolean enableOutputs = true; 67 private Rectangle savedBounds = new Rectangle(0,0,0,0); 68 private boolean isVisible = false; 69 70 private String name = ""; private String titleStatus = ""; private boolean isDirty = false; 74 private boolean isBusy = false; 75 private boolean hasViewMenu = false; 76 77 82 public PresentablePart(PartPane part, Composite parent) { 83 this.part = part; 84 getPane().addPropertyListener(getPropertyListenerProxy()); 85 getPane().addPartPropertyListener(getPartPropertyListenerProxy()); 86 } 87 88 public PartPane getPane() { 89 return part; 90 } 91 92 private IPropertyListener getPropertyListenerProxy() { 93 if (lazyPropertyListenerProxy == null) { 94 lazyPropertyListenerProxy = new IPropertyListener() { 95 public void propertyChanged(Object source, int propId) { 96 firePropertyChange(propId); 97 } 98 }; 99 } 100 101 return lazyPropertyListenerProxy; 102 } 103 104 private IPropertyChangeListener getPartPropertyListenerProxy() { 105 if (lazyPartPropertyChangeListener == null) { 106 lazyPartPropertyChangeListener = new IPropertyChangeListener() { 107 public void propertyChange(PropertyChangeEvent event) { 108 PropertyChangeEvent e = new PropertyChangeEvent(this, 109 event.getProperty(), event.getOldValue(), event.getNewValue()); 110 firePartPropertyChange(e); 111 } 112 }; 113 } 114 return lazyPartPropertyChangeListener; 115 } 116 117 121 public void dispose() { 122 getPane().removePropertyListener(getPropertyListenerProxy()); 124 getPane().removePartPropertyListener(getPartPropertyListenerProxy()); 125 126 part = null; 128 listeners.clear(); 129 listeners = null; 130 partPropertyChangeListeners.clear(); 131 partPropertyChangeListeners = null; 132 } 133 134 public void firePropertyChange(int propertyId) { 135 for (int i = 0; i < listeners.size(); i++) { 136 ((IPropertyListener) listeners.get(i)).propertyChanged(this, propertyId); 137 } 138 } 139 140 public void addPropertyListener(final IPropertyListener listener) { 141 listeners.add(listener); 142 } 143 144 public void removePropertyListener(final IPropertyListener listener) { 145 listeners.remove(listener); 146 } 147 148 protected void firePartPropertyChange(PropertyChangeEvent event) { 149 Object [] l = partPropertyChangeListeners.getListeners(); 150 for (int i = 0; i < l.length; i++) { 151 ((IPropertyChangeListener) l[i]).propertyChange(event); 152 } 153 } 154 155 public void addPartPropertyListener(IPropertyChangeListener listener) { 156 partPropertyChangeListeners.add(listener); 157 } 158 159 public void removePartPropertyListener(IPropertyChangeListener listener) { 160 partPropertyChangeListeners.remove(listener); 161 } 162 163 166 public void setBounds(Rectangle bounds) { 167 savedBounds = bounds; 168 if (enableInputs && !SwtUtil.isDisposed(part.getControl())) { 169 part.setBounds(bounds); 170 } 171 } 172 173 176 public void setVisible(boolean isVisible) { 177 this.isVisible = isVisible; 178 if (enableInputs) { 179 part.setVisible(isVisible); 180 } 181 } 182 183 186 public void setFocus() { 187 if (!SwtUtil.isDisposed(part.getControl())) { 188 if (part.getPage().getActivePart() == part.getPartReference().getPart(false)) { 189 part.setFocus(); 190 } else { 191 part.requestActivation(); 192 } 193 } 194 } 195 196 197 private WorkbenchPartReference getPartReference() { 198 return (WorkbenchPartReference) part.getPartReference(); 199 } 200 201 202 205 public String getName() { 206 if (enableOutputs) { 207 return getPartReference().getPartName(); 208 } 209 return name; 210 } 211 212 215 public String getTitle() { 216 return getPartReference().getTitle(); 217 } 218 219 222 public String getTitleStatus() { 223 if (enableOutputs) { 224 return getPartReference().getContentDescription(); 225 } 226 227 return titleStatus; 228 } 229 230 235 public Image getTitleImage() { 236 if (enableOutputs) { 241 return getPartReference().getTitleImage(); 242 } 243 244 return PlatformUI.getWorkbench().getSharedImages().getImage( 245 ISharedImages.IMG_DEF_VIEW); 246 } 247 248 253 public String getTitleToolTip() { 254 return getPartReference().getTitleToolTip(); 255 } 256 257 260 public boolean isDirty() { 261 if (enableOutputs) { 262 return getPartReference().isDirty(); 263 } 264 return isDirty; 265 } 266 267 272 public boolean isBusy() { 273 if (enableOutputs) { 274 return part.isBusy(); 275 } 276 return isBusy; 277 } 278 279 284 public Control getToolBar() { 285 if (enableOutputs) { 286 return getPane().getToolBar(); 287 } 288 return null; 289 } 290 291 296 public IPartMenu getMenu() { 297 boolean hasMenu; 298 299 if (enableOutputs) { 300 hasMenu = part.hasViewMenu(); 301 } else { 302 hasMenu = this.hasViewMenu; 303 } 304 305 if (!hasMenu) { 306 return null; 307 } 308 309 if (viewMenu == null) { 310 viewMenu = new IPartMenu() { 311 public void showMenu(Point location) { 312 part.showViewMenu(location); 313 } 314 }; 315 } 316 317 return viewMenu; 318 } 319 320 323 public boolean isCloseable() { 324 return part.isCloseable(); 325 } 326 327 330 public Control getControl() { 331 return part.getControl(); 332 } 333 334 public void enableOutputs(boolean isActive) { 335 if (isActive == this.enableOutputs) { 336 return; 337 } 338 339 this.enableOutputs = isActive; 340 341 if (isActive) { 342 if (isBusy != getPane().isBusy()) { 343 firePropertyChange(PROP_BUSY); 344 } 345 if (isDirty != isDirty()) { 346 firePropertyChange(PROP_DIRTY); 347 } 348 if (!name.equals(getName())) { 349 firePropertyChange(PROP_PART_NAME); 350 } 351 if (!titleStatus.equals(getTitleStatus())) { 352 firePropertyChange(PROP_CONTENT_DESCRIPTION); 353 } 354 if (hasViewMenu != getPane().hasViewMenu()) { 355 firePropertyChange(PROP_PANE_MENU); 356 } 357 firePropertyChange(PROP_TOOLBAR); 360 firePropertyChange(PROP_TITLE); 361 362 getPane().addPropertyListener(getPropertyListenerProxy()); 363 } else { 364 getPane().removePropertyListener(getPropertyListenerProxy()); 365 366 WorkbenchPartReference ref = getPartReference(); 367 isBusy = getPane().isBusy(); 368 isDirty = ref.isDirty(); 369 name = ref.getPartName(); 370 titleStatus = ref.getContentDescription(); 371 hasViewMenu = getPane().hasViewMenu(); 372 firePropertyChange(PROP_TITLE); 373 firePropertyChange(PROP_TOOLBAR); 374 } 375 } 376 377 public void enableInputs(boolean isActive) { 378 if (isActive == this.enableInputs) { 379 return; 380 } 381 382 this.enableInputs = isActive; 383 384 if (isActive) { 385 if (isActive && !SwtUtil.isDisposed(part.getControl())) { 386 part.setBounds(savedBounds); 387 } 388 389 part.setVisible(isVisible); 390 } 391 } 392 393 396 public String getPartProperty(String key) { 397 return getPartReference().getPartProperty(key); 398 } 399 400 } 401 | Popular Tags |