Eclipse RCP Tutorial 6

작성일: 2010. 10. 27.

원문: http://www.vogella.de/articles/EclipseRCP/article.html Version 5.1

Commands

#@ 개요

명령은 컴포넌트의 명세를 선언하고 상세 구현으로부터 독립적이다. 명령은 분류할 수 있고 단축키를 할당 할 수 있다. 명령은 메뉴, 툴바 그리고 컨텍스트 메뉴에 사용된다. 다음에서는 메뉴에 명령을 적용하는 방법을 시연할 것이다. 명령에 대한 자세한 내용은 Eclipse Commands - Tutorial 을 보아라.

명령 정의하기

어플리케이션을 종료하는 명령을 만들 것이다. de.vogella.rcp.commands.first 라는 새로운 RCP 프로젝트를 생성하고, Hello RCP 템플릿을 사용한다. plugin.xml 파일을 더블 클릭하고 Extensions 탭을 선택한다. 그리고 Add 버튼을 누른다.

org.eclipse.ui.commands 확장을 검색한다. 그것을 선택하고 Finish 를 누른다.

확장점에 오른쪽 클릭을 하여 New -> Command 를 선택해 새로운 명령을 생성한다.

ID는 de.vogella.rcp.commands.first.commands.Exit 로, 이름은 Exit 로 설정한다. 기본 핸들러로 de.vogella.rcp.commands.first.commands.ExitHandler 클래스를 넣는다. 이 클래스를 생성하기 위해 defaultHAndler 하이퍼링크를 누르고 부모 클래스로 org.eclipse.core.commands.AbstractHandler 를 선택한다.

다음의 코드를 구현한다. 당신의 새로운 명령은 나중에 사용될 준비 상태이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package de.vogella.rcp.commands.first.commands;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.handlers.HandlerUtil;
public class ExitHandler extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
HandlerUtil.getActiveWorkbenchWindow(event).close();
return null;
}
}

메뉴에서 명령 사용하기

우리가 정의한 명령은 메뉴에서 사용될 것이다. org.eclipse.ui.commands 확장과 비슷하게 어플리케이션에 org.eclipse.ui.menus 확장점을 추가한다. 확장점에서 오른쪽 클릭을 하고 New -> menuContribution 을 선택한다.

location URI 는 menu:org.eclipse.ui.main.menu 라고 하고, 새로운 메뉴 기증을 생성한다. 이 URL이 정확하지 않으면 당신의 메뉴는 보이지 않는다.

메뉴 기증을 오른쪽 클릭하고 New -> Menu 를 선택한다. label 은 File, id 는 fileMenu 로 메뉴를 추가한다.

메뉴를 선택하고, 오른쪽 클릭을 하고, New -> Command 를 선택한다. commandID 는 그대로 둔다. 레이블은 Exit, 툴팁은 Exits the application 으로 설정한다.

위의 과정을 통해 plugin.xml 파일에 다음과 같은 결과를 얻을 것이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
id="application"
point="org.eclipse.core.runtime.applications">
<application>
<run
class="de.vogella.rcp.commands.first.Application">
</run>
</application>
</extension>
<extension
point="org.eclipse.ui.perspectives">
<perspective
name="RCP Perspective"
class="de.vogella.rcp.commands.first.Perspective"
id="de.vogella.rcp.commands.first.perspective">
</perspective>
</extension>
<extension
point="org.eclipse.ui.commands">
<command
defaultHandler="de.vogella.rcp.commands.first.commands.ExitHandler"
id="de.vogella.rcp.commands.first.commands.Exit"
name="Exit">
</command>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="menu:org.eclipse.ui.main.menu">
<menu
id="fileMenu"
label="File">
<command
commandId="de.vogella.rcp.commands.first.commands.Exit"
label="Exit"
style="push"
tooltip="Exit the application">
</command>
</menu>
</menuContribution>
</extension>
</plugin>

예제를 실행한다. file 메뉴를 보게 될 것이고, Exit 항목을 선택하면 어플리케이션은 종료할 것이다.

Share Comments