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

From Eclipsepedia

Jump to: navigation, search
m (typo)
 
(One intermediate revision by one user not shown)
Line 1: Line 1:
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.
+
Eclipse does not support 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.
 
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.
Line 6: Line 6:
 
It would look something like this
 
It would look something like this
  
<pre>
+
<source lang="java">
 
/**
 
/**
 
  *  Class that supports Decoration of TableViewer and TreeViewer with TreeColumns
 
  *  Class that supports Decoration of TableViewer and TreeViewer with TreeColumns
Line 23: Line 23:
 
  */
 
  */
 
public class TableDecoratingLabelProvider extends DecoratingLabelProvider
 
public class TableDecoratingLabelProvider extends DecoratingLabelProvider
implements ITableLabelProvider {
+
    implements ITableLabelProvider {
  
 
ITableLabelProvider provider;
 
ITableLabelProvider provider;
Line 32: Line 32:
 
*/
 
*/
 
public TableDecoratingLabelProvider(ILabelProvider provider,
 
public TableDecoratingLabelProvider(ILabelProvider provider,
ILabelDecorator decorator) {
+
    ILabelDecorator decorator) {
super(provider, decorator);
+
  super(provider, decorator);
this.provider = (ITableLabelProvider) provider;
+
  this.provider = (ITableLabelProvider) provider;
        this.decorator = decorator;
+
  this.decorator = decorator;
 
}
 
}
  
Line 42: Line 42:
 
*/
 
*/
 
public Image getColumnImage(Object element, int columnIndex) {
 
public Image getColumnImage(Object element, int columnIndex) {
Image image = provider.getColumnImage(element, columnIndex);
+
  Image image = provider.getColumnImage(element, columnIndex);
        if (decorator != null) {
+
  if (decorator != null) {
            Image decorated = decorator.decorateImage(image, element);
+
    Image decorated = decorator.decorateImage(image, element);
            if (decorated != null) {
+
    if (decorated != null) {
                return decorated;
+
      return decorated;
            }
+
    }
        }
+
  }
        return image;
+
  return image;
 
}
 
}
  
Line 56: Line 56:
 
*/
 
*/
 
public String getColumnText(Object element, int columnIndex) {
 
public String getColumnText(Object element, int columnIndex) {
String text = provider.getColumnText(element, columnIndex);
+
  String text = provider.getColumnText(element, columnIndex);
        if (decorator != null) {
+
  if (decorator != null) {
            String decorated = decorator.decorateText(text, element);
+
    String decorated = decorator.decorateText(text, element);
            if (decorated != null) {
+
    if (decorated != null) {
                return decorated;
+
      return decorated;
            }
+
    }
        }
+
  }
        return text;
+
  return text;
 
}
 
}
}
+
</source>
</pre>
+
  
 
This class can now handle Trees which have Columns and TableViewers. How you use it is specified below
 
This class can now handle Trees which have Columns and TableViewers. How you use it is specified below
Line 72: Line 71:
 
While attaching the LabelProvider to your viewer use this Code
 
While attaching the LabelProvider to your viewer use this Code
  
<pre>
+
<source lang="java">
 
ILabelDecorator decorator = PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator();
 
ILabelDecorator decorator = PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator();
 
<Your Viewer>.setLabelProvider(new TableDecoratingLabelProvider(<Your LabelProvider>, decorator));
 
<Your Viewer>.setLabelProvider(new TableDecoratingLabelProvider(<Your LabelProvider>, decorator));
</pre>
+
</source>
  
 
instead of  
 
instead of  
  
<pre>
+
<source lang="java">
 
ILabelDecorator decorator = PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator();
 
ILabelDecorator decorator = PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator();
 
<Your Viewer>.setLabelProvider(new DecoratingLabelProvider(<Your LabelProvider>, decorator));
 
<Your Viewer>.setLabelProvider(new DecoratingLabelProvider(<Your LabelProvider>, decorator));
</pre>
+
</source>
  
 
--[[User:Annamalai.chockalingam.gmail.com|Annamalai.chockalingam.gmail.com]] 10:35, 31 August 2006 (EDT)
 
--[[User:Annamalai.chockalingam.gmail.com|Annamalai.chockalingam.gmail.com]] 10:35, 31 August 2006 (EDT)

Latest revision as of 09:43, 23 August 2011

Eclipse does not support 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)

[edit] See Also: