/**
*
*/
package de.tutorials;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
* @author Tom
*/
public class JFaceFilteredListExample extends ApplicationWindow {
public JFaceFilteredListExample(Shell parentShell) {
super(parentShell);
setBlockOnOpen(true);
}
@Override
protected boolean showTopSeperator() {
return false;
}
@Override
protected Control createContents(Composite parent) {
parent.setLayout(new GridLayout(1, true));
final Text txtSearch = new Text(parent, SWT.BORDER);
final ListViewer listViewer = new ListViewer(parent);
TreeNode root = createTreeModel();
Object[] flattenedTree = flatten(root);
listViewer.setSorter(new ViewerSorter());
listViewer.setContentProvider(new ArrayContentProvider());
listViewer.setInput(flattenedTree);
listViewer.addFilter(new ViewerFilter() {
public boolean select(Viewer viewer, Object parentElement, Object element) {
return element.toString().toLowerCase().startsWith(txtSearch.getText().toLowerCase());
}
});
txtSearch.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
listViewer.refresh();
}
});
parent.pack();
return parent;
};
private Object[] flatten(TreeNode root) {
List<TreeNode> nodes = new ArrayList<TreeNode>();
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode currentNode = stack.pop();
nodes.add(currentNode);
for (TreeNode child : currentNode.children) {
stack.push(child);
}
}
System.out.println(nodes);
return nodes.toArray();
}
private TreeNode createTreeModel() {
TreeNode root = new TreeNode("root");
TreeNode nodeA = root.addChild(new TreeNode("A"));
TreeNode nodeAA = nodeA.addChild(new TreeNode("AAX"));
TreeNode nodeAB = nodeA.addChild(new TreeNode("AB"));
TreeNode nodeABA = nodeAB.addChild(new TreeNode("ABAX"));
TreeNode nodeB = root.addChild(new TreeNode("B"));
TreeNode nodeBA = nodeB.addChild(new TreeNode("BA"));
TreeNode nodeBB = nodeB.addChild(new TreeNode("BBX"));
return root;
}
/**
* @param args
*/
public static void main(String[] args) {
new JFaceFilteredListExample(new Shell(SWT.MIN)).open();
}
static class TreeNode {
String id;
int value;
List<TreeNode> children;
TreeNode parent;
public TreeNode(String id) {
this.id = id;
this.value = id.hashCode();
this.children = new ArrayList<TreeNode>();
}
TreeNode addChild(TreeNode node) {
this.children.add(node);
node.parent = this;
return node;
}
TreeNode[] getChildren() {
return this.children.toArray(new TreeNode[children.size()]);
}
boolean hasChildren() {
return !this.children.isEmpty();
}
TreeNode getParent() {
return this.parent;
}
/**
* @return the id
*/
public String getId() {
return this.id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the value
*/
public int getValue() {
return this.value;
}
/**
* @param value the value to set
*/
public void setValue(int value) {
this.value = value;
}
public String toString() {
return this.id + ": " + this.value;
}
}
}