You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by ek...@apache.org on 2005/05/25 04:13:27 UTC

svn commit: r178346 - in /incubator/beehive/trunk/samples/netui-samples: ./ WEB-INF/src/org/apache/beehive/samples/netui/resources/repeaterediting/ ui/repeaterediting/

Author: ekoneil
Date: Tue May 24 19:13:26 2005
New Revision: 178346

URL: http://svn.apache.org/viewcvs?rev=178346&view=rev
Log:
Add netui-samples sample of using a repeater to edit a data set.

BB: self
DRT: netui-samples builds / runs


Added:
    incubator/beehive/trunk/samples/netui-samples/WEB-INF/src/org/apache/beehive/samples/netui/resources/repeaterediting/
    incubator/beehive/trunk/samples/netui-samples/WEB-INF/src/org/apache/beehive/samples/netui/resources/repeaterediting/messages.properties
    incubator/beehive/trunk/samples/netui-samples/ui/repeaterediting/
    incubator/beehive/trunk/samples/netui-samples/ui/repeaterediting/Controller.java
    incubator/beehive/trunk/samples/netui-samples/ui/repeaterediting/index.jsp
Modified:
    incubator/beehive/trunk/samples/netui-samples/index.jsp

Added: incubator/beehive/trunk/samples/netui-samples/WEB-INF/src/org/apache/beehive/samples/netui/resources/repeaterediting/messages.properties
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/netui-samples/WEB-INF/src/org/apache/beehive/samples/netui/resources/repeaterediting/messages.properties?rev=178346&view=auto
==============================================================================
--- incubator/beehive/trunk/samples/netui-samples/WEB-INF/src/org/apache/beehive/samples/netui/resources/repeaterediting/messages.properties (added)
+++ incubator/beehive/trunk/samples/netui-samples/WEB-INF/src/org/apache/beehive/samples/netui/resources/repeaterediting/messages.properties Tue May 24 19:13:26 2005
@@ -0,0 +1,3 @@
+# Messages used by the /ui/repeaterediting/Controller.java sample
+
+invalidquantity=The quantity {0} must be greater than zero.
\ No newline at end of file

Modified: incubator/beehive/trunk/samples/netui-samples/index.jsp
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/netui-samples/index.jsp?rev=178346&r1=178345&r2=178346&view=diff
==============================================================================
--- incubator/beehive/trunk/samples/netui-samples/index.jsp (original)
+++ incubator/beehive/trunk/samples/netui-samples/index.jsp Tue May 24 19:13:26 2005
@@ -70,5 +70,9 @@
       <dt><netui:anchor href="ui/formintagfile/Controller.jpf" value="JSP Tag File as a Form"/></dt>
       <dd>Demonstrates how to use a JSP 2.0 .tag file to contain a NetUI form</dd>
     </dl>
+    <dl>
+      <dt><netui:anchor href="ui/repeaterediting/Controller.jpf" value="Editing a Data Set using the Repeater"/></dt>
+      <dd>Demonstrates how to use a repeater tag to edit text / boolean properties of items in a repeated data set with the repeater</dd>
+    </dl>
   </netui-template:section>
 </netui-template:template>

Added: incubator/beehive/trunk/samples/netui-samples/ui/repeaterediting/Controller.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/netui-samples/ui/repeaterediting/Controller.java?rev=178346&view=auto
==============================================================================
--- incubator/beehive/trunk/samples/netui-samples/ui/repeaterediting/Controller.java (added)
+++ incubator/beehive/trunk/samples/netui-samples/ui/repeaterediting/Controller.java Tue May 24 19:13:26 2005
@@ -0,0 +1,181 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ *
+ * $Header:$
+ */
+package ui.repeaterediting;
+
+import java.io.Serializable;
+import javax.servlet.ServletRequest;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.beehive.controls.api.bean.Control;
+import org.apache.beehive.netui.pageflow.annotations.Jpf;
+import org.apache.beehive.netui.pageflow.PageFlowController;
+import org.apache.beehive.netui.pageflow.Forward;
+import org.apache.beehive.netui.pageflow.FormData;
+import org.apache.beehive.samples.controls.pets.Pets;
+import org.apache.beehive.samples.netui.beans.PetType;
+
+import org.apache.struts.action.ActionErrors;
+import org.apache.struts.action.ActionMapping;
+import org.apache.struts.action.ActionMessage;
+
+/**
+ */
+@Jpf.Controller(
+    messageBundles={
+        @Jpf.MessageBundle(bundlePath="org.apache.beehive.samples.netui.resources.repeaterediting.messages")
+    },
+    forwards=@Jpf.Forward(name="success", path="index.jsp")
+)
+public class Controller
+    extends PageFlowController {
+
+    private static final double GIFT_WRAP_PRICE = 4.95;
+
+    @Control()
+    private Pets _petControl;
+
+    private CartForm _cartForm;
+
+    @Jpf.Action(
+        forwards=
+            @Jpf.Forward(name="success", path="index.jsp",
+                actionOutputs=@Jpf.ActionOutput(name="totalPrice", type=java.lang.Double.class))
+    )
+    public Forward begin() {
+        PetType[] pets = _petControl.getPetList();
+        CartItem[] items = new CartItem[pets.length];
+        for(int i = 0; i < items.length; i++) {
+            items[i] = new PetCartItem(pets[i]);
+            items[i].setQuantity(1);
+            items[i].setGiftWrap(false);
+        }
+
+        _cartForm = new CartForm(items);
+
+        Forward forward = new Forward("success");
+        forward.addOutputForm(_cartForm);
+        forward.addActionOutput("totalPrice", calculatePrice(_cartForm.getItems()));
+        return forward;
+    }
+
+    @Jpf.Action(
+        useFormBean="_cartForm",
+        validationErrorForward=@Jpf.Forward(name="failure", navigateTo=Jpf.NavigateTo.currentPage)
+    )
+    public Forward update(CartForm cart) {
+        Forward forward = new Forward("success");
+        forward.addOutputForm(cart);
+        forward.addActionOutput("totalPrice", calculatePrice(cart.getItems()));
+        return forward;
+    }
+
+    private double calculatePrice(CartItem[] items) {
+        double price = 0.0;
+        for(CartItem item : items) {
+            price += item.getQuantity() * item.getPrice();
+            if(item.isGiftWrap())
+                price += GIFT_WRAP_PRICE;
+        }
+        return price;
+    }
+
+    public static class CartForm
+        extends FormData
+        implements Serializable {
+
+        private CartItem[] _items;
+
+        public CartForm() {}
+
+        public CartForm(CartItem[] items) {
+            _items = items;
+        }
+
+        public CartItem[] getItems() {
+            return _items;
+        }
+
+        public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
+            ActionErrors errors = new ActionErrors();
+
+            for(int i = 0; i < _items.length; i++) {
+                CartItem item = _items[i];
+                if(item.getQuantity() <= 0) {
+                    errors.add("invalidQuantity" + i, new ActionMessage("invalidquantity", item.getQuantity()));
+                }
+            }
+
+            return errors;
+        }
+    }
+
+    public static abstract class CartItem
+        implements Serializable {
+
+        private int _quantity;
+        private boolean _giftWrap;
+
+        public int getQuantity() {
+            return _quantity;
+        }
+
+        public void setQuantity(int quantity) {
+            _quantity = quantity;
+        }
+
+        public boolean isGiftWrap() {
+            return _giftWrap;
+        }
+
+        public void setGiftWrap(boolean giftWrap) {
+            _giftWrap = giftWrap;
+        }
+
+        public abstract double getPrice();
+    }
+
+    public static class PetCartItem
+        extends CartItem {
+
+        private PetType _pet;
+
+        public PetCartItem(PetType pet) {
+            _pet = pet;
+        }
+
+        public int getPetId() {
+            assert _pet != null;
+            return _pet.getPetID();
+        }
+
+        public String getName() {
+            assert _pet != null;
+            return _pet.getName();
+        }
+
+        public double getPrice() {
+            assert _pet != null;
+            return _pet.getPrice();
+        }
+
+        public String getDescription() {
+            assert _pet != null;
+            return _pet.getDescription();
+        }
+    }
+}

