KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > part > services > WorkbenchPartFactory


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.services;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IConfigurationElement;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.ui.IEditorInput;
20 import org.eclipse.ui.IEditorPart;
21 import org.eclipse.ui.IKeyBindingService;
22 import org.eclipse.ui.IMemento;
23 import org.eclipse.ui.IViewPart;
24 import org.eclipse.ui.IWorkbenchPage;
25 import org.eclipse.ui.internal.ActionDescriptor;
26 import org.eclipse.ui.internal.ViewActionBuilder;
27 import org.eclipse.ui.internal.WorkbenchMessages;
28 import org.eclipse.ui.internal.WorkbenchPlugin;
29 import org.eclipse.ui.internal.components.framework.ComponentException;
30 import org.eclipse.ui.internal.components.framework.ComponentFactory;
31 import org.eclipse.ui.internal.components.framework.FactoryMap;
32 import org.eclipse.ui.internal.components.framework.ServiceFactory;
33 import org.eclipse.ui.internal.part.ComponentPart;
34 import org.eclipse.ui.internal.part.EditorWrapper;
35 import org.eclipse.ui.internal.part.OldEditorToNewWrapperFactory;
36 import org.eclipse.ui.internal.part.OldViewToNewWrapperFactory;
37 import org.eclipse.ui.internal.part.Part;
38 import org.eclipse.ui.internal.part.PartGenerator;
39 import org.eclipse.ui.internal.part.ViewWrapper;
40 import org.eclipse.ui.internal.part.components.services.IPartDescriptor;
41 import org.eclipse.ui.internal.part.components.services.ISavedState;
42 import org.eclipse.ui.internal.part.components.services.IWorkbenchPartFactory;
43 import org.eclipse.ui.internal.registry.EditorDescriptor;
44 import org.eclipse.ui.internal.registry.EditorRegistry;
45 import org.eclipse.ui.internal.registry.ViewDescriptor;
46 import org.eclipse.ui.views.IViewRegistry;
47 import org.osgi.framework.Bundle;
48
49 /**
50  * This class can be used to construct views and editors inside arbitrary user
51  * composites.
52  *
53  * @since 3.1
54  */

