1 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 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 68 public Part createView(String 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 PartGenerator generator = new PartGenerator() { 88 public Part createPart(Composite parent, ServiceFactory context) throws ComponentException { 89 return WorkbenchPartFactory.this.createPart(descriptor.getConfigurationElement(), "class", parent, context); 91 } 92 }; 93 94 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 127 public Part createEditor(String 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 PartGenerator generator = new PartGenerator() { 148 public Part createPart(Composite parent, ServiceFactory context) throws ComponentException { 149 return WorkbenchPartFactory.this.createPart(descriptor.getConfigurationElement(), "class", parent, context); 151 } 152 }; 153 154 EditorWrapper result = new EditorWrapper(parentComposite, bundle, page, generator, context); 156 157 return result; 158 } 159 160 private Part createPart(IConfigurationElement e, 161 String attrib, Composite parentComposite, 162 ServiceFactory args) throws ComponentException { 163 164 Class extensionClass = null; 165 166 try { 167 Object 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 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 |