Eclipse TableViewer set custom row background - eclipse

How can a make my jface tableviewer display a chosen background color for specific rows?
I have a timestamp in each row and like to give a row thats timestamp is on a monday a color, different than the others.

With ColumnLabelProvider it is even easier:
col.setLabelProvider(new ColumnLabelProvider() {
#Override
public Color getBackground(final Object element) {
if (element instanceof YourClass) {
if (((YourClass) element).shouldBeRed()) {
return new Color(Display.getDefault(), 0xFF, 0xDD, 0xDD);
}
}
return super.getBackground(element);
}
});
Of course you should not create new colors on each getBackground but use resource managers for that purpose.

Something in the lines of:
col.setLabelProvider(new ColumnLabelProvider() {
#Override
public void update(final ViewerCell cell) {
YourRowItemClass rowItem = (YourRowItemClass) cell.getElement();
if (rowItem.isMonday()) {
cell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_DARK_GREEN));
}
}
});

Related

Smart GWT List Grid - Setting a hilite to a list grid on record click

I'm trying to set a hilite inside the record click handler of the list grid. I have tired the following code,
My hilites are as follows,
public static Hilite[] getWayBillSetHilites() {
return new Hilite[]{
new Hilite() {
{
setFieldNames("RECORD_VIEWED_STATUS");
setCriteria(new Criterion("RECORD_VIEWED_STATUS", OperatorId.EQUALS, "TRUE"));
setCssText(Constant.Css.TEXT_ITALIC_GRAY_32);
setTextColor("font-style:italic;color:#525252;");
setId("0");
}
}
};
}
record click handler of the grid appears as follows,
grid.addRecordClickHandler(new RecordClickHandler() {
#Override
public void onRecordClick(RecordClickEvent recordClickEvent) {
//gridWayBillSetGrid.getHiliteState()
//make RECORD_VIEWED_STATUS value "true"
recordClickEvent.getRecord().setAttribute("RECORD_VIEWED_STATUS", true);
gridWayBillSetGrid.enableHilite("0", true);
}
});
But when I click on the record, the styles are not showing up.
Please be kind to advise on this.
I think it's the wrong use case for hilites. Use getCellCSSText instead.
Try this one (override getCellCSSText method of ListGrid class):
ListGrid grid = new ListGrid(...){
#Override
protected String getCellCSSText(ListGridRecord record, int rowNum, int colNum) {
if("true".equalsIgnoreCase(record.getAttribute("RECORD_VIEWED_STATUS"))){
return "font-style:italic;color:#525252;";
}
return super.getCellCSSText(record, rowNum, colNum);
}
};

TreeViewer color row alternatively

I am new to eclipse SWT. I am trying to override the getBackground method of ITableColorProvider to color rows alternatively of a treeViewer. I was trying coloring with row index (index%2 == 0). It colors all the rows instead.
TreeViewer colors one cell at a time, instead of rows. Any pointers on how to achieve it (alternate row color for treeviewer) or code snippet will be very helpful.
List<TreeItem> treeItems = Arrays.asList( m_viewer.getTree().getItems() );
int index = treeItems.indexOf( element );
if( index % 2 == 0 )
{
backgroundColor = Display.getDefault().getSystemColor(
SWT.COLOR_YELLOW );
}
else
{
backgroundColor = Display.getDefault().getSystemColor(
SWT.COLOR_GRAY );
}
ITableColorProvider is used for TableViewer , for TreeViewer your class that extends LabelProvider should implement IColorProvider
public class MyLabelProvider extends LabelProvider implements IColorProvider{
#Override
public String getText(Object element) {
//how the label is obtained for an element
}
#Override
public Color getBackground(Object element) {
if(((TreeItem) element).getId() % 2 == 0) {
return Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
}else{
return Display.getCurrent().getSystemColor(SWT.COLOR_RED);
}
}
#Override
public Color getForeground(Object element) {
return null;
}
}
The Color Class is the one from org.eclipse.swt.graphics.Color. I supposed that every TreeItem has an id property that is generated cosecutively

Hide Column in CellTable

How to hide the columns in Cell table? I need column for grouping purpose but don't want to show in table. Is there any way to hide? Any clue? See the below Code..
TextColumn<ContactInfor> ageTxt = new TextColumn<ContactInfor>() {
#Override
public String getValue(ContactInfor object) {
return object.age;
}
};
cellTable.addColumn(ageTxt, "AGE");
TextColumn<ContactInfor> empIdTxt = new TextColumn<ContactInfor>() {
#Override
public String getValue(ContactInfor object) {
return object.empId;
}
};
cellTable.addColumn(empIdTxt, "EMPLOYEE ID");
TextColumn<ContactInfor> addressTxt = new TextColumn<ContactInfor>() {
#Override
public String getValue(ContactInfor object) {
return object.address;
}
};
cellTable.addColumn(addressTxt, "ADDRESS");
Want to hide addressTxt column from cell table?
Assuming you can't actually remove it from the table, perhaps just set the width to 0:
cellTable.setColumnWidth(addressTxt, "0px");
Javadocs: http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/cellview/client/CellTable.html#setColumnWidth(com.google.gwt.user.cellview.client.Column,java.lang.String)

