package com.positive.charts.legend.simple;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import com.positive.colorchecker.StaticColorChecker;
public class LegendControl extends Composite {
final Composite itemsComposite;
public LegendControl(final Composite parent, final int style) {
super(parent, style);
final GridLayout layout = new GridLayout();
layout.numColumns = 1;
this.setLayout(layout);
this.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
this.itemsComposite = new Composite(this, SWT.NONE);
this.itemsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
true, true));
this.itemsComposite.setBackground( StaticColorChecker.dublicateColor(SWT.COLOR_WHITE));
//parent.getDisplay().getSystemColor(
//SWT.COLOR_WHITE));
final RowLayout itemsCompositeLayout = new RowLayout(SWT.VERTICAL);
this.itemsComposite.setLayout(itemsCompositeLayout);
}
public void setInput(final LegendItem[] items) {
for (int i = 0; i < items.length; ++i) {
final Composite itemComposite = new Composite(this.itemsComposite,
SWT.NONE);
itemComposite.setBackground(this.itemsComposite.getBackground());
final GridLayout itemCompositeLayout = new GridLayout(2, false);
itemComposite.setLayout(itemCompositeLayout);
final LegendItem item = items[i];
final Label legendItemColor = new Label(itemComposite, SWT.NONE);
legendItemColor.setText("AA");
legendItemColor.addPaintListener(new PaintListener() {
public void paintControl(final PaintEvent e) {
final Point s = legendItemColor.getSize();
e.gc.setBackground(item.color);
e.gc.fillRectangle(0, 0, s.x, s.y);
}
});
final Label legendItemLabel = new Label(itemComposite, SWT.NONE);
legendItemLabel.setText(item.text);
legendItemLabel.setBackground(this.itemsComposite.getBackground());
}
this.layout();
}
}