package es.iiia.sgi.views;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.part.ViewPart;
import es.iiia.sgi.controls.ShapeIconControl;
import es.iiia.shapegrammar.shape.LineModel;
import es.iiia.shapegrammar.shape.ShapeModel;
public class RenderLineView extends ViewPart {
public static final String ID = "es.iiia.sgi.views.renderlineview";
private Composite cmp;
private static int iconSize = 64;
@Override
public void createPartControl(Composite parent) {
parent.setLayout(new FillLayout());
final ScrolledComposite scrollComposite = new ScrolledComposite(parent,
SWT.V_SCROLL | SWT.BORDER);
cmp = new Composite(scrollComposite, SWT.NONE);
RowLayout layout = new RowLayout(SWT.HORIZONTAL);
layout.wrap = true;
cmp.setLayout(layout);
scrollComposite.setContent(cmp);
scrollComposite.setExpandVertical(true);
scrollComposite.setExpandHorizontal(true);
scrollComposite.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
Rectangle r = scrollComposite.getClientArea();
scrollComposite.setMinSize(cmp
.computeSize(r.width, SWT.DEFAULT));
}
});
}
public void clear() {
// first remove all controls
for (Control cntr : cmp.getChildren()) {
cntr.dispose();
}
cmp.layout();
}
public void addShape(ShapeModel model) {
getControl(cmp, model);
getSeparator(cmp);
cmp.layout();
}
public void setIconSize(int size) {
this.iconSize = size;
}
// private methods
public static Canvas getControl(Composite cmp, final ShapeModel shape) {
final Canvas ctrl = new ShapeIconControl(cmp, shape);
return ctrl;
}
public static void getSeparator(Composite cmp) {
final Canvas ctrl = new Canvas(cmp, SWT.BORDER_SOLID);
ctrl.setLayoutData(new RowData(20, iconSize));
ctrl.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.drawLine(2, iconSize/2-2, 15, iconSize/2-2);
e.gc.drawLine(2, iconSize/2+2, 15, iconSize/2+2);
e.gc.drawLine(15, iconSize/2-5, 19, iconSize/2);
e.gc.drawLine(15, iconSize/2+5, 19, iconSize/2);
}
});
}
private static void drawLine(GC g, Point2D p1, Point2D p2) {
g.drawLine(
(int) Math.round(p1.getX()),
(int) Math.round(p1.getY()),
(int) Math.round(p2.getX()),
(int) Math.round(p2.getY()));
}
@Override
public void setFocus() {
}
}