You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by tv...@apache.org on 2009/08/05 23:30:53 UTC
svn commit: r801419 - in
/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk: Component.java
ComponentListener.java Limits.java skin/ComponentSkin.java
Author: tvolkert
Date: Wed Aug 5 21:30:50 2009
New Revision: 801419
URL: http://svn.apache.org/viewvc?rev=801419&view=rev
Log:
PIVOT-207 :: Add support for bounding preferred size
Added:
incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/Limits.java
Modified:
incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java
incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ComponentListener.java
incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/ComponentSkin.java
Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java?rev=801419&r1=801418&r2=801419&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java Wed Aug 5 21:30:50 2009
@@ -323,6 +323,22 @@
}
}
+ public void preferredWidthLimitsChanged(Component component, int previousMinPreferredWidth,
+ int previousMaxPreferredWidth) {
+ for (ComponentListener listener : this) {
+ listener.preferredWidthLimitsChanged(component, previousMinPreferredWidth,
+ previousMaxPreferredWidth);
+ }
+ }
+
+ public void preferredHeightLimitsChanged(Component component, int previousMinPreferredHeight,
+ int previousMaxPreferredHeight) {
+ for (ComponentListener listener : this) {
+ listener.preferredHeightLimitsChanged(component, previousMinPreferredHeight,
+ previousMaxPreferredHeight);
+ }
+ }
+
public void locationChanged(Component component, int previousX, int previousY) {
for (ComponentListener listener : this) {
listener.locationChanged(component, previousX, previousY);
@@ -566,6 +582,12 @@
private int preferredWidth = -1;
private int preferredHeight = -1;
+ // Bounds on preferred size
+ private int minPreferredWidth = Integer.MIN_VALUE;
+ private int maxPreferredWidth = Integer.MAX_VALUE;
+ private int minPreferredHeight = Integer.MIN_VALUE;
+ private int maxPreferredHeight = Integer.MAX_VALUE;
+
// Calculated preferred size value
private Dimensions preferredSize = null;
@@ -892,7 +914,8 @@
&& preferredSize.height == height) {
preferredWidth = preferredSize.width;
} else {
- preferredWidth = skin.getPreferredWidth(height);
+ Limits limits = new Limits(minPreferredWidth, maxPreferredWidth);
+ preferredWidth = limits.limit(skin.getPreferredWidth(height));
}
}
} else {
@@ -910,21 +933,7 @@
* value determined by the skin.
*/
public void setPreferredWidth(int preferredWidth) {
- if (preferredWidth < -1) {
- throw new IllegalArgumentException(preferredWidth
- + " is not a valid value for preferredWidth.");
- }
-
- int previousPreferredWidth = this.preferredWidth;
-
- if (previousPreferredWidth != preferredWidth) {
- this.preferredWidth = preferredWidth;
-
- invalidate();
-
- componentListeners.preferredSizeChanged(this, previousPreferredWidth,
- preferredHeight);
- }
+ setPreferredSize(preferredWidth, preferredHeight);
}
/**
@@ -967,7 +976,8 @@
&& preferredSize.width == width) {
preferredHeight = preferredSize.height;
} else {
- preferredHeight = skin.getPreferredHeight(width);
+ Limits limits = new Limits(minPreferredHeight, maxPreferredHeight);
+ preferredHeight = limits.limit(skin.getPreferredHeight(width));
}
}
} else {
@@ -985,21 +995,7 @@
* value determined by the skin.
*/
public void setPreferredHeight(int preferredHeight) {
- if (preferredHeight < -1) {
- throw new IllegalArgumentException(preferredHeight
- + " is not a valid value for preferredHeight.");
- }
-
- int previousPreferredHeight = this.preferredHeight;
-
- if (previousPreferredHeight != preferredHeight) {
- this.preferredHeight = preferredHeight;
-
- invalidate();
-
- componentListeners.preferredSizeChanged(this, preferredWidth,
- previousPreferredHeight);
- }
+ setPreferredSize(preferredWidth, preferredHeight);
}
/**
@@ -1031,6 +1027,12 @@
} else {
preferredSize = new Dimensions(preferredWidth, preferredHeight);
}
+
+ Limits preferredWidthLimits = new Limits(minPreferredWidth, maxPreferredWidth);
+ Limits preferredHeightLimits = new Limits(minPreferredHeight, maxPreferredHeight);
+
+ preferredSize = new Dimensions(preferredWidthLimits.limit(preferredSize.width),
+ preferredHeightLimits.limit(preferredSize.height));
}
return preferredSize;
@@ -1071,6 +1073,18 @@
if (previousPreferredWidth != preferredWidth
|| previousPreferredHeight != preferredHeight) {
+ if (preferredWidth != -1
+ && (preferredWidth < minPreferredWidth
+ || preferredWidth > maxPreferredWidth)) {
+ throw new IllegalArgumentException("preferredWidth is outside limits");
+ }
+
+ if (preferredHeight != -1
+ && (preferredHeight < minPreferredHeight
+ || preferredHeight > maxPreferredHeight)) {
+ throw new IllegalArgumentException("preferredHeight is outside limits");
+ }
+
this.preferredWidth = preferredWidth;
this.preferredHeight = preferredHeight;
@@ -1095,6 +1109,227 @@
}
/**
+ * Gets the minimum preferred width of this component. A component
+ * will always report a preferred size whose width is greater than or equal
+ * to its minimum preferred width.
+ *
+ * @return
+ * The minimum preferred width
+ */
+ public int getMinPreferredWidth() {
+ return minPreferredWidth;
+ }
+
+ /**
+ * Sets the minimum preferred width of this component. A component
+ * will always report a preferred size whose width is greater than or equal
+ * to its minimum preferred width.
+ *
+ * @param minPreferredWidth
+ * The minimum preferred width, or <tt>-1</tt> to specify no minimum
+ * preferred width
+ */
+ public void setMinPreferredWidth(int minPreferredWidth) {
+ setPreferredWidthLimits(minPreferredWidth, getMaxPreferredWidth());
+ }
+
+ /**
+ * Gets the maximum preferred width of this component. A component
+ * will always report a preferred size whose width is less than or equal
+ * to its maximum preferred width.
+ *
+ * @return
+ * The maximum preferred width
+ */
+ public int getMaxPreferredWidth() {
+ return maxPreferredWidth;
+ }
+
+ /**
+ * Sets the maximum preferred width of this component. A component
+ * will always report a preferred size whose width is less than or equal
+ * to its maximum preferred width.
+ *
+ * @param maxPreferredWidth
+ * The maximum preferred width
+ */
+ public void setMaxPreferredWidth(int maxPreferredWidth) {
+ setPreferredWidthLimits(getMinPreferredWidth(), maxPreferredWidth);
+ }
+
+ /**
+ * Gets the preferred width limits for this component. A component will
+ * always report a preferred size whose width is within these limits.
+ *
+ * @return
+ * The preferred width limits
+ */
+ public Limits getPreferredWidthLimits() {
+ return new Limits(minPreferredWidth, maxPreferredWidth);
+ }
+
+ /**
+ * Sets the preferred width limits for this component. A component will
+ * always report a preferred size whose width is within these limits.
+ *
+ * @param minPreferredWidth
+ * The minimum preferred width for the component
+ *
+ * @param maxPreferredWidth
+ * The maximum preferred width for the component
+ */
+ public void setPreferredWidthLimits(int minPreferredWidth, int maxPreferredWidth) {
+ int previousMinPreferredWidth = this.minPreferredWidth;
+ int previousMaxPreferredWidth = this.maxPreferredWidth;
+
+ if (previousMinPreferredWidth != minPreferredWidth
+ || previousMaxPreferredWidth != maxPreferredWidth) {
+ if (minPreferredWidth > maxPreferredWidth) {
+ throw new IllegalArgumentException("minPreferredWidth > maxPreferredWidth");
+ }
+
+ if (preferredWidth != -1
+ && (preferredWidth < minPreferredWidth
+ || preferredWidth > maxPreferredWidth)) {
+ throw new IllegalArgumentException("preferredWidth is outside limits");
+ }
+
+ this.minPreferredWidth = minPreferredWidth;
+ this.maxPreferredWidth = maxPreferredWidth;
+
+ invalidate();
+
+ componentListeners.preferredWidthLimitsChanged(this, previousMinPreferredWidth,
+ previousMaxPreferredWidth);
+ }
+ }
+
+ /**
+ * Sets the preferred width limits for this component. A component will
+ * always report a preferred size whose width is within these limits.
+ *
+ * @param preferredWidthLimits
+ * The preferred width limits for the component
+ */
+ public final void setPreferredWidthLimits(Limits preferredWidthLimits) {
+ if (preferredWidthLimits == null) {
+ throw new IllegalArgumentException("preferredWidthLimits is null.");
+ }
+
+ setPreferredWidthLimits(preferredWidthLimits.min, preferredWidthLimits.max);
+ }
+
+ /**
+ * Gets the minimum preferred height of this component. A component
+ * will always report a preferred size whose height is greater than or equal
+ * to its minimum preferred height.
+ *
+ * @return
+ * The minimum preferred height
+ */
+ public int getMinPreferredHeight() {
+ return minPreferredHeight;
+ }
+
+ /**
+ * Sets the minimum preferred height of this component. A component
+ * will always report a preferred size whose height is greater than or equal
+ * to its minimum preferred height.
+ *
+ * @param minPreferredHeight
+ * The minimum preferred height
+ */
+ public void setMinPreferredHeight(int minPreferredHeight) {
+ setPreferredHeightLimits(minPreferredHeight, getMaxPreferredHeight());
+ }
+
+ /**
+ * Gets the maximum preferred height of this component. A component
+ * will always report a preferred size whose height is less than or equal
+ * to its maximum preferred height.
+ *
+ * @return
+ * The maximum preferred height
+ */
+ public int getMaxPreferredHeight() {
+ return maxPreferredHeight;
+ }
+
+ /**
+ * Sets the maximum preferred height of this component. A component
+ * will always report a preferred size whose height is less than or equal
+ * to its maximum preferred height.
+ *
+ * @param maxPreferredHeight
+ * The maximum preferred height
+ */
+ public void setMaxPreferredHeight(int maxPreferredHeight) {
+ setPreferredHeightLimits(getMinPreferredHeight(), maxPreferredHeight);
+ }
+
+ /**
+ * Gets the preferred height limits for this component. A component will
+ * always report a preferred size whose height is within these limits.
+ *
+ * @return
+ * The preferred height limits
+ */
+ public Limits getPreferredHeightLimits() {
+ return new Limits(minPreferredHeight, maxPreferredHeight);
+ }
+
+ /**
+ * Sets the preferred height limits for this component. A component will
+ * always report a preferred size whose height is within these limits.
+ *
+ * @param minPreferredHeight
+ * The minimum preferred height for the component
+ *
+ * @param maxPreferredHeight
+ * The maximum preferred height for the component
+ */
+ public void setPreferredHeightLimits(int minPreferredHeight, int maxPreferredHeight) {
+ int previousMinPreferredHeight = this.minPreferredHeight;
+ int previousMaxPreferredHeight = this.maxPreferredHeight;
+
+ if (previousMinPreferredHeight != minPreferredHeight
+ || previousMaxPreferredHeight != maxPreferredHeight) {
+ if (minPreferredHeight > maxPreferredHeight) {
+ throw new IllegalArgumentException("minPreferredHeight > maxPreferredHeight");
+ }
+
+ if (preferredHeight != -1
+ && (preferredHeight < minPreferredHeight
+ || preferredHeight > maxPreferredHeight)) {
+ throw new IllegalArgumentException("preferredHeight is outside limits");
+ }
+
+ this.minPreferredHeight = minPreferredHeight;
+ this.maxPreferredHeight = maxPreferredHeight;
+
+ invalidate();
+
+ componentListeners.preferredHeightLimitsChanged(this, previousMinPreferredHeight,
+ previousMaxPreferredHeight);
+ }
+ }
+
+ /**
+ * Sets the preferred height limits for this component. A component will
+ * always report a preferred size whose height is within these limits.
+ *
+ * @param preferredHeightLimits
+ * The preferred height limits for the component
+ */
+ public final void setPreferredHeightLimits(Limits preferredHeightLimits) {
+ if (preferredHeightLimits == null) {
+ throw new IllegalArgumentException("preferredHeightLimits is null.");
+ }
+
+ setPreferredHeightLimits(preferredHeightLimits.min, preferredHeightLimits.max);
+ }
+
+ /**
* Returns the component's x-coordinate.
*
* @return
Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ComponentListener.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ComponentListener.java?rev=801419&r1=801418&r2=801419&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ComponentListener.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ComponentListener.java Wed Aug 5 21:30:50 2009
@@ -38,6 +38,14 @@
int previousPreferredHeight) {
}
+ public void preferredWidthLimitsChanged(Component component, int previousMinPreferredWidth,
+ int previousMaxPreferredWidth) {
+ }
+
+ public void preferredHeightLimitsChanged(Component component, int previousMinPreferredHeight,
+ int previousMaxPreferredHeight) {
+ }
+
public void locationChanged(Component component, int previousX, int previousY) {
}
@@ -95,6 +103,26 @@
int previousPreferredHeight);
/**
+ * Called when a component's preferred width limits have changed.
+ *
+ * @param component
+ * @param previousMinPreferredWidth
+ * @param previousMaxPreferredWidth
+ */
+ public void preferredWidthLimitsChanged(Component component, int previousMinPreferredWidth,
+ int previousMaxPreferredWidth);
+
+ /**
+ * Called when a component's preferred height limits have changed.
+ *
+ * @param component
+ * @param previousMinPreferredHeight
+ * @param previousMaxPreferredHeight
+ */
+ public void preferredHeightLimitsChanged(Component component, int previousMinPreferredHeight,
+ int previousMaxPreferredHeight);
+
+ /**
* Called when a component's location has changed.
*
* @param component
Added: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/Limits.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/Limits.java?rev=801419&view=auto
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/Limits.java (added)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/Limits.java Wed Aug 5 21:30:50 2009
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.wtk;
+
+import java.io.Serializable;
+
+import org.apache.pivot.collections.Dictionary;
+import org.apache.pivot.serialization.JSONSerializer;
+import org.apache.pivot.serialization.SerializationException;
+
+/**
+ * Class representing minimum and maximum values.
+ *
+ * @author tvolkert
+ */
+public final class Limits implements Serializable {
+ private static final long serialVersionUID = 0;
+
+ public final int min;
+ public final int max;
+
+ public static final String MIN_KEY = "min";
+ public static final String MAX_KEY = "max";
+
+ public Limits(int min, int max) {
+ if (min > max) {
+ throw new IllegalArgumentException("min > max");
+ }
+
+ this.min = min;
+ this.max = max;
+ }
+
+ public Limits(Limits limits) {
+ if (limits == null) {
+ throw new IllegalArgumentException("limits is null.");
+ }
+
+ min = limits.min;
+ max = limits.max;
+ }
+
+ public Limits(Dictionary<String, ?> limits) {
+ if (limits == null) {
+ throw new IllegalArgumentException("limits is null.");
+ }
+
+ if (limits.containsKey(MIN_KEY)) {
+ min = (Integer)limits.get(MIN_KEY);
+ } else {
+ min = Integer.MIN_VALUE;
+ }
+
+ if (limits.containsKey(MAX_KEY)) {
+ max = (Integer)limits.get(MAX_KEY);
+ } else {
+ max = Integer.MAX_VALUE;
+ }
+
+ if (min > max) {
+ throw new IllegalArgumentException("min > max");
+ }
+ }
+
+ /**
+ * Limits the specified value to the min and max values of this object.
+ *
+ * @param value
+ * The value to limit
+ *
+ * @return
+ * The bounded value
+ */
+ public int limit(int value) {
+ if (value < min) {
+ value = min;
+ } else if (value > max) {
+ value = max;
+ }
+
+ return value;
+ }
+
+ @Override
+ public boolean equals(Object object) {
+ boolean equals = false;
+
+ if (object instanceof Limits) {
+ Limits limits = (Limits)object;
+ equals = (min == limits.min
+ && max == limits.max);
+ }
+
+ return equals;
+ }
+
+ @Override
+ public int hashCode() {
+ // TODO This may not be the most optimal hashing function
+ return min * max;
+ }
+
+ public String toString() {
+ StringBuilder buf = new StringBuilder();
+
+ buf.append(getClass().getName());
+ buf.append(" [");
+
+ if (min == Integer.MIN_VALUE) {
+ buf.append("MIN");
+ } else {
+ buf.append(min);
+ }
+
+ buf.append("-");
+
+ if (max == Integer.MAX_VALUE) {
+ buf.append("MAX");
+ } else {
+ buf.append(max);
+ }
+
+ buf.append("]");
+
+ return buf.toString();
+ }
+
+ public static Limits decode(String value) {
+ if (value == null) {
+ throw new IllegalArgumentException();
+ }
+
+ Limits limits;
+ try {
+ limits = new Limits(JSONSerializer.parseMap(value));
+ } catch (SerializationException exception) {
+ throw new IllegalArgumentException(exception);
+ }
+
+ return limits;
+ }
+}
Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/ComponentSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/ComponentSkin.java?rev=801419&r1=801418&r2=801419&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/ComponentSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/ComponentSkin.java Wed Aug 5 21:30:50 2009
@@ -157,6 +157,16 @@
// No-op
}
+ public void preferredWidthLimitsChanged(Component component, int previousMinPreferredWidth,
+ int previousMaxPreferredWidth) {
+ // No-op
+ }
+
+ public void preferredHeightLimitsChanged(Component component, int previousMinPreferredHeight,
+ int previousMaxPreferredHeight) {
+ // No-op
+ }
+
public void locationChanged(Component component, int previousX, int previousY) {
// No-op
}