How to Color TableViewer Rows alternatively - eclipse-plugin

I'm using Viewer Framework in my rcp application, i would like to color viewer rows alternatively,i tried to override getBackground method of ColumnLabelProvider, below is code snippet
col.setLabelProvider(new ColumnLabelProvider(){
----//other methods
#override
public Color getBackground(Object element) {
return gray;//here gray is color object defined somewhere in class
}
});
this colors the columns, but not a row, below is output
how do i achieve this correctly

You can find an example here which uses an IColorProvider. Maybe you could just reuse the getBackground() method in your code, just change the reference to your tableViewer:
public Color getBackground(Object element) {
ArrayList list = (ArrayList) tableViewer.getInput();
int index = list.indexOf(element);
if ((index % 2) == 0) {
return gray;
} else {
return null;
}
}

Related

Change dynamically border color of an agent in Repast Symphony

I have to change dynamically the border color of an agent. The agent is represented as the default circle on the display. The displayed color has to change according to a boolean variable defined inside the agent Class.
When the agent is created and displayed for the first time, it has the correct style but when the boolen variable inside the agent class changes, the border color does not change.
If I do the same for the fill color of the agent instead, it works fine. I put here the code I used:
public class NodeStyle extends DefaultStyleOGL2D{
#Override
public Color getBorderColor(Object agent) {
Color borderColor = Color.BLACK;
if(agent instanceof Process) {
Process p = (Process)agent;
if(p.isParticularNode) {
borderColor = Color.RED;
}
}
return borderColor;
}
}
When the agent is created and it is added to the context, it take the correct color but if isParticularNode changes, the border color does not change.
I've tryed also to do the same importing the interface StyleOGL2D but the problem remains
I tried this with the JZombies demo, adding an "id" double to each zombie that is set with RandomHelper.nextDouble() each tick. The border color changes as expected. By default the border size is 0, so perhaps that needs to be changed in your code.
public class ZombieStyle extends DefaultStyleOGL2D {
public Color getColor(Object agent) {
return Color.RED;
}
public Color getBorderColor(Object agent) {
Zombie z = (Zombie)agent;
if (z.getID() > 0.5) {
return Color.GREEN;
}
return Color.black;
}
public int getBorderSize(Object agent) {
return 4;
}
}

Repast: set different color for different edges

