컴포넌트 구현

유형 확장점이 등록된 컴포넌트는 부분 컴포넌트와 거의 같습니다. 이러한 컴포넌트는 자신의 생성자에 종속성 설정을 사용할 수 있고 등록된 대상 인터페이스를 구현해야 합니다. 이 예제에서는 NullNameableService의 구현을 표시합니다.

/**
 * Default implementation of the Nameable service. All methods are no-ops.
 *
 * @since 3.1
 */
public class NullNameableService implements INameable {

    /**
     * Component constructor. Do not invoke directly.
     */
    public NullNameableService() {}

    public void setName(String newName) {}
    public void setContentDescription(String contentDescription) {}
    public void setImage(ImageDescriptor theImage) {}
    public void setTooltip(String toolTip) {}
}

이 예제는 클래스가 작업을 수행하지 않는 단순 예제이지만 공통 패턴을 보여 줍니다. INameable 서비스는 하위의 일부 상태에 대해 상위에 알리는 데 사용됩니다. 이 유형의 인터페이스에서 기본 구현은 대개 아무런 작업도 수행하지 않습니다. 상식적으로 상위가 하위 상태를 고려하지 않으면 하위는 무시될 수 있습니다. 규칙에 따라 아무런 작업도 수행하지 않는 컴포넌트임을 나타내는 "Null" 접두부가 사용됩니다. 이 유형의 오브젝트는 일반 오브젝트의 여러 인스턴스를 작성하기에는 낭비적이므로 대개 단일 오브젝트로 등록됩니다. 클라이언트는 상태를 보고하는 새 인터페이스가 다중화될 수 있음을 확인해야 합니다.

인터페이스의 목적이 자원을 할당하거나 출력을 생성하는 것인 경우 기본 구현은 대개 보다 유용한 작업을 수행합니다. 다음 예제에서는 DefaultMessageDialogs 클래스의 구현을 표시합니다. 이 클래스는 대화식 출력을 생성하는 데 사용되므로 유용한 기본 구현이 있고 다중화를 지원하지 않습니다. 이 클래스에는 부분의 컴포지트에 대한 액세스가 필요하므로 단일 오브젝트가 될 수 없습니다. 규칙에 따라 특정 유용한 동작이 있는 기본 컴포넌트임을 나타내는 "Default" 클래스 접두부가 사용됩니다.

/**
 * Default implementation of the IMessageDialogs interface. Takes the part's
 * control as context and allows the part to open dialogs in a child shell.
 *
 * @since 3.1
 */
public class DefaultMessageDialogs implements IMessageDialogs {

    private Composite control;
   
    /**
     * Component constructor. Do not invoke directly.
     */
    public DefaultMessageDialogs(Composite control) {
        this.control = control;
    }
   
    public void open(IStatus message) {
        if (message.getSeverity() == IStatus.ERROR) {
            ErrorDialog.openError(control.getShell(), null, null, message);
        } else {
            open(message.getSeverity(), message.getMessage());
        }
    }

    public void openError(String message, Throwable cause) {
        open(new Status(IStatus.ERROR,
                WorkbenchPlugin.getDefault().getBundle().getSymbolicName(),
                IStatus.OK,
                message,
                cause));
    }
   
    public void open(int severity, String message) {
        if (severity == IStatus.ERROR) {
            MessageDialog.openError(control.getShell(), null, message);
        } else if (severity == IStatus.WARNING) {
            MessageDialog.openWarning(control.getShell(), null, message);
        } else {
            MessageDialog.openInformation(control.getShell(), null, message);
        }   
    }
}

다음은 DefaultMessageDialog에 연관된 확장점 마크업입니다.

<extension point="org.eclipse.core.component.types">
      <component
            initializer="org.eclipse.ui.part.SiteInitializer"
            interface="org.eclipse.ui.part.services.IMessageDialogs"
            implementation="org.eclipse.ui.internal.part.services.DefaultMessageDialogs"
            singleton="false"/>
</extension>