카테고리 없음

Eclipse RCP (Rich Client Platform) - 11.annotaion을 사용해 행동 정의하기, 12. @PostConstruct 사용

TimeSave 2022. 1. 21. 21:25

 

11. Using annotations to define behavior

: annotaion을 사용해 행동 정의하기

 

11.1. API definition in a framework

If you use a framework in your application, you need to have a convention for how your application interacts with the framework. For example, if a Java object is responsible for handling a toolbar button click, the framework needs to know which method of this object needs to be called.

 

For this purpose every framework defines an Application Programming Interface (API). This API defines how you can interact with the framework from your code. The API also defines the interaction of application objects created or controlled by the framework. Typically, a framework uses inheritance or annotations for this purpose.

 

11.2. API definition via inheritance

The "traditional" way of defining an API is via inheritance. This approach requires that your classes extend or implement framework classes and interfaces. The Eclipse 3.x platform API used this approach.

 

The framework defines, for example, an abstract class which defines methods to be implemented.

 

In the example of the toolbar button the method might be called execute() and the framework knows that this method must be called once the button is clicked.

 

API definition via inheritance is a simple way to define an API, but it also couples the classes tightly to the framework. For example, testing the class without the framework is difficult. It also makes extending or updating the framework difficult as such an update may affect clients. This is why the Eclipse 4.x does not use this approach anymore.

 

11.3. API definition via annotations

The Eclipse 4.x platform API is based on annotations, e.g., annotations are used to identify which methods should be called at a certain point in time. These annotations are called behavior annotations.

 

The following table lists the available behavior annotations for parts.

 

Table 9. Eclipse life cycle annotations for parts

  • 0열 선택0열 다음에 열 추가
  • 1열 선택1열 다음에 열 추가
  • 0행 선택0행 다음에 행 추가
  • 1행 선택1행 다음에 행 추가
  • 2행 선택2행 다음에 행 추가
  • 3행 선택3행 다음에 행 추가
  • 4행 선택4행 다음에 행 추가
  • 5행 선택5행 다음에 행 추가
셀 전체 선택
열 너비 조절
행 높이 조절
Annotation
Description
@PostConstruct
Is called after the class is constructed and the field and method injection has been performed.
@PreDestroy
Is called before the class is destroyed. Can be used to clean up resources.
@Focus
Is called whenever the part gets the focus.
@Persist
Is called if a save request on the part is triggered by the Eclipse framework.
@PersistState
Is called before the model object is disposed, so that the part is able to save its instance state. This method is called before the @PreDestroy method.
  • 셀 병합
  • 행 분할
  • 열 분할
  • 너비 맞춤
  • 삭제

 

The @PostConstruct, @PreDestroy annotations are included in the javax.annotation package. @Persist, @PersistState and @Focus are part of the org.eclipse.e4.ui.di package.

Eclipse defines additional behavior annotations for commands and for the application life cycle which are covered in the respective chapters.

 

정보 : Behavior annotations imply that the framework needs to provide the specified parameters to the method, i.e., the framework also performs method dependency injection. If you also add the @Inject annotation, the method is called twice, first during the dependency injection phase and later for the behavior annotation. This is typically undesired and therefore an error.

 

11.4. Use the @PostConstruct method to build the user interface

It is recommended to construct the user interface of a part in a method annotated with the @PostConstruct annotation. It would also be possible to create the user interface in the constructor, but this is not recommended as field and method injection have not been done at this point.

Creating the user interface in an @PostConstruct method requires that @Inject methods are aware that the user interface might not have been created yet.

 

정보 : Why is the @PostConstruct method not called?

The following description is only valid for Eclipse versions before the Eclipse 4.6 (Eclipse Neon) release. As of Eclipse 4.6 the framework uses the Java version of @PostConstruct and the problem described here, cannot happen anymore.

Before Eclipse 4.6, both Java 7 and the Eclipse platform exposed the @PostConstruct annotation. In your Eclipse application you need to tell the framework that the annotation from the Eclipse platform should be used. org.eclipse.core.runtime exports javax.annotation in the correct version. If, for some reasons, you want to avoid a dependency to org.eclipse.core.runtime, you could define a package dependency to the javax.annotation package and set the version to 1.0.0. See Eclipse 4 RCP FAQ for details on this issue.

 

12. Exercise: Using @PostConstruct

12.1. Implement an @PostConstruct method

Add the following method to your TodoOverviewPart, TodoDetailsPart and PlaygroundPart classes. In case you created constructors for these classes you can remove them.

 

import javax.annotation.PostConstruct;
import org.eclipse.swt.widgets.Composite;

// more code

@PostConstruct
public void createControls(Composite parent) {
    System.out.println(this.getClass().getSimpleName()
    + " @PostConstruct method called.");
}
 

12.2. Validate

Run your application and validate that the @PostConstruct method is called.

 

If you receive an error message similar to the following: Unable to process "TodoOverviewPart#createControls()": no actual value was found for the argument "Composite"., ensure that your import statement is correct.

 

import org.eclipse.swt.widgets.Composite;

// MORE code