summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTapio Aali <tapio@vaadin.com>2012-05-30 10:02:29 +0000
committerTapio Aali <tapio@vaadin.com>2012-05-30 10:02:29 +0000
commite3c2e2efbed6452a365483aadfc147c4ae6ffdad (patch)
treec26a4eb69f694d099f005913e3aab0ddcc8ca1db /src
parent616387587d1377d0088a87d6831d984667f8eb3a (diff)
downloadvaadin-framework-e3c2e2efbed6452a365483aadfc147c4ae6ffdad.tar.gz
vaadin-framework-e3c2e2efbed6452a365483aadfc147c4ae6ffdad.zip
Added options for setting maximum and minimum split positions to SplitPanel (#1744).
svn changeset:23850/svn branch:6.8
Diffstat (limited to 'src')
-rw-r--r--src/com/vaadin/terminal/gwt/client/ui/VSplitPanel.java119
-rw-r--r--src/com/vaadin/ui/AbstractSplitPanel.java110
2 files changed, 208 insertions, 21 deletions
diff --git a/src/com/vaadin/terminal/gwt/client/ui/VSplitPanel.java b/src/com/vaadin/terminal/gwt/client/ui/VSplitPanel.java
index 5a996954a0..4cb183917f 100644
--- a/src/com/vaadin/terminal/gwt/client/ui/VSplitPanel.java
+++ b/src/com/vaadin/terminal/gwt/client/ui/VSplitPanel.java
@@ -147,6 +147,10 @@ public class VSplitPanel extends ComplexPanel implements Container,
/* The current position of the split handle in either percentages or pixels */
private String position;
+ private String maximumPosition;
+
+ private String minimumPosition;
+
private final TouchScrollHandler touchScrollHandler;
protected Element scrolledContainer;
@@ -272,6 +276,10 @@ public class VSplitPanel extends ComplexPanel implements Container,
touchScrollHandler.setElements(firstContainer, secondContainer);
position = uidl.getStringAttribute("position");
+
+ minimumPosition = uidl.getStringAttribute("minimumPosition");
+ maximumPosition = uidl.getStringAttribute("maximumPosition");
+
setSplitPosition(position);
final Paintable newFirstChild = client.getPaintable(uidl
@@ -346,11 +354,100 @@ public class VSplitPanel extends ComplexPanel implements Container,
}
}
+ /**
+ * Converts given split position string (in pixels or percentage) to a
+ * floating point pixel value.
+ *
+ * @param pos
+ * @return
+ */
+ private float convertToPixels(String pos) {
+ float posAsFloat;
+ if (pos.indexOf("%") > 0) {
+ posAsFloat = Math.round(Float.parseFloat(pos.substring(0,
+ pos.length() - 1))
+ / 100
+ * (orientation == ORIENTATION_HORIZONTAL ? getOffsetWidth()
+ : getOffsetHeight()));
+ } else {
+ posAsFloat = Float.parseFloat(pos.substring(0, pos.length() - 2));
+ }
+ return posAsFloat;
+ }
+
+ /**
+ * Converts given split position string (in pixels or percentage) to a float
+ * percentage value.
+ *
+ * @param pos
+ * @return
+ */
+ private float convertToPercentage(String pos) {
+ float posAsFloat = 0;
+
+ if (pos.indexOf("px") > 0) {
+ int posAsInt = Integer.parseInt(pos.substring(0, pos.length() - 2));
+ int offsetLength = orientation == ORIENTATION_HORIZONTAL ? getOffsetWidth()
+ : getOffsetHeight();
+
+ // 100% needs special handling
+ if (posAsInt + getSplitterSize() >= offsetLength) {
+ posAsInt = offsetLength;
+ }
+ // Reversed position
+ if (positionReversed) {
+ posAsInt = offsetLength - posAsInt - getSplitterSize();
+ }
+ posAsFloat = ((float) posAsInt / (float) getOffsetWidth() * 100);
+
+ } else {
+ posAsFloat = Float.parseFloat(pos.substring(0, pos.length() - 1));
+ }
+ return posAsFloat;
+ }
+
+ private String checkSplitPositionLimits(String pos) {
+ float positionAsFloat = convertToPixels(pos);
+ float maximumAsFloat = convertToPixels(maximumPosition);
+ float minimumAsFloat = convertToPixels(minimumPosition);
+
+ if (maximumAsFloat < positionAsFloat) {
+ pos = maximumPosition;
+ } else if (minimumAsFloat > positionAsFloat) {
+ pos = minimumPosition;
+ }
+ return pos;
+ }
+
+ /**
+ * Converts given string to the same units as the split position is.
+ *
+ * @param pos
+ * position to be converted
+ * @return converted position string
+ */
+ private String convertToPositionUnits(String pos) {
+ if (position.indexOf("%") != -1 && pos.indexOf("%") == -1) {
+ // position is in percentage, pos in pixels
+ pos = convertToPercentage(pos) + "%";
+ } else if (position.indexOf("px") > 0 && pos.indexOf("px") == -1) {
+ // position is in pixels and pos in percentage
+ pos = convertToPixels(pos) + "px";
+ }
+
+ return pos;
+ }
+
private void setSplitPosition(String pos) {
if (pos == null) {
return;
}
+ pos = checkSplitPositionLimits(pos);
+ if (!pos.equals(position)) {
+ position = convertToPositionUnits(pos);
+ }
+
// Convert percentage values to pixels
if (pos.indexOf("%") > 0) {
pos = Float.parseFloat(pos.substring(0, pos.length() - 1))
@@ -564,16 +661,7 @@ public class VSplitPanel extends ComplexPanel implements Container,
}
if (position.indexOf("%") > 0) {
- float pos = newX;
- // 100% needs special handling
- if (newX + getSplitterSize() >= getOffsetWidth()) {
- pos = getOffsetWidth();
- }
- // Reversed position
- if (positionReversed) {
- pos = getOffsetWidth() - pos - getSplitterSize();
- }
- position = (pos / getOffsetWidth() * 100) + "%";
+ position = convertToPositionUnits(newX + "px");
} else {
// Reversed position
if (positionReversed) {
@@ -606,16 +694,7 @@ public class VSplitPanel extends ComplexPanel implements Container,
}
if (position.indexOf("%") > 0) {
- float pos = newY;
- // 100% needs special handling
- if (newY + getSplitterSize() >= getOffsetHeight()) {
- pos = getOffsetHeight();
- }
- // Reversed position
- if (positionReversed) {
- pos = getOffsetHeight() - pos - getSplitterSize();
- }
- position = pos / getOffsetHeight() * 100 + "%";
+ position = convertToPositionUnits(newY + "px");
} else {
// Reversed position
if (positionReversed) {
diff --git a/src/com/vaadin/ui/AbstractSplitPanel.java b/src/com/vaadin/ui/AbstractSplitPanel.java
index b507b88478..ebdee17de2 100644
--- a/src/com/vaadin/ui/AbstractSplitPanel.java
+++ b/src/com/vaadin/ui/AbstractSplitPanel.java
@@ -22,7 +22,7 @@ import com.vaadin.tools.ReflectTools;
* AbstractSplitPanel.
*
* <code>AbstractSplitPanel</code> is base class for a component container that
- * can contain two components. The comopnents are split by a divider element.
+ * can contain two components. The components are split by a divider element.
*
* @author Vaadin Ltd.
* @version
@@ -41,6 +41,14 @@ public abstract class AbstractSplitPanel extends AbstractLayout {
private boolean posReversed = false;
+ private int posMin = 0;
+
+ private int posMinUnit = UNITS_PERCENTAGE;
+
+ private int posMax = 100;
+
+ private int posMaxUnit = UNITS_PERCENTAGE;
+
private boolean locked = false;
private static final String SPLITTER_CLICK_EVENT = VSplitPanel.SPLITTER_CLICK_EVENT_IDENTIFIER;
@@ -210,8 +218,12 @@ public abstract class AbstractSplitPanel extends AbstractLayout {
super.paintContent(target);
final String position = pos + UNIT_SYMBOLS[posUnit];
+ final String minimumPosition = posMin + UNIT_SYMBOLS[posMinUnit];
+ final String maximumPosition = posMax + UNIT_SYMBOLS[posMaxUnit];
target.addAttribute("position", position);
+ target.addAttribute("minimumPosition", minimumPosition);
+ target.addAttribute("maximumPosition", maximumPosition);
if (isLocked()) {
target.addAttribute("locked", true);
@@ -323,6 +335,102 @@ public abstract class AbstractSplitPanel extends AbstractLayout {
}
/**
+ * Sets the minimum split position to the given position and unit. If the
+ * split position is reversed, maximum and minimum are also reversed.
+ *
+ * @param pos
+ * the minimum position of the split
+ * @param unit
+ * the unit (from {@link Sizeable}) in which the size is given.
+ * Allowed units are UNITS_PERCENTAGE and UNITS_PIXELS
+ */
+ public void setMinimumSplitPosition(int pos, int unit) {
+ setSplitPositionLimits(pos, unit, posMax, posMaxUnit);
+ }
+
+ /**
+ * Returns the current minimum position of the splitter, in
+ * {@link #getMinSplitPositionUnit()} units.
+ *
+ * @return the minimum position of the splitter
+ */
+ public int getMinSplitPosition() {
+ return posMin;
+ }
+
+ /**
+ * Returns the unit of the minimum position of the splitter.
+ *
+ * @return the unit of the minimum position of the splitter
+ */
+ public int getMinSplitPositionUnit() {
+ return posMinUnit;
+ }
+
+ /**
+ * Sets the maximum split position to the given position and unit. If the
+ * split position is reversed, maximum and minimum are also reversed.
+ *
+ * @param pos
+ * the maximum position of the split
+ * @param unit
+ * the unit (from {@link Sizeable}) in which the size is given.
+ * Allowed units are UNITS_PERCENTAGE and UNITS_PIXELS
+ */
+ public void setMaxSplitPosition(int pos, int unit) {
+ setSplitPositionLimits(posMin, posMinUnit, pos, unit);
+ }
+
+ /**
+ * Returns the current maximum position of the splitter, in
+ * {@link #getMaxSplitPositionUnit()} units.
+ *
+ * @return the maximum position of the splitter
+ */
+ public int getMaxSplitPosition() {
+ return posMax;
+ }
+
+ /**
+ * Returns the unit of the maximum position of the splitter
+ *
+ * @return the unit of the maximum position of the splitter
+ */
+ public int getMaxSplitPositionUnit() {
+ return posMaxUnit;
+ }
+
+ /**
+ * Sets the maximum and minimum position of the splitter. If the split
+ * position is reversed, maximum and minimum are also reversed.
+ *
+ * @param minPos
+ * the new minimum position
+ * @param minPosUnit
+ * the unit (from {@link Sizeable}) in which the minimum position
+ * is given.
+ * @param maxPos
+ * the new maximum position
+ * @param maxPosUnit
+ * the unit (from {@link Sizeable}) in which the maximum position
+ * is given.
+ */
+ private void setSplitPositionLimits(int minPos, int minPosUnit, int maxPos,
+ int maxPosUnit) {
+ if ((minPosUnit != UNITS_PERCENTAGE && minPosUnit != UNITS_PIXELS)
+ || (maxPosUnit != UNITS_PERCENTAGE && maxPosUnit != UNITS_PIXELS)) {
+ throw new IllegalArgumentException(
+ "Only percentage and pixel units are allowed");
+ }
+
+ posMin = minPos;
+ posMinUnit = minPosUnit;
+ posMax = maxPos;
+ posMaxUnit = maxPosUnit;
+ requestRepaint();
+ }
+
+ /**
* Moves the position of the splitter.
*
* @param pos