TreeViewer color row alternatively - eclipse

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

Related

Javafx Combobox update issue when updating the observable values

I have create a ComboBox in JavaFx as in the following image
Then i have selected a particular value which shows up in the button cell as shown in the figure.
Then i updated its value so if there is a change in the value i can observe it. But it is not displaying the changed value but an object value.
When i again select from the dropdown view the selected value becomes correct.
The code for the Combobox is
ComboBox<AspectRatio> aspectRatio = new ComboBox<>();
aspectRatio.setCellFactory(new AspectRatioCellFactory());
aspectRatio.setButtonCell(new AspectRatioCell());
Cell Factory
class AspectRatioCellFactory implements Callback<ListView<AspectRatio>, ListCell<AspectRatio>> {
#Override
public ListCell<AspectRatio> call(ListView<AspectRatio> param) {
return new AspectRatioCell();
}
}
class AspectRatioCell extends ListCell<AspectRatio> {
public AspectRatioCell() {
super();
}
#Override
protected void updateItem(AspectRatio item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
setGraphic(null);
} else {
setText(item.getAspectRatio());
setGraphic(null);
}
}
}
Updating class
private class RefreshButtonListener implements ButtonListener {
#Override
public void onClick() {
theModel.setObservableAspectRatios(theModel.getAllAspectRatio());
theView.clearCombo();
}
}

Delete Multiple rows from Nattable

I am trying to delete more than one row from NatTable. Following the solution described in Delete rows from Nattable. I have created a the following classes:
the Command class looks like this :
public class DeleteMultiRowCommand extends AbstractMultiRowCommand {
public DeleteMultiRowCommand(AbstractMultiRowCommand command) {
super(command);
}
protected DeleteMultiRowCommand(ILayer layer, int[] rowPositions) {
super(layer, rowPositions);
}
#Override
public ILayerCommand cloneCommand() {
return new DeleteMultiRowCommand(this);
}
}
Command Handler class:
public class DeleteMultiRowCommandHandler<T> implements ILayerCommandHandler<DeleteMultiRowCommand> {
private List<T> bodyData;
private SelectionLayer layer;
public DeleteMultiRowCommandHandler(List<T> bodyData, SelectionLayer selectionLayer) {
this.bodyData = bodyData;
this.layer = selectionLayer;
}
public DeleteMultiRowCommandHandler(List<T> bodyData){
this.bodyData = bodyData;
}
#Override
public Class<DeleteMultiRowCommand> getCommandClass() {
return DeleteMultiRowCommand.class;
}
#Override
public boolean doCommand(ILayer targetLayer, DeleteMultiRowCommand command) {
//convert the transported position to the target layer
if (command.convertToTargetLayer(targetLayer)) {
Collection<Integer>rowpos = command.getRowPositions();
//remove the element
for(Integer val : rowpos){
this.bodyData.remove(val.intValue());
targetLayer.fireLayerEvent(new RowDeleteEvent(targetLayer, val.intValue()));
}
return true;
}
return false;
}
}
and the Command will be triggered on clicking a MenuItem
this.contextMenu = new PopupMenuBuilder(natTable)
.withInspectLabelsMenuItem()
.withClearAllFilters()
.withColumnRenameDialog()
.withMenuItemProvider(new IMenuItemProvider() {
#Override
public void addMenuItem(final NatTable natTable, Menu popupMenu) {
MenuItem deleteRow = new MenuItem(popupMenu, SWT.PUSH);
deleteRow.setText("Delete Row(s)");
deleteRow.setEnabled(true);
deleteRow.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent event) {
//int rowPosition = MenuItemProviders.getNatEventData(event).getRowPosition();
ILayer bl = ((GridLayer)natTable.getLayer()).getBodyLayer();
BodyLayerStack bl1 = (BodyLayerStack) bl;
SelectionLayer sl = bl1.getSelectionLayer();
int []poss = new int[sl.getFullySelectedRowPositions().length];
int i=0;
for(int pos1 : sl.getFullySelectedRowPositions()){
poss[i]=sl.getRowIndexByPosition(pos1);
i++;
}
//System.out.println("Menu item selected "+rowPosition);
//natTable.doCommand(new DeleteRowCommand(natTable, rowPosition));
natTable.doCommand(new DeleteMultiRowCommand(natTable, poss));
}
});
}
})
.build();
when I try to delete the rows, rows which not selected are deleted. Seems like an issue with the row postion to row index conversion. is the row postion to row index conversion correct within my IMenuItemProvider right ?
It seems like you do the conversion from position to index twice: once in the menu item selection listener and once in the command handler (by calling convertToTargetLayer). The first is not necessary.
That is not an issue of NatTable, but an issue on how to work with collections. You need to remove the items backwards if you remove the elements one by one. Otherwise the items for the indexes are changing while processing.
Let's assume you want to delete the elements at index 1 and 2. After removing the element at index 1, the elements below will move up. So the element that was before on index 2 will be on index 1 now, and the element at index 3 will be on index 2. Therefore the removal of the element at index 2 in the next iteration will remove the item that was before on index 3.
I'd suggest to sort and reverse the collection of indexes before iterating to remove items from the collection. Than it should work.

