|
| 1 | +ifdef::env-github[] |
| 2 | +:imagesdir: ../../guide/demos/ |
| 3 | +endif::[] |
| 4 | + |
| 5 | += Example 4 - Connections and Anchors |
| 6 | + |
| 7 | +image:images/demo4.gif[image] |
| 8 | + |
| 9 | +This example shows how to use Connections and ConnectionAnchors. A connection is |
| 10 | +a figure that visually connects two anchors. The anchors are usually places on |
| 11 | +figures which the connection is linking. |
| 12 | + |
| 13 | +The red and blue "nodes" can be dragged around with the mouse. The connection |
| 14 | +will re-route itself automatically. The connections children, the "Midpoint" |
| 15 | +label and the arrowhead, will also update. |
| 16 | + |
| 17 | +The connection implementation used here is a PolylineConnection. This |
| 18 | +implementation supports decorations, such as arrowheads. PolygonDecorations |
| 19 | +added to the connection will be rotated correctly by a Locator. Other |
| 20 | +decorations can be added with their own locators, such as the "Midpoint" label |
| 21 | +in the demo. |
| 22 | + |
| 23 | +[source,java] |
| 24 | +---- |
| 25 | +import org.eclipse.swt.widgets.Shell; |
| 26 | +import org.eclipse.swt.widgets.Display; |
| 27 | +import org.eclipse.draw2d.*; |
| 28 | +import org.eclipse.draw2d.geometry.*; |
| 29 | +
|
| 30 | +public class Demo4 { |
| 31 | +
|
| 32 | + public static void main(String args[]) { |
| 33 | + Shell shell = new Shell(); |
| 34 | + shell.setSize(350,350); |
| 35 | + shell.open(); |
| 36 | + shell.setText("Demo 4"); |
| 37 | + LightweightSystem lws = new LightweightSystem(shell); |
| 38 | + IFigure panel = new Figure(); |
| 39 | + lws.setContents(panel); |
| 40 | + RectangleFigure node1 = new RectangleFigure(); |
| 41 | + RectangleFigure node2 = new RectangleFigure(); |
| 42 | + node1.setBackgroundColor(ColorConstants.red); |
| 43 | + node1.setSize(64, 36); |
| 44 | + node2.setBackgroundColor(ColorConstants.blue); |
| 45 | + node2.setBounds(new Rectangle(100, 100, 64, 36)); |
| 46 | +
|
| 47 | + PolylineConnection conn = new PolylineConnection(); |
| 48 | + conn.setSourceAnchor(new ChopboxAnchor(node1)); |
| 49 | + conn.setTargetAnchor(new ChopboxAnchor(node2)); |
| 50 | + conn.setTargetDecoration(new PolygonDecoration()); |
| 51 | +
|
| 52 | + Label label = new Label("Midpoint"); |
| 53 | + label.setOpaque(true); |
| 54 | + label.setBackgroundColor(ColorConstants.buttonLightest); |
| 55 | + label.setBorder(new LineBorder()); |
| 56 | + conn.add(label, new MidpointLocator(conn, 0)); |
| 57 | +
|
| 58 | + panel.add(node1); |
| 59 | + panel.add(node2); |
| 60 | + panel.add(conn); |
| 61 | + new Dragger(node1); |
| 62 | + new Dragger(node2); |
| 63 | + Display display = Display.getDefault(); |
| 64 | + while (!shell.isDisposed()) { |
| 65 | + if (!display.readAndDispatch()) { |
| 66 | + display.sleep (); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +
|
| 71 | + static class Dragger extends MouseMotionListener.Stub implements MouseListener { |
| 72 | + public Dragger(IFigure figure){ |
| 73 | + figure.addMouseMotionListener(this); |
| 74 | + figure.addMouseListener(this); |
| 75 | + } |
| 76 | + Point last; |
| 77 | + public void mouseReleased(MouseEvent e){} |
| 78 | + public void mouseClicked(MouseEvent e){} |
| 79 | + public void mouseDoubleClicked(MouseEvent e){} |
| 80 | + public void mousePressed(MouseEvent e){ |
| 81 | + last = e.getLocation(); |
| 82 | + } |
| 83 | + public void mouseDragged(MouseEvent e){ |
| 84 | + Point p = e.getLocation(); |
| 85 | + Dimension delta = p.getDifference(last); |
| 86 | + last = p; |
| 87 | + Figure f = ((Figure)e.getSource()); |
| 88 | + f.setBounds(f.getBounds().getTranslated(delta.width, delta.height)); |
| 89 | + } |
| 90 | + }; |
| 91 | +} |
| 92 | +---- |
0 commit comments