sylvain 2003/11/03 09:05:32
Modified: src/blocks/woody/conf woody-form.xconf
src/blocks/woody/java/org/apache/cocoon/woody/formmodel
Repeater.java RepeaterDefinition.java
RepeaterDefinitionBuilder.java
src/blocks/woody/java/org/apache/cocoon/woody/util
DomHelper.java
Added: src/blocks/woody/java/org/apache/cocoon/woody/formmodel
RowAction.java RowActionDefinition.java
RowActionDefinitionBuilder.java
Log:
New row-action widget
Revision Changes Path
1.5 +1 -0 cocoon-2.1/src/blocks/woody/conf/woody-form.xconf
Index: woody-form.xconf
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/conf/woody-form.xconf,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- woody-form.xconf 24 Sep 2003 20:47:05 -0000 1.4
+++ woody-form.xconf 3 Nov 2003 17:05:32 -0000 1.5
@@ -13,6 +13,7 @@
<widget name="multivaluefield"
src="org.apache.cocoon.woody.formmodel.MultiValueFieldDefinitionBuilder"/>
<widget name="action"
src="org.apache.cocoon.woody.formmodel.ActionDefinitionBuilder"/>
<widget name="repeater-action"
src="org.apache.cocoon.woody.formmodel.RepeaterActionDefinitionBuilder"/>
+ <widget name="row-action"
src="org.apache.cocoon.woody.formmodel.RowActionDefinitionBuilder"/>
<widget name="submit"
src="org.apache.cocoon.woody.formmodel.SubmitDefinitionBuilder"/>
<widget name="button"
src="org.apache.cocoon.woody.formmodel.ButtonDefinitionBuilder"/>
<widget name="aggregatefield"
src="org.apache.cocoon.woody.formmodel.AggregateFieldDefinitionBuilder"/>
1.12 +59 -0
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Repeater.java
Index: Repeater.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Repeater.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- Repeater.java 28 Oct 2003 11:40:47 -0000 1.11
+++ Repeater.java 3 Nov 2003 17:05:32 -0000 1.12
@@ -91,16 +91,75 @@
rows.add(repeaterRow);
return repeaterRow;
}
+
+ public RepeaterRow addRow(int index) {
+ RepeaterRow repeaterRow = new RepeaterRow();
+ if (index >= this.rows.size()) {
+ rows.add(repeaterRow);
+ } else {
+ rows.add(index, repeaterRow);
+ }
+ return repeaterRow;
+ }
public RepeaterRow getRow(int index) {
return (RepeaterRow)rows.get(index);
}
+
+ /**
+ * Crawls up the parents of a widget up to finding a repeater row.
+ *
+ * @param widget the widget whose row is to be found
+ * @return the repeater row
+ */
+ public static RepeaterRow getParentRow(Widget widget) {
+ Widget result = widget;
+ while(result != null && ! (result instanceof Repeater.RepeaterRow)) {
+ result = result.getParent();
+ }
+
+ if (result == null) {
+ throw new RuntimeException("Could not find a parent row for
widget " + widget);
+
+ } else {
+ return (Repeater.RepeaterRow)result;
+ }
+ }
+
+ /**
+ * Get the position of a row in this repeater.
+ * @param row the row which we search the index for
+ * @return the row position or -1 if this row is not in this repeater
+ */
+ public int indexOf(RepeaterRow row) {
+ return this.rows.indexOf(row);
+ }
/**
* @throws IndexOutOfBoundsException if the the index is outside the
range of existing rows.
*/
public void removeRow(int index) {
rows.remove(index);
+ }
+
+ public void moveRowLeft(int index) {
+ if (index == 0 || index >= this.rows.size()) {
+ // do nothing
+ } else {
+ Object temp = this.rows.get(index-1);
+ this.rows.set(index-1, this.rows.get(index));
+ this.rows.set(index, temp);
+ }
+ }
+
+ public void moveRowRight(int index) {
+ if (index < 0 || index >= this.rows.size() - 1) {
+ // do nothing
+ } else {
+ Object temp = this.rows.get(index+1);
+ this.rows.set(index+1, this.rows.get(index));
+ this.rows.set(index, temp);
+ }
}
/**
1.2 +10 -1
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/RepeaterDefinition.java
Index: RepeaterDefinition.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/RepeaterDefinition.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RepeaterDefinition.java 22 Apr 2003 12:04:19 -0000 1.1
+++ RepeaterDefinition.java 3 Nov 2003 17:05:32 -0000 1.2
@@ -56,9 +56,14 @@
* The [EMAIL PROTECTED] WidgetDefinition} part of a Repeater widget, see
[EMAIL PROTECTED] Repeater} for more information.
*/
public class RepeaterDefinition extends AbstractWidgetDefinition {
+ private int initialSize = 0;
private List widgetDefinitions = new ArrayList();
private Map widgetDefinitionsById = new HashMap();
+ public RepeaterDefinition(int initialSize) {
+ this.initialSize = initialSize;
+ }
+
protected void addWidget(WidgetDefinition widgetDefinition) throws
DuplicateIdException {
if (widgetDefinitionsById.containsKey(widgetDefinition.getId()))
throw new DuplicateIdException("Duplicate widget id detected:
repeater " + getId() + " already has a widget with id \"" +
widgetDefinition.getId() + "\"");
@@ -75,6 +80,10 @@
}
public Widget createInstance() {
- return new Repeater(this);
+ Repeater result = new Repeater(this);
+ for (int i = 0; i < this.initialSize; i++) {
+ result.addRow();
+ }
+ return result;
}
}
1.5 +4 -1
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/RepeaterDefinitionBuilder.java
Index: RepeaterDefinitionBuilder.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/RepeaterDefinitionBuilder.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- RepeaterDefinitionBuilder.java 2 Nov 2003 09:52:05 -0000 1.4
+++ RepeaterDefinitionBuilder.java 3 Nov 2003 17:05:32 -0000 1.5
@@ -60,7 +60,10 @@
public class RepeaterDefinitionBuilder extends
AbstractWidgetDefinitionBuilder {
public WidgetDefinition buildWidgetDefinition(Element repeaterElement)
throws Exception {
- RepeaterDefinition repeaterDefinition = new RepeaterDefinition();
+
+ int initialSize = DomHelper.getAttributeAsInteger(repeaterElement,
"initial-size", 0);
+
+ RepeaterDefinition repeaterDefinition = new
RepeaterDefinition(initialSize);
setId(repeaterElement, repeaterDefinition);
setDisplayData(repeaterElement, repeaterDefinition);
1.1
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/RowAction.java
Index: RowAction.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.woody.formmodel;
import java.util.Locale;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
/**
*
* @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
* @version CVS $Id: RowAction.java,v 1.1 2003/11/03 17:05:32 sylvain Exp $
*/
public class RowAction extends Action {
public RowAction(RowActionDefinition definition) {
super(definition);
}
public static class MoveUpAction extends RowAction {
public MoveUpAction(RowActionDefinition.MoveUpDefinition definition) {
super(definition);
}
public void generateSaxFragment(ContentHandler contentHandler, Locale
locale) throws SAXException {
// Only generate if we're not at the top
Repeater.RepeaterRow row = Repeater.getParentRow(this);
if (((Repeater)row.getParent()).indexOf(row) > 0) {
super.generateSaxFragment(contentHandler, locale);
}
}
}
public static class MoveDownAction extends RowAction {
public MoveDownAction(RowActionDefinition.MoveDownDefinition
definition) {
super(definition);
}
public void generateSaxFragment(ContentHandler contentHandler, Locale
locale) throws SAXException {
// Only generate if we're not at the bottom
Repeater.RepeaterRow row = Repeater.getParentRow(this);
Repeater repeater = (Repeater)row.getParent();
if (repeater.indexOf(row) < repeater.getSize() - 1) {
super.generateSaxFragment(contentHandler, locale);
}
}
}
}
1.1
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/RowActionDefinition.java
Index: RowActionDefinition.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.woody.formmodel;
import org.apache.cocoon.woody.event.ActionEvent;
import org.apache.cocoon.woody.event.ActionListener;
/**
*
* @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
* @version CVS $Id: RowActionDefinition.java,v 1.1 2003/11/03 17:05:32
sylvain Exp $
*/
public class RowActionDefinition extends ActionDefinition {
public Widget createInstance() {
return new RowAction(this);
}
public static class DeleteRowDefinition extends RowActionDefinition {
public DeleteRowDefinition() {
super.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Repeater.RepeaterRow row =
Repeater.getParentRow(event.getSourceWidget());
Repeater repeater = (Repeater)row.getParent();
repeater.removeRow(repeater.indexOf(row));
}
});
}
}
public static class MoveUpDefinition extends RowActionDefinition {
public MoveUpDefinition() {
super.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Repeater.RepeaterRow row =
Repeater.getParentRow(event.getSourceWidget());
Repeater repeater = (Repeater)row.getParent();
// Rotation: up in a table is left in a list!
repeater.moveRowLeft(repeater.indexOf(row));
}
});
}
}
public static class MoveDownDefinition extends RowActionDefinition {
public MoveDownDefinition() {
super.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Repeater.RepeaterRow row =
Repeater.getParentRow(event.getSourceWidget());
Repeater repeater = (Repeater)row.getParent();
// Rotation : down in a table is right in a list!
repeater.moveRowRight(repeater.indexOf(row));
}
});
}
}
public static class AddAfterDefinition extends RowActionDefinition {
public AddAfterDefinition() {
super.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Repeater.RepeaterRow row =
Repeater.getParentRow(event.getSourceWidget());
Repeater repeater = (Repeater)row.getParent();
repeater.addRow(repeater.indexOf(row)+1);
}
});
}
}
}
1.1
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/RowActionDefinitionBuilder.java
Index: RowActionDefinitionBuilder.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.woody.formmodel;
import java.util.Iterator;
import org.apache.cocoon.woody.event.ActionListener;
import org.apache.cocoon.woody.util.DomHelper;
import org.w3c.dom.Element;
/**
*
* @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
* @version CVS $Id: RowActionDefinitionBuilder.java,v 1.1 2003/11/03
17:05:32 sylvain Exp $
*/
public class RowActionDefinitionBuilder extends
AbstractWidgetDefinitionBuilder {
public WidgetDefinition buildWidgetDefinition(Element widgetElement)
throws Exception {
String actionCommand = DomHelper.getAttribute(widgetElement,
"action-command");
RowActionDefinition definition = createDefinition(widgetElement,
actionCommand);
setId(widgetElement, definition);
setDisplayData(widgetElement, definition);
definition.setActionCommand(actionCommand);
Iterator iter = buildEventListeners(widgetElement, "on-activate",
ActionListener.class).iterator();
while (iter.hasNext()) {
definition.addActionListener((ActionListener)iter.next());
}
return definition;
}
protected RowActionDefinition createDefinition(Element element, String
actionCommand) throws Exception {
if ("delete".equals(actionCommand)) {
return new RowActionDefinition.DeleteRowDefinition();
} else if ("add-after".equals(actionCommand)) {
return new RowActionDefinition.AddAfterDefinition();
} else if ("move-up".equals(actionCommand)) {
return new RowActionDefinition.MoveUpDefinition();
} else if ("move-down".equals(actionCommand)) {
return new RowActionDefinition.MoveDownDefinition();
} else {
throw new Exception("Unknown repeater row action '" +
actionCommand + "' at " + DomHelper.getLineLocation(element));
}
}
}
1.8 +13 -0
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/util/DomHelper.java
Index: DomHelper.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/util/DomHelper.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- DomHelper.java 28 Oct 2003 21:21:43 -0000 1.7
+++ DomHelper.java 3 Nov 2003 17:05:32 -0000 1.8
@@ -218,6 +218,19 @@
}
}
+ public static int getAttributeAsInteger(Element element, String
attributeName, int defaultValue) throws Exception {
+ String attrValue = element.getAttribute(attributeName);
+ if (attrValue.equals("")) {
+ return defaultValue;
+ } else {
+ try {
+ return Integer.parseInt(attrValue);
+ } catch (NumberFormatException e) {
+ throw new Exception("Cannot parse the value \"" + attrValue
+ "\" as an integer in the attribute \"" + attributeName + "\" on the element
\"" + element.getTagName() + "\" at " + getLocation(element));
+ }
+ }
+ }
+
public static boolean getAttributeAsBoolean(Element element, String
attributeName, boolean defaultValue) throws Exception {
String attrValue = element.getAttribute(attributeName);
if (attrValue.equals(""))