use of org.eclipse.gef4.zest.layouts.interfaces.EntityLayout in project netxms by netxms.
the class SparseTree method applyLayout.
/* (non-Javadoc)
* @see org.eclipse.gef4.zest.layouts.LayoutAlgorithm#applyLayout(boolean)
*/
@Override
public void applyLayout(boolean clean) {
EntityLayout[] entitiesToLayout = context.getEntities();
ArrayList<ArrayList<EntityLayout>> rows = new ArrayList<ArrayList<EntityLayout>>();
for (int i = 0; i < entitiesToLayout.length; i++) addToRow(rows, entitiesToLayout[i]);
// Sort rows by vertical position
Collections.sort(rows, new Comparator<ArrayList<EntityLayout>>() {
@Override
public int compare(ArrayList<EntityLayout> o1, ArrayList<EntityLayout> o2) {
return (int) (o1.get(0).getLocation().y - o2.get(0).getLocation().y);
}
});
// any overlapping elements to the right
for (int currRowIdx = 0; currRowIdx < rows.size(); currRowIdx++) {
final ArrayList<EntityLayout> row = rows.get(currRowIdx);
Collections.sort(row, new Comparator<EntityLayout>() {
@Override
public int compare(EntityLayout node1, EntityLayout node2) {
return (int) (node1.getLocation().x - node2.getLocation().x);
}
});
for (int currNodeIdx = 1; currNodeIdx < row.size(); currNodeIdx++) {
final EntityLayout currNode = row.get(currNodeIdx);
final EntityLayout prevNode = row.get(currNodeIdx - 1);
double currNodePos = currNode.getLocation().x;
double prevNodePos = prevNode.getLocation().x;
double minimalPos = prevNodePos + prevNode.getSize().width + MINIMAL_DISTANCE;
if (currNodePos < minimalPos) {
currNode.setLocation(minimalPos, currNode.getLocation().y);
}
}
}
// Center parent nodes relatively to child nodes
for (int currRowIdx = rows.size() - 1; currRowIdx > 0; currRowIdx--) {
final ArrayList<EntityLayout> row = rows.get(currRowIdx);
double leftmostX = row.get(0).getLocation().x;
int firstChildIdx = 0;
EntityLayout parent = getParentNode(row.get(0));
for (int currNodeIdx = 1; currNodeIdx < row.size(); currNodeIdx++) {
EntityLayout currNode = row.get(currNodeIdx);
EntityLayout nextParent = getParentNode(currNode);
if (parent != nextParent) {
if (parent != null)
centerNode(rows, currRowIdx, firstChildIdx, parent, leftmostX, row.get(currNodeIdx - 1).getLocation().x + row.get(currNodeIdx - 1).getSize().width);
parent = nextParent;
leftmostX = currNode.getLocation().x;
firstChildIdx = currNodeIdx;
}
}
if (parent != null) {
EntityLayout currNode = row.get(row.size() - 1);
centerNode(rows, currRowIdx, firstChildIdx, parent, leftmostX, currNode.getLocation().x + currNode.getSize().width);
}
}
}
use of org.eclipse.gef4.zest.layouts.interfaces.EntityLayout in project netxms by netxms.
the class SparseTree method shiftChildNodes.
/**
* Shift recursively all children of given node
*
* @param rows row list
* @param currRowIdx current row
* @param parent parent node
* @param shift shift value
*/
private void shiftChildNodes(ArrayList<ArrayList<EntityLayout>> rows, int currRowIdx, EntityLayout parent, double shift) {
if (rows.size() <= currRowIdx)
return;
final ArrayList<EntityLayout> row = rows.get(currRowIdx);
for (EntityLayout n : row) {
if (getParentNode(n) == parent) {
n.setLocation(n.getLocation().x + shift, n.getLocation().y);
shiftChildNodes(rows, currRowIdx + 1, n, shift);
}
}
}
use of org.eclipse.gef4.zest.layouts.interfaces.EntityLayout in project netxms by netxms.
the class ExpansionAlgorithm method applyLayout.
/* (non-Javadoc)
* @see org.eclipse.gef4.zest.layouts.LayoutAlgorithm#applyLayout(boolean)
*/
@Override
public void applyLayout(boolean clean) {
if (!clean)
return;
int intersections = 0;
double scaleFactorX = 1.0;
double scaleFactorY = 1.0;
// Sort elements by X coordinate
EntityLayout[] entities = context.getEntities();
Arrays.sort(entities, new Comparator<EntityLayout>() {
@Override
public int compare(EntityLayout e1, EntityLayout e2) {
return (int) Math.signum(e1.getLocation().x - e2.getLocation().x);
}
});
// Scan elements from left to right
final Set<EntityLayout> currentElements = new HashSet<EntityLayout>();
for (EntityLayout e : entities) {
DisplayIndependentPoint currLoc = e.getLocation();
// current sweep line location
final double currX = currLoc.x - e.getSize().width / 2;
// remove elements passed by sweep line
// and check remaining for intersection with current element
Iterator<EntityLayout> it = currentElements.iterator();
while (it.hasNext()) {
EntityLayout l = it.next();
if (l.getLocation().x + l.getSize().width / 2 < currX) {
it.remove();
} else {
double distanceY = Math.abs(currLoc.y - l.getLocation().y);
if (distanceY < 1.0)
distanceY = 1.0;
double overlayY = (e.getSize().height / 2 + l.getSize().height / 2) - distanceY;
if (overlayY >= 0) {
// elements intersects
intersections++;
double distanceX = Math.abs(currLoc.x - l.getLocation().x);
double overlayX = (e.getSize().width / 2 + l.getSize().width / 2) - distanceX;
if (overlayX < overlayY) {
double psf = (distanceX + overlayX) / distanceX;
if (psf > scaleFactorX) {
scaleFactorX = psf;
}
} else {
double psf = (distanceY + overlayY) / distanceY;
if (psf > scaleFactorY) {
scaleFactorY = psf;
}
}
}
}
if ((scaleFactorX >= 8.0) && (scaleFactorY >= 8.0))
break;
}
if ((scaleFactorX >= 8.0) && (scaleFactorY >= 8.0))
break;
currentElements.add(e);
}
if (intersections > 0) {
if (scaleFactorX > 8.0)
scaleFactorX = 8.0;
if (scaleFactorY > 8.0)
scaleFactorY = 8.0;
for (EntityLayout e : entities) {
DisplayIndependentPoint p = e.getLocation();
e.setLocation(p.x * scaleFactorX, p.y * scaleFactorY);
}
}
}
use of org.eclipse.gef4.zest.layouts.interfaces.EntityLayout in project netxms by netxms.
the class ManualLayout method applyLayout.
/* (non-Javadoc)
* @see org.eclipse.gef4.zest.layouts.LayoutAlgorithm#applyLayout(boolean)
*/
@Override
public void applyLayout(boolean clean) {
EntityLayout[] entitiesToLayout = context.getEntities();
for (int i = 0; i < entitiesToLayout.length; i++) {
Item[] items = entitiesToLayout[i].getItems();
if ((items.length > 0) && (items[0].getData() instanceof NetworkMapElement)) {
NetworkMapElement mapObject = (NetworkMapElement) items[0].getData();
entitiesToLayout[i].setLocation(mapObject.getX(), mapObject.getY());
}
}
}
use of org.eclipse.gef4.zest.layouts.interfaces.EntityLayout in project netxms by netxms.
the class SparseTree method centerNode.
/**
* Center parent node relatively to child nodes
*
* @param rows row list
* @param currRowIdx current row (contains child nodes)
* @param firstChildIdx index of first child node
* @param node current node
* @param leftmostX position of leftmost child node
* @param rightmostX position of rightmost child node + it's length
*/
private void centerNode(ArrayList<ArrayList<EntityLayout>> rows, int currRowIdx, int firstChildIdx, EntityLayout node, double leftmostX, double rightmostX) {
double newX = leftmostX + (rightmostX - leftmostX) / 2 - node.getSize().width / 2;
double shift = newX - node.getLocation().x;
if (shift > 0) {
final ArrayList<EntityLayout> row = rows.get(currRowIdx - 1);
node.setLocation(newX, node.getLocation().y);
int index = row.indexOf(node);
if (index != -1) {
for (int i = index + 1; i < row.size(); i++) {
EntityLayout n = row.get(i);
n.setLocation(n.getLocation().x + shift, n.getLocation().y);
}
}
} else {
// Child nodes are to the left of parent, shift them right
final ArrayList<EntityLayout> row = rows.get(currRowIdx);
for (int i = firstChildIdx; i < row.size(); i++) {
EntityLayout n = row.get(i);
n.setLocation(n.getLocation().x - shift, n.getLocation().y);
shiftChildNodes(rows, currRowIdx + 1, n, -shift);
}
}
}
Aggregations