Find a figure by Label draw2d [message #1744018] |
Wed, 21 September 2016 09:08  |
Eclipse User |
|
|
|
How can i find a figure by a Label? pls help me.
Thanks
public class MainView {
private Canvas canvas;
/** Application entry point */
public static void main(String[] args) {
new MainView().open();
}
public Figure myBlockFigure(String name){
RectangleFigure f = new RectangleFigure();
f.setBackgroundColor(ColorConstants.lightGreen);
f.setLayoutManager( new ToolbarLayout() );
f.setPreferredSize(100,70);
f.add( new Label( name ) );
//Listner
new MyListener(f);
return f;
}
/** Open a Shell call a method for drawing */
public void open(){
Shell shell = new Shell(new Display());
shell.setSize(400, 400);
shell.setText("My Main View");
shell.setLayout( new GridLayout() );
//Button
org.eclipse.swt.widgets.Button button1 = new org.eclipse.swt.widgets.Button( shell, SWT.PUSH);
button1.setText("Diagramm");
button1.setLayoutData(new GridData());
button1.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
SimpleExample ex = new SimpleExample();
ex.open();
}
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
});
// build diagram
canvas = buildDiagram( shell );
// canvas.setLayoutData(new GridData(GridData.FILL_BOTH));
canvas.setLayoutData(new GridData(GridData.FILL_BOTH));
Display display = shell.getDisplay();
// open and wait until closing
shell.open();
while( !shell.isDisposed() )
while( !display.readAndDispatch() )
display.sleep();
}
private void draw(Figure root) {
String text= "first";
Figure first= myBlockFigure(text);
root.add(first,
new Rectangle( new Point(10, 10),
first.getPreferredSize() ) );
Figure second= myBlockFigure("Second");
root.add(second,
new Rectangle( new Point(10, 100),
second.getPreferredSize() ) );
Figure dritte= myBlockFigure("dritte");
root.add(dritte,
new Rectangle( new Point(200, 100),
dritte.getPreferredSize() ) );
// Figure test = Figure.getlabel("first");
root.add((myConnection(first, second)));
//root.add((myConnection(test, dritte)));
root.add((myConnection(first, dritte)));
}
public PolygonDecoration myPolygonDecoration(){
PolygonDecoration deco = new PolygonDecoration();
PointList pl = new PointList();
pl.addPoint( 0, 0 );
pl.addPoint(-2, 2);
// pl.addPoint(-4, 0);
pl.addPoint(-2,-2);
deco.setTemplate( pl );
return deco;
}
public Connection myConnection(IFigure fig1, IFigure fig2){
PolylineConnection conn = new PolylineConnection();
conn.setSourceAnchor( new ChopboxAnchor( fig1 ) );
conn.setTargetAnchor( new ChopboxAnchor( fig2 ) );
//Decoration
conn.setTargetDecoration(myPolygonDecoration());
return conn;
}
/** Instantiate the root figure, where we draw figures*/
private Canvas buildDiagram( Composite parent ){
// instantiate root figure
Figure root = new Figure();
root.setFont(parent.getFont());
root.setLayoutManager( new XYLayout() );
// insantiate a canvas on which to draw
canvas = new Canvas(parent, SWT.DOUBLE_BUFFERED);
canvas.setBackground(ColorConstants.white);
LightweightSystem lws = new LightweightSystem(canvas);
lws.setContents(root);
draw(root);
//
return canvas;
}
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.03315 seconds