Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Dynamic TableTree
Dynamic TableTree [message #445994] Mon, 15 November 2004 19:01
Nicole is currently offline NicoleFriend
Messages: 26
Registered: July 2009
Junior Member
Hi,

I am having some problems with my TableTreeViewer. Appreciate any help
that anyone can render me. Thanks.

My TableTreeViewer uses TableTreeEditors with a Canvas as its control to
paint each cell's contents. The number of columns in the TableTree is
dynamic, i.e. the user can add or delete them at any point of time.

Somehow my code is incorrect or incomplete, such that when the user
deletes a column, the cells under that column is still being painted. I
must be missing something here. I have pasted some of the code in my
application. Hope that someone can help point out my mistake. Thanks a lot.

Regards,
Nicole

P.s. Some of the code were modified based on another message posted in
this newsgroup by Thomas Fletcher. Thomas, hope you dun mind me using this
code in my application. Thanks. :)

/----------------------------------------------------------- -----------------/
private void init() {
Table table = getTableTree().getTable();
TableLayout tlayout = new TableLayout();
GC gc = new GC(table);

dataSourcesColumn = new TableColumn(table, SWT.NONE);
dataSourcesColumn.setText("Source");
dataSourcesColumn.setWidth(150);

tlayout.addColumnData(new ColumnWeightData(gc
.stringExtent(dataSourcesColumn.getText()).x));
gc.setForeground(new Color(null, 255, 255, 255));

table.setLayout(tlayout);
table.setHeaderVisible(true);
table.setLinesVisible(true);
gc.dispose();

ItemFilter filter = new ItemFilter();
setContentProvider(new TableTreeContentProvider());
setLabelProvider(new TableTreeLabelProvider());
addFilter(filter);
}

public void hookListeners() {
EntityManager.instance().addPropertyChangeListener(this);

int ops = DND.DROP_DEFAULT | DND.DROP_COPY;
Transfer[] transfers = new Transfer[] { ParticipatingModelTransfer
.getInstance() };
addDropSupport(ops, transfers, new ParticipatingModelDropAdapter(this));

getTableTree().addTreeListener(new TreeListener() {
public void treeCollapsed(TreeEvent e) {
set_editors();
}

public void treeExpanded(TreeEvent e) {
set_editors();
}
});
}

public void propertyChange(PropertyChangeEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getPropertyName().equals("MODELS")) {
EntityType entity = (EntityType) arg0.getNewValue();
Vector models = entity.get_models();

TableColumn[] columns = getTableTree().getTable().getColumns();
for (int i = 1; i < columns.length; i++) {
columns[i].dispose();
}

final Menu menu = new Menu(getTableTree());
MenuItem deleteItem = new MenuItem(menu, SWT.PUSH);
deleteItem.setText("Delete");
deleteItem.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
Object obj = menu.getData();
if (obj instanceof TableColumn) {
TableColumn column = (TableColumn) obj;
String modelName = (String) column.getData("MODEL");

((EntityType) getInput())
.removeParticipatingModel(modelName);
}
}

public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub

}
});
MenuItem viewItem = new MenuItem(menu, SWT.PUSH);
viewItem.setText("View...");
viewItem.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
MessageDialog.openInformation(Display.getCurrent()
.getActiveShell(), "Info", "View selected.");
}

public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub

}
});

for (int i = 0; i < models.size(); i++) {
ModelType model = (ModelType) models.elementAt(i);
String name = model.getModelIdentification().getName();

TableColumn modelInputColumn = new TableColumn(getTableTree()
.getTable(), SWT.CENTER | SWT.WRAP);
modelInputColumn.setData("MODEL", name);
modelInputColumn.setText(name);
modelInputColumn.setImage(input);
modelInputColumn.setWidth(150);
modelInputColumn.setAlignment(SWT.CENTER);
modelInputColumn.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
menu.setData(e.widget);
menu.setVisible(true);
}
});

TableColumn modelOutputColumn = new TableColumn(getTableTree()
.getTable(), SWT.CENTER | SWT.WRAP);
modelOutputColumn.setData("MODEL", name);
modelOutputColumn.setText(name);
modelOutputColumn.setImage(output);
modelOutputColumn.setWidth(150);
modelOutputColumn.setAlignment(SWT.CENTER);
modelOutputColumn.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
menu.setData(e.widget);
menu.setVisible(true);
}
});

GC gc = new GC(getTableTree().getTable());

