1 11 package org.eclipse.ui.internal.part; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 import org.eclipse.core.runtime.IStatus; 15 import org.eclipse.jface.action.IStatusLineManager; 16 import org.eclipse.jface.resource.ImageDescriptor; 17 import org.eclipse.jface.resource.ResourceManager; 18 import org.eclipse.jface.viewers.ISelection; 19 import org.eclipse.jface.viewers.ISelectionProvider; 20 import org.eclipse.swt.graphics.Image; 21 import org.eclipse.swt.layout.FillLayout; 22 import org.eclipse.swt.widgets.Composite; 23 import org.eclipse.ui.IKeyBindingService; 24 import org.eclipse.ui.IMemento; 25 import org.eclipse.ui.IWorkbenchPartSite; 26 import org.eclipse.ui.internal.WorkbenchPage; 27 import org.eclipse.ui.internal.WorkbenchPlugin; 28 import org.eclipse.ui.internal.components.framework.ComponentException; 29 import org.eclipse.ui.internal.components.framework.Components; 30 import org.eclipse.ui.internal.components.framework.FactoryMap; 31 import org.eclipse.ui.internal.components.framework.ServiceFactory; 32 import org.eclipse.ui.internal.part.components.services.IPartActionBars; 33 import org.eclipse.ui.internal.part.components.services.ISecondaryId; 34 import org.eclipse.ui.internal.part.components.services.ISelectionHandler; 35 import org.eclipse.ui.internal.part.components.services.IStatusHandler; 36 import org.eclipse.ui.internal.part.components.services.IWorkbenchPartFactory; 37 38 47 abstract class NewPartToOldWrapper extends NewPartToWorkbenchPartAdapter { 48 49 private Part part = null; 50 private SelectionProviderAdapter selectionProvider; 51 52 private PartServices services; 53 private IWorkbenchPartSite partSite; 54 private IPartActionBars partActionBars = null; 55 private ResourceManager rm; 56 private ImageDescriptor currentStatusImageDescriptor = null; 57 private Image currentStatusImage = null; 58 59 private final class PartServices implements ISecondaryId, IAdaptable, ISelectionHandler, IStatusHandler { 60 61 64 public String getSecondaryId() { 65 return NewPartToOldWrapper.this.getSecondaryId(); 66 } 67 68 public void set(IStatus message, ImageDescriptor image) { 69 setStatus(message, image); 70 } 71 72 75 public Object getAdapter(Class adapter) { 76 77 if (adapter == IPartActionBars.class) { 78 return getPartActionBars(); 79 } 80 81 if (adapter == IKeyBindingService.class) { 86 return getSite().getKeyBindingService(); 87 } 88 return null; 89 } 90 91 94 public void setSelection(ISelection newSelection) { 95 ISelectionProvider newSelectionProvider = null; 96 97 if (newSelection != null) { 98 newSelectionProvider = getSelectionProvider(); 100 newSelectionProvider.setSelection(newSelection); 101 } 102 103 if (getSite().getSelectionProvider() != newSelectionProvider) { 104 getSite().setSelectionProvider(newSelectionProvider); 105 } 106 } 107 108 }; 109 110 public NewPartToOldWrapper(IPartPropertyProvider provider) { 111 super(provider); 112 113 this.services = new PartServices(); 114 } 115 116 public void setStatus(IStatus message, ImageDescriptor image) { 117 IStatusLineManager mgr = getStatusLineManager(); 118 119 Image newImage = null; 120 121 if (image != null) { 122 newImage = getResources().createImageWithDefault(image); 123 } 124 125 if (message == null) { 126 mgr.setErrorMessage(null); 127 mgr.setMessage(null); 128 } else if (message.getSeverity() == IStatus.ERROR){ 129 mgr.setMessage(null); 130 131 if (newImage == null) { 132 mgr.setErrorMessage(message.getMessage()); 133 } else { 134 mgr.setErrorMessage(newImage, message.getMessage()); 135 } 136 } else { 137 mgr.setErrorMessage(null); 138 139 if (newImage == null) { 140 mgr.setMessage(message.getMessage()); 141 } else { 142 mgr.setMessage(newImage, message.getMessage()); 143 } 144 } 145 146 disposeStatusImage(); 147 currentStatusImage = newImage; 148 currentStatusImageDescriptor = image; 149 150 } 151 152 private void disposeStatusImage() { 153 if (currentStatusImageDescriptor != null) { 154 getResources().destroy(currentStatusImageDescriptor); 155 currentStatusImage = null; 156 currentStatusImageDescriptor = null; 157 } 158 } 159 160 protected abstract IStatusLineManager getStatusLineManager(); 161 162 public IPartActionBars getPartActionBars() { 163 if (partActionBars == null) { 164 partActionBars = createPartActionBars(); 165 } 166 return partActionBars ; 167 } 168 169 protected abstract IPartActionBars createPartActionBars(); 170 171 protected abstract IMemento getMemento(); 172 173 176 public void createPartControl(Composite parent) { 177 try { 178 179 180 FactoryMap args = new FactoryMap(); 181 args.addInstance(services); 182 args.addInstance(getPropertyProvider()); 183 addServices(args); 184 args.mapInstance(Composite.class, parent); 185 parent.setLayout(new FillLayout()); 186 187 part = createPart(parent, args); 188 } catch (ComponentException e) { 190 WorkbenchPlugin.getDefault().getLog().log(e.getStatus()); 191 new StatusPart(parent, e.getStatus()); 192 } 193 } 194 195 private ResourceManager getResources() { 196 if (rm == null) { 197 rm = (ResourceManager)getSite().getAdapter(ResourceManager.class); 198 } 199 return rm; 200 } 201 202 protected void addServices(FactoryMap context) { 203 context.mapInstance(IPartPropertyProvider.class, getPropertyProvider()); 204 } 205 206 212 protected abstract Part createPart(Composite parent, ServiceFactory args) throws ComponentException; 213 214 219 protected abstract String getSecondaryId(); 220 221 224 public void setFocus() { 225 if (part != null) { 226 part.getControl().setFocus(); 227 } 228 } 229 230 private SelectionProviderAdapter getSelectionProvider() { 231 if (selectionProvider == null) { 232 selectionProvider = new SelectionProviderAdapter(); 233 getSite().setSelectionProvider(selectionProvider); 234 } 235 return selectionProvider; 236 } 237 238 protected Part getPart() { 239 return part; 240 } 241 242 protected IWorkbenchPartFactory getFactory() { 243 IWorkbenchPartFactory siteFactory = (IWorkbenchPartFactory) getSite().getAdapter(IWorkbenchPartFactory.class); 245 246 if (siteFactory == null) { 248 return ((WorkbenchPage)getSite().getPage()).getPartFactory(); 249 } 250 251 return siteFactory; 252 } 253 254 262 protected void setSite(IWorkbenchPartSite site) { 263 this.partSite = site; 264 } 265 268 public IWorkbenchPartSite getSite() { 269 return partSite; 270 } 271 272 275 public Object getAdapter(Class adapter) { 276 if (adapter.isInstance(this)) { 277 return this; 278 } 279 280 if (part != null) { 281 return Components.getAdapter(part, adapter); 282 } 283 284 return null; 285 } 286 287 public void dispose() { 288 disposeStatusImage(); 289 290 super.dispose(); 291 } 292 293 } 294 295 296 | Popular Tags |