int r = config.getIntParameter(keys[i] + ".red", -1);
if (r >= 0) {
int g = config.getIntParameter(keys[i] + ".green");
int b = config.getIntParameter(keys[i] + ".blue");
Color color = ColorCache.getColor(panel.getDisplay(), r, g, b);
blockColors[i] = color;
}
Composite colorSet = new Composite(legend, SWT.NONE);
colorSet.setLayout(new RowLayout(SWT.HORIZONTAL));
final Canvas cColor = new Canvas(colorSet, SWT.BORDER);
cColor.setData("Index", new Integer(i));
// XXX Use paint instead of setBackgrond, because OSX does translucent
// crap
cColor.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
int i = ((Integer)cColor.getData("Index")).intValue();
e.gc.setBackground(blockColors[i]);
e.gc.fillRectangle(e.x, e.y, e.width, e.height);
}
});
cColor.addMouseListener(new MouseAdapter() {
public void mouseUp(MouseEvent e) {
Integer iIndex = (Integer)cColor.getData("Index");
if (iIndex == null)
return;
int index = iIndex.intValue();
if (e.button == 1) {
ColorDialog cd = new ColorDialog(panel.getShell());
cd.setRGB(blockColors[index].getRGB());
RGB rgb = cd.open();
if (rgb != null)
config.setRGBParameter(keys[index], rgb.red, rgb.green, rgb.blue);
} else {
config.removeRGBParameter(keys[index]);
}
}
});
Label lblDesc = new Label(colorSet, SWT.NULL);
Messages.setLanguageText(lblDesc, keys[i]);
data = new RowData();
data.width = 20;
data.height = lblDesc.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 3;
cColor.setLayoutData(data);
// If color changes, update our legend
config.addParameterListener(keys[i],paramListeners[i] = new ParameterListener() {
public void parameterChanged(String parameterName) {
for (int j = 0; j < keys.length; j++) {
if (keys[j].equals(parameterName)) {
final int index = j;
final int r = config.getIntParameter(keys[j] + ".red", -1);
if (r >= 0) {
final int g = config.getIntParameter(keys[j] + ".green");
final int b = config.getIntParameter(keys[j] + ".blue");
final RGB rgb = new RGB(r, g, b);
if (blockColors[j].isDisposed()
|| !rgb.equals(blockColors[j].getRGB())) {
Utils.execSWTThread(new AERunnable() {
public void runSupport() {
if (panel == null || panel.isDisposed())
return;
Color color = ColorCache.getColor(panel.getDisplay(), r, g, b);
blockColors[index] = color;
cColor.redraw();
}
});
}