KickJava   Java API By Example, From Geeks To Geeks.

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


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.jface.util.Assert;
14 import org.eclipse.swt.SWTException;
15 import org.eclipse.swt.events.DisposeEvent;
16 import org.eclipse.swt.events.DisposeListener;
17 import org.eclipse.swt.layout.FillLayout;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.ui.IMemento;
21 import org.eclipse.ui.IPersistable;
22 import org.eclipse.ui.internal.components.ComponentUtil;
23 import org.eclipse.ui.internal.components.framework.ComponentException;
24 import org.eclipse.ui.internal.components.framework.ComponentFactory;
25 import org.eclipse.ui.internal.components.framework.ComponentHandle;
26 import org.eclipse.ui.internal.components.framework.Components;
27 import org.eclipse.ui.internal.components.framework.Container;
28 import org.eclipse.ui.internal.components.framework.FactoryMap;
29 import org.eclipse.ui.internal.components.framework.ServiceFactory;
30 import org.eclipse.ui.internal.part.components.interfaces.IFocusable;
31 import org.eclipse.ui.internal.part.components.services.IActionBarContributor;
32 import org.eclipse.ui.internal.part.multiplexer.SiteServices;
33
34 /**
35  * Standard implementation of a Part, built by an AbstractComponentFactory.
36  *
37  * <p>Not intended to be subclassed by clients.</p>
38  *
39  * <p>EXPERIMENTAL: The components framework is currently under active development. All
40  * aspects of this class including its existence, name, and public interface are likely
41  * to change during the development of Eclipse 3.1</p>
42  *
43  * @since 3.1
44  */

45 public final class ComponentPart extends Part implements IFocusable {
46
47     private IPersistable persistable;
48     private SiteServices container;
49     private Container adapters;
50     private SiteComposite control;
51     private ComponentHandle component;
52     private IActionBarContributor contributor = null;
53     
54     /**
55      * Context that is visible from outside the part (used as a filter -- unless
56      * a key exists in this context, it won't be returned by the part).
57      */

58     private FactoryMap visibleContext;
59         
60     private DisposeListener partDisposeListener = new DisposeListener() {
61         public void widgetDisposed(DisposeEvent e) {
62             disposed();
63         }
64     };
65     
66     /**
67      * Constructs a new Part from the given factory with the given set of services.
68      *
69      * @param parent parent composite
70      * @param overrides services available to the part
71      * @param factory factory for the main implementation of the part
72      * @throws ComponentException if unable to construct the part
73      */

74     public ComponentPart(Composite parent, ServiceFactory overrides, ComponentFactory factory) throws ComponentException {
75         Assert.isNotNull(parent);
76         Assert.isNotNull(overrides);
77         Assert.isNotNull(factory);
78         Assert.isTrue(!(parent.isDisposed()));
79
80         if (overrides == null) {
81             overrides = new FactoryMap();
82         }
83         try {
84             control = new SiteComposite(parent);
85             control.setLayout(new FillLayout());
86        
87             
88             ServiceFactory outputContext = ComponentUtil.getContext(IWorkbenchScopeConstants.PART_SCOPE);
89             
90             // create the site for this part
91
this.container = new SiteServices(new FactoryMap().add(overrides).mapInstance(Composite.class, control));
92                    
93             // Only hook the dispose listener after the container is created, since the purpose of this
94
// listener is to dispose the container when the control goes away
95
control.addDisposeListener(partDisposeListener);
96
97             component = factory.createHandle(container);
98             
99             Object JavaDoc part = component.getInstance();
100             
101             // Construct the context for publicly-visible components in this part
102
{
103                 visibleContext = new FactoryMap();
104                 
105                 visibleContext.addInstance(part);
106     
107                 ServiceFactory additionalContext = (ServiceFactory) Components.getAdapter(part, ServiceFactory.class);
108                 if (additionalContext != null) {
109                     visibleContext.add(additionalContext);
110                 }
111                 
112                 visibleContext.add(outputContext);
113             }
114             
115             // Construct the container for adapters on this part.
116
adapters = new Container(new FactoryMap().add(visibleContext).add(container));
117             
118             Object JavaDoc[] interfaces = Components.queryInterfaces(adapters, new Class JavaDoc[] {
119                     IPersistable.class
120                     });
121             persistable = (IPersistable) interfaces[0];
122             
123             control.setFocusable((IFocusable)Components.getAdapter(adapters, IFocusable.class));
124             
125             control.layout(true);
126            
127             
128         } catch (SWTException e) {
129             control.dispose();
130             throw new ComponentException(factory, e);
131         } catch (ComponentException e) {
132             control.dispose();
133             throw e;
134         }
135
136     }
137     
138     /* (non-Javadoc)
139      * @see org.eclipse.ui.internal.part.components.interfaces.IPart#getControl()
140      */

141     public Control getControl() {
142         return control;
143     }
144     
145     private void disposed() {
146         if (adapters != null) {
147             adapters.dispose();
148         }
149         if (component != null) {
150             component.getDisposable().dispose();
151         }
152         if (container != null) {
153             container.dispose();
154         }
155     }
156
157     /* (non-Javadoc)
158      * @see org.eclipse.ui.internal.part.components.interfaces.IPersistable#saveState(org.eclipse.ui.IMemento)
159      */

160     public void saveState(IMemento memento) {
161         persistable.saveState(memento);
162     }
163
164     /* (non-Javadoc)
165      * @see org.eclipse.core.components.IComponentProvider#getComponent(java.lang.Object)
166      */

167     public Object JavaDoc getService(Object JavaDoc key) throws ComponentException {
168      
169         if (key == IFocusable.class) {
170             return this;
171         }
172         
173         // Only return components that are supposed to be publicly visible from this part.
174
// The "adapters" container will also be able to find components supplied by the site
175
// and we don't want to expose those.
176
if (visibleContext.hasService(key)) {
177             return Components.queryInterface(adapters, key);
178         }
179         return null;
180     }
181
182     /* (non-Javadoc)
183      * @see org.eclipse.core.components.IComponentProvider#hasKey(java.lang.Object)
184      */

185     public boolean hasService(Object JavaDoc key) {
186         return visibleContext.hasService(key);
187     }
188     
189     public boolean setFocus() {
190         return getControl().setFocus();
191     }
192 }
193
Popular Tags