Legendpublic class Legend extends Object TODO: have a callback when color changes |
Methods Summary |
---|
public static org.eclipse.swt.widgets.Composite | createLegendComposite(org.eclipse.swt.widgets.Composite panel, org.eclipse.swt.graphics.Color[] blockColors, java.lang.String[] keys)Create a legend containing a modifyable color box and description
Object layout = panel.getLayout();
Object layoutData = null;
if (layout instanceof GridLayout)
layoutData = new GridData(GridData.FILL_HORIZONTAL);
return createLegendComposite(panel, blockColors, keys, layoutData);
| public static org.eclipse.swt.widgets.Composite | createLegendComposite(org.eclipse.swt.widgets.Composite panel, org.eclipse.swt.graphics.Color[] blockColors, java.lang.String[] keys, java.lang.Object layoutData)Create a legend containing a modifyable color box and description
final ConfigurationManager config = ConfigurationManager.getInstance();
if (blockColors.length != keys.length)
return null;
final Color[] defaultColors = new Color[blockColors.length];
System.arraycopy(blockColors, 0, defaultColors, 0, blockColors.length);
Composite legend = new Composite(panel, SWT.NONE);
if (layoutData != null)
legend.setLayoutData(layoutData);
RowLayout layout = new RowLayout(SWT.HORIZONTAL);
layout.wrap = true;
layout.marginBottom = 0;
layout.marginTop = 0;
layout.marginLeft = 0;
layout.marginRight = 0;
layout.spacing = 0;
legend.setLayout(layout);
RowData data;
for (int i = 0; i < blockColors.length; i++) {
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], 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();
}
});
}
} else {
if (blockColors[j].isDisposed()
|| !blockColors[j].equals(defaultColors[j])) {
Utils.execSWTThread(new AERunnable() {
public void runSupport() {
if (panel == null || panel.isDisposed())
return;
blockColors[index] = defaultColors[index];
cColor.redraw();
}
});
}
}
}
}
}
});
}
legend.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
// We don't want to give them disposed colors
// Restore defaults in case blockColors is a static or is used
// afterwards, or if the view wants to dispose of the old colors.
for (int i = 0; i < blockColors.length; i++) {
blockColors[i] = defaultColors[i];
}
}
});
return legend;
|
|