Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Modifying KTable
Modifying KTable [message #465598] Thu, 15 December 2005 18:31 Go to next message
Greg is currently offline GregFriend
Messages: 21
Registered: July 2009
Junior Member
I've been wrestling with this for a few days; and I'm almost ready to give
up.

I'd like to add "Syntax" coloring to the cells on a KTable.

I thought I could overwrite the DefaultCellRenderer.drawCellContent, but
that is not working as I'd expect.

To color individual letters, I take the rect that drew the cell, and write
individual rects for each letter (setting foreground color appropriately).
But it looks like there is a conflict between the rect that cell needs and
the individual rects that I'm creating.

Here's my drawCellContect

Please tell me there is a way to do this (Eclipse 3.1; KTable 2.1.2; Win XP)
Any help is greatly appreciated

protected void drawCellContent(GC gc, Rectangle rect, String text,
Image img, Color textColor, Color backColor) {

if (text.length() == 0) {
gc.setForeground(this.COLOR_TEXT);
gc.setBackground(backColor);
gc.fillRectangle(rect);
SWTX.drawTextImage(gc, text, getAlignment(), img, getAlignment(),
rect.x + 3, rect.y + 2, rect.width - 6, rect.height - 4);
} else {
int printedCharacters = 0 ;
while (printedCharacters < text.length()) {
AsmBasedScanner scanner = new AsmBasedScanner(text) ;
IToken token = scanner.nextToken() ;
Rectangle tokenRect = new Rectangle(rect.x+3, rect.y+2, rect.height-4,
rect.width-6) ;
gc.setBackground(backColor);

while (token!=Token.EOF) {
Color color = ((TextAttribute)token.getData()).getForeground();

int tokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() ;
String textBlock = text.substring(scanner.getTokenOffset(), tokenEnd) ;

System.out.println("Scanning " + text + "\n" +
"from " + scanner.getTokenOffset() + " to " + tokenEnd + ".\n" +
"Drawing " + textBlock );

//figure out where to draw based on textblock size

int fontWidth = gc.textExtent(textBlock).x ;
int fontOffset = printedCharacters*fontWidth ;
//offset the font by the width of previous written text.

//reduce the tokenRec so it's just big enough to contain this token
text
gc.setForeground(color);
gc.setBackground(backColor);
tokenRect.x = tokenRect.x + fontOffset ;
tokenRect.width = textBlock.length()*fontWidth ;


System.out.println("Drawing image bounded by " + tokenRect.x + ", " +
tokenRect.y + ", " + tokenRect.width + ", " + tokenRect.height) ;
SWTX.drawTextImage(gc, textBlock, getAlignment(), img, getAlignment(),
tokenRect.x, tokenRect.y , tokenRect.width ,
tokenRect.height);
printedCharacters += textBlock.length() ;
token = scanner.nextToken() ;
}

}
}
}
Re: Modifying KTable [message #465641 is a reply to message #465598] Thu, 15 December 2005 23:56 Go to previous messageGo to next message
Lorenz Maierhofer is currently offline Lorenz MaierhoferFriend
Messages: 88
Registered: July 2009
Member
Hi,

> To color individual letters, I take the rect that drew the cell, and write
> individual rects for each letter (setting foreground color appropriately).
> But it looks like there is a conflict between the rect that cell needs and
> the individual rects that I'm creating.

I've heard about a reuse of the StyledText widget infrastructure
(StyleRange, etc.) to implement a KTableCellRenderer. This would surely
solve your problems, but I think this was closed source work.

I am not sure what your exact problem is - could you explain the effects
you see when executing your code?
KTable restricts the cell renderer to the bounds of the cell it should
paint - so maybe there is a conflict with those bounds. Check out
gc.setClipping() / gc.getClipping().

Lorenz
Re: Modifying KTable [message #465645 is a reply to message #465641] Fri, 16 December 2005 01:39 Go to previous messageGo to next message
Greg Akins is currently offline Greg AkinsFriend
Messages: 4
Registered: July 2009
Junior Member
Lorenz Maierhofer wrote:

> I've heard about a reuse of the StyledText widget infrastructure
> (StyleRange, etc.) to implement a KTableCellRenderer. This would surely
> solve your problems, but I think this was closed source work.

> I am not sure what your exact problem is - could you explain the effects
> you see when executing your code?
> KTable restricts the cell renderer to the bounds of the cell it should
> paint - so maybe there is a conflict with those bounds. Check out
> gc.setClipping() / gc.getClipping().

Thanks. I'll look into using StyledText, if I can get more information.

The behavior I get varies depending on whether I paint the background of
the full rect (after painting the rect for each character).

I think the main issue is that I'm creating text over top of the widget
that is actually getting keyboard input for the cell. When it partially
works, the text appears after entering the first time, but disappears when
I refresh the screen (by minimizing, or hiding) or by returning focus to
the cell.

-greg
Re: Modifying KTable [message #465648 is a reply to message #465645] Fri, 16 December 2005 07:58 Go to previous messageGo to next message
Lorenz Maierhofer is currently offline Lorenz MaierhoferFriend
Messages: 88
Registered: July 2009
Member
Hi,

> The behavior I get varies depending on whether I paint the background of
> the full rect (after painting the rect for each character).
You would have to fillRect() the cell area first with the background
color. Then draw your text letter by letter and switch the foreground
color as needed. Try not to use SWTX.drawTextImage(), but check out that
implementation for some more basic stuff (handling GC directly). I
assume the SWTX class makes assumptions that might not be correct for
your case.

> I think the main issue is that I'm creating text over top of the widget
> that is actually getting keyboard input for the cell. When it partially
> works, the text appears after entering the first time, but disappears
> when I refresh the screen (by minimizing, or hiding) or by returning
> focus to the cell.

OK, this sounds interesting.
First, note that there are 2 different classes involved in showing the
content.
- The renderer draws the content for the table cell.
- The celleditor is invoked as soon as you start to edit your cell.

Since you probably use KTableCellEditorText or something similar when
you talk about getting keyboard input, you can't get colors there. The
class uses a Text widget, but it should be possible to use a StyledText
instead.

You described that the text is rendererd correctly the first time, but
on subsequent paint events it disappears. Check if you are really using
the same renderer class (KTableModel.doGetCellRenderer()), and if the
correct cell content can be retrieved from the model.

Lorenz
Re: Modifying KTable [message #465659 is a reply to message #465648] Fri, 16 December 2005 15:04 Go to previous message
Greg is currently offline GregFriend
Messages: 21
Registered: July 2009
Junior Member
"Lorenz Maierhofer" <lorenz.m@gmx.at> wrote in message
news:dnts62$nfr$1@news.eclipse.org...
> Hi,
>
>> The behavior I get varies depending on whether I paint the background of
>> the full rect (after painting the rect for each character).
> You would have to fillRect() the cell area first with the background
> color. Then draw your text letter by letter and switch the foreground
> color as needed. Try not to use SWTX.drawTextImage(), but check out that
> implementation for some more basic stuff (handling GC directly). I assume
> the SWTX class makes assumptions that might not be correct for your case.
>

Thanks! This worked (and a lot easier than I thought it would. I just
subclassed SWTX and overrode drawTextVerticalAlign

> OK, this sounds interesting.
> First, note that there are 2 different classes involved in showing the
> content.
> - The renderer draws the content for the table cell.
> - The celleditor is invoked as soon as you start to edit your cell.
>
> Since you probably use KTableCellEditorText or something similar when you
> talk about getting keyboard input, you can't get colors there. The class
> uses a Text widget, but it should be possible to use a StyledText instead.

Not sure if I'll have to do this immediately but thanks for the direction.
I'll post back when I get a closer look at this.
Previous Topic:Setting context menu on editable CCombo
Next Topic:Calling Sort on Trees and Tables
Goto Forum:
  


Current Time: Sat Dec 14 17:47:44 GMT 2024

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

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

Back to the top