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 2010/02/04 21:50:43 UTC

svn commit: r906647 - in /pivot/trunk/wtk/src/org/apache/pivot/wtk: Accordion.java Component.java Form.java TabPane.java TablePane.java

Author: tvolkert
Date: Thu Feb  4 20:50:43 2010
New Revision: 906647

URL: http://svn.apache.org/viewvc?rev=906647&view=rev
Log:
PIVOT-404 - update attribute methods

Modified:
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Accordion.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Form.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/TabPane.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/TablePane.java

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Accordion.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Accordion.java?rev=906647&r1=906646&r2=906647&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Accordion.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Accordion.java Thu Feb  4 20:50:43 2010
@@ -61,9 +61,6 @@
             Accordion.this.add(panel);
             panels.insert(panel, index);
 
-            // Attach the attributes
-            panel.setAttributes(new Attributes());
-
             // Update the selection
             if (selectedIndex >= index) {
                 selectedIndex++;
@@ -92,11 +89,6 @@
             // Remove the panels from the panel list
             Sequence<Component> removed = panels.remove(index, count);
 
-            // Detach the attributes
-            for (int i = 0, n = removed.getLength(); i < n; i++) {
-                removed.get(i).setAttributes(null);
-            }
-
             // Update the selection
             if (selectedIndex >= index) {
                 if (selectedIndex < index + count) {
@@ -138,9 +130,9 @@
         }
     }
 
-    private static class Attributes {
-        public String label = null;
-        public Image icon = null;
+    private enum Attribute {
+        LABEL,
+        ICON;
     }
 
     private static class AccordionListenerList extends ListenerList<AccordionListener>
@@ -277,45 +269,37 @@
     }
 
     public static String getLabel(Component component) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        return (attributes == null) ? null : attributes.label;
+        return (String)component.getAttribute(Attribute.LABEL);
     }
 
     public static void setLabel(Component component, String label) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        if (attributes == null) {
-            throw new IllegalStateException();
-        }
+        String previousLabel = (String)component.setAttribute(Attribute.LABEL, label);
 
-        String previousLabel = attributes.label;
         if (previousLabel != label) {
-            attributes.label = label;
+            Container parent = component.getParent();
 
-            Accordion accordion = (Accordion)component.getParent();
-            if (accordion != null) {
-                accordion.accordionAttributeListeners.labelChanged(accordion, component, previousLabel);
+            if (parent instanceof Accordion) {
+                Accordion accordion = (Accordion)parent;
+                accordion.accordionAttributeListeners.labelChanged(accordion, component,
+                    previousLabel);
             }
         }
     }
 
     public static Image getIcon(Component component) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        return (attributes == null) ? null : attributes.icon;
+        return (Image)component.getAttribute(Attribute.ICON);
     }
 
     public static void setIcon(Component component, Image icon) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        if (attributes == null) {
-            throw new IllegalStateException();
-        }
+        Image previousIcon = (Image)component.setAttribute(Attribute.ICON, icon);
 
-        Image previousIcon = attributes.icon;
         if (previousIcon != icon) {
-            attributes.icon = icon;
+            Container parent = component.getParent();
 
-            Accordion accordion = (Accordion)component.getParent();
-            if (accordion != null) {
-                accordion.accordionAttributeListeners.iconChanged(accordion, component, previousIcon);
+            if (parent instanceof Accordion) {
+                Accordion accordion = (Accordion)parent;
+                accordion.accordionAttributeListeners.iconChanged(accordion, component,
+                    previousIcon);
             }
         }
     }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java?rev=906647&r1=906646&r2=906647&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java Thu Feb  4 20:50:43 2010
@@ -631,7 +631,7 @@
     private UserDataDictionary userDataDictionary = new UserDataDictionary();
 
     // Container attributes
-    private Object attributes = null;
+    private HashMap<? extends Enum<?>, Object> attributes = null;
 
     // The component's automation ID
     private String automationID;
@@ -2523,23 +2523,57 @@
     }
 
     /**
-     * Returns the component's attributes.
+     * Gets the specified component attribute. While attributes can be used to
+     * store arbitrary data, they are intended to be used by containers to store
+     * layout-related metadata in their child components.
+     *
+     * @param key
+     * The attribute key
      *
      * @return
-     * The component's attributes, or <tt>null</tt> if no attributes are
-     * installed.
+     * The attribute value, or <tt>null</tt> if no such attribute exists
      */
