001: package visualdebugger.draw2d;
002:
003: import java.util.List;
004:
005: import org.eclipse.draw2d.Graphics;
006: import org.eclipse.draw2d.IFigure;
007: import org.eclipse.draw2d.PositionConstants;
008: import org.eclipse.draw2d.geometry.Dimension;
009: import org.eclipse.draw2d.geometry.Point;
010: import org.eclipse.draw2d.geometry.Rectangle;
011: import org.eclipse.draw2d.geometry.Transposer;
012:
013: class NormalLayout extends BranchLayout {
014:
015: NormalLayout(TreeBranch branch) {
016: super (branch);
017: }
018:
019: /**
020: * @see org.eclipse.draw2d.examples.tree.BranchLayout#calculateDepth()
021: */
022: void calculateDepth() {
023: depth = 0;
024: List subtrees = getSubtrees();
025: for (int i = 0; i < subtrees.size(); i++)
026: depth = Math.max(depth, ((TreeBranch) subtrees.get(i))
027: .getDepth());
028: depth++;
029: }
030:
031: protected Dimension calculatePreferredSize(IFigure container,
032: int wHint, int hHint) {
033: Rectangle union = branch.getNodeBounds().getCopy();
034: // if (branch.isExpanded())
035: union.union(branch.getContentsPane().getBounds());
036:
037: return union.getSize();
038: }
039:
040: public void layout(IFigure f) {
041: // Animation.recordInitialState(f);
042: // if (Animation.playbackState(f))
043: // return;
044:
045: Transposer transposer = getTransposer();
046: IFigure contents = branch.getContentsPane();
047: IFigure node = branch.getNode();
048: contents.validate();
049:
050: Rectangle branchBounds = transposer.t(branch.getBounds());
051: Point topLeft = branchBounds.getTopLeft();
052: Rectangle nodeLocation = new Rectangle(topLeft, transposer
053: .t(node.getPreferredSize()));
054: nodeLocation.height = rowHeight - getMajorSpacing();
055:
056: if (!contents.isVisible() || contents.getChildren().isEmpty()) {
057: nodeLocation.x += (branchBounds.width - nodeLocation.width) / 2;
058: node.setBounds(transposer.t(nodeLocation));
059: contents.setBounds(transposer.t(nodeLocation.getTranslated(
060: 0, rowHeight).setSize(0, 0)));
061: return;
062: }
063:
064: Rectangle contentsLocation = new Rectangle(topLeft, transposer
065: .t(contents.getPreferredSize()));
066: contents.setSize(contents.getPreferredSize());
067: contentsLocation.y += rowHeight;
068:
069: TreeBranch firstChild = (TreeBranch) contents.getChildren()
070: .get(0);
071: TreeBranch lastChild = (TreeBranch) contents.getChildren().get(
072: contents.getChildren().size() - 1);
073: int leftInset = firstChild.getContourLeft()[0]
074: + transposer.t(firstChild.getBounds()).x
075: - transposer.t(contents.getBounds()).x;
076: int rightInset = lastChild.getContourRight()[0]
077: - transposer.t(lastChild.getBounds()).right()
078: + transposer.t(contents.getBounds()).right();
079: rightInset = Math.max(rightInset, 0);
080: leftInset = Math.max(leftInset, 0);
081: int childrenSpan = contentsLocation.width - leftInset
082: - rightInset;
083:
084: switch (branch.getAlignment()) {
085: case PositionConstants.CENTER:
086: leftInset += (childrenSpan - nodeLocation.width) / 2;
087: }
088:
089: if (leftInset > 0)
090: nodeLocation.x += leftInset;
091: else
092: contentsLocation.x -= leftInset;
093:
094: int adjust = branchBounds.width
095: - Rectangle.SINGLETON.setBounds(contentsLocation)
096: .union(nodeLocation).width;
097: adjust /= 2;
098: nodeLocation.x += adjust;
099: contentsLocation.x += adjust;
100: if (node instanceof SourceElementFigure
101: || node instanceof MethodInvocationFigure
102: || node instanceof MethodReturnFigure) //TODO
103: node.setBounds(transposer.t(nodeLocation));
104: else
105: node.setBounds(transposer.t(nodeLocation).setSize(10, 10));
106: // Animation.setBounds(node, transposer.t(nodeLocation));
107: // Animation.setBounds(contents, transposer.t(contentsLocation));
108: contents.setBounds(transposer.t(contentsLocation));
109: }
110:
111: void mergeContour(int[] destination, int[] source, int startdepth,
112: int offset) {
113: for (int i = startdepth; i < source.length; i++)
114: destination[i + 1] = source[i] + offset;
115: }
116:
117: /**
118: * @see org.eclipse.draw2d.examples.tree.BranchLayout#paintLines(org.eclipse.draw2d.Graphics)
119: */
120: void paintLines(Graphics g) {
121:
122: if (true)
123: return; //TODO
124:
125: if (getTransposer().isEnabled()) {
126: IFigure node = branch.getNode();
127: int left = node.getBounds().right();
128: int right = branch.getContentsPane().getBounds().x - 1;
129: int yMid = node.getBounds().getCenter().y;
130: int xMid = (left + right) / 2;
131: List children = getSubtrees();
132: if (children.size() == 0)
133: return;
134: g.drawLine(left, yMid, xMid, yMid);
135: int yMin = yMid;
136: int yMax = yMid;
137: for (int i = 0; i < children.size(); i++) {
138: int y = ((TreeBranch) children.get(i)).getNodeBounds()
139: .getCenter().y;
140: g.drawLine(xMid, y, right, y);
141: yMin = Math.min(yMin, y);
142: yMax = Math.max(yMax, y);
143: }
144: g.drawLine(xMid, yMin, xMid, yMax);
145:
146: } else {
147: IFigure node = branch.getNode();
148: int xMid = node.getBounds().getCenter().x;
149: int top = node.getBounds().bottom();
150: int bottom = branch.getContentsPane().getBounds().y - 1;
151: int yMid = (top + bottom) / 2;
152: List children = getSubtrees();
153: if (children.size() == 0)
154: return;
155: g.drawLine(xMid, top, xMid, yMid);
156: int xMin = xMid;
157: int xMax = xMid;
158: for (int i = 0; i < children.size(); i++) {
159: int x = ((TreeBranch) children.get(i)).getNodeBounds()
160: .getCenter().x;
161: g.drawLine(x, yMid, x, bottom);
162: xMin = Math.min(xMin, x);
163: xMax = Math.max(xMax, x);
164: }
165: g.drawLine(xMin, yMid, xMax, yMid);
166: }
167: }
168:
169: /**
170: * @see org.eclipse.draw2d.examples.tree.BranchLayout#updateContours()
171: */
172: void updateContours() {
173: Transposer transposer = getTransposer();
174: //Make sure we layout first
175: branch.validate();
176:
177: cachedContourLeft = new int[getDepth()];
178: cachedContourRight = new int[getDepth()];
179:
180: Rectangle clientArea = transposer.t(branch.getNodeBounds()
181: .getUnion(branch.contents.getBounds()));
182: Rectangle nodeBounds = transposer.t(branch.getNodeBounds());
183: Rectangle contentsBounds = transposer.t(branch
184: .getContentsPane().getBounds());
185:
186: cachedContourLeft[0] = nodeBounds.x - clientArea.x;
187: cachedContourRight[0] = clientArea.right() - nodeBounds.right();
188: // if (!branch.isExpanded())
189: // return;
190:
191: List subtrees = getSubtrees();
192: TreeBranch subtree;
193:
194: int currentDepth = 0;
195: for (int i = 0; i < subtrees.size()
196: && currentDepth < getDepth(); i++) {
197: subtree = (TreeBranch) subtrees.get(i);
198: if (subtree.getDepth() > currentDepth) {
199: int leftContour[] = subtree.getContourLeft();
200: int leftOffset = transposer.t(subtree.getBounds()).x
201: - clientArea.x;
202: mergeContour(cachedContourLeft, leftContour,
203: currentDepth, leftOffset);
204: currentDepth = subtree.getDepth();
205: }
206: }
207:
208: currentDepth = 0;
209: for (int i = subtrees.size() - 1; i >= 0
210: && currentDepth < getDepth(); i--) {
211: subtree = (TreeBranch) subtrees.get(i);
212: if (subtree.getDepth() > currentDepth) {
213: int rightContour[] = subtree.getContourRight();
214: int rightOffset = clientArea.right()
215: - transposer.t(subtree.getBounds()).right();
216: mergeContour(cachedContourRight, rightContour,
217: currentDepth, rightOffset);
218: currentDepth = subtree.getDepth();
219: }
220: }
221: }
222:
223: /**
224: * @see org.eclipse.draw2d.examples.tree.BranchLayout#updateRowHeights()
225: */
226: void updateRowHeights() {
227: Transposer transposer = getTransposer();
228: preferredRowHeights = new int[getDepth()];
229:
230: preferredRowHeights[0] = transposer.t(branch.getNode()
231: .getPreferredSize()).height
232: + getMajorSpacing();
233:
234: // if (!branch.isExpanded())
235: // return;
236:
237: List subtrees = getSubtrees();
238: TreeBranch subtree;
239:
240: for (int i = 0; i < subtrees.size(); i++) {
241: subtree = (TreeBranch) subtrees.get(i);
242: int rowHeights[] = subtree.getPreferredRowHeights();
243: for (int row = 0; row < rowHeights.length; row++)
244: preferredRowHeights[row + 1] = Math.max(
245: preferredRowHeights[row + 1], rowHeights[row]);
246: }
247: }
248:
249: /**
250: * @see org.eclipse.draw2d.examples.tree.BranchLayout#setRowHeights(int[], int)
251: */
252: void setRowHeights(int[] heights, int offset) {
253: super .setRowHeights(heights, offset);
254: if (true/*branch.isExpanded()*/) {
255: List subtrees = getSubtrees();
256: offset++;
257: for (int i = 0; i < subtrees.size(); i++)
258: ((TreeBranch) subtrees.get(i)).setRowHeights(heights,
259: offset);
260: }
261: }
262:
263: }
|