How to add alternate colors to rows in Jface tableviewer - swt

I am using a Jface table viewer with OwnerDrawLabelProvider for multiline rows, now I want to add alternate colors to the rows. I tried using colLabelProvider, but it is overwriting OwnerDrawLabelProvider. How do I achieve both?
Thanks in advance!

If you are using OwnerDrawLabelProvider you will have to do this in your provider. You can fill the cell background in the erase method using something like:
#Override
protected void erase(Event event, Object element)
{
Table table = (Table)event.widget;
TableItem item = (TableItem)event.item;
int row = table.indexOf(item);
Color color;
if ((row & 1) == 0)
color = event.gc.getDevice().getSystemColor(SWT.COLOR_GREEN);
else
color = event.gc.getDevice().getSystemColor(SWT.COLOR_RED);
event.gc.setBackground(color);
event.gc.fillRectangle(event.x, event.y, event.width, event.height);
}

Related

Java SWT - Create a pure SWT Table with different color on even and odd rows

I am very new to the SWT and I am trying to create a Java SWT table with different color on even and odd rows (ex: 1st row is red, second row is blue, third row is res, fourth row is blue ... ).
Since the rows on table can be deleted, inserted so if I manually set color by using a for loop I will have to run that loop again and again to set color every time there's something change in the table. And I don't want to use JFace, just pure Java SWT
Do you have some better solutions for this problems?
You can use custom drawn table items .
By adding a listener for the SWT.EraseItem you can just draw the background and let the rest do the default implementation. Use the item field of the event to decide whether an even or odd row is to be drawn.
For example:
table.addListener( SWT.EraseItem, new Listener() {
#Override
public void handleEvent( Event event ) {
int index = table.indexOf( ( TableItem )event.item );
if( index % 2 == 0 ) {
Color oldBackground = event.gc.getBackground();
event.gc.setBackground( event.display.getSystemColor( SWT.COLOR_DARK_YELLOW ) );
event.gc.fillRectangle( 0, event.y, table.getClientArea().width, event.height );
event.gc.setBackground( oldBackground );
}
}
} );

Eclipse Birt - Set style cell table dynamically with Event Handler

