package Table;


import org.eclipse.jface.action.Action;

import org.eclipse.jface.action.IAction;

import org.eclipse.jface.action.MenuManager;

import org.eclipse.jface.viewers.CellEditor;

import org.eclipse.jface.viewers.ColumnWeightData;

import org.eclipse.jface.viewers.ComboBoxCellEditor;

import org.eclipse.jface.viewers.ICellModifier;

import org.eclipse.jface.viewers.ILabelProviderListener;

import org.eclipse.jface.viewers.IStructuredContentProvider;

import org.eclipse.jface.viewers.ITableLabelProvider;

import org.eclipse.jface.viewers.TableLayout;

import org.eclipse.jface.viewers.TableViewer;

import org.eclipse.jface.viewers.TextCellEditor;

import org.eclipse.jface.viewers.Viewer;

import org.eclipse.swt.SWT;

import org.eclipse.swt.graphics.Image;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Menu;

import org.eclipse.swt.widgets.Table;

import org.eclipse.swt.widgets.TableColumn;

import org.eclipse.swt.widgets.TableItem;


public class Ch9Table extends Composite {

private static final Object[] CONTENT = new Object[]{

new EditableTableItem("item 1", new Integer(0)),

new EditableTableItem("item 2", new Integer(1))

};

private static final String[] VALUE_SET = new String[]{

"xxx", "yyy","zzz"

};

private static final String NAME_PROPERTY = "name";

private static final String VALUE_PROPERTY = "value";

private TableViewer viewer;

public Ch9Table(Composite parent){

super(parent, SWT.NULL);

buildControls();

}

private class NewRowAction extends Action{

public NewRowAction(){

super("Insert New Row");

}

public void run(){

EditableTableItem newItem = new EditableTableItem("new row", new Integer(2));

viewer.add(newItem);

}

}



protected void buildControls(){

FillLayout compositeLayout = new FillLayout();

setLayout(compositeLayout);

final Table table = new Table(this, SWT.FULL_SELECTION);

viewer = buildAndLayoutTable(table); //table 생성

attachContentProvider(viewer);

attachLabelProvider(viewer); //data 제공

attachCellEditors(viewer, table); //Data Editor 제공

MenuManager popupMenu = new MenuManager();//  팝업 메뉴 생성

IAction newRowAction = new NewRowAction();

popupMenu.add(newRowAction);

Menu menu = popupMenu.createContextMenu(table);

table.setMenu(menu);

viewer.setInput(CONTENT);

}

private void attachLabelProvider(TableViewer viewer){

viewer.setLabelProvider(new ITableLabelProvider(){

public Image getColumnImage(Object element, int columnIndex){

return null;

}

public String getColumnText(Object element, int columnIndex){

switch(columnIndex){

case 0:

return ((EditableTableItem)element).name;

case 1:

Number index = ((EditableTableItem)element).value;

return VALUE_SET[index.intValue()];

default:

return "Invalid column: " + columnIndex;

}

}

public void addListener(ILabelProviderListener listener){

}

public void dispose(){

}

public boolean isLabelProperty(Object element, String property){

return false;

}

public void removeListener(ILabelProviderListener lpl){

}

});

}

private void attachContentProvider(TableViewer viewer){

viewer.setContentProvider(new IStructuredContentProvider(){

public Object[] getElements(Object inputElement){

return (Object[])inputElement;

}

public void dispose(){

}

public void inputChanged(Viewer viewer, Object oldInput, Object newInput){

}

});

}

private TableViewer buildAndLayoutTable(final Table table){

TableViewer tableViewer = new TableViewer(table);

TableLayout layout = new TableLayout();

layout.addColumnData(new ColumnWeightData(50, 75, true));

layout.addColumnData(new ColumnWeightData(50, 75, true));

table.setLayout(layout);

TableColumn nameColumn = new TableColumn(table, SWT.CENTER);

nameColumn.setText("Name");

TableColumn valColumn = new TableColumn(table, SWT.CENTER);

valColumn.setText("value");

table.setHeaderVisible(true);

return tableViewer;

}

private void attachCellEditors(final TableViewer viewer, Composite parent){

viewer.setCellModifier(new ICellModifier(){

public boolean canModify(Object element, String property){

return true;

}

public Object getValue(Object element, String property){

if(NAME_PROPERTY.equals(property))

return ((EditableTableItem)element).name;

else

return ((EditableTableItem)element).value;

}

public void modify(Object element, String property, Object value){

TableItem tableItem = (TableItem)element;

EditableTableItem data = (EditableTableItem)tableItem.getData();

if(NAME_PROPERTY.equals(property))

data.name = value.toString();

else

data.value = (Integer)value;

viewer.refresh(data);

}

});

viewer.setCellEditors(new CellEditor[]{

new TextCellEditor(parent), new ComboBoxCellEditor(parent, VALUE_SET)

});

viewer.setColumnProperties(new String[] { NAME_PROPERTY, VALUE_PROPERTY});

}

}

