| 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 SinkFigure 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 SinkFigure(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 for the top border of the sink |
| 46 | // - a line for the lower border of the sink |
| 47 | // - a line from the center to the right upper corner |
| 48 | // - a line from the center to the right lower corner |
| 49 | switch(side){ |
| 50 | case PositionConstants.EAST: |
| 51 | // Sink Connector |
| 52 | graphics.drawLine( rect.getLeft().x, |
| 53 | rect.getCenter().y, |
| 54 | rect.getCenter().x-rect.width/4, |
| 55 | rect.getCenter().y); |
| 56 | |
| 57 | // Sink Base |
| 58 | graphics.drawLine( rect.getCenter().x-rect.width/4, |
| 59 | rect.getTop().y+rect.height/4, |
| 60 | rect.getCenter().x-rect.width/4, |
| 61 | rect.getBottom().y-rect.height/4); |
| 62 | |
| 63 | // upper border |
| 64 | graphics.drawLine( rect.getCenter().x-rect.width/4, |
| 65 | rect.getTop().y+rect.height/4, |
| 66 | rect.getRight().x-rect.width/4, |
| 67 | rect.getTop().y+rect.height/4); |
| 68 | |
| 69 | // lower border |
| 70 | graphics.drawLine( rect.getCenter().x-rect.width/4, |
| 71 | rect.getBottom().y-rect.height/4, |
| 72 | rect.getRight().x-rect.width/4, |
| 73 | rect.getBottom().y-rect.height/4); |
| 74 | |
| 75 | // upper dash |
| 76 | graphics.drawLine( rect.getCenter().x, |
| 77 | rect.getCenter().y, |
| 78 | rect.getRight().x-rect.width/4, |
| 79 | rect.getTop().y+rect.height/4); |
| 80 | |
| 81 | // lower dash |
| 82 | graphics.drawLine( rect.getCenter().x, |
| 83 | rect.getCenter().y, |
| 84 | rect.getRight().x-rect.width/4, |
| 85 | rect.getBottom().y-rect.height/4); |
| 86 | break; |
| 87 | case PositionConstants.WEST: |
| 88 | // Sink Connector |
| 89 | graphics.drawLine( rect.getRight().x, |
| 90 | rect.getCenter().y, |
| 91 | rect.getCenter().x+rect.width/4, |
| 92 | rect.getCenter().y); |
| 93 | |
| 94 | // Sink Base |
| 95 | graphics.drawLine( rect.getCenter().x+rect.width/4, |
| 96 | rect.getTop().y+rect.height/4, |
| 97 | rect.getCenter().x+rect.width/4, |
| 98 | rect.getBottom().y-rect.height/4); |
| 99 | |
| 100 | // upper border |
| 101 | graphics.drawLine( rect.getLeft().x+rect.width/4, |
| 102 | rect.getTop().y+rect.height/4, |
| 103 | rect.getCenter().x+rect.width/4, |
| 104 | rect.getTop().y+rect.height/4); |
| 105 | |
| 106 | // lower border |
| 107 | graphics.drawLine( rect.getLeft().x+rect.width/4, |
| 108 | rect.getBottom().y-rect.height/4, |
| 109 | rect.getCenter().x+rect.width/4, |
| 110 | rect.getBottom().y-rect.height/4); |
| 111 | |
| 112 | // upper dash |
| 113 | graphics.drawLine( rect.getCenter().x, |
| 114 | rect.getCenter().y, |
| 115 | rect.getLeft().x+rect.width/4, |
| 116 | rect.getTop().y+rect.height/4); |
| 117 | |
| 118 | // lower dash |
| 119 | graphics.drawLine( rect.getCenter().x, |
| 120 | rect.getCenter().y, |
| 121 | rect.getLeft().x+rect.width/4, |
| 122 | rect.getBottom().y-rect.height/4); |
| 123 | break; |
| 124 | case PositionConstants.NORTH: |
| 125 | // Sink Connector |
| 126 | graphics.drawLine( rect.getCenter().x, |
| 127 | rect.getBottom().y, |
| 128 | rect.getCenter().x, |
| 129 | rect.getCenter().y+rect.height/4); |
| 130 | |
| 131 | // Sink Base |
| 132 | graphics.drawLine( rect.getLeft().x+rect.width/4, |
| 133 | rect.getCenter().y+rect.height/4, |
| 134 | rect.getRight().x-rect.width/4, |
| 135 | rect.getCenter().y+rect.height/4); |
| 136 | |
| 137 | // left border |
| 138 | graphics.drawLine( rect.getLeft().x+rect.width/4, |
| 139 | rect.getCenter().y+rect.height/4, |
| 140 | rect.getLeft().x+rect.width/4, |
| 141 | rect.getTop().y+rect.height/4); |
| 142 | |
| 143 | // right border |
| 144 | graphics.drawLine( rect.getRight().x-rect.width/4, |
| 145 | rect.getCenter().y+rect.height/4, |
| 146 | rect.getRight().x-rect.width/4, |
| 147 | rect.getTop().y+rect.height/4); |
| 148 | |
| 149 | // left dash |
| 150 | graphics.drawLine( rect.getCenter().x, |
| 151 | rect.getCenter().y, |
| 152 | rect.getLeft().x+rect.width/4, |
| 153 | rect.getTop().y+rect.height/4); |
| 154 | |
| 155 | // right dash |
| 156 | graphics.drawLine( rect.getCenter().x, |
| 157 | rect.getCenter().y, |
| 158 | rect.getRight().x-rect.width/4, |
| 159 | rect.getTop().y+rect.height/4); |
| 160 | |
| 161 | break; |
| 162 | case PositionConstants.SOUTH: |
| 163 | // Sink Connector |
| 164 | graphics.drawLine( rect.getCenter().x, |
| 165 | rect.getTop().y, |
| 166 | rect.getCenter().x, |
| 167 | rect.getCenter().y-rect.height/4); |
| 168 | |
| 169 | // Sink Base |
| 170 | graphics.drawLine( rect.getLeft().x+rect.width/4, |
| 171 | rect.getCenter().y-rect.height/4, |
| 172 | rect.getRight().x-rect.width/4, |
| 173 | rect.getCenter().y-rect.height/4); |
| 174 | |
| 175 | // left border |
| 176 | graphics.drawLine( rect.getLeft().x+rect.width/4, |
| 177 | rect.getCenter().y+rect.height/4, |
| 178 | rect.getLeft().x+rect.width/4, |
| 179 | rect.getTop().y+rect.height/4); |
| 180 | |
| 181 | // right border |
| 182 | graphics.drawLine( rect.getRight().x-rect.width/4, |
| 183 | rect.getCenter().y+rect.height/4, |
| 184 | rect.getRight().x-rect.width/4, |
| 185 | rect.getTop().y+rect.height/4); |
| 186 | |
| 187 | // left dash |
| 188 | graphics.drawLine( rect.getLeft().x+rect.width/4, |
| 189 | rect.getBottom().y-rect.height/4, |
| 190 | rect.getCenter().x, |
| 191 | rect.getCenter().y); |
| 192 | |
| 193 | // right dash |
| 194 | graphics.drawLine( rect.getCenter().x, |
| 195 | rect.getCenter().y, |
| 196 | rect.getRight().x-rect.width/4, |
| 197 | rect.getBottom().y-rect.height/4); |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @return the center between the ( part of the figure |
| 204 | * relative to the position of the figure |
| 205 | */ |
| 206 | private Point getSinkCenter() { |
| 207 | int side = (getBorderItemLocator() == null ? PositionConstants.WEST : getBorderItemLocator().getCurrentSideOfParent()); |
| 208 | |
| 209 | Rectangle rect = getBounds(); |
| 210 | Point result = null; |
| 211 | |
| 212 | switch(side){ |
| 213 | case PositionConstants.EAST: |
| 214 | result = new Point(rect.getCenter().x, rect.getCenter().y); |
| 215 | break; |
| 216 | case PositionConstants.WEST: |
| 217 | result = new Point(rect.getCenter().x, rect.getCenter().y); |
| 218 | break; |
| 219 | case PositionConstants.NORTH: |
| 220 | result = new Point(rect.getCenter().x, rect.getCenter().y); |
| 221 | break; |
| 222 | case PositionConstants.SOUTH: |
| 223 | result = new Point(rect.getCenter().x, rect.getCenter().y); |
| 224 | break; |
| 225 | } |
| 226 | return result; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * places the anchor point at the center of the ( part of the figure |
| 231 | */ |
| 232 | private class SinkAnchor extends AbstractConnectionAnchor { |
| 233 | |
| 234 | public SinkAnchor(IFigure owner) { |
| 235 | super(owner); |
| 236 | } |
| 237 | |
| 238 | public Point getLocation(Point reference) { |
| 239 | Point p = getSinkCenter(); |
| 240 | getOwner().translateToAbsolute(p); |
| 241 | return p; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Get the anchor for connectors linked to this figure. |
| 247 | * The anchor internal represents the anchor if this |
| 248 | * sink belongs to an internal assembly context. |
| 249 | * |
| 250 | * @return The connection anchor with the appropriate position |
| 251 | */ |
| 252 | protected ConnectionAnchor createAnchorInternal() { |
| 253 | return new SinkAnchor(this); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | /** |
| 258 | * Get the anchor for connectors linked to this figure. |
| 259 | * The anchor external represents the anchor if this |
| 260 | * sink belongs to an external system sink. |
| 261 | * |
| 262 | * @return The connection anchor with the appropriate position |
| 263 | */ |
| 264 | protected ConnectionAnchor createAnchorExternal() { |
| 265 | return new StemAnchor(this); |
| 266 | } |
| 267 | } |