KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > part > NewPartToOldWrapper


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
39  * Wraps a new-style Part in an IWorkbenchPart. This object will create and manage
40  * the lifecycle of the part. Subclasses will support wrapping a Part in an
41  * IViewPart and IEditorPart respectively. If you are interested in adapting
42  * an existing Part rather than wrapping a new one, use <code>NewPartToOldAdapter</code>
43  * instead.
44  *
45  * @since 3.1
46  */

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         /* (non-Javadoc)
62          * @see org.eclipse.ui.workbench.services.ISecondaryId#getSecondaryId()
63          */

64         public String JavaDoc getSecondaryId() {
65             return NewPartToOldWrapper.this.getSecondaryId();
66         }
67         
68         public void set(IStatus message, ImageDescriptor image) {
69             setStatus(message, image);
70         }
71         
72         /* (non-Javadoc)
73          * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
74          */

75         public Object JavaDoc getAdapter(Class JavaDoc adapter) {
76             
77             if (adapter == IPartActionBars.class) {
78                 return getPartActionBars();
79             }
80             
81 // return getViewSite().getAdapter(adapter);
82
// if (adapter == IActionBars.class) {
83
// return getViewSite().getActionBars();
84
// } else
85
if (adapter == IKeyBindingService.class) {
86                 return getSite().getKeyBindingService();
87             }
88             return null;
89         }
90
91         /* (non-Javadoc)
92          * @see org.eclipse.ui.internal.part.components.interfaces.ISelectable#setSelection(org.eclipse.jface.viewers.ISelection)
93          */

94         public void setSelection(ISelection newSelection) {
95             ISelectionProvider newSelectionProvider = null;
96             
97             if (newSelection != null) {
98                 //getSelectionProvider().setSelection(newSelection);
99
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     /* (non-Javadoc)
174      * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
175      */

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             //parent.layout(true);
189
} 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     /**
207      * @since 3.1
208      *
209      * @param args
210      * @return
211      */

212     protected abstract Part createPart(Composite parent, ServiceFactory args) throws ComponentException;
213
214     /**
215      * @since 3.1
216      *
217      * @return
218      */

219     protected abstract String JavaDoc getSecondaryId();
220
221     /* (non-Javadoc)
222      * @see org.eclipse.ui.IWorkbenchPart#setFocus()
223      */

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         // Try to be well-behaved and get the factory from our site
244
IWorkbenchPartFactory siteFactory = (IWorkbenchPartFactory) getSite().getAdapter(IWorkbenchPartFactory.class);
245         
246         // If the site doesn't want to play nicely, reach to the workbench page
247
if (siteFactory == null) {
248             return ((WorkbenchPage)getSite().getPage()).getPartFactory();
249         }
250         
251         return siteFactory;
252     }
253
254     /**
255      * Sets the part site.
256      * <p>
257      * Subclasses must invoke this method from <code>IEditorPart.init</code>
258      * and <code>IViewPart.init</code>.
259      *
260      * @param site the workbench part site
261      */

262     protected void setSite(IWorkbenchPartSite site) {
263         this.partSite = site;
264     }
265     /* (non-Javadoc)
266      * @see org.eclipse.ui.IWorkbenchPart#getSite()
267      */

268     public IWorkbenchPartSite getSite() {
269         return partSite;
270     }
271     
272     /* (non-Javadoc)
273      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
274      */

275     public Object JavaDoc getAdapter(Class JavaDoc 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