class EditableTableItem{

public String name;

public Integer value;

public EditableTableItem(String n, Integer v){

name=n;

value=v;

}

}


Ch8List.Java

package Ch8;


import java.util.ArrayList;

import java.util.List;


import org.eclipse.jface.viewers.IStructuredContentProvider;

import org.eclipse.jface.viewers.LabelProvider;

import org.eclipse.jface.viewers.ListViewer;

import org.eclipse.jface.viewers.ViewerFilter;

import org.eclipse.jface.viewers.ViewerSorter;

import org.eclipse.jface.viewers.Viewer;

import org.eclipse.swt.SWT;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.widgets.Composite;


public class Ch8List extends Composite {

public Ch8List(Composite parent){

super(parent,SWT.NULL);

populateControl();

}

protected void populateControl(){

FillLayout compositeLayout = new FillLayout();

setLayout(compositeLayout);

int [] Style = {SWT.SINGLE, SWT.MULTI};

for(int style =0; style<Style.length;style++){

createListViewer(Style[style]);

}

}

private void createListViewer(int style){

ListViewer viewer = new ListViewer(this,style);

viewer.setLabelProvider(new LabelProvider(){

public String getText(Object element){

return ((ListItem)element).name;

}

});

viewer.addFilter(new ViewerFilter(){


public boolean select(Viewer viewer, Object parent, Object element) {

// TODO Auto-generated method stub

return((ListItem)element).value % 2 == 0;

}

});

viewer.setSorter(new ViewerSorter(){

public int compare(Viewer viewer, Object obj1, Object obj2){

return ((ListItem)obj2).value - ((ListItem)obj1).value;

}

});

viewer.setContentProvider(new IStructuredContentProvider(){

public Object[] getElements(Object inputElement){

return ((List)inputElement).toArray();

}

public void dispose(){}

public void inputChanged(Viewer viewer, Object oldInput, Object newInput){}


});

List input = new ArrayList();

for(int i=0;i<20;i++){

input.add(new ListItem("item " + i,i));

}

viewer.setInput(input);

}

}


class ListItem

{

public String name;

public int value;

public ListItem(String n, int v){

name=n;

value = v;

}

}



Viewer.java

package Ch8;


import org.eclipse.jface.window.ApplicationWindow;

import org.eclipse.swt.SWT;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Control;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.TabFolder;

import org.eclipse.swt.widgets.TabItem;


public class Viewer extends ApplicationWindow {

public Viewer()

{

super(null);

}

protected Control createContents(Composite parent){

TabItem chap8List = new TabItem(tf,SWT.NONE);

chap8List.setText("Chapter 8 List");

chap8List.setControl(new Ch8List(tf));

return parent;

}

public static void main(String args[]){

Viewer vi = new Viewer();

vi.setBlockOnOpen(true);

vi.open();

Display.getCurrent().dispose();

}

 

}

'RCP > SWT & JFACE' 카테고리의 다른 글

현재 화면의 활성화된 shell 얻는 방법  (0) 2016.12.23
Tableviwer와 편집모드, 컨텍스트 메뉴 생성  (0) 2016.12.23
JFace의 treeList 예제  (0) 2016.12.23
JFACE TreeViewer 예제  (0) 2016.12.23
SashForm 소개와 예제  (0) 2016.12.22
SWT의 Text와 StyledText  (0) 2016.12.22

TreeViewer 메뉴

- 모든 TreeViewr 구조의 예제를 제작해보았다.


1. Ch8TreeComposite

package Ch8Tree;


import java.util.ArrayList;

import java.util.List;


import org.eclipse.jface.viewers.ITreeContentProvider;

import org.eclipse.jface.viewers.TreeViewer;

import org.eclipse.jface.viewers.Viewer;

import org.eclipse.swt.SWT;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.widgets.Composite;


