파트 인스턴스화

파트와의 모든 상호작용은 ISite를 통해 발생합니다. 파트를 작성하는 사용자의 시점에서 ISite 해당 파트입니다. ISite는 IPartFactory에서 작성하거나 직접 Site 클래스를 인스턴스화하여 작성할 수 있습니다.

파트를 제거하려면 해당 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...
}

Site 클래스를 사용하여 파트 인스턴스화

일부 파트는 보기나 편집기가 아닙니다. 클라이언트는 파트 API를 사용하여 재사용 가능한 컴포넌트를 작성할 수도 있습니다. 이러한 파트에는 보기 또는 편집기 확장점이 등록되지 않지만 해당 파트는 보기와 편집기에 사용할 수 있는 대부분의 API를 사용할 수 있습니다. 클라이언트가 자신의 재사용 가능한 컴포넌트용 API를 만들지 못하도록 할 수는 없지만 파트 패턴을 만들면 클라이언트의 컴포넌트를 보기 또는 편집기의 포함 기능을 지원하는 어떤 것 내부에 포함할 수 있습니다.

Site 클래스를 사용하여 작성한 파트에는 다음과 같은 특성이 있습니다. Site 생성자에는 파트의 구현 클래스와 연관된 플러그인 번들이 제공되어야 합니다. 플러그인 번들은 로그 오류 메시지가 기록되는 위치와 파트가 자체 플러그인에서 자원을 찾는 시기를 검색하는 위치에서 확인됩니다.

다음 예제에서는 Site 클래스를 사용하여 파트를 직접 인스턴스화하는 방법을 보여 줍니다. 이 예제에서는 대화 상자에서 NameTestView 파트를 인스턴스화합니다. NameTestView는 보기라고 불리고 보기 API를 사용할 수 있지만 Workbench가 실제로 이 파트를 보기로 사용한다고 가정하지 않으면 이 파트에 실제로 org.eclipse.ui.views 확장점을 등록할 필요가 없습니다.


/**
 * Demonstrate how to open a part programmatically using the Site class.
 */
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...
}