TableLayout tlayout = (TableLayout) getTableTree().getTable()
.getLayout();
tlayout.addColumnData(new ColumnWeightData(gc
.stringExtent(modelInputColumn.getText()).x));
tlayout.addColumnData(new ColumnWeightData(gc
.stringExtent(modelOutputColumn.getText()).x));
gc.setForeground(new Color(null, 255, 255, 255));

gc.dispose();
}
refresh();

set_editors();
}
}

private void set_editors() {
TableTree ttable = getTableTree();
TableTreeItem[] items = ttable.getItems();
System.out.println(items.length + " ITEMS");

for (int i = 0; i < items.length; i++) {
set_editors_for_children(ttable, ttable.getTable(), items[i]);
}
}

public void set_editors_for_children(TableTree tableTree, Table table,
TableTreeItem tableTreeItem) {
if (tableTreeItem.getItemCount() > 0 && tableTreeItem.getExpanded()) {
// tableTreeItem.setBackground(new Color(null, 147, 154, 180));
TableTreeItem sub[] = tableTreeItem.getItems();
System.out.println("CHILDREN : " + sub.length);
for (int i = 0; i < sub.length; i++) {
set_editors_for_children(tableTree, table, sub[i]);
}
}
// tableTreeItem.setBackground(new Color(null, 167, 187, 180));
for (int col = 1; col < table.getColumns().length; col++) {
add_editor(tableTree, table, tableTreeItem, col);
}
}

void add_editor(TableTree tableTree, Table table,
TableTreeItem tableTreeItem, int column) {
TableTreeEditor editor = new TableTreeEditor(tableTree);

editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
final int col = column;
final TableTreeItem tti = tableTreeItem;

TableColumn[] columns = tableTree.getTable().getColumns();
String model = columns[col].getText();
final Canvas canvas = new Canvas(table, SWT.NONE);
canvas.setData(tti);

canvas.setData("STATE", State.FALSE);
canvas.setData("MODEL", model);

canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
IOModel mod = null;
TableTreeItem it = null;
Object o = canvas.getData();
if (o instanceof TableTreeItem) {
it = (TableTreeItem) o;
if (it == null || it.isDisposed()) {
System.out.println("it is disposed");
canvas.setBackground(new Color(null, 255, 255, 255));
return;
}
Object obj = it.getData();
System.out.println("it is not disposed : " + obj);
if (obj instanceof IOModel)
mod = (IOModel) obj;
}
Rectangle r = canvas.getClientArea();

int x = r.width / 2;
if (mod == null) {
e.gc.drawImage(off, x, 0);
canvas.setData("STATE", State.FALSE);
} else {
String modName = (String) canvas.getData("MODEL");
System.out.println("modName : " + modName);
if(modName == null || modName.equals("")) {
canvas.setBackground(new Color(null, 255, 255, 255));
return;
}
if (col % 2 == 0) {
State state = mod.getOutState(modName);
if (state.equals(State.TRUE)) {
canvas
.setBackground(new Color(null, 100, 100,
200));
e.gc.drawImage(on, x, 0);
canvas.setData("STATE", State.TRUE);
} else if (state.equals(State.PARTIAL)) {
e.gc.drawImage(partial, x, 0);
canvas.setData("STATE", State.PARTIAL);
} else if (state.equals(State.DISABLED)) {
e.gc.drawImage(disabled, x, 0);
canvas.setData("STATE", State.DISABLED);
} else {
canvas
.setBackground(new Color(null, 150, 150,
255));
e.gc.drawImage(off, x, 0);
canvas.setData("STATE", State.FALSE);
}
} else {
State state = mod.getInState(modName);
if (state.equals(State.TRUE)) {
canvas
.setBackground(new Color(null, 100, 100,
200));
e.gc.drawImage(on, x, 0);
canvas.setData("STATE", State.TRUE);
} else if (state.equals(State.PARTIAL)) {
e.gc.drawImage(partial, x, 0);
canvas.setData("STATE", State.PARTIAL);
} else if (state.equals(State.DISABLED)) {
e.gc.drawImage(disabled, x, 0);
canvas.setData("STATE", State.DISABLED);
} else {
canvas
.setBackground(new Color(null, 150, 150,
255));
e.gc.drawImage(off, x, 0);
canvas.setData("STATE", State.FALSE);
}
}
}
}

});

editor.setEditor(canvas, tti, col);
canvas.redraw();
}
Previous Topic:Look of the views
Next Topic:Drag and Drop in a Tree with dynamic insertion mark?
Goto Forum:
  


Current Time: Wed Oct 28 21:23:29 GMT 2020

Powered by FUDForum. Page generated in 0.06376 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top