help needed with custom drawing in SWT.Tree

T

Tech Id

Hello,

In the tiny piece of code below, I am not able to figure out whats
wrong with the custom drawing of code.
I want it to use for displaying text that spans multiple columns
(starting from column 0)

Thanks a lot in advance.
Techie



class test {

Display display;
Shell shell;

public test () {
display = new Display ();
shell = new Shell (display);

text_spanning_all_columns ();

shell.pack ();
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}

void text_spanning_all_columns () {
shell.setLayout (new FillLayout());
final Tree table = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
int columnCount = 4;
for (int i=0; i<columnCount; i++) {
TreeColumn column = new TreeColumn(table, SWT.NONE);
column.setText("Column " + i);
}
int itemCount = 8;
for (int i = 0; i < itemCount; i++) {
TreeItem item = new TreeItem(table, SWT.NONE);
item.setText(0, "item "+i+" a");
item.setText(3, "item "+i+" d");
TreeItem subItem = new TreeItem (item, SWT.NONE);
}
/*
* NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
* Therefore, it is critical for performance that these methods be
* as efficient as possible.
*/
final String string = "text that spans two columns and much much
more";
GC gc = new GC(table);
final Point extent = gc.stringExtent(string);
gc.dispose();
final Color red = display.getSystemColor(SWT.COLOR_RED);
Listener paintListener = new Listener() {
public void handleEvent(Event event) {
switch(event.type) {
case SWT.MeasureItem: {
TreeItem item = (TreeItem) event.item;
if (item.getParentItem() != null) {
//event.width = extent.x/2;
//event.height = Math.max(event.height, extent.y + 2);
}
break;
}
case SWT.PaintItem: {

TreeItem item = (TreeItem) event.item;
if (item.getParentItem() != null) {
int offset = 0;
for (int i=0; i<event.index; i++)
offset += table.getColumn(i).getWidth();

event.gc.setForeground(red);
int y = event.y + (event.height - extent.y)/2;
event.gc.drawString(string, event.x - offset, y, true);
}
break;
}
}
}
};
table.addListener(SWT.MeasureItem, paintListener);
table.addListener(SWT.PaintItem, paintListener);
for (int i = 0; i < columnCount; i++) {
table.getColumn(i).pack();
}
}
}


public class abc {

public static void main(String [] args) {
new test();
}
}
 
J

Jeff Higgins

Hello,

In the tiny piece of code below, I am not able to figure out whats
wrong with the custom drawing of code.
I want it to use for displaying text that spans multiple columns
(starting from column 0)

Thanks a lot in advance.
Techie



class test {

Display display;
Shell shell;

public test () {
display = new Display ();
shell = new Shell (display);

text_spanning_all_columns ();

shell.pack ();
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}

void text_spanning_all_columns () {
shell.setLayout (new FillLayout());
final Tree table = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
int columnCount = 4;
for (int i=0; i<columnCount; i++) {
TreeColumn column = new TreeColumn(table, SWT.NONE);
column.setText("Column " + i);
}
int itemCount = 8;
for (int i = 0; i< itemCount; i++) {
TreeItem item = new TreeItem(table, SWT.NONE);
item.setText(0, "item "+i+" a");
item.setText(3, "item "+i+" d");
TreeItem subItem = new TreeItem (item, SWT.NONE);
}
/*
* NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
* Therefore, it is critical for performance that these methods be
* as efficient as possible.
*/
final String string = "text that spans two columns and much much
more";
GC gc = new GC(table);
final Point extent = gc.stringExtent(string);
gc.dispose();
final Color red = display.getSystemColor(SWT.COLOR_RED);
Listener paintListener = new Listener() {
public void handleEvent(Event event) {
switch(event.type) {
case SWT.MeasureItem: {
TreeItem item = (TreeItem) event.item;
if (item.getParentItem() != null) {
//event.width = extent.x/2;
//event.height = Math.max(event.height, extent.y + 2);
}
break;
}
case SWT.PaintItem: {

TreeItem item = (TreeItem) event.item;
if (item.getParentItem() != null) {
int offset = 0;
for (int i=0; i<event.index; i++)
offset += table.getColumn(i).getWidth();

event.gc.setForeground(red);
int y = event.y + (event.height - extent.y)/2;
event.gc.drawString(string, event.x - offset, y, true);
}
break;
}
}
}
};
table.addListener(SWT.MeasureItem, paintListener);
table.addListener(SWT.PaintItem, paintListener);
for (int i = 0; i< columnCount; i++) {
table.getColumn(i).pack();
}
}
}


