문제

I mean drawing somg simple shape like circle,rectangleFigure,and polylineConnectistrong.It seems that a LightweightSystem has to been constructed on a Canvas such as a Shell.And in a RCP application when I add an extension of an editor,the editor extends org.eclipse.ui.part.EditorPart by default.It has a method called createPartControl.This method has a parameter (Composite parent).

So I write the following code and it give me a Unhandled event loop exception

public void createPartControl(Composite parent) {
    Shell shell = parent.getShell();
    shell.open();
    Display display = shell.getDisplay();
    LightweightSystem lws = new LightweightSystem(shell);
    IFigure panel = new Figure();
    lws.setContents(panel);
    RectangleFigure node1 = new RectangleFigure();
    RectangleFigure node2 = new RectangleFigure();
    node1.setBackgroundColor(ColorConstants.red);
    node1.setBounds(new Rectangle(30, 30, 64, 36));
    node2.setBackgroundColor(ColorConstants.blue);
    node2.setBounds(new Rectangle(100, 100, 64, 36));
    PolylineConnection conn = new PolylineConnection();
    conn.setSourceAnchor(new ChopboxAnchor(node1));
    conn.setTargetAnchor(new ChopboxAnchor(node2));
    conn.setTargetDecoration(new PolygonDecoration());
    Label label = new Label("Midpoint");
    label.setOpaque(true);
    label.setBackgroundColor(ColorConstants.buttonLightest);
    label.setBorder(new LineBorder());
    conn.add(label, new MidpointLocator(conn, 0));
    panel.add(node1);
    panel.add(node2);
    panel.add(conn);
    while (!shell.isDisposed ()) { 
        if (!display.readAndDispatch ()) 
           display.sleep (); 
    }
}

So how to solve this problem and how to draw these figures on the editor?

도움이 되었습니까?

해결책

As you want to draw inside the editor, you don't need to create new Shell nor dispatch events from the event queue as you would do in a standalone SWT application; just create a Canvas and draw into it. This should help you:

public void createPartControl(Composite parent) {
    Canvas canvas = new Canvas(parent, SWT.NONE);
    LightweightSystem lws = new LightweightSystem(canvas);
    IFigure panel = new Figure();
    lws.setContents(panel);
    [...]
    panel.add(node1);
    panel.add(node2);
    panel.add(conn);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top