1 | package de.uka.ipd.sdq.pcm.gmf.composite; |
2 | |
3 | import org.eclipse.draw2d.AbstractConnectionAnchor; |
4 | import org.eclipse.draw2d.ConnectionAnchor; |
5 | import org.eclipse.draw2d.Graphics; |
6 | import org.eclipse.draw2d.IFigure; |
7 | import org.eclipse.draw2d.PositionConstants; |
8 | import org.eclipse.draw2d.geometry.Point; |
9 | import org.eclipse.draw2d.geometry.Rectangle; |
10 | |
11 | /** |
12 | * Represents the UML style --( border figure |
13 | * which rotates depending on the side the figure |
14 | * is located in relation to it's parent. |
15 | * |
16 | * @author Philipp Meier |
17 | */ |
18 | public class SocketFigure extends AbstractBorderFigure { |
19 | |
20 | /** |
21 | * @param size width and hight of the figure in logical units (LP) |
22 | * @param posType position type of the figure |
23 | */ |
24 | public SocketFigure(int logicalSize, POSITION_TYPE posType) { |
25 | super(logicalSize, posType); |
26 | } |
27 | |
28 | protected void paintFigure(Graphics graphics) { |
29 | super.paintFigure(graphics); |
30 | |
31 | if (getBorderItemLocator() == null) { |
32 | System.out.println("border item locator null in SocketFigure.paintFigure"); |
33 | } |
34 | |
35 | // determine the side the border item is located relative to it's parent |
36 | int side = (getBorderItemLocator() == null ? PositionConstants.WEST : getBorderItemLocator().getCurrentSideOfParent()); |
37 | |
38 | Rectangle rect = new Rectangle(); |
39 | graphics.getClip(rect); |
40 | |
41 | // depending on the side draw a line from the center of the side of the bounding box touching the parent |
42 | // to the center of the bounding box and then draw a half circle in the remaining half of the bounding box |
43 | // so that the open side points away from the parent |
44 | switch(side){ |
45 | case PositionConstants.EAST: |
46 | graphics.drawLine(rect.getLeft().x,rect.getCenter().y,rect.getCenter().x,rect.getCenter().y); |
47 | graphics.drawArc(rect.getCenter().x, rect.getTop().y+rect.height/4, rect.height/2, rect.height/2, 90, 180); |
48 | break; |
49 | case PositionConstants.WEST: |
50 | graphics.drawLine(rect.getRight().x,rect.getCenter().y,rect.getCenter().x,rect.getCenter().y); |
51 | graphics.drawArc(rect.getLeft().x, rect.getTop().y+rect.height/4, rect.height/2, rect.height/2, -90, 180); |
52 | break; |
53 | case PositionConstants.NORTH: |
54 | graphics.drawLine(rect.getCenter().x,rect.getBottom().y,rect.getCenter().x,rect.getCenter().y); |
55 | graphics.drawArc(rect.getCenter().x-rect.width/4, rect.getTop().y, rect.height/2, rect.height/2, 180, 180); |
56 | break; |
57 | case PositionConstants.SOUTH: |
58 | graphics.drawLine(rect.getCenter().x,rect.getTop().y,rect.getCenter().x,rect.getCenter().y); |
59 | graphics.drawArc(rect.getCenter().x-rect.width/4, rect.getCenter().y, rect.height/2, rect.height/2, 0, 180); |
60 | break; |
61 | } |
62 | } |
63 | |
64 | /** |
65 | * @return the center between the ( part of the figure |
66 | * relative to the position of the figure |
67 | */ |
68 | private Point getArcCenter() { |
69 | int side = (getBorderItemLocator() == null ? PositionConstants.WEST : getBorderItemLocator().getCurrentSideOfParent()); |
70 | |
71 | Rectangle rect = getBounds(); |
72 | Point result = null; |
73 | |
74 | switch(side){ |
75 | case PositionConstants.EAST: |
76 | result = new Point(rect.getCenter().x+rect.height/4, rect.getCenter().y); |
77 | break; |
78 | case PositionConstants.WEST: |
79 | result = new Point(rect.getLeft().x+rect.height/4, rect.getCenter().y); |
80 | break; |
81 | case PositionConstants.NORTH: |
82 | result = new Point(rect.getCenter().x, rect.getTop().y+rect.height/4); |
83 | break; |
84 | case PositionConstants.SOUTH: |
85 | result = new Point(rect.getCenter().x, rect.getCenter().y+rect.height/4); |
86 | break; |
87 | } |
88 | return result; |
89 | } |
90 | |
91 | /** |
92 | * places the anchor point at the center of the ( part of the figure |
93 | */ |
94 | private class SocketAnchor extends AbstractConnectionAnchor { |
95 | |
96 | public SocketAnchor(IFigure owner) { |
97 | super(owner); |
98 | } |
99 | |
100 | public Point getLocation(Point reference) { |
101 | Point p = getArcCenter(); |
102 | getOwner().translateToAbsolute(p); |
103 | return p; |
104 | } |
105 | } |
106 | |
107 | protected ConnectionAnchor createAnchorInternal() { |
108 | return new SocketAnchor(this); |
109 | } |
110 | |
111 | protected ConnectionAnchor createAnchorExternal() { |
112 | return new StemAnchor(this); |
113 | } |
114 | } |