Eclipse RCP Tutorial 10

작성일: 2010. 10. 27.

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

JFace 뷰어

개요

JFace는 사용자 인터페이스를 생성시 자바 객체를 바로 사용할 수 있는 뷰어를 제공한다. 뷰어는 트리, 테이블, 리스트와 콤보박스가 가능하다. 다음에서 콤보 뷰어의 사용 예를 보여줄 것이다. 테이블 뷰어의 자세한 사용법은 Eclipse JFaces Tables 에서 찾을 수 있다.

트리 뷰어의 간략한 사용 예는 JFace Tree Viewer 에서 찾을 수 있다.

콤보뷰어

새로운 RCP 프로젝트 de.vogella.rcp.intro.jfaceviewer 를 만들고 RCP application with a view 템플릿을 선택한다. 다음의 자바 클래스를 생성한다.

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
package de.vogella.rcp.intro.jfaceviewer;
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

View.java 를 다음과 같이 변경한다.

package de.vogella.rcp.intro.jfaceviewer;

import org.eclipse.jface.viewers.ArrayContentProvider;

public class View extends ViewPart {
  public static final String ID = "de.vogella.rcp.intro.jfaceviewer.view";

  private ComboViewer viewer;

  public void createPartControl(Composite parent) {
    GridLayout layout = new GridLayout(2, false);
    parent.setLayout(layout);
    Label label = new Label(parent, SWT.NONE);
    label.setText("Select a person:");
    viewer = new ComboViewer(parent, SWT.READ_ONLY);
    viewer.setContentProvider(new ArrayContentProvider());
    viewer.setLabelProvider(new LabelProvider() {
      @Override
      public String getText(Object element) {
        if (element instanceof Person) {
          Person person = (Person) element;
          return person.getFirstName();
        }
      return super.getText(element);
      }
    });
    Person[] persons = new Person[] { new Person("Lars", "Vogel"),
    new Person("Tim", "Taler"), new Person("Jim", "Knopf") };
    // Set set the input to the viewer this input will be send to the
    // content provider
    viewer.setInput(persons);
    // React to the selection of the viewer
    // Note that the viewer return the real object and not just a string
    // representation
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {
      @Override
      public void selectionChanged(SelectionChangedEvent event) {
        IStructuredSelection selection = (IStructuredSelection) event.getSelection();
        System.out.println(((Person) selection.getFirstElement()).getLastName());
      }
    });
    // You can select a object directly via the domain object
    Person person = persons[0];
    viewer.setSelection(new StructuredSelection(person));
  }

  /**
  * Passing the focus request to the viewer's control.
  */
  public void setFocus() {
    viewer.getControl().setFocus();
  }
}
Share Comments