Skip to content

Commit

Permalink
Cleaned Draw2d Tree example
Browse files Browse the repository at this point in the history
To show how updated IFgiure getChildren typed interface can be used to
improve client code the Draw2d example was cleaned and reworked.
  • Loading branch information
azoitl committed Apr 10, 2023
1 parent 6b3c9ab commit 1149e92
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@

import org.eclipse.draw2d.AbstractLayout;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Transposer;

/**
*
* @author hudsonr Created on Apr 22, 2003
*/
public abstract class BranchLayout extends AbstractLayout {
public abstract class AbstractBranchLayout extends AbstractLayout {

private Transposer transposer;
final TreeBranch branch;
Expand All @@ -33,7 +32,7 @@ public abstract class BranchLayout extends AbstractLayout {
int[] preferredRowHeights;
int rowHeight;

public BranchLayout(TreeBranch branch) {
protected AbstractBranchLayout(TreeBranch branch) {
this.branch = branch;
}

Expand Down Expand Up @@ -65,8 +64,8 @@ public int[] getPreferredRowHeights() {
return preferredRowHeights;
}

List<? extends IFigure> getSubtrees() {
return branch.getContentsPane().getChildren();
List<TreeBranch> getSubtrees() {
return branch.getSubtrees();
}

Transposer getTransposer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -40,13 +39,11 @@ public class Animation {
static boolean PLAYBACK;
static boolean RECORDING;

static Map initialStates;
static Map finalStates;
static Map<IFigure, List<Rectangle>> initialStates;
static Map<IFigure, List<Rectangle>> finalStates;

static void end() {
Iterator iter = initialStates.keySet().iterator();
while (iter.hasNext())
((IFigure) iter.next()).revalidate();
initialStates.keySet().forEach(IFigure::revalidate);
initialStates = null;
finalStates = null;
PLAYBACK = false;
Expand All @@ -58,12 +55,13 @@ static void end() {
static void mark(IFigure figure) {
trackMe = figure;
trackLocation = trackMe.getBounds().getLocation();
while (!(figure instanceof Viewport))
while (!(figure instanceof Viewport)) {
figure = figure.getParent();
}
viewport = (Viewport) figure;

initialStates = new HashMap();
finalStates = new HashMap();
initialStates = new HashMap<>();
finalStates = new HashMap<>();
start = System.currentTimeMillis();
finish = start + DURATION;
current = start + 20;
Expand All @@ -75,62 +73,56 @@ static void captureLayout(IFigure root) {
root = root.getParent();

root.validate();
Iterator iter = initialStates.keySet().iterator();
while (iter.hasNext())
recordFinalStates((IFigure) iter.next());
initialStates.keySet().forEach(Animation::recordFinalStates);

RECORDING = false;
PLAYBACK = true;
}

static boolean playbackState(IFigure container) {
if (!PLAYBACK)
return false;
List initial = (List) initialStates.get(container);
List<Rectangle> initial = initialStates.get(container);
if (initial == null) {
System.out.println("Error playing back state");
return false;
}
List target = (List) finalStates.get(container);
List<Rectangle> target = finalStates.get(container);
List<? extends IFigure> children = container.getChildren();
Rectangle rect1, rect2;

for (int i = 0; i < children.size(); i++) {
IFigure child = (IFigure) children.get(i);
rect1 = (Rectangle) initial.get(i);
rect2 = (Rectangle) target.get(i);
IFigure child = children.get(i);
Rectangle rect1 = initial.get(i);
Rectangle rect2 = target.get(i);
child.setBounds(new Rectangle((int) Math.round(progress * rect2.x + (1 - progress) * rect1.x),
(int) Math.round(progress * rect2.y + (1 - progress) * rect1.y),
(int) Math.round(progress * rect2.width + (1 - progress) * rect1.width),
(int) Math.round(progress * rect2.height + (1 - progress) * rect1.height)));
// child.invalidate();
}
return true;
}

static void recordFinalStates(IFigure container) {
List list = new ArrayList();
finalStates.put(container, list);
List children = container.getChildren();
list.clear();
for (int i = 0; i < children.size(); i++)
list.add(((IFigure) children.get(i)).getBounds().getCopy());
recordStates(container, finalStates);
}

static void recordInitialState(IFigure container) {
if (!RECORDING)
return;
List list = (List) initialStates.get(container);
List<Rectangle> list = initialStates.get(container);
if (list != null)
return;
// System.out.println("Error recording initial state");
initialStates.put(container, list = new ArrayList());
list.clear();
for (IFigure child : container.getChildren()) {
list.add(child.getBounds().getCopy());
}
recordStates(container, initialStates);
}

private static void recordStates(IFigure container, Map<IFigure, List<Rectangle>> state) {
final List<Rectangle> newList = new ArrayList<>();
state.put(container, newList);
container.getChildren().forEach(child -> newList.add(child.getBounds().getCopy()));
}

static void swap() {
Map temp = finalStates;
var temp = finalStates;
finalStates = initialStates;
initialStates = temp;
}
Expand All @@ -139,10 +131,8 @@ static boolean step() {
current = System.currentTimeMillis() + 30;
progress = (double) (current - start) / (finish - start);
progress = Math.min(progress, 0.999);
Iterator iter = initialStates.keySet().iterator();

while (iter.hasNext())
((IFigure) iter.next()).revalidate();
initialStates.keySet().forEach(IFigure::revalidate);
viewport.validate();

Point loc = viewport.getViewLocation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,31 @@
*
* @author hudsonr Created on Apr 22, 2003
*/
class HangingLayout extends BranchLayout {
class HangingLayout extends AbstractBranchLayout {

HangingLayout(TreeBranch branch) {
super(branch);
}

@Override
void calculateDepth() {
depth = 0;
for (IFigure child : branch.contents.getChildren()) {
depth += ((TreeBranch) child).getDepth();
}
depth++;
depth = getSubtrees().stream().mapToInt(TreeBranch::getDepth).sum() + 1;
}

@Override
void setRowHeights(int[] heights, int offset) {
super.setRowHeights(heights, offset);
offset++;
if (branch.isExpanded()) {
TreeBranch subtree;
for (IFigure child : branch.contents.getChildren()) {
subtree = (TreeBranch) child;
for (TreeBranch subtree : branch.getSubtrees()) {
subtree.setRowHeights(heights, offset);
offset += subtree.getDepth();
}
}
}

/**
* @see org.eclipse.draw2d.examples.tree.BranchLayout#updateRowHeights()
* @see org.eclipse.draw2d.examples.tree.AbstractBranchLayout#updateRowHeights()
*/
@Override
void updateRowHeights() {
Expand All @@ -63,13 +57,9 @@ void updateRowHeights() {
if (!branch.isExpanded())
return;

List subtrees = getSubtrees();
TreeBranch subtree;

int offset = 1;
for (int i = 0; i < subtrees.size(); i++) {
subtree = (TreeBranch) subtrees.get(i);
int rowHeights[] = subtree.getPreferredRowHeights();
for (TreeBranch subtree : branch.getSubtrees()) {
int[] rowHeights = subtree.getPreferredRowHeights();
for (int row = 0; row < rowHeights.length; row++)
preferredRowHeights[row + offset] = rowHeights[row];
offset += subtree.getDepth();
Expand All @@ -80,6 +70,7 @@ int getGap() {
return branch.getRoot().getMinorSpacing();
}

@Override
protected Dimension calculatePreferredSize(IFigure container, int wHint, int hHint) {
Transposer transposer = branch.getRoot().getTransposer();
Dimension result = transposer.t(branch.getNode().getPreferredSize().getCopy());
Expand All @@ -93,6 +84,7 @@ protected Dimension calculatePreferredSize(IFigure container, int wHint, int hHi
return transposer.t(result);
}

@Override
public void layout(IFigure f) {
Animation.recordInitialState(f);
if (Animation.playbackState(f))
Expand All @@ -116,38 +108,34 @@ public void layout(IFigure f) {
}

/**
* @see org.eclipse.draw2d.examples.tree.BranchLayout#paintLines(org.eclipse.draw2d.Graphics)
* @see org.eclipse.draw2d.examples.tree.AbstractBranchLayout#paintLines(org.eclipse.draw2d.Graphics)
*/
@Override
void paintLines(Graphics g) {
List<TreeBranch> children = branch.getSubtrees();
if (children.isEmpty())
return;

int gap = getGap();
IFigure node = branch.getNode();

if (getTransposer().isEnabled()) {
IFigure node = branch.getNode();
IFigure contents = branch.getContentsPane();
int x = node.getBounds().right();
int y = node.getBounds().y() + gap;
List<? extends IFigure> children = contents.getChildren();
if (children.isEmpty())
return;
int right = x;
for (int i = 0; i < children.size(); i++) {
Point pt = ((TreeBranch) children.get(i)).getNodeBounds().getTop();
for (TreeBranch subTree : children) {
Point pt = subTree.getNodeBounds().getTop();
g.drawLine(pt.x(), y, pt.x(), pt.y());
right = Math.max(right, pt.x());
}
g.drawLine(x, y, right, y);

} else {
IFigure node = branch.getNode();
IFigure contents = branch.getContentsPane();
int x = node.getBounds().x() + gap;
int y = node.getBounds().bottom();
List<? extends IFigure> children = contents.getChildren();
if (children.isEmpty())
return;
int bottom = y;
for (int i = 0; i < children.size(); i++) {
Point pt = ((TreeBranch) children.get(i)).getNodeBounds().getLeft();
for (TreeBranch subTree : children) {
Point pt = subTree.getNodeBounds().getLeft();
g.drawLine(x, pt.y(), pt.x(), pt.y());
bottom = Math.max(bottom, pt.y());
}
Expand All @@ -174,18 +162,15 @@ void updateContours() {
if (!branch.isExpanded())
return;

TreeBranch subtree;

int leftSide = getGap();
for (int i = 1; i < getDepth(); i++)
cachedContourLeft[i] = leftSide;

int rightMargin;
int offset = 1;
for (IFigure child : branch.contents.getChildren()) {
subtree = (TreeBranch) child;
for (TreeBranch subtree : branch.getSubtrees()) {
rightMargin = rightEdge - transposer.t(subtree.getBounds()).right();
int rightContour[] = subtree.getContourRight();
int[] rightContour = subtree.getContourRight();
for (int j = 0; j < rightContour.length; j++)
cachedContourRight[j + offset] = rightContour[j] + rightMargin;
offset += subtree.getDepth();
Expand Down
Loading

0 comments on commit 1149e92

Please sign in to comment.