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 BallFigure 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 BallFigure(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 BallFigure.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 | // shrink rect so the last pixel of the circle is not clipped by the bounding box |
42 | rect.shrink(1, 1); |
43 | |
44 | // depending on the side draw a line from the center of the side of the bounding box touching the parent |
45 | // to the center of the bounding box and then draw a circle in the remaining half of the bounding box |
46 | switch(side){ |
47 | case PositionConstants.EAST: |
48 | graphics.drawLine(rect.getLeft().x,rect.getCenter().y,rect.getCenter().x,rect.getCenter().y); |
49 | graphics.drawArc(rect.getCenter().x, rect.getTop().y+rect.height/4, rect.height/2, rect.height/2, 0, 360); |
50 | break; |
51 | case PositionConstants.WEST: |
52 | graphics.drawLine(rect.getRight().x,rect.getCenter().y,rect.getCenter().x,rect.getCenter().y); |
53 | graphics.drawArc(rect.getLeft().x, rect.getTop().y+rect.height/4, rect.height/2, rect.height/2, 0, 360); |
54 | break; |
55 | case PositionConstants.NORTH: |
56 | graphics.drawLine(rect.getCenter().x,rect.getBottom().y,rect.getCenter().x,rect.getCenter().y); |
57 | graphics.drawArc(rect.getCenter().x-rect.width/4, rect.getTop().y, rect.height/2, rect.height/2, 0, 360); |
58 | break; |
59 | case PositionConstants.SOUTH: |
60 | graphics.drawLine(rect.getCenter().x,rect.getTop().y,rect.getCenter().x,rect.getCenter().y); |
61 | graphics.drawArc(rect.getCenter().x-rect.width/4, rect.getCenter().y, rect.height/2, rect.height/2, 0, 360); |
62 | break; |
63 | } |
64 | } |
65 | |
66 | /** |
67 | * @return the rectangle surrounding the ( ) part of the figure |
68 | * relative to the position of the figure |
69 | */ |
70 | private Rectangle getBallBounds() { |
71 | int side = (getBorderItemLocator() == null ? PositionConstants.WEST : getBorderItemLocator().getCurrentSideOfParent()); |
72 | |
73 | Rectangle rect = getBounds(); |
74 | Rectangle result = null; |
75 | |
76 | switch(side){ |
77 | case PositionConstants.EAST: |
78 | result = new Rectangle(rect.getCenter().x, rect.getTop().y+rect.height/4, rect.height/2, rect.height/2); |
79 | break; |
80 | case PositionConstants.WEST: |
81 | result = new Rectangle(rect.getLeft().x, rect.getTop().y+rect.height/4, rect.height/2, rect.height/2); |
82 | break; |
83 | case PositionConstants.NORTH: |
84 | result = new Rectangle(rect.getCenter().x-rect.width/4, rect.getTop().y, rect.height/2, rect.height/2); |
85 | break; |
86 | case PositionConstants.SOUTH: |
87 | result = new Rectangle(rect.getCenter().x-rect.width/4, rect.getCenter().y, rect.height/2, rect.height/2); |
88 | break; |
89 | } |
90 | return result; |
91 | } |
92 | |
93 | /** |
94 | * places the anchor point around the ( ) part of the figure |
95 | * which is closest to the reference point |
96 | */ |
97 | private class BallAnchor extends AbstractConnectionAnchor { |
98 | |
99 | public BallAnchor(IFigure owner) { |
100 | super(owner); |
101 | } |
102 | |
103 | public Point getLocation(Point reference) { |
104 | Point p = getAnchorPoint(reference); |
105 | return p; |
106 | } |
107 | |
108 | private Point getAnchorPoint(Point reference) { |
109 | Rectangle r = Rectangle.SINGLETON; |
110 | r.setBounds(getBallBounds()); |
111 | r.translate(-1, -1); |
112 | r.resize(1, 1); |
113 | getOwner().translateToAbsolute(r); |
114 | |
115 | Point ref = r.getCenter().negate().translate(reference); |
116 | |
117 | if (ref.x == 0) |
118 | return new Point(reference.x, (ref.y > 0) ? r.bottom() : r.y); |
119 | if (ref.y == 0) |
120 | return new Point((ref.x > 0) ? r.right() : r.x, reference.y); |
121 | |
122 | float dx = (ref.x > 0) ? 0.5f : -0.5f; |
123 | float dy = (ref.y > 0) ? 0.5f : -0.5f; |
124 | |
125 | // ref.x, ref.y, r.width, r.height != 0 => safe to proceed |
126 | |
127 | float k = (float)(ref.y * r.width) / (ref.x * r.height); |
128 | k = k * k; |
129 | |
130 | return r.getCenter().translate((int)(r.width * dx / Math.sqrt(1 + k)), |
131 | (int)(r.height * dy / Math.sqrt(1 + 1 / k))); |
132 | } |
133 | |
134 | } |
135 | |
136 | protected ConnectionAnchor createAnchorInternal() { |
137 | return new BallAnchor(this); |
138 | } |
139 | |
140 | protected ConnectionAnchor createAnchorExternal() { |
141 | return new StemAnchor(this); |
142 | } |
143 | } |