GWT CellTable-Need to have two buttons in last single cell of each row

I am using CellTable to add columns to it.
It works fine when I add rows and single data on each cell.
It has header like Name ,Age, Address with rows below it which contains the values
I now want to have a Actions cloumn in the last with two buttons (Edit and Delete Button) in single cell in on the rows below this column and to capture the button click events acordingly.
Name Age Address Actions
A 15 123 Edit Delete
B 20 578 Edit Delete
C
Could you please let me know how to do it.
Thanks
There are two ways to achieve that:
Subclass AbstractCell and implement the render method to create two buttons and handle its events (see here for more details).
Use a CompositeCell to add two ActionCells
Second approach is easier and cleaner. Here is the code for that:
public void onModuleLoad() {
CellTable<Person> table = new CellTable<Person>();
List<HasCell<Person, ?>> cells = new LinkedList<HasCell<Person, ?>>();
cells.add(new ActionHasCell("Edit", new Delegate<Person>() {
#Override
public void execute(Person object) {
// EDIT CODE
}
}));
cells.add(new ActionHasCell("Delete", new Delegate<Person>() {
#Override
public void execute(Person object) {
// DELETE CODE
}
}));
CompositeCell<Person> cell = new CompositeCell<Person>(cells);
table.addColumn(new TextColumn<Person>() {
#Override
public String getValue(Person object) {
return object.getName()
}
}, "Name");
// ADD Cells for Age and Address
table.addColumn(new Column<Person, Person>(cell) {
#Override
public Person getValue(Person object) {
return object;
}
}, "Actions");
}
private class ActionHasCell implements HasCell<Person, Person> {
private ActionCell<Person> cell;
public ActionHasCell(String text, Delegate<Person> delegate) {
cell = new ActionCell<Person>(text, delegate);
}
#Override
public Cell<Person> getCell() {
return cell;
}
#Override
public FieldUpdater<Person, Person> getFieldUpdater() {
return null;
}
#Override
public Person getValue(Person object) {
return object;
}
}
The solution above is perfect!THX.
In addition: If you liked to design the buttons in the ActionCell, then you could do this -> In the constructon of the class you can build a html input and in "class" attribute, you can add a css style, which will be used.:
public ActionHasCell(String text, Delegate<Person> delegate) {
cell = new ActionCell<Person>(text, delegate) {
public void render(Context context, Person person, SafeHtmlBuilder sb)
{
SafeHtml html = SafeHtmlUtils.fromTrustedString("<input type=\"button\" value=\"anynameyouwant\" class=\"cssstylename\" />");
sb.append(html);
}
};
}
This time you don't need the String, but you can pass parameters and use them to build the button.

How to hide column in Cell table GWT?

I am using Cell Table in GWT.In that cell table I am adding these columns.
TextColumn<Document> idColumn = new TextColumn<Document>() {
#Override
public String getValue(Document object) {
return Long.toString(object.getId());
}
};
TextColumn<Document> refColumn = new TextColumn<Document>() {
#Override
public String getValue(Document object) {
return object.getReferenceNumber();
}
};
/*
* DateCell dateCell = new DateCell(); Column<Contact, Date> dateColumn
* = new Column<Contact, Date>(dateCell) {
*
* #Override public Date getValue(Contact object) { return
* object.birthday; } };
*/
TextColumn<Document> nameColumn = new TextColumn<Document>() {
#Override
public String getValue(Document object) {
return object.getDocumentName();
}
};
table = new CellTable<T>();
table.addColumn(idColumn, "Id");
table.addColumn(refColumn, "Reference Number");
table.addColumn(nameColumn, "Name");
}
Now I have some queries:
How to hide the id column?
On click of row how can i get the from selected row?
Please help me out.
Thanks in advance.
Well you could try to use fixed layout for the CellTable and set the width of the specific column you want to hide to 0px.
I did use another approach.
In my case I have a cellTable which should display a checkbox column as soon as I press a button (which puts the celltable in edit mode).
I do this by creating a CheckBoxColumn and inserting and removing it when I press on the button. It looks seomething like that:
#Override
public void insertCheckBoxColumn(Column<Object,Boolean> column) {
if (cellTable.getColumnIndex(column) == -1) {
cellTable.addColumn(column,"");
cellTable.setColumnWidth(column,50, Unit.PX);
}
}
#Override
public void removeCheckBoxColumn(Column<Object, Boolean> column) {
int index = cellTable.getColumnIndex(column);
if (index != -1)
cellTable.removeColumn(index);
}
However note that you might run into this issue on google chrome.