KickJava   Java API By Example, From Geeks To Geeks.

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


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.swt.events.DisposeEvent;
14 import org.eclipse.swt.events.DisposeListener;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Control;
17 import org.eclipse.ui.IMemento;
18 import org.eclipse.ui.IWorkbenchPage;
19 import org.eclipse.ui.internal.components.framework.ComponentException;
20 import org.eclipse.ui.internal.components.framework.FactoryMap;
21 import org.eclipse.ui.internal.components.framework.IServiceProvider;
22 import org.eclipse.ui.internal.components.framework.ServiceFactory;
23 import org.eclipse.ui.internal.part.multiplexer.SiteServices;
24 import org.osgi.framework.Bundle;
25
26 /**
27  * Wraps a Part in a form that can be adapted to IViewPart and IEditorPart
28  *
29  * Note: this is a bit of a hack. This should really be done using a custom
30  * component factory and a raw ComponentPart. The current implementation
31  * means that we need to create a Site before the part's composite, which
32  * means that the Site in a PartWrapper points to the wrong composite.
33  * This currently isn't causing problems since the site is only needed
34  * for one component (the IPropertyProvider) and it doesn't use a composite.
35  * The rest of a part's dependencies get filled by the container in the ComponentPart,
36  * which knows the correct composite.
37  */

38 public abstract class PartWrapper extends Part {
39
40     private Part wrappedPart;
41     private SiteServices container;
42     
43     public PartWrapper(Composite parentControl, Bundle bundle, IWorkbenchPage page, PartGenerator gen, ServiceFactory context) throws ComponentException {
44         
45         container = new SiteServices(parentControl, bundle, page, context);
46         IPartPropertyProvider provider;
47
48         try {
49             provider = (IPartPropertyProvider)container.getService(IPartPropertyProvider.class);
50         
51             FactoryMap childContext = new FactoryMap()
52                 .addInstance(provider)
53                 .add(container);
54             
55             wrappedPart = gen.createPart(parentControl, childContext);
56             wrappedPart.getControl().addDisposeListener(new DisposeListener() {
57                 public void widgetDisposed(DisposeEvent e) {
58                     disposed();
59                 }
60             });
61             
62         } catch (ComponentException e1) {
63             container.dispose();
64             throw e1;
65         }
66     }
67     
68     protected Part getWrappedPart() {
69         return wrappedPart;
70     }
71     
72     protected IServiceProvider getContainer() {
73         return container;
74     }
75     
76     /* (non-Javadoc)
77      * @see org.eclipse.ui.part.Part#getControl()
78      */

79     public Control getControl() {
80         return wrappedPart.getControl();
81     }
82
83     /* (non-Javadoc)
84      * @see org.eclipse.ui.internal.part.components.interfaces.IPersistable#saveState(org.eclipse.ui.IMemento)
85      */

86     public void saveState(IMemento memento) {
87         wrappedPart.saveState(memento);
88     }
89
90     private void disposed() {
91         container.dispose();
92     }
93     
94     /* (non-Javadoc)
95      * @see org.eclipse.core.components.IComponentProvider#getComponent(java.lang.Object)
96      */

97     public Object JavaDoc getService(Object JavaDoc key) throws ComponentException {
98         return wrappedPart.getService(key);
99     }
100     
101     /* (non-Javadoc)
102      * @see org.eclipse.core.components.IComponentProvider#hasKey(java.lang.Object)
103      */

104     public boolean hasService(Object JavaDoc key) {
105         return wrappedPart.hasService(key);
106     }
107 }
108
Popular Tags