Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » TableViewer and RGB
TableViewer and RGB [message #514959] Wed, 17 February 2010 10:43 Go to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 225
Registered: July 2009
Senior Member
Hello,

how can I show in a TableViewer RGB Color like in the ColorCellEditor? The Viewer shows only the String, but I want to show the Color. How can i do this?

import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.ColorCellEditor;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
 
import snippet.TestEditor.Kunde;
import snippet.TestEditor.MyCellEditingSupport;
import snippet.TestEditor.MyCellLabelProvider;
 
public class Snippet007FullSelection {  
    public static class MyModel {
        private RGB rgb;
        
        public MyModel(RGB rgb) {
            this.setRgb(rgb);
        }
 
        public void setRgb(RGB rgb) {
            this.rgb = rgb;
        }
 
        public RGB getRgb() {
            return rgb;
        }
    
    }
    
    public Snippet007FullSelection(Shell shell) {
        
        Composite tableComposite = new Composite(shell, SWT.NONE);
        TableColumnLayout tableColumnLayout = new TableColumnLayout();
        tableComposite.setLayout(tableColumnLayout);
        final TableViewer v = new TableViewer(tableComposite,SWT.BORDER | SWT.SINGLE);
 
        
        final TableViewerColumn viewerNameColumn = new TableViewerColumn(
                v, SWT.NONE);
        viewerNameColumn.getColumn().setText("RGB");
        tableColumnLayout.setColumnData(viewerNameColumn.getColumn(),
                new ColumnWeightData(100, 10, true));
        v.setLabelProvider(new ILabelProvider() {
            
            @Override
            public void removeListener(ILabelProviderListener listener) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public boolean isLabelProperty(Object element, String property) {
                // TODO Auto-generated method stub
                return false;
            }
            
            @Override
            public void dispose() {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void addListener(ILabelProviderListener listener) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public String getText(Object element) {
                // TODO Auto-generated method stub
                return ((MyModel)element).getRgb().toString();
            }
            
            @Override
            public Image getImage(Object element) {
                // TODO Auto-generated method stub
                return null;
            }
        });
        v.setContentProvider(new ArrayContentProvider());
        v.setCellEditors(new CellEditor[] {new ColorCellEditor(v.getTable()) });
        v.setColumnProperties(new String[]{"rgb"});
        v.setCellModifier(new ICellModifier() {
            
            @Override
            public void modify(Object element, String property, Object value) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public Object getValue(Object element, String property) {
                return null;
            }
            
            @Override
            public boolean canModify(Object element, String property) {
                return true;
            }
        });
        MyModel[] model = createModel();
        v.setInput(model);
        v.getTable().setLinesVisible(true);
        v.getTable().setHeaderVisible(true);
        
        
    }
    
    private MyModel[] createModel() {
        MyModel[] elements = new MyModel[10];
        
        for( int i = 0; i < 10; i++ ) {
            elements[i] = new MyModel(new RGB(0,0, 0));
        }
        
        return elements;
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        Display display = new Display ();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        new Snippet007FullSelection(shell);
        shell.open ();
        
        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        
        display.dispose ();
 
    }
 
}
Re: TableViewer and RGB [message #514964 is a reply to message #514959] Wed, 17 February 2010 10:48 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5487
Registered: July 2009
Senior Member
You should not set the LabelProvider on the TableViewer but a
ColumnLabelProvider on the column and overwrite getBackgroundColor()

Please take a look at our snippets http://wiki.eclipse.org/JFaceSnippets

Tom

Am 17.02.10 11:43, schrieb SirWayne:
> Hello,
> how can I show in a TableViewer RGB Color like in the ColorCellEditor?
> The Viewer shows only the String, but I want to show the Color. How can
> i do this?
>
>
> import org.eclipse.jface.layout.TableColumnLayout;
> import org.eclipse.jface.viewers.ArrayContentProvider;
> import org.eclipse.jface.viewers.CellEditor;
> import org.eclipse.jface.viewers.CellLabelProvider;
> import org.eclipse.jface.viewers.ColorCellEditor;
> import org.eclipse.jface.viewers.ColumnWeightData;
> import org.eclipse.jface.viewers.EditingSupport;
> import org.eclipse.jface.viewers.ICellModifier;
> import org.eclipse.jface.viewers.ILabelProvider;
> import org.eclipse.jface.viewers.ILabelProviderListener;
> import org.eclipse.jface.viewers.IStructuredContentProvider;
> import org.eclipse.jface.viewers.LabelProvider;
> import org.eclipse.jface.viewers.TableViewer;
> import org.eclipse.jface.viewers.TableViewerColumn;
> import org.eclipse.jface.viewers.TextCellEditor;
> import org.eclipse.jface.viewers.Viewer;
> import org.eclipse.jface.viewers.ViewerCell;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.graphics.Image;
> import org.eclipse.swt.graphics.RGB;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Event;
> import org.eclipse.swt.widgets.Listener;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.TableColumn;
> import org.eclipse.swt.widgets.TableItem;
>
> import snippet.TestEditor.Kunde;
> import snippet.TestEditor.MyCellEditingSupport;
> import snippet.TestEditor.MyCellLabelProvider;
>
> public class Snippet007FullSelection { public static class MyModel {
> private RGB rgb;
> public MyModel(RGB rgb) {
> this.setRgb(rgb);
> }
>
> public void setRgb(RGB rgb) {
> this.rgb = rgb;
> }
>
> public RGB getRgb() {
> return rgb;
> }
> }
> public Snippet007FullSelection(Shell shell) {
> Composite tableComposite = new Composite(shell, SWT.NONE);
> TableColumnLayout tableColumnLayout = new TableColumnLayout();
> tableComposite.setLayout(tableColumnLayout);
> final TableViewer v = new TableViewer(tableComposite,SWT.BORDER |
> SWT.SINGLE);
>
> final TableViewerColumn viewerNameColumn = new
> TableViewerColumn(
> v, SWT.NONE);
> viewerNameColumn.getColumn().setText("RGB");
> tableColumnLayout.setColumnData(viewerNameColumn.getColumn() ,
> new ColumnWeightData(100, 10, true));
> v.setLabelProvider(new ILabelProvider() {
> @Override
> public void removeListener(ILabelProviderListener listener) {
> // TODO Auto-generated method stub
> }
> @Override
> public boolean isLabelProperty(Object element, String
> property) {
> // TODO Auto-generated method stub
> return false;
> }
> @Override
> public void dispose() {
> // TODO Auto-generated method stub
> }
> @Override
> public void addListener(ILabelProviderListener listener) {
> // TODO Auto-generated method stub
> }
> @Override
> public String getText(Object element) {
> // TODO Auto-generated method stub
> return ((MyModel)element).getRgb().toString();
> }
> @Override
> public Image getImage(Object element) {
> // TODO Auto-generated method stub
> return null;
> }
> });
> v.setContentProvider(new ArrayContentProvider());
> v.setCellEditors(new CellEditor[] {new
> ColorCellEditor(v.getTable()) });
> v.setColumnProperties(new String[]{"rgb"});
> v.setCellModifier(new ICellModifier() {
> @Override
> public void modify(Object element, String property, Object
> value) {
> // TODO Auto-generated method stub
> }
> @Override
> public Object getValue(Object element, String property) {
> return null;
> }
> @Override
> public boolean canModify(Object element, String property) {
> return true;
> }
> });
> MyModel[] model = createModel();
> v.setInput(model);
> v.getTable().setLinesVisible(true);
> v.getTable().setHeaderVisible(true);
> }
> private MyModel[] createModel() {
> MyModel[] elements = new MyModel[10];
> for( int i = 0; i < 10; i++ ) {
> elements[i] = new MyModel(new RGB(0,0, 0));
> }
> return elements;
> }
> /**
> * @param args
> */
> public static void main(String[] args) {
> Display display = new Display ();
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
> new Snippet007FullSelection(shell);
> shell.open ();
> while (!shell.isDisposed ()) {
> if (!display.readAndDispatch ()) display.sleep ();
> }
> display.dispose ();
>
> }
>
> }
>
Re: TableViewer and RGB [message #514974 is a reply to message #514964] Wed, 17 February 2010 11:43 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 225
Registered: July 2009
Senior Member
Hello,

thanks for your answer. Which Snippet do you mean?
If i modify my code, it will paint the whole line with cyan. I want only a small Image like in the CellEditor. Do i have to create the image myself?

        viewerNameColumn.setLabelProvider(new ColumnLabelProvider(){
        	
        	public String getText(Object element) {
        		return "";
//				return element.toString();
			}
        	
        	@Override
        	public Color getBackground(Object element) {
        		return v.getTable().getDisplay().getSystemColor(SWT.COLOR_CYAN);
        	}

		});


How can I show a checkbox e.g in col 2? Is there a Snippet?I only find the Snippet056BooleanCellEditor, but i miss the BooleanCellEditor class.
Re: TableViewer and RGB [message #514976 is a reply to message #514974] Wed, 17 February 2010 11:54 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5487
Registered: July 2009
Senior Member
The CellEditor is also part of the snippet project the best thing is to
checkout project from cvs.

Yes you need to generate the image on the file or use the
OwnerDraw-LabelProvider to put your Table into ownerdraw mode and draw
on the GC.

Tom

Am 17.02.10 12:43, schrieb SirWayne:
> Hello,
>
> thanks for your answer. Which Snippet do you mean?
> If i modify my code, it will paint the whole line with cyan. I want only
> a small Image like in the CellEditor. Do i have to create the image myself?
>
>
> viewerNameColumn.setLabelProvider(new ColumnLabelProvider(){
>
> public String getText(Object element) {
> return "";
> // return element.toString();
> }
>
> @Override
> public Color getBackground(Object element) {
> return
> v.getTable().getDisplay().getSystemColor(SWT.COLOR_CYAN);
> }
>
> });
>
>
> How can I show a checkbox e.g in col 2? Is there a Snippet?I only find
> the Snippet056BooleanCellEditor, but i miss the BooleanCellEditor class.
Re: TableViewer and RGB [message #515044 is a reply to message #514976] Wed, 17 February 2010 14:48 Go to previous message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 225
Registered: July 2009
Senior Member
Ah thanks!!! I have the BooleanEditor and my Owner drawer. It works fine. Smile

No iam looking for a example how i can show a checkbox or button in a TableViewerColumn. Is there a own CellProvider?
Previous Topic:Adding a MouseTrackListener to a TreeViewerColumn
Next Topic:Implementing IPartitionTokenScanner With Multi-Line Tokens
Goto Forum:
  


Current Time: Mon Dec 22 07:55:41 GMT 2014

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

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