Difference between revisions of "FAQ How to decorate a TableViewer or TreeViewer with Columns?"

From Eclipsepedia

Jump to: navigation, search
Line 10: Line 10:
 
  *  Class that supports Decoration of TableViewer and TreeViewer with TreeColumns
 
  *  Class that supports Decoration of TableViewer and TreeViewer with TreeColumns
 
  */
 
  */
package com.bosch.mars.in.decorators;
+
package decorators;
  
 
import org.eclipse.jface.viewers.DecoratingLabelProvider;
 
import org.eclipse.jface.viewers.DecoratingLabelProvider;
Line 19: Line 19:
  
 
/**
 
/**
  * @author anc2kor
+
  * @author Annamalai Chockalingam
 
  *
 
  *
 
  */
 
  */

Revision as of 06:17, 1 September 2006

Eclipse doesnt not provide a provision for decorating TableViewers and TreeViewers with TreeColumns as they have proposed Decorators for TreeViewers. But in many cases where we use TreeViewers with TreeColumns or TableViewers we need to provide some decorations based on some Condition.

This can be achieved by extending the DecoratingLabelProvider class and creating your own class called as <Your Class Name> for e.g.TableDecoratingLabelProvider and implement ITableLabelProvider in that class.


It would look something like this

/**
 *  Class that supports Decoration of TableViewer and TreeViewer with TreeColumns
 */
package decorators;

import org.eclipse.jface.viewers.DecoratingLabelProvider;
import org.eclipse.jface.viewers.ILabelDecorator;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.swt.graphics.Image;

/**
 * @author Annamalai Chockalingam
 *
 */
public class TableDecoratingLabelProvider extends DecoratingLabelProvider
implements ITableLabelProvider {

ITableLabelProvider provider;
ILabelDecorator decorator;
/**
* @param provider
* @param decorator
*/
public TableDecoratingLabelProvider(ILabelProvider provider,
ILabelDecorator decorator) {
super(provider, decorator);
this.provider = (ITableLabelProvider) provider;
        this.decorator = decorator;
}

/* (non-Javadoc)
* @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
*/
public Image getColumnImage(Object element, int columnIndex) {
Image image = provider.getColumnImage(element, columnIndex);
        if (decorator != null) {
            Image decorated = decorator.decorateImage(image, element);
            if (decorated != null) {
                return decorated;
            }
        }
        return image;
}

/* (non-Javadoc)
* @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
*/
public String getColumnText(Object element, int columnIndex) {
String text = provider.getColumnText(element, columnIndex);
        if (decorator != null) {
            String decorated = decorator.decorateText(text, element);
            if (decorated != null) {
                return decorated;
            }
        }
        return text;
}
}

This class can now handle Trees which have Columns and TableViewers. How you use it is specified below

While attaching the LabelProvider to your viewer use this Code

ILabelDecorator decorator = PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator();
<Your Viewer>.setLabelProvider(new TableDecoratingLabelProvider(<Your LabelProvider>, decorator));

instead of

ILabelDecorator decorator = PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator();
<Your Viewer>.setLabelProvider(new DecoratingLabelProvider(<Your LabelProvider>, decorator));

--Annamalai.chockalingam.gmail.com 10:35, 31 August 2006 (EDT)

See Also: