보기 및 편집기는 일반적으로 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...
}
/**
* 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...
}