[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: Adding element in TableViewer and edit it

The table is configured by this (3 columns the RGB values and 4th column a color chooser):

final CellEditor compEditor = new TextCellEditor(table);
final CellEditor colorCellEditor = new ColorCellEditor(table);

colorTable.setCellEditors(new CellEditor[] {
	                              compEditor, compEditor,
	                              compEditor, colorCellEditor
		                          });



Here the implementation of ICellModifier:

colorTable.setCellModifier(new ICellModifier() {

@Override
public boolean canModify(final Object element, final String property)
{
return true;
}
@Override
public Object getValue(final Object element, final String property)
{
final RGB rgb = (RGB) element;
if (COLUMN_RED.equals(property)) {
return Integer.toString(rgb.red);
}


   if (COLUMN_GREEN.equals(property)) {
     return Integer.toString(rgb.green);
   }

   if (COLUMN_BLUE.equals(property)) {
     return Integer.toString(rgb.blue);
   }

   if (COLUMN_RGB.equals(property)) {
     return rgb;
   }

@Override
public void modify(final Object element, final String property, final Object value)
{
isDirty = true;


   final RGB rgb = (RGB) ((TableItem) element).getData();

   if (COLUMN_RED.equals(property)) {
     try {
       rgb.red = limitRGB(Integer.parseInt((String) value));
     } catch (final NumberFormatException nfe) {
     }

     colorTable.update(rgb, null);
   } else if (COLUMN_GREEN.equals(property)) {
     try {
       rgb.green = limitRGB(Integer.parseInt((String) value));
     } catch (final NumberFormatException nfe) {
     }

     colorTable.update(rgb, null);
   } else if (COLUMN_BLUE.equals(property)) {
     try {
       rgb.blue = limitRGB(Integer.parseInt((String) value));
     } catch (final NumberFormatException nfe) {
     }

     colorTable.update(rgb, null);
   } else if (COLUMN_RGB.equals(property)) {
     final RGB col = (RGB) value;
     rgb.red = col.red;
     rgb.green = col.green;
     rgb.blue = col.blue;

colorTable.update(rgb, null);
} else {
throw new IllegalArgumentException("Unsupported property for editing: " + property);
}
}
});