I need the ability to have a blinking (red, maybe more colors) background for rows in a TableViewer/TreeViewer. What are the best options?
There may be more than one row blinking, the blinking MUST be synchron and I need two blinking modes, fast and slow.
I would do something similar to this. Update the elements that you need to change the colors for at a regular interval. At each update toggle the colors depending on how you want them to flash.
void scheduleColorChange(final Color colors[], final int startIndex, final int changeInterval)
{
getDisplay().timerExec(changeInterval, new Runnable()
{
public void run()
{
Object[] elements = getColorChangingElements();
setColorsForFlashingElements(elements, colors[index%colors.length]);
getViewer().update(elements);
scheduleColorChange(colors, startIndex+1, changeInterval)
}
});
}
and the have you label provider implement IColorProvider.
Howdy, this is a fast hack that shows the idea, improvable in many ways. I show the three classes doing the job. If you want I can provide an exported source plugin ready to install into your eclipse workbench tomorrow. Here are the core classes:
import java.util.TimerTask;
import org.eclipse.jface.resource.ColorDescriptor;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.ui.PlatformUI;
public class Blinker extends LabelProvider implements ITableLabelProvider, IColorProvider {
private final TableViewer viewer;
public Blinker(TableViewer viewer){
this.viewer = viewer;
}
// this is just a lousy way to store blink_on/blink_off...
private byte state;
// there must be a better way to get a color...
final static Color red = ColorDescriptor.createFrom(new RGB(255,0,0)).createColor(PlatformUI.getWorkbench().getDisplay());
final static Color green = ColorDescriptor.createFrom(new RGB(0,255,0)).createColor(PlatformUI.getWorkbench().getDisplay());
private final TimerTask task = new TimerTask(){
@Override
public void run() {
state++;
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable(){
public void run() {
viewer.refresh();
}
});
}
};
private Timer t;
synchronized byte getState(){
return state;
}
@Override
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
@Override
public String getColumnText(Object element, int columnIndex) {
return ((Element) element).text;
}
@Override
public Color getBackground(Object object) {
Element element = (Element) object;
if (element.isBlinking()){
return getState() % 2 == 0 ? Blinker.red : Blinker.green;
} else {
return Blinker.green;
}
}
@Override
public Color getForeground(Object element) {
return null;
}
public void start() {
t = new Timer();
t.schedule(task, 200, 1000);
}
public void stop() {
t.cancel();
t = null;
}
}
This is a sample model class. Blink state is stored within the object, but you might want to improve this by using some sort of Adapter:
package com.example.blinker;
public class Element {
private boolean blinking;
public final String text;
public Element(String string, boolean b) {
this.text = string;
this.blinking = b;
}
public synchronized boolean isBlinking(){
return blinking;
}
public synchronized void setBlinking(boolean b){
this.blinking = b;
}
public static final Element[] sampledata = new Element[] {
new Element("Merkur", false),
new Element("Venus", true),
new Element("Erde", false),
new Element("Mars", true),
new Element("Jupiter", false),
new Element("Saturn", true),
new Element("Uranus", false),
new Element("Neptun", true),
new Element("Pluto", false),
};
}
And finally a TableViewer embedded in a View, making use of the two above:
package com.example.blinker.views;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import com.example.blinker.Blinker;
import com.example.blinker.Element;
public class SampleView extends ViewPart {
public static final String ID = "com.example.blinker.views.SampleView";
private TableViewer viewer;
private Blinker blinker;
class ViewContentProvider implements IStructuredContentProvider {
public void inputChanged(Viewer v, Object oldInput, Object newInput) {}
public void dispose() {}
public Object[] getElements(Object parent) {
return Element.sampledata;
}
}
public void createPartControl(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
viewer.setContentProvider(new ViewContentProvider());
blinker = new Blinker(viewer);
viewer.setLabelProvider(blinker);
viewer.setInput(new Object());
blinker.start();
}
public void setFocus() {
viewer.getControl().setFocus();
}
public void dispose() {
blinker.stop();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With