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 event source -|> border figure |
13 | * which rotates depending on the side the figure |
14 | * is located in relation to it's parent. |
15 | * |
16 | * @author Benjamin Klatt |
17 | */ |
18 | public class SourceFigure extends AbstractBorderFigure { |
19 | |
20 | /** |
21 | * @param size width and height of the figure in logical units (LP) |
22 | * @param posType position type of the figure |
23 | */ |
24 | public SourceFigure(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: |
42 | // - a line from the center of the side of the bounding box touching the parent |
43 | // to the center of the bounding box. Result: "-" |
44 | // - a line between the centers of the orthogonal borders of the bounding box. Result: "-|" |
45 | // - a line from the center of the left orthogonal border to the center |
46 | // of the border not connected with the parent. Result: "-|\" |
47 | // - a line from the center of the left orthogonal border to the center |
48 | // of the border not connected with the parent. Result: "-|>" |
49 | switch(side){ |
50 | case PositionConstants.EAST: |
51 | graphics.drawLine(rect.getLeft().x,rect.getCenter().y,rect.getCenter().x,rect.getCenter().y); |
52 | graphics.drawLine(rect.getCenter().x,rect.getTop().y+rect.height/4,rect.getCenter().x,rect.getBottom().y-rect.height/4); |
53 | graphics.drawLine(rect.getCenter().x,rect.getTop().y+rect.height/4,rect.getRight().x-rect.width/4,rect.getCenter().y); |
54 | graphics.drawLine(rect.getCenter().x,rect.getBottom().y-rect.height/4,rect.getRight().x-rect.width/4,rect.getCenter().y); |
55 | break; |
56 | case PositionConstants.WEST: |
57 | graphics.drawLine(rect.getRight().x,rect.getCenter().y,rect.getCenter().x,rect.getCenter().y); |
58 | graphics.drawLine(rect.getCenter().x,rect.getTop().y+rect.height/4,rect.getCenter().x,rect.getBottom().y-rect.height/4); |
59 | graphics.drawLine(rect.getCenter().x,rect.getTop().y+rect.height/4,rect.getLeft().x+rect.width/4,rect.getCenter().y); |
60 | graphics.drawLine(rect.getCenter().x,rect.getBottom().y-rect.height/4,rect.getLeft().x+rect.width/4,rect.getCenter().y); |
61 | break; |
62 | case PositionConstants.NORTH: |
63 | graphics.drawLine( rect.getCenter().x, |
64 | rect.getBottom().y, |
65 | rect.getCenter().x, |
66 | rect.getCenter().y); |
67 | |
68 | graphics.drawLine( rect.getLeft().x+rect.width/4, |
69 | rect.getCenter().y, |
70 | rect.getRight().x-rect.width/4, |
71 | rect.getCenter().y); |
72 | |
73 | graphics.drawLine( rect.getRight().x-rect.width/4, |
74 | rect.getCenter().y, |
75 | rect.getCenter().x, |
76 | rect.getTop().y+rect.height/4); |
77 | |
78 | graphics.drawLine( rect.getLeft().x+rect.width/4, |
79 | rect.getCenter().y, |
80 | rect.getCenter().x, |
81 | rect.getTop().y+rect.height/4); |
82 | |
83 | break; |
84 | case PositionConstants.SOUTH: |
85 | graphics.drawLine( rect.getCenter().x, |
86 | rect.getTop().y, |
87 | rect.getCenter().x, |
88 | rect.getCenter().y); |
89 | |
90 | graphics.drawLine( rect.getLeft().x+rect.width/4, |
91 | rect.getCenter().y, |
92 | rect.getRight().x-rect.width/4, |
93 | rect.getCenter().y); |
94 | |
95 | graphics.drawLine( rect.getLeft().x+rect.width/4, |
96 | rect.getCenter().y, |
97 | rect.getCenter().x, |
98 | rect.getBottom().y-rect.height/4); |
99 | |
100 | graphics.drawLine( rect.getRight().x-rect.width/4, |
101 | rect.getCenter().y, |
102 | rect.getCenter().x, |
103 | rect.getBottom().y-rect.height/4); |
104 | break; |
105 | } |
106 | } |
107 | |
108 | /** |
109 | * Get the point representing the pike of the source figure. |
110 | * |
111 | * @return The point representing the location of the pike |
112 | */ |
113 | private Point getSourceCenter() { |
114 | int side = (getBorderItemLocator() == null ? PositionConstants.WEST : getBorderItemLocator().getCurrentSideOfParent()); |
115 | |
116 | Rectangle rect = getBounds(); |
117 | Point result = null; |
118 | |
119 | switch(side){ |
120 | case PositionConstants.EAST: |
121 | result = new Point(rect.getRight().x-rect.width/4, rect.getCenter().y); |
122 | break; |
123 | case PositionConstants.WEST: |
124 | result = new Point(rect.getLeft().x+rect.width/4, rect.getCenter().y); |
125 | break; |
126 | case PositionConstants.NORTH: |
127 | result = new Point(rect.getCenter().x, rect.getTop().y+rect.height/4); |
128 | break; |
129 | case PositionConstants.SOUTH: |
130 | result = new Point(rect.getCenter().x, rect.getBottom().y-rect.height/4); |
131 | break; |
132 | } |
133 | return result; |
134 | } |
135 | |
136 | /** |
137 | * places the anchor point at the center of the ( part of the figure |
138 | */ |
139 | private class SourceAnchor extends AbstractConnectionAnchor { |
140 | |
141 | public SourceAnchor(IFigure owner) { |
142 | super(owner); |
143 | } |
144 | |
145 | public Point getLocation(Point reference) { |
146 | Point p = getSourceCenter(); |
147 | getOwner().translateToAbsolute(p); |
148 | return p; |
149 | } |
150 | } |
151 | |
152 | protected ConnectionAnchor createAnchorInternal() { |
153 | return new SourceAnchor(this); |
154 | } |
155 | |
156 | protected ConnectionAnchor createAnchorExternal() { |
157 | return new StemAnchor(this); |
158 | } |
159 | } |