I have two types edges in my supply chain model: demand_links and supply_links.
the default color is gray for all links. But I want to change the color of demand_links to red each time the attribute of the demand_link is changed (Note:the edge is the custom edge agent through edge creator). How to do this?
Below is my codes for simple test and it didn't work.
public class EdgeStyle2D extends DefaultStyleOGL2D {
#Override
public Color getColor(Object o){
// if (((CustomEdge) o).getCurrent_dl() == 1) {
// return Color.RED;
// }
// else {
// return Color.BLACK;
// }
if (o instanceof Distributor)
return Color.YELLOW;
return null;
}
}
I get the error when initialize.
Caused by: java.lang.ClassCastException: class supplyChainSystem.EdgeStyle2D cannot be cast to class repast.simphony.visualizationOGL2D.EdgeStyleOGL2D (supplyChainSystem.EdgeStyle2D and repast.simphony.visualizationOGL2D.EdgeStyleOGL2D are in unnamed module of loader repast.simphony.plugin.ExtendablePluginClassLoader #61af1510)
For styling links in this way, you should follow the example in the zombies.style.LinkStyle class within the Zombies_Demo model. Here's what the relevant parts of that class look like:
public class LinkStyle implements EdgeStyleOGL2D {
public Color getColor(RepastEdge<?> edge) {
BaseLink<?> link = (BaseLink<?>) edge;
return ReLogoSupport.lookupColor(link.getColor());
}
public int getLineWidth(RepastEdge<?> edge) {
return (int) (Math.abs(edge.getWeight()));
}
}
And you would use a class like this for the network (as opposed to agent) style.

Eclipse plugin - ColumnLabelProvider display only image

So I am developing an Eclipse plug-in and using ColumnLabelProvider to provide label for the columns of my tree viewer.
However, in one of the columns, I only intend to display an image and no text. However, in the final display, Eclipse reserves blank space for the text element even if I return a null.
Is there any way to make it display only image and in the full space provided?
Here is the code snippet:
column4.setLabelProvider(new ColumnLabelProvider() {
#Override
public String getText(Object element) {
return null;
}
#Override
public Image getImage(Object element) {
/* Code to Display an image follows */
.....
}
});
ColumnLabelProvider will always leave space for the text.
You can use a class derived from OwnerDrawLabelProvider to draw the column yourself.
Something like:
public abstract class CentredImageCellLabelProvider extends OwnerDrawLabelProvider
{
protected CentredImageCellLabelProvider()
{
}
#Override
protected void measure(Event event, Object element)
{
}
#Override
protected void erase(final Event event, final Object element)
{
// Don't call super.erase() to suppress non-standard selection draw
}
#Override
protected void paint(final Event event, final Object element)
{
TableItem item = (TableItem)event.item;
Rectangle itemBounds = item.getBounds(event.index);
GC gc = event.gc;
Image image = getImage(element);
Rectangle imageBounds = image.getBounds();
int x = event.x + Math.max(0, (itemBounds.width - imageBounds.width) / 2);
int y = event.y + Math.max(0, (itemBounds.height - imageBounds.height) / 2);
gc.drawImage(image, x, y);
}
protected abstract Image getImage(Object element);
}

Using StyledCellLabelProvider in a TableViewer killed my tooltips

I have a TableViewer in my Eclipse plugin.
When I was just using a regular label provider, my tooltips worked beautifully:
However, when I switched to have my LabelProvider implement IStyledLabelProvider, my tooltips went haywire:
Here is the code creating the StyledString
#Override
public StyledString getStyledText(final Object element) {
if( !(element instanceof MyInterface<?>) ) {
return null;
}
final String elemText = getColumnText(element, this.columnIndex);
final StyledString styledString = new StyledString(elemText == null ? "" : elemText);
if( !(element instanceof MyObject) ) {
return styledString;
}
final MyObject settingElement = (MyObject) element;
// grayed out text
if( settingElement.shouldBeGray() ) {
styledString.setStyle(0, elemText.length(), AdaptabilityStyles.GRAY_STYLER;
} else if( !settingElement.shouldBeBlue() ) {
styledString.setStyle(0, elemText.length(), AdaptabilityStyles.BLUE_STYLER);
}
return styledString;
}
And getTooltTipText()
#Override
public String getToolTipText(final Object element) {
return getColumnText(element, this.columnIndex);
}
What am I doing wrong?
As I was writing this question, I wanted to reference a bug report that I am familiar with that is related to tooltips. I looked at the bug report again and came across the following line:
For now, I simply try this :
ColumnViewerToolTipSupport.enableFor(commonViewer)
I wasn't calling that method when I created my viewer. When I tried that, my tooltips came back (though slightly different than they were before.

adding dropdown to table cell

I am facing some issues in eclipse rcp for setting the dropdown for a particular cell.
My requrement is to set the dropdown in the first row of the table. And that dropdown should be able to remove also.
One more thing that drop should be able to filter the contents in the table. So My question is that
1) Is it possible to add the dropdown only to the particular cell or row?
2) Can that filter act as a filter for the table?
3) How do I remove once I add the dropdown to the table cell?
Yes this is entirely possible. I suggest you start by reading Building and delivering a table editor with SWT/JFace, this tutorial contains everything you need to know.
As a rough outline, you will need to make the first item in your content model different from your data items - it will store the filter values. Then setup editing support on your TableViewerColumns something like (this is just a starter - this code will not work on its own):
tableViewerColumn.setEditingSupport(new EditingSupport(tableViewer)
{
#Override
protected boolean canEdit(Object element) {
if(object instanceof FilterDataObject) // your model object you are using to store the filter selections
{
return true;
}
}
#Override
protected CellEditor getCellEditor(Object element)
{
final ComboBoxCellEditor editor = new ComboBoxCellEditor(table, getPossibleFilterValues(), SWT.READ_ONLY);
((CCombo)editor.getControl()).addModifyListener(new ModifyListener()
{
public void modifyText(ModifyEvent e)
{
IStructuredSelection sel = (IStructuredSelection)m_tableViewer.getSelection();
FilterDataObject filterValue = (FilterDataObject)sel.getFirstElement();
// .. update the filter on your TableViewer
}
});
return editor;
}
#Override
protected Object getValue(Object element)
{
if(object instanceof FilterDataObject)
{
// get the filter value
}
else
{
// get your data model's value for this column
}
}
#Override
protected void setValue(Object element, Object value)
{
if(object instanceof FilterDataObject)
{
// update your FilterDataObject
}
}
});