-    protected Object getAttributes() {
-        return attributes;
+    @SuppressWarnings("unchecked")
+    public <T extends Enum<T>> Object getAttribute(T key) {
+        Object attribute = null;
+
+        if (attributes != null) {
+            attribute = ((HashMap<T, Object>)attributes).get(key);
+        }
+
+        return attribute;
     }
 
     /**
-     * Sets the component's attributes.
+     * Sets the specified component attribute. While attributes can be used to
+     * store arbitrary data, they are intended to be used by containers to store
+     * layout-related metadata in their child components.
+     *
+     * @param key
+     * The attribute key
      *
-     * @param attributes
+     * @param value
+     * The attribute value, or <tt>null</tt> to clear the attribute
+     *
+     * @return
+     * The previous value of the attribute, or <tt>null</tt> if the attribute
+     * was unset
      */
-    protected void setAttributes(Object attributes) {
-        this.attributes = attributes;
+    @SuppressWarnings("unchecked")
+    public <T extends Enum<T>> Object setAttribute(T key, Object value) {
+        if (attributes == null) {
+            attributes = new HashMap<T, Object>();
+        }
+
+        Object previousValue;
+
+        if (value != null) {
+            previousValue = ((HashMap<T, Object>)attributes).put(key, value);
+        } else {
+            previousValue = ((HashMap<T, Object>)attributes).remove(key);
+        }
+
+        return previousValue;
     }
 
     /**

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Form.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Form.java?rev=906647&r1=906646&r2=906647&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Form.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Form.java Thu Feb  4 20:50:43 2010
@@ -85,7 +85,7 @@
             }
 
             fields.insert(field, index);
-            field.setAttributes(new Attributes(this));
+            field.setAttribute(Attribute.SECTION, this);
 
             if (form != null) {
                 form.add(field);
@@ -114,7 +114,7 @@
 
             for (int i = 0, n = removed.getLength(); i < n; i++) {
                 Component field = removed.get(i);
-                field.setAttributes(null);
+                field.setAttribute(Attribute.SECTION, null);
 
                 if (form != null) {
                     form.remove(field);
@@ -322,15 +322,11 @@
         }
     }
 
-    private static class Attributes {
-        public final Section section;
-        public String label = null;
-        public boolean required = false;
-        public Flag flag = null;
-
-        public Attributes(Section section) {
-            this.section = section;
-        }
+    private enum Attribute {
+        SECTION,
+        LABEL,
+        REQUIRED,
+        FLAG;
     }
 
     private static class FormListenerList extends ListenerList<FormListener>
@@ -482,70 +478,57 @@
     }
 
     public static Section getSection(Component component) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        return (attributes == null) ? null : attributes.section;
+        return (Section)component.getAttribute(Attribute.SECTION);
     }
 
     public static String getLabel(Component component) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        return (attributes == null) ? null : attributes.label;
+        return (String)component.getAttribute(Attribute.LABEL);
     }
 
     public static void setLabel(Component component, String label) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        if (attributes == null) {
-            throw new IllegalStateException();
-        }
+        String previousLabel = (String)component.setAttribute(Attribute.LABEL, label);
 
-        String previousLabel = attributes.label;
         if (previousLabel != label) {
-            attributes.label = label;
+            Container parent = component.getParent();
 
-            Form form = (Form)component.getParent();
-            if (form != null) {
+            if (parent instanceof Form) {
+                Form form = (Form)parent;
                 form.formAttributeListeners.labelChanged(form, component, previousLabel);
             }
         }
     }
 
     public static boolean isRequired(Component component) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        return (attributes == null) ? false : attributes.required;
+        Boolean value = (Boolean)component.getAttribute(Attribute.REQUIRED);
+        return (value == null) ? false : value;
     }
 
     public static void setRequired(Component component, boolean required) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        if (attributes == null) {
-            throw new IllegalStateException();
-        }
+        Boolean previousValue = (Boolean)component.setAttribute(Attribute.REQUIRED, required);
+        boolean previousRequired = (previousValue == null) ? false : previousValue;
 
-        if (attributes.required != required) {
-            attributes.required = required;
+        if (previousRequired != required) {
+            Container parent = component.getParent();
 
-            Form form = (Form)component.getParent();
-            if (form != null) {
+            if (parent instanceof Form) {
+                Form form = (Form)parent;
                 form.formAttributeListeners.requiredChanged(form, component);
             }
         }
     }
 
     public static Flag getFlag(Component component) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        return (attributes == null) ? null : attributes.flag;
+        return (Flag)component.getAttribute(Attribute.FLAG);
     }
 
     public static void setFlag(Component component, Flag flag) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        if (attributes == null) {
-            throw new IllegalStateException();
-        }
+        Flag previousFlag = (Flag)component.setAttribute(Attribute.FLAG, flag);
 
-        Flag previousFlag = attributes.flag;
         if (previousFlag != flag) {
-            attributes.flag = flag;
+            Container parent = component.getParent();
 
-            Form form = (Form)component.getParent();
-            if (form != null) {
+            if (parent instanceof Form) {
+                Form form = (Form)parent;
                 form.formAttributeListeners.flagChanged(form, component, previousFlag);
             }
         }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/TabPane.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/TabPane.java?rev=906647&r1=906646&r2=906647&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/TabPane.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TabPane.java Thu Feb  4 20:50:43 2010
@@ -61,9 +61,6 @@
             TabPane.this.add(tab);
             tabs.insert(tab, index);
 
-            // Attach the attributes
-            tab.setAttributes(new Attributes());
-
             // Update the selection
             if (selectedIndex >= index) {
                 selectedIndex++;
@@ -92,11 +89,6 @@
             // Remove the tabs from the tab list
             Sequence<Component> removed = tabs.remove(index, count);
 
-            // Detach the attributes
-            for (int i = 0, n = removed.getLength(); i < n; i++) {
-                removed.get(i).setAttributes(null);
-            }
-
             // Update the selection
             if (selectedIndex >= index) {
                 if (selectedIndex < index + count) {
@@ -138,10 +130,10 @@
         }
     }
 
-    private static class Attributes {
-        public String label = null;
-        public Image icon = null;
-        public boolean closeable = false;
+    private enum Attribute {
+        LABEL,
+        ICON,
+        CLOSEABLE;
     }
 
     private static class TabPaneListenerList extends ListenerList<TabPaneListener>
@@ -322,44 +314,34 @@
     }
 
     public static String getLabel(Component component) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        return (attributes == null) ? null : attributes.label;
+        return (String)component.getAttribute(Attribute.LABEL);
     }
 
     public static void setLabel(Component component, String label) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        if (attributes == null) {
-            throw new IllegalStateException();
-        }
+        String previousLabel = (String)component.setAttribute(Attribute.LABEL, label);
 
-        String previousLabel = attributes.label;
         if (previousLabel != label) {
-            attributes.label = label;
+            Container parent = component.getParent();
 
-            TabPane tabPane = (TabPane)component.getParent();
-            if (tabPane != null) {
+            if (parent instanceof TabPane) {
+                TabPane tabPane = (TabPane)parent;
                 tabPane.tabPaneAttributeListeners.labelChanged(tabPane, component, previousLabel);
             }
         }
     }
 
     public static Image getIcon(Component component) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        return (attributes == null) ? null : attributes.icon;
+        return (Image)component.getAttribute(Attribute.ICON);
     }
 
     public static void setIcon(Component component, Image icon) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        if (attributes == null) {
-            throw new IllegalStateException();
-        }
+        Image previousIcon = (Image)component.setAttribute(Attribute.ICON, icon);
 
-        Image previousIcon = attributes.icon;
         if (previousIcon != icon) {
-            attributes.icon = icon;
+            Container parent = component.getParent();
 
-            TabPane tabPane = (TabPane)component.getParent();
-            if (tabPane != null) {
+            if (parent instanceof TabPane) {
+                TabPane tabPane = (TabPane)parent;
                 tabPane.tabPaneAttributeListeners.iconChanged(tabPane, component, previousIcon);
             }
         }
@@ -395,21 +377,19 @@
     }
 
     public static boolean isCloseable(Component component) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        return (attributes == null) ? false : attributes.closeable;
+        Boolean value = (Boolean)component.getAttribute(Attribute.CLOSEABLE);
+        return (value == null) ? false : value;
     }
 
     public static void setCloseable(Component component, boolean closeable) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        if (attributes == null) {
-            throw new IllegalStateException();
-        }
+        Boolean previousValue = (Boolean)component.setAttribute(Attribute.CLOSEABLE, closeable);
+        boolean previousCloseable = (previousValue == null) ? false : previousValue;
 
-        if (attributes.closeable != closeable) {
-            attributes.closeable = closeable;
+        if (previousCloseable != closeable) {
+            Container parent = component.getParent();
 
-            TabPane tabPane = (TabPane)component.getParent();
-            if (tabPane != null) {
+            if (parent instanceof TabPane) {
+                TabPane tabPane = (TabPane)parent;
                 tabPane.tabPaneAttributeListeners.closeableChanged(tabPane, component);
             }
         }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/TablePane.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/TablePane.java?rev=906647&r1=906646&r2=906647&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/TablePane.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TablePane.java Thu Feb  4 20:50:43 2010
@@ -217,7 +217,6 @@
             }
 
             cells.insert(component, index);
-            component.setAttributes(new Attributes());
 
             if (tablePane != null) {
                 tablePane.add(component);
@@ -239,8 +238,6 @@
                 }
 
                 cells.update(index, component);
-                previousComponent.setAttributes(null);
-                component.setAttributes(new Attributes());
 
                 if (tablePane != null) {
                     tablePane.add(component);
@@ -266,11 +263,6 @@
         public Sequence<Component> remove(int index, int count) {
             Sequence<Component> removed = cells.remove(index, count);
 
-            for (int i = 0, n = removed.getLength(); i < n; i++) {
-                Component component = removed.get(i);
-                component.setAttributes(null);
-            }
-
             if (tablePane != null) {
                 tablePane.tablePaneListeners.cellsRemoved(this, index, removed);
 
@@ -679,9 +671,9 @@
         }
     }
 
-    private static class Attributes {
-        public int rowSpan = 1;
-        public int columnSpan = 1;
+    private enum Attribute {
+        ROW_SPAN,
+        COLUMN_SPAN;
     }
 
     private static class TablePaneListenerList extends ListenerList<TablePaneListener>
@@ -992,22 +984,19 @@
     }
 
     public static int getRowSpan(Component component) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        return (attributes == null) ? -1 : attributes.rowSpan;
+        Integer value = (Integer)component.getAttribute(Attribute.ROW_SPAN);
+        return (value == null) ? 1 : value;
     }
 
     public static void setRowSpan(Component component, int rowSpan) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        if (attributes == null) {
-            throw new UnsupportedOperationException();
-        }
+        Integer previousValue = (Integer)component.setAttribute(Attribute.ROW_SPAN, rowSpan);
+        int previousRowSpan = (previousValue == null) ? 1 : previousValue;
 
-        int previousRowSpan = attributes.rowSpan;
         if (previousRowSpan != rowSpan) {
-            attributes.rowSpan = rowSpan;
+            Container parent = component.getParent();
 
-            TablePane tablePane = (TablePane)component.getParent();
-            if (tablePane != null) {
+            if (parent instanceof TablePane) {
+                TablePane tablePane = (TablePane)parent;
                 tablePane.tablePaneAttributeListeners.rowSpanChanged(tablePane,
                     component, previousRowSpan);
             }
@@ -1015,22 +1004,19 @@
     }
 
     public static int getColumnSpan(Component component) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        return (attributes == null) ? -1 : attributes.columnSpan;
+        Integer value = (Integer)component.getAttribute(Attribute.COLUMN_SPAN);
+        return (value == null) ? 1 : value;
     }
 
     public static void setColumnSpan(Component component, int columnSpan) {
-        Attributes attributes = (Attributes)component.getAttributes();
-        if (attributes == null) {
-            throw new UnsupportedOperationException();
-        }
+        Integer previousValue = (Integer)component.setAttribute(Attribute.COLUMN_SPAN, columnSpan);
+        int previousColumnSpan = (previousValue == null) ? 1 : previousValue;
 
-        int previousColumnSpan = attributes.columnSpan;
         if (previousColumnSpan != columnSpan) {
-            attributes.columnSpan = columnSpan;
+            Container parent = component.getParent();
 
-            TablePane tablePane = (TablePane)component.getParent();
-            if (tablePane != null) {
+            if (parent instanceof TablePane) {
+                TablePane tablePane = (TablePane)parent;
                 tablePane.tablePaneAttributeListeners.columnSpanChanged(tablePane,
                     component, previousColumnSpan);
             }