パーツのインスタンス化

パーツでのすべての相互作用は、ISite を通じて行われます。 パーツの作成者の観点からは、ISite はパーツです。 ISite は、IPartFactory からか、またはサイト・クラスを直接インスタンス化するかのいずれかにより、作成できます。

パーツを破棄するには、その ISite の制御を処理します。

IPartFactory を使用したパーツのインスタンス化

ビューおよびエディターは、通常、IPartFactory を使用して作成され、IPartFactory は、ビューまたはエディターの拡張ポイントに登録されたパーツのみを作成できます。 IPartFactory により作成されたパーツは、IWorkbenchPage に依存し、IWorkbenchPage より長くは存続できません。 IPartFactory は、ID によりパーツを作成し、そのパーツの実装クラスが API である必要はありません。 IPartFactory は IWorkbenchPage から取得できます。 パーツは、そのコンストラクターに IPartFactory を取り込み、ネストされた子を作成することもできます。

この例では、IPartFactory を使用してビューを作成する方法が示されています。

/**
 * Used to create instances of editors and views.
 *
 * @since 3.1
 */
public interface IPartFactory {
    /**
     * Creates an instance of a view. Returns an <code>ISite</code> for the newly created view.
     * When the caller is done with the part it should dispose the part's main control. This can
     * be accomplished by calling <code>ISite.getControl().dispose()</code>, or by disposing the
     * parent composite.
     *
     * @param viewId ID of the view, as registered with the org.eclipse.ui.views extension point
     * @param parentComposite parent composite for the view. If the view is successfully created, it
     *        will create exactly one new child control in this composite.
     * @param context local context for the view. This object can override any or all of the view's dependencies.
     *        If the view has a dependency that isn't found in the local context, a default implementation will
     *        be supplied by the org.eclipse.core.component.types extension point.
     * @param savedState previously saved state of the part, or null if none
     * @return an ISite for the newly created view
     * @throws CoreException if unable to create the part
     */
    public ISite createView(String viewId, Composite parentComposite, IContainerContext context, IMemento savedState) throws CoreException;
   
    /**
     * Creates an instance of an editor. Returns an <code>ISite</code> for the newly created editor.
     * When the caller is done with the part it should dispose the part's main control. This can
     * be accomplished by calling <code>ISite.getControl().dispose()</code>, or by disposing the
     * parent composite.
     *
     * @param editorId ID of the editor, as registered with the org.eclipse.ui.editors extension point
     * @param parentComposite parent composite for the editor. If the editor is successfully created,
     *        it will create exactly one new child control in this composite.
     * @param context local context for the editor. This object can override any or all of the part's dependencies.
     *        If the part has a dependency that isn't found in the local context, a default implementation will
     *        be supplied by the org.eclipse.core.component.types extension point.
     * @param input IEditorInput for this editor
     * @param savedState previously saved state for the part, or null if none
     * @return an ISite for the newly created editor
     * @throws CoreException if unable to create the part
     */
    public ISite createEditor(String editorId, Composite parentComposite, IContainerContext context, IEditorInput input, IMemento savedState) throws CoreException;
}


IPartFactory を使用してビューを作成するアクションの例を以下に示します。

/**
 * Demonstrate how to open a view by its ID from an IWorkbenchPage.
 */
public class CreateViewByIdAction implements IWorkbenchWindowActionDelegate {
    private IWorkbenchWindow window;

    public void run(IAction action) {       
        IWorkbenchPage page = window.getActivePage();
       
        if (page == null) {
           
// ...uninteresting error-handling code removed...
            return;
        }
       
        final Shell tempShell = new Shell(window.getShell(), SWT.DIALOG_TRIM | SWT.RESIZE);
       
        tempShell.setLayout(new FillLayout());
        tempShell.setText("Problems");
       
        IPartFactory factory = page.getPartFactory();
        try {
            factory.createView(IPageLayout.ID_PROBLEM_VIEW, tempShell, new ContainerContext(), null);
        } catch (CoreException e) {
           
// ...uninteresting error-handling code removed...
        }
       
        // Open the dialog
        tempShell.open();
        Display d = tempShell.getDisplay();
        while (!tempShell.isDisposed()) {
            if (!d.readAndDispatch())
                d.sleep();
        }
       
    }

    public void init(IWorkbenchWindow window) {
        this.window = window;
    }

    // ...remaining (empty) methods removed...
}

サイト・クラスを使用したパーツのインスタンス化

すべてのパーツがビューまたはエディターであるとは限りません。 クライアントもまた、パーツ API を使用して、独自の再使用可能コンポーネントを作成できます。 そのようなパーツは、ビューまたはエディターの拡張ポイントには登録されませんが、ビューおよびエディターで使用可能なほとんどの API を使用できます。 クライアントが再使用可能コンポーネントの独自の API を創作することは妨げられませんが、パーツ・パターンを活用することにより、ビューまたはエディターの組み込みをサポートするすべてのものにそのコンポーネントを組み込むことができます。

サイト・クラスを使用して作成されたパーツには、以下の特性があります。 サイト・コンストラクターには、パーツの実装クラスおよび関連するプラグイン・バンドルが提供される必要があります。 プラグイン・バンドルにより、ログ・エラー・メッセージがログに記録される場所、およびパーツがそのプラグイン内のリソースを検索する場合の検索場所が決定されます。

以下の例には、サイト・クラスを使用してパーツを直接インスタンス化する方法が示されています。 この例では、NameTestView パーツをダイアログにインスタンス化しています。 NameTestView はビューを呼び出し、ビュー API を活用できますが、ワークベンチがそれをビューとして実際に使用することが想定されない限り、org.eclipse.ui.views 拡張ポイントにそれを実際に登録する必要はありません。


/**
 * Site クラスを使用して、パーツを プログラマチックに開く方法を説明します。
 */
public class ProgrammaticViewCreationExampleAction implements IWorkbenchWindowActionDelegate {
    private IWorkbenchWindow window;

    public void run(IAction action) {       
        // Create a shell
        final Shell tempShell = new Shell(window.getShell(), SWT.DIALOG_TRIM | SWT.RESIZE);
        tempShell.setLayout(new FillLayout());
        tempShell.setText("Name test view");
       
        Bundle thisPlugin = ComponentExamplesPlugin.getDefault().getBundle();
       
        try {
            // Instantiate the NameTestView part (this line is the whole point of the example)
            // It demonstrates how the Site class can be used instead of calling NameTestView's constructor.
            Site testPart = new Site(tempShell, new ContainerContext(),
                    thisPlugin,
                    NameTestView.class);           
           
        } catch (CoreException e) {
            // ...uninteresting error-handling code removed...
        }
       
        // Open a modal dialog
        tempShell.open();
        Display d = tempShell.getDisplay();
        while (!tempShell.isDisposed()) {
            if (!d.readAndDispatch())
                d.sleep();
        }
       
    }
\
    public void init(IWorkbenchWindow window) {
        this.window = window;
    }

   
// ...remaining (empty) methods removed...
}