| 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 | import org.eclipse.gmf.runtime.draw2d.ui.figures.IBorderItemLocator; |
| 11 | import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure; |
| 12 | |
| 13 | /** |
| 14 | * Baseclass for both --( and --() type UML style border figures |
| 15 | * |
| 16 | * @author Philipp Meier |
| 17 | */ |
| 18 | abstract public class AbstractBorderFigure extends DefaultSizeNodeFigure { |
| 19 | |
| 20 | /** |
| 21 | * position type of the figure. internal or external. |
| 22 | * internal means the figure is connected towards the outside |
| 23 | * external means the figure is connected towards the inside |
| 24 | */ |
| 25 | public static enum POSITION_TYPE { |
| 26 | /** conntected towards the outside */ |
| 27 | POS_INTERNAL , |
| 28 | |
| 29 | /** connected towards the inside */ |
| 30 | POS_EXTERNAL |
| 31 | } |
| 32 | |
| 33 | private POSITION_TYPE myPosType; |
| 34 | |
| 35 | /** |
| 36 | * reference to the anchor to be used. |
| 37 | */ |
| 38 | private ConnectionAnchor myAnchor = null; |
| 39 | |
| 40 | /** |
| 41 | * @param size width and hight of the figure in logical units (LP) |
| 42 | */ |
| 43 | public AbstractBorderFigure(int logicalSize, POSITION_TYPE posType) { |
| 44 | super(logicalSize, logicalSize); |
| 45 | myPosType = posType; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /* (non-Javadoc) |
| 50 | * @see org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure#paintFigure(org.eclipse.draw2d.Graphics) |
| 51 | */ |
| 52 | @Override |
| 53 | protected void paintFigure(Graphics graphics) { |
| 54 | // |
| 55 | super.paintFigure(graphics); |
| 56 | //Next line needed for better display of Ports on Mac OS |
| 57 | graphics.setLineWidthFloat(new Float(1.01)); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Helper function to get the parent's border item locator |
| 64 | * |
| 65 | * @return the parent's border item locator |
| 66 | */ |
| 67 | protected IBorderItemLocator getBorderItemLocator() { |
| 68 | IFigure parentFigure = this.getParent().getParent(); |
| 69 | if (parentFigure != null && parentFigure.getLayoutManager() != null) { |
| 70 | Object constraint = parentFigure.getLayoutManager().getConstraint( |
| 71 | this.getParent()); |
| 72 | if (constraint instanceof IBorderItemLocator) { |
| 73 | return (IBorderItemLocator) constraint; |
| 74 | } |
| 75 | } |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * factory method for the anchor to be used when figure is |
| 81 | * in an internal position |
| 82 | * |
| 83 | * @return a reference to the new anchor. must not be null |
| 84 | */ |
| 85 | abstract protected ConnectionAnchor createAnchorInternal(); |
| 86 | |
| 87 | /** |
| 88 | * factory method for the anchor to be used when figure is |
| 89 | * in an external position |
| 90 | * |
| 91 | * @return a reference to the new anchor. must not be null |
| 92 | */ |
| 93 | abstract protected ConnectionAnchor createAnchorExternal(); |
| 94 | |
| 95 | public ConnectionAnchor getConnectionAnchor(String terminal) { |
| 96 | if (myAnchor == null) { |
| 97 | if (myPosType == POSITION_TYPE.POS_INTERNAL) { |
| 98 | myAnchor = createAnchorInternal(); |
| 99 | } |
| 100 | |
| 101 | if (myPosType == POSITION_TYPE.POS_EXTERNAL) { |
| 102 | myAnchor = createAnchorExternal(); |
| 103 | } |
| 104 | } |
| 105 | assert(myAnchor != null); |
| 106 | |
| 107 | return myAnchor; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @return the end position of the -- part of the figure |
| 112 | * relative to the position of the figure |
| 113 | */ |
| 114 | private Point getStemPosition() { |
| 115 | int side = (getBorderItemLocator() == null ? PositionConstants.WEST : getBorderItemLocator().getCurrentSideOfParent()); |
| 116 | |
| 117 | Rectangle rect = getBounds(); |
| 118 | Point result = null; |
| 119 | |
| 120 | switch(side){ |
| 121 | case PositionConstants.EAST: |
| 122 | result = new Point(rect.getLeft().x, rect.getCenter().y); |
| 123 | break; |
| 124 | case PositionConstants.WEST: |
| 125 | result = new Point(rect.getRight().x, rect.getCenter().y); |
| 126 | break; |
| 127 | case PositionConstants.NORTH: |
| 128 | result = new Point(rect.getCenter().x, rect.getBottom().y); |
| 129 | break; |
| 130 | case PositionConstants.SOUTH: |
| 131 | result = new Point(rect.getCenter().x, rect.getTop().y); |
| 132 | break; |
| 133 | } |
| 134 | return result; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * places the anchor point at the -- part of the figure. |
| 139 | * this class is to be used by subclasses |
| 140 | */ |
| 141 | protected class StemAnchor extends AbstractConnectionAnchor { |
| 142 | |
| 143 | public StemAnchor(IFigure owner) { |
| 144 | super(owner); |
| 145 | } |
| 146 | |
| 147 | public Point getLocation(Point reference) { |
| 148 | Point p = getStemPosition(); |
| 149 | getOwner().translateToAbsolute(p); |
| 150 | return p; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | } |