Files
Brainstorm/Snippets/src/snippets/TreeTable.java

158 lines
4.9 KiB
Java
Raw Normal View History

2014-05-30 10:31:51 -07:00
package snippets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.events.TreeListener;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* A sample tree table which is a modification of the eclipse snippet 170.
*
* Demonstrates: the size of the open/close icon bug. Setting the width/height variables below to low values makes the open/close graphics
* impossibly small, and the activation rectangle is not over the graphic (it is in fact to the right of the graphic, just to the left of
* the image attached to the row/first column.
*
* Demonstrates: the user opens a node and there is a delay before data is given to the display.
* During the delay we don't want the UI to become non-reponsive, so we thread the collection of the data.
* For some reason the threading causes the buttons to be displayed incorrectly.
* Any change in size of the view results in the correct display.
*/
public class TreeTable {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
int width = 4;
int height = 4;
final Image image = new Image(display, width, height);
final Image image2 = new Image(display, width * 2, height * 2);
GC imageGc = new GC(image);
imageGc.setLineWidth(3);
imageGc.drawLine(0, 0, width, height);
imageGc.dispose();
imageGc = new GC(image2);
imageGc.setLineWidth(3);
imageGc.drawLine(0, 0, width * 2, height * 2);
imageGc.dispose();
final Tree tree = new Tree(shell, SWT.FULL_SELECTION | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
tree.setHeaderVisible(true);
TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
column1.setText("Column 1");
column1.setWidth(130);
//column1.setImage(image);
TreeColumn column2 = new TreeColumn(tree, SWT.CENTER);
column2.setText("Column 2");
column2.setWidth(130);
TreeColumn column3 = new TreeColumn(tree, SWT.RIGHT);
column3.setText("Column 3");
column3.setWidth(130);
for(int i = 0; i < 4; i++) {
TreeItem item = new TreeItem(tree, SWT.NONE);
item.setText(new String[] {"item " + i, "abc", "defghi"});
item.setImage(new Image[] {image2, null, image});
item.setData(Boolean.TRUE);
TreeEditor editor = new TreeEditor(tree);
Button control = new Button(tree, SWT.PUSH);
editor.setEditor(control, item, 1);
control.setText("...");
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.minimumWidth = 30;
editor.minimumHeight = 17;
TreeItem subItem = new TreeItem(item, SWT.NONE);
subItem.setText(new String[] {"", "", ""});
}
tree.addListener(SWT.MeasureItem, new Listener() {
public void handleEvent(Event event) {
switch(event.type) {
case SWT.MeasureItem: {
TreeItem item = (TreeItem) event.item;
event.height = 60;
break;
}//case//
}//switch//
}//handleEvent()//
});
tree.addTreeListener(new TreeListener() {
public void treeExpanded(TreeEvent e) {
final TreeItem item = (TreeItem) e.item;
if((item.getData() != null) && (((Boolean) item.getData()).booleanValue() == true)) {
//Things that were irrelevant to the problem.
//e.doit = false;
//item.setExpanded(false);
//tree.setEnabled(false);
//treeSwtUtilities.setRedraw(, false);
item.setData(Boolean.FALSE);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(400);
} catch(InterruptedException e) {
}
display.syncExec(new Runnable() {
public void run() {
TreeItem[] oldItems = item.getItems();
for(int index = 0; index < oldItems.length; index++) {
oldItems[index].dispose();
}
for(int j = 0; j < 4; j++) {
TreeItem subItem = new TreeItem(item, SWT.NONE);
subItem.setText(new String[] {"subitem " + j, "jklmnop", "qrs"});
}
TreeItem topItem = tree.getTopItem();
//shellSwtUtilities.setRedraw(, false);
tree.pack();
shell.layout(true, true);
tree.setTopItem(topItem);
//shellSwtUtilities.setRedraw(, true);
shell.redraw();
//Things that were irrelevant to the problem.
//item.setExpanded(true);
//tree.setEnabled(true);
//treeSwtUtilities.setRedraw(, true);
/* Some things I tried to force a refresh...
tree.layout(true, true);
tree.redraw();
tree.update();
*/
}
});
}
}).start();
}
}
public void treeCollapsed(TreeEvent e) {
}
});
shell.pack();
Rectangle bounds = shell.getBounds();
bounds.height += 100;
shell.setBounds(bounds);
shell.open();
while(!shell.isDisposed()) {
if(!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}