You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ti...@apache.org on 2004/09/09 20:06:54 UTC

svn commit: rev 43618 - in cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms: . formmodel

Author: tim
Date: Thu Sep  9 11:06:53 2004
New Revision: 43618

Modified:
   cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/DefaultFormManager.java
   cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/AbstractContainerWidget.java
   cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/AbstractWidget.java
   cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Repeater.java
   cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Union.java
   cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Widget.java
Log:
Introduce initialize() to allow widgets to do setup after the
widget tree has been setup, required for next item:
Modify union widget to use lookupWidget(path) to find its case
widget, (e.g. allowing paths like "../../somewidget".)
Fix minor error in lookupWidget (replace && with ||.)


Modified: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/DefaultFormManager.java
==============================================================================
--- cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/DefaultFormManager.java	(original)
+++ cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/DefaultFormManager.java	Thu Sep  9 11:06:53 2004
@@ -109,7 +109,9 @@
 
     public Form createForm(Source source) throws Exception {
         FormDefinition formDefinition = getFormDefinition(source);
-        return (Form)formDefinition.createInstance();
+        Form form = (Form)formDefinition.createInstance();
+        form.initialize();
+        return form;
     }
 
     public Form createForm(String uri) throws Exception {
@@ -133,7 +135,9 @@
     }
 
     public Form createForm(Element formElement) throws Exception {
-        return (Form)getFormDefinition(formElement).createInstance();
+        Form form = (Form)getFormDefinition(formElement).createInstance();
+        form.initialize();
+        return form;
     }
 
     public FormDefinition createFormDefinition(Element formElement) throws Exception {

Modified: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/AbstractContainerWidget.java
==============================================================================
--- cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/AbstractContainerWidget.java	(original)
+++ cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/AbstractContainerWidget.java	Thu Sep  9 11:06:53 2004
@@ -25,7 +25,7 @@
 /**
  * A general-purpose abstract Widget which can hold zero or more widgets.
  *
- * @version $Id: AbstractContainerWidget.java,v 1.13 2004/07/07 01:39:54 vgritsenko Exp $
+ * @version $Id$
  */
 public abstract class AbstractContainerWidget extends AbstractWidget implements ContainerWidget {
 
@@ -41,9 +41,21 @@
         widgets = new WidgetList();
     }
 
+    /**
+     * Called after widget's environment has been setup,
+     * to allow for any contextual initalization such as
+     * looking up case widgets for union widgets.
+     */
+    public void initialize() {
+        Iterator it = this.getChildren();
+        while(it.hasNext()) {
+          ((Widget)it.next()).initialize();
+        }
+    }
+
     public void addChild(Widget widget) {
-        widget.setParent(this);
         widgets.addWidget(widget);
+        widget.setParent(this);
     }
 
     public boolean hasChild(String id) {

Modified: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/AbstractWidget.java
==============================================================================
--- cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/AbstractWidget.java	(original)
+++ cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/AbstractWidget.java	Thu Sep  9 11:06:53 2004
@@ -33,7 +33,7 @@
  * Abstract base class for Widget implementations. Provides functionality
  * common to many widgets.
  * 
- * @version $Id: AbstractWidget.java,v 1.21 2004/05/11 12:55:53 vgritsenko Exp $
+ * @version $Id$
  */
 public abstract class AbstractWidget implements Widget {
     
@@ -59,7 +59,15 @@
      */
     private Map attributes;
  
-    
+    /**
+     * Called after widget's environment has been setup,
+     * to allow for any contextual initalization, such as
+     * looking up case widgets for union widgets.
+     */
+    public void initialize() {
+        // Do nothing.
+    }
+
     /**
      * Gets the id of this widget.
      */
@@ -135,11 +143,11 @@
         return getId();
     }
 
-	public Widget lookupWidget(String path)	{
-        
-        if (path == null && path.equals(""))
+    public Widget lookupWidget(String path) {
+
+        if (path == null || path.equals(""))
             return this;
-        
+
         Widget relativeWidget;
         String relativePath = null;        
         int sepPosition = path.indexOf("" + Widget.PATH_SEPARATOR);

Modified: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Repeater.java
==============================================================================
--- cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Repeater.java	(original)
+++ cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Repeater.java	Thu Sep  9 11:06:53 2004
@@ -39,7 +39,7 @@
  * <p>Using the methods {@link #getSize()} and {@link #getWidget(int, java.lang.String)}
  * you can access all of the repeated widget instances.
  * 
- * @version $Id: Repeater.java,v 1.14 2004/05/07 20:54:21 mpo Exp $
+ * @version $Id$
  */
 public class Repeater extends AbstractWidget 
 //implements ContainerWidget 
@@ -64,6 +64,7 @@
     public RepeaterRow addRow() {
         RepeaterRow repeaterRow = new RepeaterRow(definition);
         rows.add(repeaterRow);
+        repeaterRow.initialize();
         return repeaterRow;
     }
     
@@ -74,6 +75,7 @@
         } else {
             rows.add(index, repeaterRow);
         }
+        repeaterRow.initialize();
         return repeaterRow;
     }
 

Modified: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Union.java
==============================================================================
--- cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Union.java	(original)
+++ cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Union.java	Thu Sep  9 11:06:53 2004
@@ -24,7 +24,7 @@
  * for the widget id, just wrap the widget(s) in a container widget named
  * with the desired case id.
  *
- * @version $Id: Union.java,v 1.12 2004/06/29 13:06:04 sylvain Exp $
+ * @version $Id$
  */
 public class Union extends AbstractContainerWidget {
     
@@ -46,20 +46,18 @@
         return definition;
     }
 
-    // TODO: This whole union mess is too dependent on undefined sequences of execution.
-    // These need to be ordered into a contract of sequences.
-
-    public void setParent(Widget widget) {
-        super.setParent(widget);
-        resolve();
-    }
-
-    // TODO: The "resolve" step currently expands each "New" into the list of widgets in the corresponding "Class".
-    // "resolve" should be changed to "expand", and a new step, "resolve" should be introduced which patches up any
-    // *widget* (not definition) references after the expansion has put all of the widgets in place.
-    public void resolve() {
+    /**
+     * Called after widget's environment has been setup,
+     * to allow for any contextual initalization such as
+     * looking up case widgets for union widgets.
+     */
+    public void initialize() {
         String caseWidgetId = definition.getCaseWidgetId();
-        caseWidget = ((ContainerWidget)getParent()).getChild(caseWidgetId);
+        this.caseWidget = getParent().lookupWidget(caseWidgetId);
+        if(this.caseWidget == null) {
+            throw new RuntimeException("Could not find case widget \""
+                + caseWidgetId + "\" for union \"" + getId() + "\" at " + getLocation());
+        }
     }
 
     /**
@@ -112,8 +110,12 @@
     }
 
     public Widget getChild(String id) {
-        if (!widgets.hasWidget(id) && ((ContainerDefinition)definition).hasWidget(id))
+        if (!widgets.hasWidget(id) && ((ContainerDefinition)definition).hasWidget(id)) {
             ((ContainerDefinition)definition).createWidget(this, id);
+            Widget child = super.getChild(id);
+            child.initialize();
+            return child;
+         }
         return super.getChild(id);
     }
 

Modified: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Widget.java
==============================================================================
--- cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Widget.java	(original)
+++ cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Widget.java	Thu Sep  9 11:06:53 2004
@@ -49,7 +49,7 @@
  * keeps the Widgets small and light to create. This mechanism is similar to
  * classes and objects in Java.
  * 
- * @version CVS $Id: Widget.java,v 1.15 2004/05/11 12:55:53 vgritsenko Exp $
+ * @version CVS $Id$
  */
 public interface Widget {
     
@@ -58,6 +58,13 @@
      * @see #lookupWidget(String)
      */
     public static final char PATH_SEPARATOR = '/';
+
+    /**
+     * Called after widget's environment has been setup,
+     * to allow for any contextual initalization such as
+     * looking up case widgets for union widgets.
+     */
+    public void initialize();
 
     /**
      * @return  the source location of this widget.