| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> |
| <HEAD> |
| |
| <meta name="copyright" content="Copyright (c) IBM Corporation and others 2000, 2005. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." > |
| |
| <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"> |
| <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"> |
| |
| <LINK REL="STYLESHEET" HREF="../book.css" CHARSET="ISO-8859-1" TYPE="text/css"> |
| <title>Passing arguments to a part</title> |
| |
| </HEAD> |
| <BODY BGCOLOR="#ffffff"> |
| <h2> Passing arguments to a part</h2> |
| A parent passes arguments to its children using a ContainerContext. The context |
| can pass arguments to the constructor of a part or specify a factory for constructing |
| those arguments. For example, a particular ContainerContext might specify rules |
| like:<br> |
| <ul> |
| <li>If the child asks for an INameable, use object X; </li> |
| <li>If the child asks for an IActionBars, construct one using factory Y;</li> |
| <li>If the child asks for any unknown type and object Z can be adapted to that |
| type, return the adapter; </li> |
| <li>If the child asks for a dependency that can't be satisfied in any other |
| way, redirect to some other context.</li> |
| </ul> |
| If the context doesn't supply a particular dependency, the child will use the |
| default implementation from the <span |
| style="font-style: italic;">org.eclipse.core.component.types</span> extension |
| point. Since the context can override any of the arguments a part gets in its |
| constructor, the parent can override any of the interfaces the part normally obtains |
| from its site. For example, the parent could force its child to use a different |
| implementation of IActionBars by supplying an IActionBars in the context.<br> |
| <p>The parent has several options for constructing the context:</p> |
| <ul> |
| <li>Provide the child with a specific instance for one of its dependencies;</li> |
| <li>Provide the child with a factory to construct one of its dependencies, if |
| needed; </li> |
| <li>Redirect some or all of its own dependencies to one of its children; </li> |
| <li>Use a multiplexer to dynamically redirect one or more dependencies to its |
| active child; </li> |
| <li>Let the child use the default implementation for some or all of its dependencies. |
| </li> |
| </ul> |
| The following <span style="font-weight: bold;">DefaultContextView</span>, <span style="font-weight: bold;">RedirectContextView</span> |
| and <span style="font-weight: bold;">OverrideInstanceView</span> examples demonstrate |
| each possibility. |
| <h3> Default context</h3> |
| The following example shows the source for a view that creates two nested children |
| in the default context. The fact that the children are created in the default |
| context means that the parent doesn't care about the child's name, toolbar, selection, |
| and so on and isn't prepared to deal with them. If the parent wanted to do something |
| with, say, the current selection in one of its children, it would have needed |
| to pass an ISelectionHandler to that child. The end result is shown below the |
| example.<br> |
| <br> |
| <span style="font-weight: bold;"> </span> |
| <div style="margin-left: 40px;"><code>/**</code><br> |
| <code> * View that demonstrates how to create two nested children </code><br> |
| <code> * with the default context.</code><br> |
| <code> */</code><br> |
| <code>public class DefaultContextView {</code><br> |
| <code> public DefaultContextView(Composite parent, IPartFactory |
| factory) throws CoreException {</code><br> |
| <code> // Create a resource navigator</code><br> |
| <code> ContainerContext viewContext1 |
| = new ContainerContext(); </code><br> |
| <code> ISite view1 = factory.createView(</code><br> |
| <code> |
| IPageLayout.ID_RES_NAV, parent, viewContext1, null);</code><br> |
| <code> </code><br> |
| <code> // Create property view</code><br> |
| <code> ContainerContext viewContext2 |
| = new ContainerContext();</code><br> |
| <code> ISite view2 = factory.createView(IPageLayout.ID_PROP_SHEET, |
| parent, viewContext2, null);</code><br> |
| <code> </code><br> |
| <code> parent.setLayout(new FillLayout()); |
| </code><br> |
| <code> }</code><br> |
| <code>}</code><br> |
| </div> |
| <span style="font-weight: bold;"> <br> |
| </span> |
| <div style="margin-left: 40px;"><img src="images/DefaultContextView.PNG" alt="Screenshot of DefaultContextView"><br> |
| </div> |
| <br> |
| <h3>Redirecting dependencies from a parent to a child<br> |
| </h3> |
| This example demonstrates how to redirect |
| a site interface from the parent to a child. This composite view contains a resource |
| navigator and a property view. It redirects its selection handler to the resource |
| navigator and it redirects its action bars to the properties view. The result |
| is a view that provides a resource selection and contains the toolbar and menu |
| from the properties view, as shown below. <br> |
| <span style="font-weight: bold;"><br> |
| </span> |
| <div style="margin-left: 40px;"><code>public class RedirectContextView {</code><br> |
| <code> /**</code><br> |
| <code> * Component constructor. Do not invoke directly.</code><br> |
| <code> */</code><br> |
| <code> public RedirectContextView(Composite parent, IPartFactory |
| factory, ISelectionHandler selection, IActionBars actionBars) throws CoreException |
| {</code><br> |
| <code> // Create a resource navigator. |
| Redirect the navigator's selection directly to our parent.</code><br> |
| <code> ContainerContext viewContext1 |
| = new ContainerContext()</code><br> |
| <code> .addInstance(ISelectionHandler.class, |
| selection); </code><br> |
| <code> ISite view1 = factory.createView(</code><br> |
| <code> |
| IPageLayout.ID_RES_NAV, </code><br> |
| <code> |
| parent, viewContext1, null);</code><br> |
| <code> </code><br> |
| <code> // Create property view. Allow |
| the property view to use our action bars directly.</code><br> |
| <code> ContainerContext viewContext2 |
| = new ContainerContext()</code><br> |
| <code> .addInstance(IActionBars.class, |
| actionBars);</code><br> |
| <code> ISite view2 = factory.createView(IPageLayout.ID_PROP_SHEET, |
| parent, viewContext2, null);</code><br> |
| <code> </code><br> |
| <code> parent.setLayout(new FillLayout()); |
| </code><br> |
| <code> }</code><br> |
| <code>}</code><br> |
| </div> |
| <br> |
| <br> |
| <div style="margin-left: 40px;"><img src="images/RedirectContextView.PNG" alt="Screenshot of RedirectContextView"><br> |
| </div> |
| <h3>Providing dependencies directly<br> |
| </h3> |
| This example demonstrates how to supply a child with one of its dependencies directly. |
| In this example, we create a composite view containing a problems view and a properties |
| view. We supply the problem view with an ISelectionHandler in order to display |
| the number of selected problems in the content description. The end result is |
| shown below.<br> |
| <br> |
| <span style="font-weight: bold;"> </span> |
| <div style="margin-left: 40px;"><code>public class OverrideInstanceView {<br> |
| <br> |
| /**<br> |
| * Component constructor. Do not invoke directly.<br> |
| */<br> |
| public OverrideInstanceView(Composite parent, IPartFactory |
| factory, final INameable name) throws CoreException {<br> |
| ContainerContext viewContext1 = new |
| ContainerContext();<br> |
| <br> |
| // Add an ISelectionHandler to the |
| view's context. Whenever the view changes its selection,<br> |
| // display the number of selected |
| items in the content description<br> |
| viewContext1.addInstance(ISelectionHandler.class, |
| new ISelectionHandler() {<br> |
| /* (non-Javadoc)<br> |
| * @see |
| org.eclipse.ui.part.services.ISelectionHandler#setSelection(org.eclipse.jface.viewers.ISelection)<br> |
| */<br> |
| public void |
| setSelection(ISelection newSelection) {<br> |
| |
| if (newSelection instanceof IStructuredSelection) {<br> |
| |
| IStructuredSelection sel = (IStructuredSelection)newSelection;<br> |
| |
| int selectionSize = sel.size();<br> |
| |
| <br> |
| |
| name.setContentDescription(MessageFormat.format("{0} problems selected", <br> |
| |
| new String[] {Integer.toString(selectionSize)}));<br> |
| |
| }<br> |
| }<br> |
| });<br> |
| <br> |
| // Create a problem view<br> |
| ISite view1 = factory.createView(<br> |
| |
| IPageLayout.ID_PROBLEM_VIEW, parent, viewContext1, null);<br> |
| <br> |
| // Create property view<br> |
| ContainerContext viewContext2 = new |
| ContainerContext();<br> |
| ISite view2 = factory.createView(IPageLayout.ID_PROP_SHEET, |
| parent, viewContext2, null);<br> |
| <br> |
| parent.setLayout(new FillLayout());<br> |
| }<br> |
| }</code><br> |
| </div> |
| <br> |
| <span style="font-weight: bold;"> <br> |
| </span> |
| <div style="margin-left: 40px;"><img src="images/OverrideInstanceView.PNG" alt="Screenshot of OverrideInstanceView"><br> |
| <br> |
| </div> |
| |
| </BODY> |
| </HTML> |