특집기사:Eclipse 4 플랫폼 서비스 튜토리얼
| | |
|---|---|
| 원문 | 보기 |
| 저자 | Lars Vogel |
| 역자 | 사용자:Jeeeyul |
| |
이 문서는 번역된 문서입니다. 원문 읽기 |
|---|
| 역자주 | 원저작자의 확실한 오타로 여겨지는 부분들은 일부가 수정되었음. |
버전5.8, Copyright © 2009, 2010 , 2011, 2012 Lars Vogel
이 튜토리얼은 Eclipse 4.X RCP 애플리케이션 및 플러그인 개발에서 사용가능한 서비스들을 소개한다.
목차 |
[편집] 개요
서비스란 특정 기능들을 제공하는 소프트웨어 컴포넌트이다. 이클립스 플랫폼은 플러그인들이 사용하는 서비스들을 정의하며, 기술적으로는 OSGi 서비스로 정의된다.
이 서비스들에 접근하기 위해 디펜던시 인젝션을 사용할 수 있다.
@Inject 어노테이션을 이용하여 필요한 서비스를 디펜던시로 선언하면,
이클립스 프레임웍이 필요한 서비스 컴포넌트들을 꽂아준다(Inject).
이클립스 서비스의 일반적인 명명 규칙은 E로 시작하고 Service로 끝난다. 예: E*Service.
가장 중요한 서비스들은 다음과 같다:
[편집] 워크벤치 서비스들
| 서비스 | 설명 |
|---|---|
| EModelService | 모델(E4 애플리케이션 모델) 접근및 수정을 가능하게한다, 예: 엘리먼트의 추가 및 삭제. |
| ESelectionService | 워크벤치의 선택을 얻을 수 있게 한다. |
| ECommandService | 커맨드 체계에 접근하게 하고, 커맨드를 변경하거나 실행 할 수 있게 한다. |
| EHandlerService | 핸들러들에 접근하고 변경 및 실행을 허용한다. |
| EPartService | 각 파트(뷰, 에디터)에 접근 할 수 있게 한다. 예:파트의 ID얻기 |
| IEventBroker | 특정한 이벤트에 대해 이벤트 데이터를 발송하거나 수신할 수 있게 한다. |
| StatusReporter | 상태 객체를 이용해 리포트하는 것을 허용한다. 표준 IStatus API들에 비해 사용하기 쉽다. |
| EMenuService | 특정 SWT 컨트롤에 팝업 메뉴(MPopupMenu)등을 등록할 수 있게 한다.
|
| org.eclipse.e4.core.services.Logger | 로그 관련 기능 제공 |
| IThemeEngine | 런타임에서 CSS 스타일링을 변경할 수 있도록 함. |
다음과 같은 서비스도 사용 가능하다:
- org.eclipse.e4.core.services.Adapter - 특정 객체에 대해 도메인 특화된 기능을 수행하는 롤 객체를 어댑터[1]라고 한다.
- EBindingService
- EContextService
- org.eclipse.jface.window.IShellProvider - Shell에 접근할 수 있도록 해 준다.
[편집] 파트 서비스
EPartService는 애플리케이션 모델 안의 파트(에디터나 뷰)를 찾고 특정 액션을 수행할 수 있게 한다. 예를 들어 파트를 찾아, 이를 숨기거나 보여줄 수 있다.
@Inject private EPartService partService; // 파트 얻기 detailsTodoPart = partService.findPart("de.vogella.e4.tododetails"); // 파트 숨기기 partService.hidePart(detailsTodoPart); // 파트 보이기 partService.showPart(detailsTodoPart, PartState.VISIBLE);
다음 코드는 애플리케이션 모델의 파트들 중, 더티마크를 가진 파드들을 저장하는 예제이다:
package com.example.e4.rcp.todo.handlers; import org.eclipse.e4.core.di.annotations.Execute; import org.eclipse.e4.ui.workbench.modeling.EPartService; public class SaveHandler { @Execute void execute(EPartService partService) { partService.saveAll(false); } }
[편집] 모델 서비스
EModelService는 모델의 접근과 수정을 허용한다, 예: 모델 삭제및 추가.
이는 @Inject EModelService를 통해 인젝트 된다.
findElements() 메서드를 이용하여 특정 모델 요소를 찾을 수 있다. 다음 예제는 이를 사용한 방법을 보여준다:
package de.vogella.e4.modelservice.handlers; import java.util.ArrayList; import java.util.List; import javax.inject.Inject; import org.eclipse.e4.core.di.annotations.Execute; import org.eclipse.e4.ui.model.application.MApplication; import org.eclipse.e4.ui.model.application.ui.MUIElement; import org.eclipse.e4.ui.model.application.ui.basic.MPart; import org.eclipse.e4.ui.model.application.ui.basic.MWindow; import org.eclipse.e4.ui.workbench.modeling.EModelService; import org.eclipse.swt.widgets.Display; public class ModelServiceExampleHandler { @Execute public void execute(MApplication application, EModelService service, Display display) { System.out.println("Got Model Service: " + (service != null)); // 모델 서비스를 얻는 다른 방법. EModelService modelService = (EModelService) application.getContext().get(EModelService.class.getName()); // 두 서비스는 동일하다. System.out.println("Got Model Service: " + (service != modelService)); // ID로 모델 찾기. List<MPart> findElements = service.findElements(application, "mypart", MPart.class, null); System.out.println("Found part(s) : " + findElements.size()); // 타입으로 모델 찾기. List<MPart> parts = service.findElements(application, null, MPart.class, null); System.out.println("Found parts(s) : " + parts.size()); // 태그를 이용한 모델 찾기. List<String> tags = new ArrayList<String>(); tags.add("justatag"); List<MUIElement> elementsWithTags = modelService.findElements(application, null, null, tags); System.out.println("Found parts(s) : " + elementsWithTags.size()); // MWindow를 찾아서 크기 변경하기. List<MWindow> windows = modelService.findElements(application, null, MWindow.class, null); if (windows.size()>=1){ MWindow mWindow = windows.get(0); System.out.println("Got the window"); for (int i = mWindow.getWidth(); i >= mWindow.getWidth() - 100; i--) { while (!display.readAndDispatch()){ mWindow.setWidth(i); wait10(); } } } } private void wait10(){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }
다음 예제는 "mypartsashcontainer"라는 아이디를 이용해 MPartSashContainer를 찾고, 자식들이 배치될 공간을 재 설정하기 위해, 컨테이너 데이터를 지정하는 과정을 보여준다.
이 코드는 컨테이너 안에 있는 파트들을 재배치(layout) 하게 된다.
@Execute public void execute(EModelService service, MWindow window) { MPartSashContainer find = (MPartSashContainer) service.find("mypartsashcontainer", window); List<MPartSashContainerElement> list = find.getChildren(); int i = 0; // 컨테이너 안의 첫번째 파트를 더 크게(80) 한다. for (MPartSashContainerElement element : list) { element.setContainerData("80"); if (i > 0) { element.setContainerData("20"); } i++; } }
[편집] 셀렉션 서비스
[편집] 4.1 셀렉션 서비스 얻기
ESelectionService를 이용하여 현재 워크벤치 선택을 얻을 수 있다.
클라이언트는 @Inject ESelectionService를 이용해 셀렉션 서비스를 얻을 수 있다.
[편집] 현재 선택 바꾸기
현재 선택을 변경하고 싶으면 setSelection() 메서드를 호출하면 된다.
셀렉션 서비스를 사용하는 클라이언트는, @Named 어노테이션과, 어노테이션 파라미터, 그리고 타입을 이용해 선택된 모델(셀렉션)을 적합하게 특정할 수 있다.
@Inject ESelectionService selectionService; viewer.addSelectionChangedListener(new ISelectionChangedListener() { @Override public void selectionChanged(SelectionChangedEvent event) { IStructuredSelection selection = (IStructuredSelection) viewer.getSelection(); Object firstElement = selection.getFirstElement(); selectionService.setSelection(selection.getFirstElement()); } });
@Inject public void setTodo(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) Todo todo) { if (todo != null) { // Do something with the value } }
[편집] 커맨드와 핸들러 서비스
ECommandService와 EHandlerService는 커맨드와 핸들러를 다룰 수 있게 한다.
디펜던시 인젝션을 통해 이 서비스들을 얻을 수 있다. 이 서비스들은 커맨드나 핸들러를 새로 만들거나 변경 할 수 있게 한다.
예를 들어, 애플리케이션 모델에 미리 선언되어 있지 않더라도, 핸들러 서비스를 통해 퍼스펙티브의 IEclipseContext에 핸들러를 추가할 수 있다.
아래의 예제는 기존에 존재하는 Command에 핸드러를 추가하는 방법을 보여준다. AboutHandler라는 클래스가 이미 있다고 가정한다.
Command command = commandService.getCommand("com.example.mycommand"); // true를 반환. System.out.println(command.isDefined()); // 핸들러를 활성화 한다. AboutHandler가 이미 존재한다고 가정한다. handerService.activateHandler("com.example.mycommand", new AboutHandler()); ParameterizedCommand cmd = commandService.createCommand("com.example.mycommand", null); // 커맨드의 몇몇 메서드들은 하위버전 호환성만을 위해 존재하며 무의미한 값을 리턴한다. // 아래의 두 메서드는 false나 null을 리턴한다. System.out.println(command.getHandler()); System.out.println(command.isEnabled()); // 실행이 가능한 경우에만 수행하게 한다. System.out.println(handerService.canExecute(cmd)); handerService.executeHandler(cmd);
현재 몇몇 메서드들은 오직 호환성 계층에서만 사용된다. Command API에 대한 버그 리포트[1]를 참조하라.
[편집] 감사합니다
원문 하단의 페이팔 버튼을 통해, 이 문서의 저자에게 기부할 수 있습니다.
| 역자주 | 이 위키에 기부하고 싶으면, 위클립스:기여 도우미 페이지를 이용할 수 있습니다. |
