パーツへの引数の引き渡し

親は、ContainerContext を使用して子に引数を受け渡します。 コンテキストは、パーツのコンストラクターに引数を受け渡すか、またはそれらの引数を組み立てるファクトリーを指定できます。 例えば、特定の ContainerContext では、以下のようにルールを指定します。
コンテキストが特定の依存関係を提供しない場合、子は org.eclipse.core.component.types 拡張ポイントからのデフォルトの実装を使用します。 コンテキストは、パーツがそのコンストラクターで受け取った任意の引数をオーバーライドできるため、親は、パーツが通常そのサイトから取得する任意のインターフェースをオーバーライドできます。 例えば、親は、コンテキストに IActionBars を提供することにより、子に対して IActionBars の異なる実装を使用するよう強制できます。

親には、以下のようなコンテキストを組み立てる複数のオプションがあります。

以下の DefaultContextViewRedirectContextView、および OverrideInstanceView の例は、それぞれの可能性を示しています。

デフォルト・コンテキスト

以下の例は、デフォルト・コンテキストに 2 つのネストされた子を作成するビューのソースを示しています。 子がデフォルト・コンテキストに作成されるという事実は、親が子の名前、ツールバー、および選択などを管理せず、またそれらを処理するよう準備されないことを意味します。 親が、その子の 1 つにおける現在の選択など、何らかの関与を行う場合は、その子に ISelectionHandler を受け渡す必要があります。 最終的な結果は、以下の例に示されています。

/**
 * View that demonstrates how to create two nested children
 * with the default context.
 */
public class DefaultContextView {
    public DefaultContextView(Composite parent, IPartFactory factory) throws CoreException {
        // Create a resource navigator
        ContainerContext viewContext1 = new ContainerContext();  
        ISite view1 = factory.createView(
                IPageLayout.ID_RES_NAV, parent, viewContext1, null);
   
        // Create property view
        ContainerContext viewContext2 = new ContainerContext();
        ISite view2 = factory.createView(IPageLayout.ID_PROP_SHEET, parent, viewContext2, null);
   
        parent.setLayout(new FillLayout());       
    }
}

DefaultContextView のスクリーン・ショット

親から子への依存関係のリダイレクト

この例は、親から子にサイト・インターフェースをリダイレクトする方法を示します。 このコンポジット・ビューには、リソース・ナビゲーターおよび「プロパティー」ビューが含まれています。 その選択ハンドラーがリソース・ナビゲーターにリダイレクトされ、そのアクション・バーが「プロパティー」ビューにリダイレクトされます。 結果は、以下に示されるように、リソースの選択を行えるビューとなり、「プロパティー」ビューからのツールバーおよびメニューが含まれます。

public class RedirectContextView {
    /**
     * Component constructor. Do not invoke directly.
     */
    public RedirectContextView(Composite parent, IPartFactory factory, ISelectionHandler selection, IActionBars actionBars) throws CoreException {
        // Create a resource navigator. Redirect the navigator's selection directly to our parent.
        ContainerContext viewContext1 = new ContainerContext()
            .addInstance(ISelectionHandler.class, selection);
        ISite view1 = factory.createView(
                IPageLayout.ID_RES_NAV,
                parent, viewContext1, null);
   
        // Create property view. Allow the property view to use our action bars directly.
        ContainerContext viewContext2 = new ContainerContext()
            .addInstance(IActionBars.class, actionBars);
        ISite view2 = factory.createView(IPageLayout.ID_PROP_SHEET, parent, viewContext2, null);
   
        parent.setLayout(new FillLayout());       
    }
}


RedirectContextView のスクリーン・ショット

依存関係の直接の提供

以下の例には、子にその依存関係の 1 つを直接提供する方法が示されています。 以下の例では、「問題」ビューおよび「プロパティー」ビューが含まれているコンポジット・ビューを作成しています。 コンテンツの説明で選択された問題の数を表示するため、ISelectionHandler を使用して「問題」ビューを提供しています。 最終的な結果を以下に示します。

public class OverrideInstanceView {
   
    /**
     * Component constructor. Do not invoke directly.
     */
    public OverrideInstanceView(Composite parent, IPartFactory factory, final INameable name) throws CoreException {
        ContainerContext viewContext1 = new ContainerContext();
       
        // Add an ISelectionHandler to the view's context. Whenever the view changes its selection,
        // display the number of selected items in the content description
        viewContext1.addInstance(ISelectionHandler.class, new ISelectionHandler() {
            /* (non-Javadoc)
             * @see org.eclipse.ui.part.services.ISelectionHandler#setSelection(org.eclipse.jface.viewers.ISelection)
             */
            public void setSelection(ISelection newSelection) {
                if (newSelection instanceof IStructuredSelection) {
                    IStructuredSelection sel = (IStructuredSelection)newSelection;
                    int selectionSize = sel.size();
                   
                    name.setContentDescription(MessageFormat.format("{0} problems selected",
                            new String[] {Integer.toString(selectionSize)}));
                }
            }
        });

        // Create a problem view
        ISite view1 = factory.createView(
                IPageLayout.ID_PROBLEM_VIEW, parent, viewContext1, null);
   
        // Create property view
        ContainerContext viewContext2 = new ContainerContext();
        ISite view2 = factory.createView(IPageLayout.ID_PROP_SHEET, parent, viewContext2, null);
   
        parent.setLayout(new FillLayout());
    }
}



OverrideInstanceView のスクリーン・ショット