1

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?

Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
Sam Su
  • 6,532
  • 8
  • 39
  • 80
  • it seems you copy-and-pasted an swt sample showing a window with a painted canvas inside a jface editor class – guido Jan 22 '13 at 03:41
  • yes,this sample runs successfully in a class with a main method.And I wish it can be used in Editor which is a plug-in extension. – Sam Su Jan 22 '13 at 03:48
  • 1
    create a Canvas using parent, then pass that canvas to LightweightSystem. remove all unneeded shell/display code – guido Jan 22 '13 at 03:51
  • Well done.It works!Tks a lot!Nice guy. – Sam Su Jan 22 '13 at 03:54
  • Welcome; I converted the comment in an answer for you to accept. – guido Jan 23 '13 at 05:11

1 Answers1

1

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);
}
guido
  • 18,864
  • 6
  • 70
  • 95