use of com.servoy.j2db.util.EnableScrollPanel in project servoy-client by Servoy.
the class DataRendererFactory method addSpringsBetweenComponents.
public static void addSpringsBetweenComponents(IApplication application, DataRenderer panel) {
Map componentsListToBeHandeldOnX = new HashMap();
Map componentsListToBeHandeldOnY = new HashMap();
Map componentsListToBeHandeldOnWidth = new HashMap();
Map componentsListToBeHandeldOnHeigth = new HashMap();
FixedSpringLayout sl = (FixedSpringLayout) panel.getLayout();
FixedSpringLayout.Constraints pcons = sl.getConstraints(panel);
Component[] allc = panel.getComponents();
for (Component comp : allc) {
if (comp instanceof EnableScrollPanel) {
((EnableScrollPanel) comp).setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
((EnableScrollPanel) comp).setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
}
int slide = ISupportPrintSliding.NO_SLIDING;
Map componentsListToBeHandeld = panel.getComponentsUsingSliding();
if (componentsListToBeHandeld != null && componentsListToBeHandeld.containsKey(comp)) {
slide = ((Integer) componentsListToBeHandeld.get(comp)).intValue();
for (Component ccomp : allc) {
if (slide != ISupportPrintSliding.NO_SLIDING && ccomp != comp && ccomp.getBounds().intersects(comp.getBounds())) {
application.reportJSError(application.getI18NMessage("servoy.sliding.error.overlap"), // $NON-NLS-1$ //$NON-NLS-2$
application.getI18NMessage("servoy.sliding.error.overlap.components", new Object[] { comp, ccomp }));
}
}
}
Spring sx = Spring.constant(comp.getX());
Spring sy = Spring.constant(comp.getY());
Spring sw = Spring.constant(comp.getWidth());
Spring sh = Spring.constant(comp.getHeight());
if (((slide & ISupportPrintSliding.ALLOW_MOVE_MIN_X) == ISupportPrintSliding.ALLOW_MOVE_MIN_X) || ((slide & ISupportPrintSliding.ALLOW_MOVE_PLUS_X) == ISupportPrintSliding.ALLOW_MOVE_PLUS_X)) {
componentsListToBeHandeldOnX.put(comp, new Integer(slide));
}
if (((slide & ISupportPrintSliding.ALLOW_MOVE_MIN_Y) == ISupportPrintSliding.ALLOW_MOVE_MIN_Y) || ((slide & ISupportPrintSliding.ALLOW_MOVE_PLUS_Y) == ISupportPrintSliding.ALLOW_MOVE_PLUS_Y)) {
componentsListToBeHandeldOnY.put(comp, new Integer(slide));
}
if (((slide & ISupportPrintSliding.GROW_WIDTH) == ISupportPrintSliding.GROW_WIDTH) || ((slide & ISupportPrintSliding.SHRINK_WIDTH) == ISupportPrintSliding.SHRINK_WIDTH)) {
// if (comp instanceof EnableScrollPanel)
// {
// ((EnableScrollPanel)comp).setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// ((EnableScrollPanel)comp).setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
// }
componentsListToBeHandeldOnWidth.put(comp, new Integer(slide));
int max = Short.MAX_VALUE;
int min = 0;
if (((slide & ISupportPrintSliding.GROW_WIDTH) != ISupportPrintSliding.GROW_WIDTH) && ((slide & ISupportPrintSliding.SHRINK_WIDTH) == ISupportPrintSliding.SHRINK_WIDTH)) {
min = 0;
max = comp.getWidth();
}
if (((slide & ISupportPrintSliding.GROW_WIDTH) == ISupportPrintSliding.GROW_WIDTH) && ((slide & ISupportPrintSliding.SHRINK_WIDTH) != ISupportPrintSliding.SHRINK_WIDTH)) {
min = comp.getWidth();
max = Short.MAX_VALUE;
}
sw = new AbsWidthSpring(comp, min, max);
}
if (((slide & ISupportPrintSliding.GROW_HEIGHT) == ISupportPrintSliding.GROW_HEIGHT) || ((slide & ISupportPrintSliding.SHRINK_HEIGHT) == ISupportPrintSliding.SHRINK_HEIGHT)) {
componentsListToBeHandeldOnHeigth.put(comp, new Integer(slide));
int max = Short.MAX_VALUE;
int min = 0;
if (((slide & ISupportPrintSliding.GROW_HEIGHT) != ISupportPrintSliding.GROW_HEIGHT) && ((slide & ISupportPrintSliding.SHRINK_HEIGHT) == ISupportPrintSliding.SHRINK_HEIGHT)) {
min = 0;
max = comp.getHeight();
}
if (((slide & ISupportPrintSliding.GROW_HEIGHT) == ISupportPrintSliding.GROW_HEIGHT) && ((slide & ISupportPrintSliding.SHRINK_HEIGHT) != ISupportPrintSliding.SHRINK_HEIGHT)) {
min = comp.getHeight();
max = Short.MAX_VALUE;
}
sh = new AbsHeightSpring(comp, min, max);
}
// new FixedSpringLayout.Constraints(sx,sy,sw,sh);
FixedSpringLayout.Constraints constr = sl.getConstraints(comp);
constr.setX(sx);
constr.setY(sy);
constr.setWidth(sw);
constr.setHeight(sh);
}
Component[] allSortedXy = new Component[allc.length];
System.arraycopy(allc, 0, allSortedXy, 0, allc.length);
Arrays.sort(allSortedXy, PositionComparator.XY_COMPONENT_COMPARATOR);
Component[] allSortedYx = new Component[allc.length];
System.arraycopy(allc, 0, allSortedYx, 0, allc.length);
Arrays.sort(allSortedYx, PositionComparator.YX_COMPONENT_COMPARATOR);
// add west component spring for sliding components that can move horizontally, to set component location
Iterator it1 = componentsListToBeHandeldOnX.keySet().iterator();
while (it1.hasNext()) {
Component element1 = (Component) it1.next();
BarSpring bs = null;
// X space left to the element1's WEST border from
int minXSpaceOnLeft = Integer.MAX_VALUE;
// components on X exactly on the left of element1
// sliding X components that are exactly on the left of element1
ArrayList slidingComponentsOnLeft = new ArrayList();
// the right most non sliding component that is on the left of element1
Component rightMostNonSlidingLastComponentOnLeft = null;
int xSpaceForRightmostNonSlidingLastComponentOnLeft = Integer.MAX_VALUE;
for (Component element2 : allSortedYx) {
boolean isExactlyOnLeftOfElement1 = true;
if (element2 == element1)
continue;
Rectangle join = createSmallestHorizontalJoin(element2.getBounds(), element1.getBounds());
if (join == null || join.isEmpty()) {
isExactlyOnLeftOfElement1 = false;
} else {
for (Component element3 : allSortedYx) {
if (element3 != element1 && element3 != element2 && join.intersects(element3.getBounds())) {
isExactlyOnLeftOfElement1 = false;
// too large, contains other eleemnts in between
break;
}
}
}
if (isExactlyOnLeftOfElement1) {
int xSpace = element1.getX() - (element2.getX() + element2.getWidth());
minXSpaceOnLeft = Math.min(minXSpaceOnLeft, xSpace);
if (componentsListToBeHandeldOnWidth.containsKey(element2) || componentsListToBeHandeldOnX.containsKey(element2)) {
slidingComponentsOnLeft.add(element2);
} else {
if (xSpaceForRightmostNonSlidingLastComponentOnLeft > xSpace) {
xSpaceForRightmostNonSlidingLastComponentOnLeft = xSpace;
rightMostNonSlidingLastComponentOnLeft = element2;
}
}
}
}
if (rightMostNonSlidingLastComponentOnLeft != null) {
bs = new BarSpring();
sl.getConstraints(element1).setConstraint(FixedSpringLayout.WEST, bs);
bs.add(Spring.sum(Spring.constant(minXSpaceOnLeft, minXSpaceOnLeft, Short.MAX_VALUE), sl.getConstraint(FixedSpringLayout.EAST, rightMostNonSlidingLastComponentOnLeft)));
} else if (slidingComponentsOnLeft.size() > 0) {
bs = new BarSpring();
sl.getConstraints(element1).setConstraint(FixedSpringLayout.WEST, bs);
}
for (int i = slidingComponentsOnLeft.size() - 1; i >= 0; i--) {
bs.add(Spring.sum(Spring.constant(minXSpaceOnLeft, minXSpaceOnLeft, Short.MAX_VALUE), sl.getConstraint(FixedSpringLayout.EAST, (Component) slidingComponentsOnLeft.get(i))));
}
}
// add north component spring for sliding components that allow vertical move, to set component location
Iterator it2 = componentsListToBeHandeldOnY.keySet().iterator();
while (it2.hasNext()) {
Component element1 = (Component) it2.next();
BarSpring bs = null;
// Y space left to the element1's NORTH border from
int minYSpaceOnTop = Integer.MAX_VALUE;
// components on Y right on top of element1
// sliding Y components that are right on top of element1
ArrayList slidingComponentsOnTop = new ArrayList();
// the lowest non sliding component that is on top of element1
Component lowestNonSlidingLastComponentOnTop = null;
int ySpaceForLowestNonSlidingLastComponentOnTop = Integer.MAX_VALUE;
for (Component element2 : allSortedXy) {
boolean isRightOnTopOfElement1 = true;
if (element2 == element1)
continue;
Rectangle join = createSmallestVerticalJoin(element2.getBounds(), element1.getBounds());
if (join == null || join.isEmpty()) {
isRightOnTopOfElement1 = false;
} else {
for (Component element3 : allSortedXy) {
if (element3 != element1 && element3 != element2 && join.intersects(element3.getBounds())) {
isRightOnTopOfElement1 = false;
// too large, contains other elements in between
break;
}
}
}
if (isRightOnTopOfElement1) {
int ySpace = element1.getY() - (element2.getY() + element2.getHeight());
minYSpaceOnTop = Math.min(minYSpaceOnTop, ySpace);
if (componentsListToBeHandeldOnHeigth.containsKey(element2) || componentsListToBeHandeldOnY.containsKey(element2)) {
slidingComponentsOnTop.add(element2);
} else {
if (ySpaceForLowestNonSlidingLastComponentOnTop > ySpace) {
ySpaceForLowestNonSlidingLastComponentOnTop = ySpace;
lowestNonSlidingLastComponentOnTop = element2;
}
}
}
}
if (lowestNonSlidingLastComponentOnTop != null) {
bs = new BarSpring();
sl.getConstraints(element1).setConstraint(FixedSpringLayout.NORTH, bs);
bs.add(Spring.sum(Spring.constant(minYSpaceOnTop, minYSpaceOnTop, Short.MAX_VALUE), sl.getConstraint(FixedSpringLayout.SOUTH, lowestNonSlidingLastComponentOnTop)));
} else if (slidingComponentsOnTop.size() > 0) {
bs = new BarSpring();
sl.getConstraints(element1).setConstraint(FixedSpringLayout.NORTH, bs);
}
for (int i = slidingComponentsOnTop.size() - 1; i >= 0; i--) {
bs.add(Spring.sum(Spring.constant(minYSpaceOnTop, minYSpaceOnTop, Short.MAX_VALUE), sl.getConstraint(FixedSpringLayout.SOUTH, (Component) slidingComponentsOnTop.get(i))));
}
}
// add east panel spring, to set Panel width
Rectangle pb = panel.getBounds();
BarSpring bs = null;
int spaceToRightMargin = 0;
if (componentsListToBeHandeldOnWidth.size() > 0) {
// width left to the part's EAST border from the right-most component on Y
int minWidthLeft = Integer.MAX_VALUE;
// sliding X components that are last on X axis
ArrayList lastSlidingComponentsOnX = new ArrayList();
// the right most non sliding component that is last on X axis
Component lowestNonSlidingLastComponentOnX = null;
int widthLeftForLowestNonSlidingLastComponentOnX = Integer.MAX_VALUE;
for (int i = allSortedXy.length - 1; i >= 0; i--) {
// are there no other components between it and the part east border?
boolean isALastComponentOnX = true;
Component element = allSortedXy[i];
Rectangle rect1 = new Rectangle(element.getBounds());
int widthLeft = pb.width - (rect1.x + rect1.width);
rect1.width += widthLeft;
for (Component element2 : allSortedXy) {
if (element2 != element && element.getBounds().intersects(element2.getBounds())) {
// overlapping elements
continue;
}
if (element2 != element && rect1.intersects(element2.getBounds())) {
isALastComponentOnX = false;
// too large, contains other elements in between
break;
}
}
if (isALastComponentOnX) {
minWidthLeft = Math.min(minWidthLeft, widthLeft);
if (componentsListToBeHandeldOnWidth.containsKey(element) || componentsListToBeHandeldOnX.containsKey(element)) {
lastSlidingComponentsOnX.add(element);
} else {
if (widthLeftForLowestNonSlidingLastComponentOnX > widthLeft) {
widthLeftForLowestNonSlidingLastComponentOnX = widthLeft;
lowestNonSlidingLastComponentOnX = element;
}
}
}
}
// add springs needed to determine the width of the part
if (lowestNonSlidingLastComponentOnX != null) {
bs = new BarSpring();
pcons.setConstraint(FixedSpringLayout.EAST, bs);
bs.add(Spring.sum(Spring.constant(minWidthLeft, minWidthLeft, Short.MAX_VALUE), sl.getConstraint(FixedSpringLayout.EAST, lowestNonSlidingLastComponentOnX)));
spaceToRightMargin = minWidthLeft;
} else if (lastSlidingComponentsOnX.size() > 0) {
bs = new BarSpring();
pcons.setConstraint(FixedSpringLayout.EAST, bs);
spaceToRightMargin = minWidthLeft;
}
for (int i = lastSlidingComponentsOnX.size() - 1; i >= 0; i--) {
bs.add(Spring.sum(Spring.constant(minWidthLeft, minWidthLeft, Short.MAX_VALUE), sl.getConstraint(FixedSpringLayout.EAST, (Component) lastSlidingComponentsOnX.get(i))));
}
panel.setSpaceToRightMargin(spaceToRightMargin);
}
if (// add fixed spring for panel size
bs == null) {
pcons.setConstraint(FixedSpringLayout.EAST, Spring.constant(panel.getWidth()));
}
// add south panel spring, to set Panel height
bs = null;
if (componentsListToBeHandeldOnHeigth.size() > 0) {
// height left to the part's SOUTH border from the lowest component on Y
int minHeightLeft = Integer.MAX_VALUE;
// sliding Y components that are last on Y axis
ArrayList lastSlidingComponentsOnY = new ArrayList();
// the lowest non sliding component that is last on Y axis
Component lowestNonSlidingLastComponentOnY = null;
int heightLeftForLowestNonSlidingLastComponentOnY = Integer.MAX_VALUE;
for (int i = allSortedYx.length - 1; i >= 0; i--) {
// are there no other components between it and the part south border?
boolean isALastComponentOnY = true;
Component element = allSortedYx[i];
Rectangle rect1 = new Rectangle(element.getBounds());
int heightLeft = pb.height - (rect1.y + rect1.height);
rect1.height += heightLeft;
for (Component element2 : allSortedYx) {
if (element2 != element && element.getBounds().intersects(element2.getBounds())) {
// overlapping elements
continue;
}
if (element2 != element && rect1.intersects(element2.getBounds())) {
isALastComponentOnY = false;
// too large, contains other elements in between
break;
}
}
if (isALastComponentOnY) {
minHeightLeft = Math.min(minHeightLeft, heightLeft);
if (componentsListToBeHandeldOnHeigth.containsKey(element) || componentsListToBeHandeldOnY.containsKey(element)) {
lastSlidingComponentsOnY.add(element);
} else {
if (heightLeftForLowestNonSlidingLastComponentOnY > heightLeft) {
heightLeftForLowestNonSlidingLastComponentOnY = heightLeft;
lowestNonSlidingLastComponentOnY = element;
}
}
}
}
// add springs needed to determine the height of the part
if (lowestNonSlidingLastComponentOnY != null) {
bs = new BarSpring();
pcons.setConstraint(FixedSpringLayout.SOUTH, bs);
bs.add(Spring.sum(Spring.constant(minHeightLeft, minHeightLeft, Short.MAX_VALUE), sl.getConstraint(FixedSpringLayout.SOUTH, lowestNonSlidingLastComponentOnY)));
} else if (lastSlidingComponentsOnY.size() > 0) {
bs = new BarSpring();
pcons.setConstraint(FixedSpringLayout.SOUTH, bs);
}
for (int i = lastSlidingComponentsOnY.size() - 1; i >= 0; i--) {
bs.add(Spring.sum(Spring.constant(minHeightLeft, minHeightLeft, Short.MAX_VALUE), sl.getConstraint(FixedSpringLayout.SOUTH, (Component) lastSlidingComponentsOnY.get(i))));
}
}
if (// add fixed spring for panel size
bs == null) {
pcons.setConstraint(FixedSpringLayout.SOUTH, Spring.constant(panel.getHeight()));
}
}
Aggregations