55 public class WorkbenchPartFactory implements IWorkbenchPartFactory {
56
57     private IWorkbenchPage page;
58     private OldEditorToNewWrapperFactory oldEditorFactory = new OldEditorToNewWrapperFactory();
59     private OldViewToNewWrapperFactory oldViewFactory = new OldViewToNewWrapperFactory();
60         
61     public WorkbenchPartFactory(IWorkbenchPage page) {
62         this.page = page;
63     }
64     
65     /* (non-Javadoc)
66      * @see org.eclipse.ui.internal.part.components.interfaces.IPartFactory#createView(java.lang.String, org.eclipse.swt.widgets.Composite, org.eclipse.ui.IMemento, org.eclipse.core.component.IComponentArguments)
67      */

68     public Part createView(String JavaDoc viewId, Composite parentComposite,
69             final IMemento savedState, ServiceFactory args) throws ComponentException {
70         
71         IViewRegistry viewRegistry = WorkbenchPlugin.getDefault().getViewRegistry();
72         
73         final ViewDescriptor descriptor = (ViewDescriptor)viewRegistry.find(viewId);
74         
75         Bundle bundle = Platform.getBundle(descriptor.getConfigurationElement().getNamespace());
76         
77         FactoryMap context = new FactoryMap()
78             .mapInstance(IPartDescriptor.class, descriptor.getPartDescriptor())
79             .add(args);
80     
81         if (savedState != null) {
82             context.mapInstance(ISavedState.class, new SavedState(savedState));
83         }
84         
85         // Factory for the real part (the part returned here might not be adaptable to IViewPart, so
86
// we need to wrap it)
87
PartGenerator generator = new PartGenerator() {
88             public Part createPart(Composite parent, ServiceFactory context) throws ComponentException {
89                 return WorkbenchPartFactory.this.createPart(descriptor.getConfigurationElement(), "class", //$NON-NLS-1$
90
parent, context);
91             }
92         };
93         
94         // Create a wrapper for the part -- the wrapper makes it possible to adapt it to an IViewPart
95
ViewWrapper result = new ViewWrapper(parentComposite, bundle, page, generator, context);
96         
97         IViewPart viewPart = result.getViewPart();
98         
99         ViewActionBuilder builder = new ViewActionBuilder();
100         builder.readActionExtensions(viewPart);
101         
102         IKeyBindingService keyBindingService = viewPart.getSite().getKeyBindingService();
103         
104         ActionDescriptor[] actionDescriptors = builder.getExtendedActions();
105         
106         if (actionDescriptors != null) {
107             for (int i = 0; i < actionDescriptors.length; i++) {
108                 ActionDescriptor actionDescriptor = actionDescriptors[i];
109     
110                 if (actionDescriptor != null) {
111                     IAction action = actionDescriptors[i]
112                             .getAction();
113     
114                     if (action != null
115                             && action.getActionDefinitionId() != null)
116                         keyBindingService.registerAction(action);
117                 }
118             }
119         }
120         
121         return result;
122     }
123
124     /* (non-Javadoc)
125      * @see org.eclipse.ui.internal.part.components.interfaces.IPartFactory#createEditor(java.lang.String, org.eclipse.swt.widgets.Composite, org.eclipse.ui.IMemento, org.eclipse.core.component.IComponentArguments, org.eclipse.ui.IEditorInput)
126      */

127     public Part createEditor(String JavaDoc editorId, Composite parentComposite,
128             IEditorInput input, IMemento savedState, ServiceFactory args) throws ComponentException {
129         
130         EditorRegistry editorRegistry = (EditorRegistry)WorkbenchPlugin.getDefault().getEditorRegistry();
131         
132         final EditorDescriptor descriptor = (EditorDescriptor)editorRegistry.findEditor(editorId);
133         
134         Bundle bundle = Platform.getBundle(descriptor.getConfigurationElement().getNamespace());
135         
136         FactoryMap context = new FactoryMap()
137             .mapInstance(IPartDescriptor.class, descriptor.getPartDescriptor())
138             .mapInstance(IEditorInput.class, input)
139             .add(args);
140     
141         if (savedState != null) {
142             context.mapInstance(ISavedState.class, new SavedState(savedState));
143         }
144         
145         // Factory for the real part (the part returned here might not be adaptable to IViewPart, so
146
// we need to wrap it)
147
PartGenerator generator = new PartGenerator() {
148             public Part createPart(Composite parent, ServiceFactory context) throws ComponentException {
149                 return WorkbenchPartFactory.this.createPart(descriptor.getConfigurationElement(), "class", //$NON-NLS-1$
150
parent, context);
151             }
152         };
153         
154         // Create a wrapper for the part -- the wrapper makes it possible to adapt it to an IEditorPart
155
EditorWrapper result = new EditorWrapper(parentComposite, bundle, page, generator, context);
156         
157         return result;
158     }
159     
160     private Part createPart(IConfigurationElement e,
161             String JavaDoc attrib, Composite parentComposite,
162             ServiceFactory args) throws ComponentException {
163
164         Class JavaDoc extensionClass = null;
165         
166         try {
167             Object JavaDoc partOrFactory = e.createExecutableExtension(attrib);
168             
169             if (partOrFactory instanceof IEditorPart) {
170                 return new ComponentPart(parentComposite, new FactoryMap()
171                         .add(args).mapInstance(IEditorPart.class, partOrFactory),
172                         oldEditorFactory);
173             } else if (partOrFactory instanceof IViewPart) {
174                 IViewPart part = (IViewPart) e.createExecutableExtension(attrib);
175                 
176                 return new ComponentPart(parentComposite, new FactoryMap()
177                         .add(args).mapInstance(IViewPart.class, part),
178                         oldViewFactory);
179             } else if (partOrFactory instanceof ComponentFactory) {
180                 return new ComponentPart(parentComposite, args, (ComponentFactory)partOrFactory);
181             }
182             
183             String JavaDoc msg = NLS.bind(WorkbenchMessages.PartFactory_wrongtype,partOrFactory.getClass().getName());
184             
185             throw new ComponentException(msg, null);
186             
187         } catch (CoreException ex) {
188             throw new ComponentException(extensionClass != null ? extensionClass : getClass(), ex);
189         }
190     }
191 }
192
Popular Tags