public class abc {

public static void main(String [] args) {
new test();
}
}

class test {

Display display;
Shell shell;

public test() {
display = new Display();
shell = new Shell(display);
text_spanning_all_columns();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

void text_spanning_all_columns () {
shell.setLayout (new FillLayout());
final Tree table = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
int columnCount = 4;
for (int i=0; i<columnCount; i++) {
TreeColumn column = new TreeColumn(table, SWT.NONE);
column.setText("Column " + i);
}
int itemCount = 8;
for (int i = 0; i < itemCount; i++) {
TreeItem item = new TreeItem(table, SWT.NONE);
item.setText(0, "item "+i+" a");
item.setText(3, "item "+i+" d");
TreeItem subItem = new TreeItem (item, SWT.NONE);
}
/*
* NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
* Therefore, it is critical for performance that these methods be
* as efficient as possible.
*/
final String string =
"text that spans two columns and much much more";
GC gc = new GC(table);
final Point extent = gc.stringExtent(string);
gc.dispose();
final Color red = display.getSystemColor(SWT.COLOR_RED);
Listener paintListener = new Listener() {
public void handleEvent(Event event) {
switch(event.type) {
case SWT.MeasureItem: {
TreeItem item = (TreeItem) event.item;
if (item.getParentItem() != null) {
//event.width = extent.x/2;
//event.height = Math.max(event.height, extent.y + 2);
}
break;
}
case SWT.PaintItem: {
TreeItem item = (TreeItem) event.item;
if (item.getParentItem() != null) {
int offset = 0;
for (int i=0; i<event.index; i++) {
offset += table.getColumn(i).getWidth();
}
event.gc.setForeground(red);
int y = event.y + (event.height - extent.y)/2;
event.gc.drawString(string, event.x - offset, y, true);
}
break;
}
}
}
};
table.addListener(SWT.MeasureItem, paintListener);
table.addListener(SWT.PaintItem, paintListener);
for (int i = 0; i < columnCount; i++) {
table.getColumn(i).pack();
}
}
}
 
L

Lew

Tech said:
In the tiny piece of code below, I am not able to figure out whats
wrong with the custom drawing of code.

Side notes:

Do not use TAB to indent source listings for Usenet, use up to four space
characters per indent level.

You should follow the naming conventions.
http://java.sun.com/docs/codeconv/index.html
I want it to use for displaying text that spans multiple columns
(starting from column 0) ....
public class abc {

public static void main(String [] args) {
new test();
}
}

Aside from what Jeff told you, you need to ensure that GUI actions occur on
the Event Dispath Thread (EDT), as explained in the Swing tutorial, or strange
things will happen.
 
T

Tech Id

Yes, the code is copied from the snippets site.
The original code only makes a cell (at column 1) span 2 columns for
Table widget and it works well for Tree Widget too.

But I want cell at column 0 to span all the columns.
That's why the above code.

Above actually works but there is some tiny-miny bug that messes up
the string starting and ending :(
I was hoping to get help with that.
 
T

Tech Id

On 7/9/2010 2:35 AM, Tech Id wrote:> No one could get the bug here?

It looks like you are drawing the string twice, once in column0 and
again in column1. I don't have the time to investigate the API but you
might want to check Event.index() and how it relates to your
multi-column Tree widget. There is a group discussion focused on the SWT
API located at:
<http://www.eclipse.org/forums/eclipse.platform.swt>

The drawing has to be done twice I guess.
This is so because a cell only draws that much amount as can be fit
inside it.
So for cell0, we draw from the beginning of text and the cell and it
displays till column width.
For cell1, we draw text from the beginning too but from cell.start-
column0.width. This makes sure we draw it right for both the cells.
And this is also the way it has been drawn in the original SWT example
but its done for Table widget and for 2nd and 3rd column. And it works
there :( but not here :( :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top