Added: incubator/beehive/trunk/samples/netui-samples/ui/repeaterediting/index.jsp
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/netui-samples/ui/repeaterediting/index.jsp?rev=178346&view=auto
==============================================================================
--- incubator/beehive/trunk/samples/netui-samples/ui/repeaterediting/index.jsp (added)
+++ incubator/beehive/trunk/samples/netui-samples/ui/repeaterediting/index.jsp Tue May 24 19:13:26 2005
@@ -0,0 +1,76 @@
+<%--
+   Copyright 2004-2005 The Apache Software Foundation.
+
+   Licensed 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.
+
+   $Header:$
+--%>
+<%@ page language="java" contentType="text/html;charset=UTF-8"%>
+<%@ taglib uri="http://beehive.apache.org/netui/tags-databinding-1.0" prefix="netui-data"%>
+<%@ taglib uri="http://beehive.apache.org/netui/tags-html-1.0" prefix="netui"%>
+<%@ taglib uri="http://beehive.apache.org/netui/tags-template-1.0" prefix="netui-template"%>
+
+<netui-data:declarePageInput name="totalPrice" type="java.lang.Double" required="true"/>
+
+<netui-template:template templatePage="/resources/template/template.jsp">
+  <netui-template:setAttribute name="samTitle" value="Editable Repeater"/>
+  <netui-template:section name="main">
+      <netui:form action="update">
+          <table border="0" cellspacing="5" cellpadding="5" width="50%">
+              <tr>
+                  <td colspan="4">
+                      <table border="0" width="100%">
+                          <tr>
+                              <td align="right">
+                                  Subtotal: <netui:label value="${pageInput.totalPrice}">
+                                                <netui:formatNumber pattern="$###,###.00"/>
+                                            </netui:label>
+                                  <br/>
+                                  <netui:anchor formSubmit="true">Update</netui:anchor>
+                              </td>
+                          </tr>
+                          <tr bgcolor="#eeeeff">
+                              <td>Items in Cart:</td>
+                          </tr>
+                      </table>
+                  </td>
+              </tr>
+              <tr>
+                  <td width="30%">Product</td><td>Price</td><td>Quantity</td><td>Gift Wrap <font size="1">(adds $4.96 / item)</font></td>
+              </tr>
+              <netui-data:repeater dataSource="actionForm.items">
+              <tr cellpadding="5" ${container.index % 2 == 1 ? "style=\"background-color:#eeeeff\"" : ""}>
+                  <td>${container.item.name}</td>
+                  <td>
+                      <netui:span value="${container.item.price}">
+                          <netui:formatNumber pattern="$#####.00"/>
+                      </netui:span>
+                  </td>
+                  <td>
+                      <netui:textBox dataSource="container.item.quantity" size="5"/><br/>
+                      <font size="2" color="#ee0000"><netui:error key="invalidQuantity${container.index}"/></font>
+                  </td>
+                  <td>
+                      <netui:checkBox dataSource="container.item.giftWrap"/>
+                  </td>
+              </tr>
+          </netui-data:repeater>
+          <tr>
+              <td colspan="4" align="right">
+                  <netui:anchor formSubmit="true">Update</netui:anchor>
+              </td>
+          </tr>
+      </table>
+  </netui:form>
+  </netui-template:section>
+</netui-template:template>
\ No newline at end of file