public class Ch8TreeComposite extends Composite {


public Ch8TreeComposite(Composite parent) {

super(parent, SWT.NULL);

populateControl();

}


protected void populateControl() {

FillLayout compositeLayout = new FillLayout();

setLayout(compositeLayout);


int[] selectionStyle = { SWT.SINGLE, SWT.MULTI };

int[] checkStyle = { SWT.NONE, SWT.CHECK };


for (int selection = 0; selection < selectionStyle.length; selection++) {

for (int check = 0; check < checkStyle.length; check++) {

int style = selectionStyle[selection] | checkStyle[check];

createTreeViewer(style);

}

}

}


private void createTreeViewer(int style) {

TreeViewer viewer = new TreeViewer(this, style);


viewer.setContentProvider(new ITreeContentProvider() {

public Object[] getChildren(Object parentElement) {

return ((TreeNode) parentElement).getChildren().toArray();

}


public Object getParent(Object element) {

return ((TreeNode) element).getParent();

}


public boolean hasChildren(Object element) {

return ((TreeNode) element).getChildren().size() > 0;

}


public Object[] getElements(Object inputElement) {

return ((TreeNode) inputElement).getChildren().toArray();

}


public void dispose() {

}


public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {


}

});

viewer.setInput(getRootNode());

}


private TreeNode getRootNode() {

TreeNode root = new TreeNode("root");

root.addChild(new TreeNode("Child1").addChild(new TreeNode("subchild 1")));

root.addChild(

new TreeNode("Child2").addChild(new TreeNode("subchild 2").addChild(new TreeNode("gradnchild 1"))));

return root;

}

}


class TreeNode{

 private String name;

 private List children = new ArrayList();

 private TreeNode parent;

 

 public TreeNode(String n){

 name = n;

 }

 

 protected Object getParent(){

 return parent;

 }

 

 public TreeNode addChild(TreeNode child)

 {

 children.add(child);

 child.parent = this;

 return this;

 }

 

 public List getChildren(){

 return children;

 }

 

 public String toString(){

 return name;

 }

}


2. Viewer

package Ch8Tree;

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

public class Viewer extends ApplicationWindow {
public Viewer()
{
super(null);
}
protected Control createContents(Composite parent){
TabFolder tf = new TabFolder(parent, SWT.BORDER);
TabItem Ch8Tree = new TabItem(tf, SWT.NONE);
Ch8Tree.setText("Chapter 8 Tree");
Ch8Tree.setControl(new Ch8TreeComposite(tf));
return parent;
}
public static void main(String args[]){
Viewer vi = new Viewer();
vi.setBlockOnOpen(true);
vi.open();
Display.getCurrent().dispose();
}
}


'RCP > SWT & JFACE' 카테고리의 다른 글

현재 화면의 활성화된 shell 얻는 방법  (0) 2016.12.23
Tableviwer와 편집모드, 컨텍스트 메뉴 생성  (0) 2016.12.23
JFace의 treeList 예제  (0) 2016.12.23
JFACE TreeViewer 예제  (0) 2016.12.23
SashForm 소개와 예제  (0) 2016.12.22
SWT의 Text와 StyledText  (0) 2016.12.22

SashForm 이동이 가능한 경계선을 자식 위젯 사이에 생성함으로써 하나의 위젯을 늘리면 다른 한쪽이 감소하게 만들 있다.

 

Sash 클래스는 대부분 위젯처럼 org.eclipse.swt.widges패키지에 위치하지만 SashForm 크래스는 org.eclipse.swt.custom 패키지에서 찾을  있다.

 

Public Ch3_SashForm(Composite parent)

{

Super(parent, SWT.NONE);

 

SashForm sf = new SashForm(this, SWT.VERTICAL);

Sf.setsize(120,80);

 

Button button1 = new Button(sf, SWT.ARROW | SWT.UP);

Button1.setSize(120,40);

 

Button button2 = new Button(sf, SWT.ARROW | SWT.DOWN);

Button2.setbound(0,40,120,40);

}

]

'RCP > SWT & JFACE' 카테고리의 다른 글

현재 화면의 활성화된 shell 얻는 방법  (0) 2016.12.23
Tableviwer와 편집모드, 컨텍스트 메뉴 생성  (0) 2016.12.23
JFace의 treeList 예제  (0) 2016.12.23
JFACE TreeViewer 예제  (0) 2016.12.23
SashForm 소개와 예제  (0) 2016.12.22
SWT의 Text와 StyledText  (0) 2016.12.22

SWT에서는 Text StyledText 두가지를 제공한다.

 

Text 여러 이벤트를 연관 시킬  있다.

addModifyListener() // 텍스트 수정 리스너

addSelectionListener() // 컨트롤 선택 리스너

addVerifyListener() // 텍스트에 변화가 일어났을때 발생

Append() // 추가

Insert() 대체

setSelection(), selectAll() // 선택

