1 11 package org.eclipse.ui.internal.part; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 17 import org.eclipse.core.runtime.IAdaptable; 18 import org.eclipse.jface.resource.ImageDescriptor; 19 import org.eclipse.swt.graphics.Image; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Display; 22 import org.eclipse.ui.IMemento; 23 import org.eclipse.ui.IPersistable; 24 import org.eclipse.ui.IPropertyListener; 25 import org.eclipse.ui.IWorkbenchPart; 26 import org.eclipse.ui.IWorkbenchPart2; 27 import org.eclipse.ui.IWorkbenchPartConstants; 28 import org.eclipse.ui.internal.components.framework.Components; 29 import org.eclipse.ui.internal.components.framework.IDisposable; 30 import org.eclipse.ui.internal.part.components.interfaces.IFocusable; 31 import org.eclipse.ui.internal.part.components.services.INameable; 32 import org.eclipse.ui.internal.part.components.services.IStatusFactory; 33 34 37 public abstract class OldPartToNewWrapper implements IFocusable, IAdaptable, IDisposable, IPersistable, IPartPropertyProvider { 38 private INameable nameable; 39 private IWorkbenchPart part; 40 private IWorkbenchPart2 wb2; 41 private IStatusFactory errorContext; 42 private Image lastImage = null; 43 private IMemento savedState = null; 44 private Composite control; 45 46 private IPropertyListener propertyListener = new IPropertyListener() { 47 public void propertyChanged(Object source, int propId) { 48 OldPartToNewWrapper.this.propertyChanged(propId); 49 } 50 }; 51 52 private Map listenerMap = new HashMap (); 53 54 58 private static final class ListenerWrapper implements IPropertyListener { 59 IWorkbenchPart part; 60 IPropertyListener listener; 61 62 public ListenerWrapper(IWorkbenchPart part, IPropertyListener listener) { 63 this.part = part; 64 this.listener = listener; 65 } 66 67 70 public void propertyChanged(Object source, int propId) { 71 listener.propertyChanged(source, propId); 72 } 73 }; 74 75 public OldPartToNewWrapper(StandardWorkbenchServices services) { 76 this.control = services.getParentComposite(); 77 this.nameable = services.getNameable(); 78 this.savedState = services.getState(); 79 this.errorContext = services.getStatusFactory(); 80 } 81 82 protected void setPart(IWorkbenchPart newPart) { 83 part = newPart; 84 85 if (part instanceof IWorkbenchPart2) { 86 wb2 = (IWorkbenchPart2)part; 87 } 88 89 part.addPropertyListener(propertyListener); 90 savedState = null; 91 } 92 93 protected IStatusFactory getErrorContext() { 94 return errorContext; 95 } 96 97 protected IWorkbenchPart getPart() { 98 return part; 99 } 100 101 106 private void propertyChanged(int propId) { 107 switch(propId) { 108 case IWorkbenchPartConstants.PROP_TITLE: 109 if (part.getTitleImage() != lastImage ) { 110 lastImage = part.getTitleImage(); 111 nameable.setImage(ImageDescriptor.createFromImage(part.getTitleImage(), Display.getCurrent())); 112 } 113 nameable.setTooltip(part.getTitleToolTip()); 114 if (wb2 == null) { 115 nameable.setContentDescription(part.getTitle()); 116 } 117 break; 118 case IWorkbenchPartConstants.PROP_PART_NAME: 119 if (wb2 != null) { 120 nameable.setName(wb2.getPartName()); 121 } 122 break; 123 case IWorkbenchPartConstants.PROP_CONTENT_DESCRIPTION: 124 if (wb2 != null) { 125 nameable.setContentDescription(wb2.getContentDescription()); 126 } 127 break; 128 } 129 } 130 131 public void dispose() { 132 if (part != null) { 133 part.dispose(); 134 for (Iterator iter = listenerMap.values().iterator(); iter.hasNext();) { 135 IPropertyListener listener = (IPropertyListener) iter.next(); 136 137 part.removePropertyListener(listener); 138 } 139 listenerMap.clear(); 140 } 141 } 142 143 146 public boolean setFocus() { 147 if (part != null) { 148 part.setFocus(); 149 return true; 150 } 151 152 return false; 153 } 154 155 158 public Object getAdapter(Class adapter) { 159 if (adapter.isInstance(this)) { 160 return this; 161 } 162 163 return Components.getAdapter(part, adapter); 164 } 165 166 169 public void saveState(IMemento memento) { 170 if (part != null) { 171 if (part instanceof IPersistable) { 172 IPersistable persistable = (IPersistable) part; 173 persistable.saveState(memento); 174 } 175 } else if (savedState != null) { 176 memento.putMemento(savedState); 177 } 178 } 179 180 183 public void addPropertyListener(IWorkbenchPart part, IPropertyListener l) { 184 if (part != null) { 185 IPropertyListener wrapper = new ListenerWrapper(part, l); 186 part.addPropertyListener(wrapper); 187 188 listenerMap.put(l, wrapper); 189 } 190 } 191 192 195 public void removePropertyListener(IWorkbenchPart part, IPropertyListener l) { 196 if (part != null) { 197 IPropertyListener wrapper = (IPropertyListener)listenerMap.get(l); 198 199 if (wrapper != null) { 200 part.removePropertyListener(wrapper); 201 listenerMap.remove(l); 202 } 203 } 204 } 205 206 209 public String getTitleToolTip() { 210 return part.getTitleToolTip(); 211 } 212 213 216 public Image getTitleImage() { 217 return part.getTitleImage(); 218 } 219 220 223 public String getTitle() { 224 return part.getTitle(); 225 } 226 } 227 | Popular Tags |