Skip to content

Commit

Permalink
[295002] Optional node space configuration (contributed by Miles Parker)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Jul 29, 2011
1 parent ee7744d commit 3c1b093
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
Expand All @@ -28,11 +29,11 @@ public static void main(String[] args) {
// Create the shell
Display d = new Display();
Shell shell = new Shell(d);
shell.setText("GraphSnippet1");
shell.setText("TreeLayoutExample");
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 10;
shell.setLayout(gridLayout);
shell.setSize(500, 500);
shell.setSize(600, 500);

final Graph g = new Graph(shell, SWT.NONE);
final TreeLayoutAlgorithm algorithm = new TreeLayoutAlgorithm();
Expand Down Expand Up @@ -103,6 +104,15 @@ public void widgetSelected(SelectionEvent e) {
}
});

final Button nodeSpaceButton = new Button(shell, SWT.CHECK);
nodeSpaceButton.setText("Set node space");
nodeSpaceButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
algorithm.setNodeSpace(nodeSpaceButton.getSelection() ? new Dimension(
100, 100) : null);
}
});

shell.open();
while (!shell.isDisposed()) {
while (!d.readAndDispatch()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
* Contributors: The Chisel Group - initial API and implementation
* Mateusz Matela
* Ian Bull
* Miles Parker - optional node space configuration
******************************************************************************/
package org.eclipse.zest.layouts.algorithms;

import java.util.Iterator;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.zest.layouts.LayoutAlgorithm;
import org.eclipse.zest.layouts.algorithms.TreeLayoutObserver.TreeNode;
import org.eclipse.zest.layouts.dataStructures.DisplayIndependentRectangle;
Expand Down Expand Up @@ -65,11 +67,52 @@ public class TreeLayoutAlgorithm implements LayoutAlgorithm {

private TreeLayoutObserver treeObserver;

private Dimension nodeSpace;

/**
* Create a default Tree Layout.
*/
public TreeLayoutAlgorithm() {
}

/**
* Create a Tree Layout with a specified direction.
*
* @param direction
* The direction, one of {@link TreeLayoutAlgorithm#BOTTOM_UP},
* {@link TreeLayoutAlgorithm#LEFT_RIGHT},
* {@link TreeLayoutAlgorithm#RIGHT_LEFT},
* {@link TreeLayoutAlgorithm#TOP_DOWN}
*/
public TreeLayoutAlgorithm(int direction) {
this(direction, null);
}

/**
* Create a Tree Layout with fixed size spacing around nodes. If nodeSpace
* is not null, the layout will size the container to the ideal space to
* just contain all nodes of fixed size without any overlap. Otherwise, the
* algorithm will size for the container's available space.
*
* @param direction
* The direction, one of {@link TreeLayoutAlgorithm#BOTTOM_UP},
* {@link TreeLayoutAlgorithm#LEFT_RIGHT},
* {@link TreeLayoutAlgorithm#RIGHT_LEFT},
* {@link TreeLayoutAlgorithm#TOP_DOWN}
* @param nodeSpace
* the size to make each node. May be null.
*/
public TreeLayoutAlgorithm(int direction, Dimension nodeSpace) {
setDirection(direction);
this.nodeSpace = nodeSpace;
}

/**
* @param nodeSpaceSize
* the nodeSpaceSize to set
*/
public void setNodeSpace(Dimension nodeSpace) {
this.nodeSpace = nodeSpace;
}

public int getDirection() {
Expand Down Expand Up @@ -108,7 +151,9 @@ public void setLayoutContext(LayoutContext context) {
treeObserver.stop();
}
this.context = context;
treeObserver = new TreeLayoutObserver(context, null);
if (context != null) {
treeObserver = new TreeLayoutObserver(context, null);
}
}

public void applyLayout(boolean clean) {
Expand All @@ -121,27 +166,26 @@ public void applyLayout(boolean clean) {

if (resize)
AlgorithmHelper.maximizeSizes(entities);
scaleEntities(entities);
}

DisplayIndependentRectangle bounds2 = new DisplayIndependentRectangle(
bounds);
int insets = 4;
bounds2.x += insets;
bounds2.y += insets;
bounds2.width -= 2 * insets;
bounds2.height -= 2 * insets;
AlgorithmHelper.fitWithinBounds(entities, bounds2, resize);
private void scaleEntities(EntityLayout[] entities) {
if (nodeSpace == null) {
DisplayIndependentRectangle bounds2 = new DisplayIndependentRectangle(
bounds);
int insets = 4;
bounds2.x += insets;
bounds2.y += insets;
bounds2.width -= 2 * insets;
bounds2.height -= 2 * insets;
AlgorithmHelper.fitWithinBounds(entities, bounds2, resize);
}
}

void internalApplyLayout() {
TreeNode superRoot = treeObserver.getSuperRoot();
bounds = context.getBounds();
if (direction == TOP_DOWN || direction == BOTTOM_UP) {
leafSize = bounds.width / superRoot.numOfLeaves;
layerSize = bounds.height / superRoot.height;
} else {
leafSize = bounds.height / superRoot.numOfLeaves;
layerSize = bounds.width / superRoot.height;
}
updateLeafAndLayerSizes();
int leafCountSoFar = 0;
for (Iterator iterator = superRoot.getChildren().iterator(); iterator
.hasNext();) {
Expand All @@ -151,6 +195,27 @@ void internalApplyLayout() {
}
}

private void updateLeafAndLayerSizes() {
if (nodeSpace != null) {
if (getDirection() == TOP_DOWN || getDirection() == BOTTOM_UP) {
leafSize = nodeSpace.preciseWidth();
layerSize = nodeSpace.preciseHeight();
} else {
leafSize = nodeSpace.preciseHeight();
layerSize = nodeSpace.preciseWidth();
}
} else {
TreeNode superRoot = treeObserver.getSuperRoot();
if (direction == TOP_DOWN || direction == BOTTOM_UP) {
leafSize = bounds.width / superRoot.numOfLeaves;
layerSize = bounds.height / superRoot.height;
} else {
leafSize = bounds.height / superRoot.numOfLeaves;
layerSize = bounds.width / superRoot.height;
}
}
}

/**
* Computes positions recursively until the leaf nodes are reached.
*/
Expand Down

0 comments on commit 3c1b093

Please sign in to comment.