001: /**
002: *
003: */package newprocess.diagram.cust.locator;
004:
005: import java.util.Iterator;
006: import java.util.List;
007:
008: import newprocess.diagram.cust.figures.TermDefaultSizeNodeFigure;
009: import newprocess.diagram.edit.parts.ConditionTermConditionProxyEditPart;
010: import newprocess.diagram.edit.parts.ConditionTermEditPart;
011:
012: import org.eclipse.draw2d.IFigure;
013: import org.eclipse.draw2d.PositionConstants;
014: import org.eclipse.draw2d.geometry.Point;
015: import org.eclipse.draw2d.geometry.Rectangle;
016: import org.eclipse.gef.EditPart;
017: import org.eclipse.gef.RootEditPart;
018: import org.eclipse.gmf.runtime.diagram.ui.figures.BorderItemLocator;
019:
020: /**
021: * @author sh
022: *
023: */
024: public class TermtBorderItemLocator extends BorderItemLocator {
025:
026: public TermtBorderItemLocator(IFigure borderItem,
027: IFigure parentFigure, Rectangle constraint) {
028: super (borderItem, parentFigure, constraint);
029: }
030:
031: public TermtBorderItemLocator(IFigure parentFigure,
032: int preferredSide) {
033: super (parentFigure, preferredSide);
034: }
035:
036: public TermtBorderItemLocator(IFigure parentFigure) {
037: super (parentFigure);
038: }
039:
040: /**
041: *
042: * If the Parent shape has been resized, this method calculates the new
043: * positions of the border items especially the spacings between them
044: * @see org.eclipse.gmf.runtime.diagram.ui.figures.BorderItemLocator#relocate(org.eclipse.draw2d.IFigure)
045: *
046: * @author sh
047: */
048: public void relocate(IFigure borderItem) {
049: // get the parent border
050: Rectangle parentBorder = getParentBorder();
051:
052: // get the sizes
053: int itemX = parentBorder.x;
054: int borderWidth = getParentBorder().width;
055: int numberPorts = borderItem.getParent().getChildren().size() - 1;
056: int portWidth = borderItem.getBounds().width;
057: // check the position in the children collection
058: int index = getPositionIndex(borderItem);
059:
060: // Get the preferred location. If none has been previously set, use the
061: // preferred side to take an initial guess.
062: Point preferredLocation = getPreferredLocation(borderItem);
063: int preferredSide = getPreferredSideOfParent();
064:
065: // calculate the horizontal gap between the ports
066: int horizontal_gap = (borderWidth - numberPorts * portWidth)
067: / (numberPorts + 1);
068:
069: // place all ports with the same spacing
070: preferredLocation.x = itemX + horizontal_gap * index
071: + (index - 1) * portWidth;
072:
073: Point ptNewLocation = locateOnBorder(preferredLocation,
074: preferredSide, 0, borderItem, horizontal_gap);
075: borderItem.setLocation(ptNewLocation);
076:
077: // set the current/next side of the parent
078: setCurrentSideOfParent(findClosestSideOfParent(new Rectangle(
079: ptNewLocation, borderItem.getBounds().getSize()),
080: getParentBorder()));
081: }
082:
083: /**
084: * The preferred side takes precendence.
085: *
086: * @param suggestedLocation
087: * @param suggestedSide
088: * @param circuitCount
089: * recursion count to avoid an infinite loop
090: * @return point
091: */
092:
093: private Point locateOnBorder(Point suggestedLocation,
094: int suggestedSide, int circuitCount, IFigure borderItem,
095: int gab) {
096: return locateOnParent(suggestedLocation, suggestedSide,
097: borderItem);
098: }
099:
100: /**
101: * Ensure the suggested location actually lies on the parent boundary. The
102: * side takes precendence.
103: *
104: * @param suggestedLocation
105: * @param suggestedSide
106: * @return point
107: */
108: private Point locateOnParent(Point suggestedLocation,
109: int suggestedSide, IFigure borderItem) {
110:
111: Rectangle bounds = getParentBorder();
112: int parentFigureWidth = bounds.width;
113: int parentFigureHeight = bounds.height;
114: int parentFigureX = bounds.x;
115: int parentFigureY = bounds.y;
116: Rectangle borderItemBounds = borderItem.getBounds();
117: int newX = suggestedLocation.x;
118: int newY = suggestedLocation.y;
119: int westX = parentFigureX - borderItemBounds.width
120: + getBorderItemOffset().width;
121: int eastX = parentFigureX + parentFigureWidth
122: - getBorderItemOffset().width;
123: int southY = parentFigureY + parentFigureHeight
124: - getBorderItemOffset().height;
125: int northY = parentFigureY - borderItemBounds.height
126: + getBorderItemOffset().height;
127:
128: if (suggestedSide == PositionConstants.WEST) {
129: if (suggestedLocation.x != westX) {
130: newX = westX;
131: }
132: if (suggestedLocation.y < bounds.getTopLeft().y) {
133: newY = northY + borderItemBounds.height;
134: } else if (suggestedLocation.y > bounds.getBottomLeft().y
135: - borderItemBounds.height) {
136: newY = southY - borderItemBounds.height;
137: }
138: } else if (suggestedSide == PositionConstants.EAST) {
139: if (suggestedLocation.x != eastX) {
140: newX = eastX;
141: }
142: if (suggestedLocation.y < bounds.getTopLeft().y) {
143: newY = northY + borderItemBounds.height;
144: } else if (suggestedLocation.y > bounds.getBottomLeft().y
145: - borderItemBounds.height) {
146: newY = southY - borderItemBounds.height;
147: }
148: } else if (suggestedSide == PositionConstants.SOUTH) {
149: if (suggestedLocation.y != southY) {
150: newY = southY;
151: }
152: if (suggestedLocation.x < bounds.getBottomLeft().x) {
153: newX = westX + borderItemBounds.width;
154: } else if (suggestedLocation.x > bounds.getBottomRight().x
155: - borderItemBounds.width) {
156: newX = eastX - borderItemBounds.width;
157: }
158: } else {
159: // NORTH
160: if (suggestedLocation.y != northY) {
161: newY = northY;
162: }
163: if (suggestedLocation.x < bounds.getBottomLeft().x) {
164: newX = westX + borderItemBounds.width;
165: } else if (suggestedLocation.x > bounds.getBottomRight().x
166: - borderItemBounds.width) {
167: newX = eastX - borderItemBounds.width;
168: }
169: }
170: return new Point(newX, newY);
171: }
172:
173: /**
174: *
175: * get a x coordinate of the connection to the proxy
176: *
177: * @author sh
178: *
179: * @param fig
180: * @return
181: */
182: private int aquireProxyX(IFigure fig) {
183: if (fig instanceof TermDefaultSizeNodeFigure) {
184: EditPart myPart = ((TermDefaultSizeNodeFigure) fig)
185: .getMyPort();
186: List conList = null;
187: if (myPart instanceof RootEditPart) {
188: //conList = ((RootEditPart)myPart).getSourceConnections();
189: } else if (myPart instanceof ConditionTermEditPart) {
190: conList = ((ConditionTermEditPart) myPart)
191: .getSourceConnections();
192: }
193: if (conList == null || conList.isEmpty())
194: return 0;
195: ConditionTermConditionProxyEditPart con = (ConditionTermConditionProxyEditPart) conList
196: .get(0);
197: return con.getConnectionFigure().getTargetAnchor()
198: .getLocation(new Point(0, 0)).x;
199: }
200: return 0;
201: }
202:
203: /**
204: *
205: * Calculate the position index based on referenced proxies as little pretty layout
206: *
207: * @author ts + sh
208: *
209: * @param borderItem
210: * @return
211: */
212: private int getPositionIndex(IFigure borderItem) {
213: List portList = borderItem.getParent().getChildren();
214: Iterator<IFigure> it = portList.iterator();
215: int order = 1;
216: while (it.hasNext()) {
217: IFigure item = it.next();
218: if (item != borderItem
219: && item instanceof TermDefaultSizeNodeFigure) {
220: int bx = aquireProxyX(borderItem);
221: int ix = aquireProxyX(item);
222: if (bx > ix)
223: ++order;
224: if (bx == ix) {
225: int bi = portList.indexOf(borderItem);
226: int ii = portList.indexOf(item);
227: if (bi < ii) {
228: ++order;
229: }
230: }
231: }
232: }
233: return order;
234: }
235: }
|