You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/04/14 14:31:21 UTC
svn commit: r764758 - in /incubator/pivot/trunk/wtk/src/pivot/wtk:
Component.java skin/FlowPaneSkin.java
Author: gbrown
Date: Tue Apr 14 12:31:21 2009
New Revision: 764758
URL: http://svn.apache.org/viewvc?rev=764758&view=rev
Log:
Add source code comments to Component; implement getPreferredSize() locally in FlowPaneSkin rather than delegating to getPreferredWidth() and getPreferredHeight().
Modified:
incubator/pivot/trunk/wtk/src/pivot/wtk/Component.java
incubator/pivot/trunk/wtk/src/pivot/wtk/skin/FlowPaneSkin.java
Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/Component.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/Component.java?rev=764758&r1=764757&r2=764758&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/Component.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/Component.java Tue Apr 14 12:31:21 2009
@@ -447,109 +447,68 @@
}
}
- /**
- * The component's handle.
- */
+ // The component's handle
private final Integer handle;
- /**
- * The currently installed skin, or null if no skin is installed.
- */
+ // The currently installed skin, or null if no skin is installed
private pivot.wtk.Skin skin = null;
- /**
- * The component's explicitly-set preferred width and height.
- */
+ // Preferred width and height values explicitly set by the user
private int preferredWidth = -1;
private int preferredHeight = -1;
- /**
- * The preferred size reported by the skin.
- */
+ // Calculated preferred size value
private Dimensions preferredSize = null;
- /**
- * The component's parent container, or null if the component does not have
- * a parent.
- */
+ // The component's parent container, or null if the component does not have
+ // a parent
private Container parent = null;
- /**
- * The component's location. These coordinates are relative to the origin of
- * the component's parent.
- */
+ // The component's location, relative to the parent's origin
private int x = 0;
private int y = 0;
- /**
- * The component's visible flag.
- */
+ // The component's visible flag
private boolean visible = true;
- /**
- * The component's displayable flag.
- */
+ // The component's displayable flag
private boolean displayable = true;
- /**
- * The component's decorators.
- */
+ // The component's decorators
private ArrayList<Decorator> decorators = new ArrayList<Decorator>();
private DecoratorSequence decoratorSequence = new DecoratorSequence();
- /**
- * The component's enabled flag.
- */
+ // The component's enabled flag
private boolean enabled = true;
- /**
- * The component's mouse-over flag.
- */
+ // The component's mouse-over flag
private boolean mouseOver = false;
- /**
- * The cursor that is displayed over the component.
- */
+ // The cursor that is displayed over the component
private Cursor cursor = null;
- /**
- * The tooltip text.
- */
+ // The tooltip text
private String tooltipText = null;
- /**
- * The component's drag source.
- */
+ // The component's drag source
private DragSource dragSource = null;
- /**
- * The component's drop target.
- */
+ // The component's drop target
private DropTarget dropTarget = null;
- /**
- * User data.
- */
+ // User data
private Object userData = null;
- /**
- * Proxy class for getting/setting style properties on the skin.
- */
+ // Proxy class for getting/setting style properties on the skin
private StyleDictionary styleDictionary = null;
- /**
- * Custom style keys.
- */
+ // Custom style keys
private HashSet<String> customStyles = new HashSet<String>();
- /**
- * Attached properties.
- */
+ // Attached properties
private Attributes attributes = null;
- /**
- * Instance event listener lists.
- */
+ // Event listener lists.
private ComponentListenerList componentListeners = new ComponentListenerList();
private ComponentLayoutListenerList componentLayoutListeners = new ComponentLayoutListenerList();
private ComponentStateListenerList componentStateListeners = new ComponentStateListenerList();
@@ -561,25 +520,17 @@
private ComponentDragDropListenerList componentDragDropListeners = new ComponentDragDropListenerList();
private ComponentDataListenerList componentDataListeners = new ComponentDataListenerList();
- /**
- * The component that currently has the focus.
- */
+ // The component that currently has the focus
private static Component focusedComponent = null;
- /**
- * The next available component handle.
- */
+ // The next available component handle
private static int nextHandle = 0;
- /**
- * Static map of all components by handle.
- */
+ // Map of all components by handle
private static HashMap<Integer, Component> components = new HashMap<Integer, Component>(true);
private static ComponentDictionary componentDictionary = new ComponentDictionary();
- /**
- * Class event listener list.
- */
+ // Class event listeners
private static ComponentClassListenerList componentClassListeners = new ComponentClassListenerList();
/**
@@ -801,10 +752,23 @@
}
}
+ /**
+ * Returns the component's unconstrained preferred width.
+ */
public int getPreferredWidth() {
return getPreferredWidth(-1);
}
+ /**
+ * Returns the component's constrained preferred width.
+ *
+ * @param height
+ * The height value by which the preferred width should be constrained, or
+ * <tt>-1</tt> for no constraint.
+ *
+ * @return
+ * The constrained preferred width.
+ */
public int getPreferredWidth(int height) {
int preferredWidth;
@@ -858,10 +822,23 @@
return (preferredWidth != -1);
}
+ /**
+ * Returns the component's unconstrained preferred height.
+ */
public int getPreferredHeight() {
return getPreferredHeight(-1);
}
+ /**
+ * Returns the component's constrained preferred height.
+ *
+ * @param height
+ * The width value by which the preferred height should be constrained, or
+ * <tt>-1</tt> for no constraint.
+ *
+ * @return
+ * The constrained preferred height.
+ */
public int getPreferredHeight(int width) {
int preferredHeight;
Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/skin/FlowPaneSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/skin/FlowPaneSkin.java?rev=764758&r1=764757&r2=764758&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/skin/FlowPaneSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/skin/FlowPaneSkin.java Tue Apr 14 12:31:21 2009
@@ -28,6 +28,9 @@
/**
* Flow pane skin.
+ * <p>
+ * TODO Cache preferred component sizes when alignment is justified, so we
+ * don't need to recalculate them during layout.
*
* @author gbrown
*/
@@ -219,8 +222,66 @@
}
public Dimensions getPreferredSize() {
- // TODO Optimize by performing calculations here?
- return new Dimensions(getPreferredWidth(-1), getPreferredHeight(-1));
+ int preferredWidth = 0;
+ int preferredHeight = 0;
+
+ FlowPane flowPane = (FlowPane)getComponent();
+
+ switch (flowPane.getOrientation()) {
+ case HORIZONTAL: {
+ // Preferred width is the sum of the preferred widths of all
+ // components, plus spacing and padding
+ int displayableComponentCount = 0;
+
+ for (int i = 0, n = flowPane.getLength(); i < n; i++) {
+ Component component = flowPane.get(i);
+
+ if (component.isDisplayable()) {
+ Dimensions preferredSize = component.getPreferredSize();
+ preferredWidth += preferredSize.width;
+ preferredHeight = Math.max(preferredSize.height, preferredHeight);
+ displayableComponentCount++;
+ }
+ }
+
+ // Include spacing
+ if (displayableComponentCount > 1) {
+ preferredWidth += spacing * (displayableComponentCount - 1);
+ }
+
+ break;
+ }
+
+ case VERTICAL: {
+ // Preferred height is the sum of the preferred heights of all
+ // components, plus spacing and padding
+ int displayableComponentCount = 0;
+
+ for (int i = 0, n = flowPane.getLength(); i < n; i++) {
+ Component component = flowPane.get(i);
+
+ if (component.isDisplayable()) {
+ Dimensions preferredSize = component.getPreferredSize();
+ preferredWidth = Math.max(preferredSize.width, preferredWidth);
+ preferredHeight += preferredSize.height;
+ displayableComponentCount++;
+ }
+ }
+
+ // Include spacing
+ if (displayableComponentCount > 1) {
+ preferredHeight += spacing * (displayableComponentCount - 1);
+ }
+
+ break;
+ }
+ }
+
+ // Include padding
+ preferredWidth += padding.left + padding.right;
+ preferredHeight += padding.top + padding.bottom;
+
+ return new Dimensions(preferredWidth, preferredHeight);
}
public void layout() {