Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Aligment in a TableViewer
Aligment in a TableViewer [message #885116] Tue, 12 June 2012 13:46 Go to next message
Joaquin Morcate is currently offline Joaquin MorcateFriend
Messages: 52
Registered: March 2010
Member
Hi all,

I have the following problem with a TableViewer. I have a TableViwer that is displaying a set of properties with their values. Some of these properties has values that are just text and some are better displayed as images. The problem is that the viewer reserve space for the images in all the rows, doesn't matter if there image or not.
-----------------------------------------
|  prop1       |                val1    |
-----------------------------------------
|  prop1       |                val2    |
-----------------------------------------
| prop3        | <    image   >         |
-----------------------------------------


but I will like to have:
-----------------------------------------
|  prop1       | val1                   |
-----------------------------------------
|  prop1       | val2                   |
-----------------------------------------
| prop3        | <    image   >         |
-----------------------------------------


Any ideas how can I get that alignment?

Thank you very much.

Joaquin Morcate
Re: Aligment in a TableViewer [message #885156 is a reply to message #885116] Tue, 12 June 2012 14:39 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
You need to use owner draw.

Tom

Am 12.06.12 15:46, schrieb Joaquin Morcate:
> Hi all,
>
> I have the following problem with a TableViewer. I have a TableViwer
> that is displaying a set of properties with their values. Some of these
> properties has values that are just text and some are better displayed
> as images. The problem is that the viewer reserve space for the images
> in all the rows, doesn't matter if there image or not.
>
> -----------------------------------------
> | prop1 | val1 |
> -----------------------------------------
> | prop1 | val2 |
> -----------------------------------------
> | prop3 | < image > |
> -----------------------------------------
>
>
> but I will like to have:
>
> -----------------------------------------
> | prop1 | val1 |
> -----------------------------------------
> | prop1 | val2 |
> -----------------------------------------
> | prop3 | < image > |
> -----------------------------------------
>
>
> Any ideas how can I get that alignment?
>
> Thank you very much.
>
> Joaquin Morcate
>
Re: Aligment in a TableViewer [message #885215 is a reply to message #885156] Tue, 12 June 2012 16:14 Go to previous messageGo to next message
Joaquin Morcate is currently offline Joaquin MorcateFriend
Messages: 52
Registered: March 2010
Member
Hi Tom,

Thank you very much for your answer. I'm not really an expert in SWT. Could you please give some hints about how to use this owner draw?

Thank you very much in advance

Joaquin
Re: Aligment in a TableViewer [message #885569 is a reply to message #885215] Wed, 13 June 2012 09:00 Go to previous messageGo to next message
Jan Krakora is currently offline Jan KrakoraFriend
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
Check this snippet:
http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippets/viewers/Snippet010OwnerDraw.java?revision=1.8&view=co
Re: Aligment in a TableViewer [message #885634 is a reply to message #885569] Wed, 13 June 2012 11:03 Go to previous messageGo to next message
Joaquin Morcate is currently offline Joaquin MorcateFriend
Messages: 52
Registered: March 2010
Member
Thanks a lot for you answer Jan!

In the mean time I have managed to solve my problem. I have added a Listener for SWT.EraseTime and SWT.PaintItem events. The code is more or less as follows:

TableViewer viewer = new TableViewer(...);
Table table = viewer.getTable();
Listener paintListener = new Listener() {
   public void handleEvent(Event event) {
        sitch (event.type) {
        case SWT.EraseItem:
           // I need to fixe only the second column (index=1)
           // when there is no image.
           if (event.index == 1 && item.getImage(1)==null) {
               // Don't make native painting of the foreground we will take care 
               // of it. at the reception of the SWT.PaintEvent
               event.detail &= ~SWT.FOREGROUND;
            }
            break;
        case SWT.Paint:
           if (event.index == 1 && item.getImage(1)==null) {
               // Draw the text where the image is expected.
               event.gc.drawString(item.getText(), item.getImageBounds(1).x, item.getImageBounds(1).y);
           }
        break;
        }
    }
}
table.addListener(SWT.EraseItem,paintListener);
table.addListener(SWT.PaintItem,paintListener);


This is my first solution. I would try to find a better one.

Joaquin
Re: Aligment in a TableViewer [message #889797 is a reply to message #885634] Tue, 19 June 2012 16:46 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Your approach is generally correct, but since you're using a TableViewer
(not just a Table), you're probably expected to hook your Paint and
Erase listeners in an OwnerDrawLabelProvider like in
http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippets/viewers/Snippet010OwnerDraw.java?revision=1.8&view=co
..

In case you haven't seen the article yet that describes Table/Tree owner
draw in detail see
http://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html
..

Grant


On 6/13/2012 7:03 AM, Joaquin Morcate wrote:
> Thanks a lot for you answer Jan!
>
> In the mean time I have managed to solve my problem. I have added a
> Listener for SWT.EraseTime and SWT.PaintItem events. The code is more or
> less as follows:
>
>
> TableViewer viewer = new TableViewer(...);
> Table table = viewer.getTable();
> Listener paintListener = new Listener() {
> public void handleEvent(Event event) {
> sitch (event.type) {
> case SWT.EraseItem:
> // I need to fixe only the second column (index=1)
> // when there is no image.
> if (event.index == 1 && item.getImage(1)==null) {
> // Don't make native painting of the foreground we will
> take care // of it. at the reception of the SWT.PaintEvent
> event.detail &= ~SWT.FOREGROUND;
> }
> break;
> case SWT.Paint:
> if (event.index == 1 && item.getImage(1)==null) {
> // Draw the text where the image is expected.
> event.gc.drawString(item.getText(),
> item.getImageBounds(1).x, item.getImageBounds(1).y);
> }
> break;
> }
> }
> }
> table.addListener(SWT.EraseItem,paintListener);
> table.addListener(SWT.PaintItem,paintListener);
>
>
> This is my first solution. I would try to find a better one.
> Joaquin
Re: Aligment in a TableViewer [message #889805 is a reply to message #889797] Tue, 19 June 2012 17:23 Go to previous messageGo to next message
Joaquin Morcate is currently offline Joaquin MorcateFriend
Messages: 52
Registered: March 2010
Member
Thanks a lot Grant for your comment. Actually, today I've working on that and I was thinking to update the example. But still I haven't find a solution that is good enough.

Joaquin
Re: Aligment in a TableViewer [message #890011 is a reply to message #889797] Wed, 20 June 2012 14:51 Go to previous messageGo to next message
Joaquin Morcate is currently offline Joaquin MorcateFriend
Messages: 52
Registered: March 2010
Member
Hi,

Still I am not able to find a good solution to my problem. If I use a OwnerDrawLabelProvider as Grant suggested yesterday, I get a good rendering of my values (i.e. the text entries are aligned to the left without space for the images). This is the code:

valueColumn.setLabelProvider(new OwnerDrawLabelProvider() {             
	@Override                                                           
	public void update(ViewerCell cell) {                               
		ReadoutEntry entry = (ReadoutEntry) cell.getElement();          
		cell.setText(entry.getStringValue());                           
		cell.setImage(entry.getImageValue(Display.getDefault()));       
	}                                                                   
                                                                        
	@Override                                                           
	protected void paint(Event event, Object element) {                 
		ReadoutEntry entry = (ReadoutEntry) element;                    
		GC gc = event.gc;                                               
		Table table = (Table) event.widget;                             
		int width = table.getColumn(0).getWidth();                      
		if (entry.getImageValue(table) != null) {                       
			gc.drawImage(entry.getImageValue(Display.getDefault()),     
					width + 2, event.y + 2);                            
                                                                        
		} else {                                                        
			gc.drawText(entry.getStringValue(), width + 2, event.y + 2, 
					true);                                              
		}                                                               
	}                                                                   
                                                                        
	@Override                                                           
	protected void erase(Event event, Object element) {                 
		event.detail &= ~SWT.FOREGROUND;                                
	}                                                                   
                                                                        
	@Override                                                           
	protected void measure(Event event, Object element) {               
                                                                        
	}                                                                   
});                                                                     


But if I enable the editors they are placed in the space defined by textBounds. This is a snapshot:

http://i1099.photobucket.com/albums/g383/CitizenKino/editor.jpg


The problem, I guess, is that I am not able to modify the image and text bounds in the TableItem's. But I'm really confused now. May be there is a simpler solution or I'm doing something wrong.

Any help?


Joaquin

Re: Aligment in a TableViewer [message #890112 is a reply to message #890011] Thu, 21 June 2012 07:37 Go to previous message
Joaquin Morcate is currently offline Joaquin MorcateFriend
Messages: 52
Registered: March 2010
Member
Well, I'm a little bit embarrassed Embarrassed

The solution is obvious:

valueColumn.setLabelProvider(new OwnerDrawLabelProvider() {             
        // Don't even think on update!!!!
        
        /**
         * Paint an image or text as required. I never need to paint both.
         */                                    
	@Override                                                           
	protected void paint(Event event, Object element) {                 
		Contact entry = (Contact) element;                    
		GC gc = event.gc;                                               
		Table table = (Table) event.widget;                             
		int width = table.getColumn(0).getWidth();                      
		if (entry.getImageValue(table) != null) {                       
			gc.drawImage(entry.getImageValue(),     
					width + 2, event.y + 2);                            
                                                                        
		} else {                                                        
			gc.drawText(entry.getStringValue(), width + 2, event.y + 2, 
					true);                                              
		}                                                               
	}                                                                   
          
        // Forget erase. As I don't fill the cell I don't need to signal that
        // I will draw the contents myself.                                                              
         
        /**
         * Images are normally taller that text so I need to adjust the height 
         * before painting.
         */                                                    
	@Override                                                           
	protected void measure(Event event, Object element) {               
 		Contact entry = (Contact) element; 
                Image image = entry.getImageValue();    
                if (image!=null) {
                    event.height=image.getBounds().height;
                }
                                                                     
	}                                                                   
});                                                                     


I wonder why I always realize when I am in my car driving home and I have to wait till the day after to check it.

Joaquin
Previous Topic:Dynamically change the parent without disposing the children
Next Topic:SWT Table - Performance
Goto Forum:
  


Current Time: Thu Apr 18 01:22:23 GMT 2024

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

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

Back to the top