I have Birt table in a report and i want to create a handler class that change the style color of the cell in the table dynamically if the cell contains the max of the column.
I try this for first step and i hooked the class to the row of the table report.
public class RowMinMax implements IRowEventHandler
{
public void onCreate(IRowInstance rowInstance, IReportContext reportContext)
{
double max=0;
IRowData rowData = rowInstance.getRowData();
double cellValue = (double) rowData.getColumnValue("nameColumn");
if(cellValue>max)
{
//change cell style
}
} ... //OTHER METHOD INTERFACE
}
I can not find a suitable method to find the object cell and set the style (es. font color red).
How can I fix this?
Thanks
Why so complicated? You can achieve this with Javascript in the rptdesign file. No need to use a custom Java class.
First, you should compute the maximum (let's call it "maxValue") using BIRT's aggregation columns - either in the DataSet itself or in the Table item definition.
Next, you can define a function like this in the reports's initialize event:
function modifyStyle(cell, row) {
if (row["nameColumn"] >= row["maxValue"]) {
cell.getStyle().borderLeftWidth = "3px";
cell.getStyle().borderRightWidth = "3px";
}
}
In the cell's onCreate event, call the function:
modifyStyle(this, row);

GWT Cell Table Column Sort Handler

Im using a GWT Cell Table with sorting enabled on the columns. I am also providing an option of filtering the column. This is roughly how the header looks.
----ColumnName (Image) -------
When the user clicks on the image, i start off a filtering process. I achieved this using the Cell Table's addCellPreviewHandler. The issue I am facing is , when the image is clicked , the column sorts itself too. Can anyone tell me how to prevent this from happening ? To be clearer, I want the sort to be called when the user clicks anywhere in the header but the image.
Thanks.
breakDownCellTable.addColumn(indexedColumn, columnHeader);
final int currentColumnIndex = columnIndex;
// Add a ColumnSortEvent.ListHandler to connect sorting to the
// java.util.List.
ListHandler<List<GwtContributorCellData>> columnSortHandler = new ListHandler<List<GwtContributorCellData>>(contributorList);
columnSortHandler.setComparator(indexedColumn, new Comparator<List<CustomClass>>() {
public int compare(List<CustomClass> o1, List<CustomClass> o2) {
if (o1 == o2) {
return 0;
}
// Compare the columns.
if (o1 != null) {
return (o2 != null) ? o1.get(currentColumnIndex).compareTo(o2.get(currentColumnIndex)) : 1;
}
return -1;
}
});
breakDownCellTable.addColumnSortHandler(columnSortHandler);
and in the onBrowserEvent method:
if("click".equals(event.getType())){
EventTarget eventTarget = event.getEventTarget();
if(eventTarget.toString().contains("img") && !eventTarget.toString().contains("<th")){
//event.stopPropagation();
// Window.alert("here");
//breakDownCellTable.fireEvent()
ColumnSortEvent.fire(breakDownCellTable, breakDownCellTable.getColumnSortList());
Your can implement a subclass of the cell you're using and override the onBrowserEvent() method.
This post explains how to find out which DOM element is the exact source of the event.
Once you know that the image has been clicked or not, fire the filtering / sorting event accordingly.

GWT:how to change row color in GWT Celltable

i have a celltable in GWT, I can change color of a specific column by this
celltable.addColumnStyleName(4, "bluetext");
but how can i change for example color of row No 3
Thanks
You have to provide a RowStyles object that returns css class names for each row. So, to set a particular color for a row, you'd have to define a css class with that color, and then cause your RowStyles object to return that class for the relevant rows.
I think you set this with cellTable.setRowStyles or something similar.
cellTable.setRowStyles(new RowStyles<T>() {
#Override
public String getStyleNames(T rowObject, int rowIndex) {
if (rowIndex == 3) {
return "bluetext";
} else {
return "normaltext";
}
});
If you need to update row color based on a value changed in one of the cells, you can add the following code to the fieldUpdater of this cell:
#Override
public void update(int index, Object object, String value) {
if (someConditionIsMet) {
myTable.getRowElement(index).addClassName("redBackground");
}
}
In your CSS file add this style:
.redBackground {
background-color: red !important;
}
To answer the last comment that the style is in the row element but is not being rendered:
Using setRowStyles(new RowStyles() ...
The only way I got the styles to appear was to use brute force.
I had to remove the row from my List store, add it back to the same index and then refresh the RowModel.
For what it's worth.

Editable SWT table

How to edit SWT table Values without Using Mouse Listeners?
Do the TableEditor snippets in the below link help?
SWT Snippets
The first example in the TableEditor section uses a SelectionListener on the table (unlike the second example which uses a MouseDown event you mentioned you don't want)
You could perhaps make use of the TraverseListener or KeyListener too to help you achieve what you want.
final int EDITABLECOLUMN = 1;
tblProvisionInfo.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
// Clean up any previous editor control
final TableEditor editor = new TableEditor(tblProvisionInfo);
// The editor must have the same size as the cell and must
// not be any smaller than 50 pixels.
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
editor.minimumWidth = 50;
Control oldEditor = editor.getEditor();
if (oldEditor != null)
oldEditor.dispose();
// Identify the selected row
TableItem item = (TableItem) e.item;
if (item == null)
return;
// The control that will be the editor must be a child of the
// Table
Text newEditor = new Text(tblProvisionInfo, SWT.NONE);
newEditor.setText(item.getText(EDITABLECOLUMN));
newEditor.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent me) {
Text text = (Text) editor.getEditor();
editor.getItem()
.setText(EDITABLECOLUMN, text.getText());
}
});
newEditor.selectAll();
newEditor.setFocus();
editor.setEditor(newEditor, item, EDITABLECOLUMN);
}
});
Here tblProvision is the name of your table. you can just now edit Your table by clicking on it. I have Declare EDITABLECOLUMN. this is the column that u want to edit.
If you can use JFace as well and not just pain SWT, have a look at the JFace Snippets, especially
Snippet036FocusBorderCellHighlighter - Demonstrates keyboard navigation by highlighting the currently selected cell with a focus border showing once more the flexibility of the new cell navigation support
Snippet034CellEditorPerRowNewAPI - Demonstrates different CellEditor-Types in one COLUMN with 3.3-API of JFace-Viewers
You can get or set the value of a item, for example:
Table table = new Table(parent, SWT.NONE);
TableItem item = new TableItem(table, SWT.NONE);
item.setText("My new Text");
I suggest you to us TableViewer, it is very powerful table which it you can use databinding very easy too.