setEchoCharacter() // 사용자가 타이핑 하는 문자 대신 보여줄 다른 문자열 지정

setEditable() // 편집을 가능하게 하거나 불가능하게 한다.

setFont()

 

 

StyledText text에서 제공하는 모든 메소드를 제공하고 폰트텍스트 색상폰트 스타일 등으 ㅣ기능이 추가된다.

 

StyledText 키를 바인딩   있다.

 번째 방법은 st 클래스를 이용하여 코드상에 직접 구현하거나, setKeyBinding() 이용하여  입력과 연동 시킬  있다.

StyledText.setKeyBinding( 'Q' | SWT.CONTROl, ST.PASTE);

 

 

StyleRange 이용한 텍스트 스타일

StyledText StyledRange 클래스를 이용하여 서로 다른 스타일을 관리   있다.

StyleRange style  = new StyleRange(int start, int length, Color foreground, Color background, int fontStyle)

styledText.setStyleRange(style);

 

 

Styledtext에서 사용되는 ExtendedModifyListener ModifyListener보다 정확하게 어디서 어떤 부분이 변경되었는지  있다.

Styledtext.addExtendedModifyListener(

New ExtenedModifyListener() {

Public void modifyText(ExtendedModifyEvnet event){

}

}

 

styledText.addkeyListener(new KeyAdapter(){

Public void keyPressed(KeyEvent e){

}

}

 

Jface에서는 텍스트 지원을 하는 Idocument ITextViewer 인터페이스를 제공한다.

Document

Document 실제 텍스트 부분을 보유한다그렇기 때문에 표준 메스드와 더불어 IDocumentListener 통해서 내용 편집에대한 이벤트를 받을  있다.

Idocument 고급기능

  1. Position : 텍스트의 영역(문서의 내용이 변해도 항상 동일한 오프셋을 부여하므로  Document 이용하여 북마크를구현할  있다. -> 추적장치를 제공한다.)
  2. 컨텐츠 타입 구분 : plain text, rich text, html  여러 타입을 지정할  있다. Idocument Partitioner  통해 특정 영역의 컨텐츠 타입을 정의 하여야한다.
  3. 검색 : search() 메소드를 통해 검색을 제공한다특정 패넌을 지원하지는 않지만시작위치 검색방향 소문자 구분단어일 경우와 같은 경우의 검색이 가능하다.

 

ITextViewer

표준 텍스트 위젯을 문서 기분 텍스트 위젯으로 바꾸기 위한 viewer 이다.

StyledText 사용하여 텍스트를 보여준다.

텍스트 수정과 시각적인 이벤트(현재 보이는 지역인  포트에서 발생하는 변경양쪽에 대한 리스너를 지원한다.

 

TextViewer 다양한 플러그인을 제공한다 플러그인을 사용하려면 이런 인터페이스의 인스턴스를 텍스트 뷰어에 할당하고 나서 activatePlugins() 호출해야한다.

유요한 플러그인

Org.eclipse.jface.text.contentassist // 텍스트를 입력할  텍스트 자동 완성을 위한 프레임 워크를 제공하며 IContentAssistant IContentAssistantProcessor 함꼐 동작한다.

Org.eclipse.jface.text.formatter // 텍스트의 서식을 설정하는 유틸리티로서 IContentFormatter IFormattingStrategy 인스턴스를 컨텐트 타입과 함께 등록한다.

Org.eclipse.jface.text.presentation // 변경과 관련하여 문서의 시각적인 모양새를 업데이트 한다.

Org.eclipse.jface.text.reconciler // 문서를 텍스트의 외부 저장소와 함께 동기화 하는데 사용한다.

Org.eclipse.jface.text.rules // 환경 설정이 가능한 Irule 기반혀여 텍스트를 스캔하고 매치하기 위한 클래스단어숫자공백 종료 문자  자주 발생하는 구문을 찾기 위한 내장 규칙을 가지고 있다.

Org.eclipse.jface.text.source // text 컴파일에러 표시등  출력할  사용한다하위 클래스인 Annotation 필요하다.

'RCP > SWT & JFACE' 카테고리의 다른 글

현재 화면의 활성화된 shell 얻는 방법  (0) 2016.12.23
Tableviwer와 편집모드, 컨텍스트 메뉴 생성  (0) 2016.12.23
JFace의 treeList 예제  (0) 2016.12.23
JFACE TreeViewer 예제  (0) 2016.12.23
SashForm 소개와 예제  (0) 2016.12.22
SWT의 Text와 StyledText  (0) 2016.12.22

+ Recent posts