1 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 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 58 private FactoryMap visibleContext; 59 60 private DisposeListener partDisposeListener = new DisposeListener() { 61 public void widgetDisposed(DisposeEvent e) { 62 disposed(); 63 } 64 }; 65 66 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 this.container = new SiteServices(new FactoryMap().add(overrides).mapInstance(Composite.class, control)); 92 93 control.addDisposeListener(partDisposeListener); 96 97 component = factory.createHandle(container); 98 99 Object part = component.getInstance(); 100 101 { 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 adapters = new Container(new FactoryMap().add(visibleContext).add(container)); 117 118 Object [] interfaces = Components.queryInterfaces(adapters, new Class [] { 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 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 160 public void saveState(IMemento memento) { 161 persistable.saveState(memento); 162 } 163 164 167 public Object getService(Object key) throws ComponentException { 168 169 if (key == IFocusable.class) { 170 return this; 171 } 172 173 if (visibleContext.hasService(key)) { 177 return Components.queryInterface(adapters, key); 178 } 179 return null; 180 } 181 182 185 public boolean hasService(Object key) { 186 return visibleContext.hasService(key); 187 } 188 189 public boolean setFocus() { 190 return getControl().setFocus(); 191 } 192 } 193 | Popular Tags |