Eclipse TableViewer set custom row background

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));
}
}
});

Double click handler to flextable in GWT 2.3

I want to add double click handler to FlexTable in GWT.
I used below code :
flexTable.addDoubleClickHandler(new DoubleClickHandler() {
#Override
public void onDoubleClick(DoubleClickEvent event) {
}
});
But how can i get row index.
Please suggest me.
Sadly there is no built-in method to retrieve a cell from a double-click event in FlexTable. It can be implemented in a few lines though. This is how I did it.
Create a subclass of FlexTable with the following code:
public class DoubleClickTable extends FlexTable {
class MyCell extends Cell {
protected MyCell(int rowIndex, int cellIndex) {
super(rowIndex, cellIndex);
}
}
public Cell getCellForEvent(MouseEvent<? extends EventHandler> event) {
Element td = getEventTargetCell(Event.as(event.getNativeEvent()));
if (td == null) {
return null;
}
int row = TableRowElement.as(td.getParentElement()).getSectionRowIndex();
int column = TableCellElement.as(td).getCellIndex();
return new MyCell(row, column);
}
}
Then in the DoubleClickHandler call getCellForEvent() to retrieve the clicked cell:
flexTable.addDoubleClickHandler(new DoubleClickHandler() {
#Override
public void onDoubleClick(DoubleClickEvent event) {
Cell cell = flexTable.getCellForEvent(event);
GWT.log("Row index: " + cell.getRowIndex());
}
});
Implementation details: method getCellForEvent() is a copy of the method with the same name in class HTMLTable (the parent class of FlexTable), except that it has a different signature for the parameters. The MyCell class is a workaround to be able to call the Cell constructor, which is protected.

GWT CellTable Custom Selection Model

I need a 'custom selection model' for GWT CellTable. One of the columns in CellTable is a Checkbox column.
Basic rquirements (both work in solution below):
- Row click (not on checkbox), selects that row and un-selects all other rows.
- Checkbox selection should select/un-select that row only.
Following is the code I am using, but its very very slow. Any guidance would be appreciated.
final SelectionModel<T> selectionModel = new MultiSelectionModel<T>();
dataTable.setSelectionModel(selectionModel,
DefaultSelectionEventManager.createCustomManager(
new DefaultSelectionEventManager.CheckboxEventTranslator<T>() {
#Override
public SelectAction translateSelectionEvent(CellPreviewEvent<T> event) {
SelectAction action = super.translateSelectionEvent(event);
if (action.equals(SelectAction.IGNORE)) {
selectionModel.clear();
return SelectAction.TOGGLE;
}
return action;
}
}
)
);
Following is the code snipped for CheckColumn callback.
Column<T, Boolean> checkColumn = new Column<T, Boolean>(
new CheckboxCell(true, false))
{
#Override
public Boolean getValue(T t)
{
// Get the value from the selection model.
return selectionModel.isSelected(t);
}
};
I have put in a KeyProvider for the CellTable and its not slow anymore. :)
ProvidesKey<T> keyProvider = new ProvidesKey<T>() {
public Object getKey(T t) {
return tip == null? null : tip.getId();
}
};
dataTable = new CellTable<T>(PAGE_SIZE, keyProvider);
You could just whitelist your checkbox
int checkboxColumn = 0;
DefaultSelectionEventManager.createCustomManager(new DefaultSelectionEventManager
.WhitelistEventTranslator(checkboxColumn));