RCP/SWT & JFACE

JFACE TreeViewer 예제

반응형

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
SashForm 소개와 예제  (3) 2016.12.22
SWT의 Text와 StyledText  (0) 2016.12.22