Source Code Cross Referenced for ContentsTableFormatter.java in  » J2EE » Enhydra-Demos » golfShop » presentation » xmlc » cart » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Enhydra Demos » golfShop.presentation.xmlc.cart 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Enhydra Java Application Server 
003:         * The Initial Developer of the Original Code is Lutris Technologies Inc. 
004:         * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
005:         * Inc. 
006:         * All Rights Reserved. 
007:         *
008:         * The contents of this file are subject to the Enhydra Public License Version
009:         * 1.0 (the "License"); you may not use this file except in compliance with the
010:         * License. You may obtain a copy of the License at
011:         * http://www.enhydra.org/software/license/epl.html 
012:         *
013:         * Software distributed under the License is distributed on an "AS IS" basis,
014:         * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
015:         * License for the specific language governing rights and limitations under the
016:         * License. 
017:         *
018:         * 
019:         */
020:
021:        package golfShop.presentation.xmlc.cart;
022:
023:        import org.enhydra.xml.xmlc.*;
024:        import org.enhydra.xml.xmlc.html.*;
025:        import com.lutris.appserver.server.httpPresentation.*;
026:        import java.io.*;
027:        import java.net.URLEncoder;
028:        import org.w3c.dom.*;
029:        import org.w3c.dom.html.*;
030:        import golfShop.presentation.xmlc.utilities.*;
031:        import java.math.BigDecimal;
032:        import java.util.Enumeration;
033:        import golfShop.spec.cart.*;
034:
035:        /**
036:         * Generate a HTML table to display the cart contents.  It is based on the
037:         * prototype table in the Contents.html or Confirm.html page, which
038:         * implement ContentsTable.
039:         */
040:        public class ContentsTableFormatter {
041:            /**
042:             * Prefix for dynamic quantify field; will have object id added.
043:             */
044:            public static final String QTY_PREFIX = "qty_";
045:
046:            /**
047:             * Prefix for delete field; will have object id added.
048:             */
049:            public static final String DEL_PREFIX = "del_";
050:
051:            /**
052:             * Id for item num in prototype row.
053:             */
054:            private static final String ITEM_NUM_ID = "itemNum";
055:
056:            /**
057:             * Id for item quantity in prototype row.
058:             */
059:            private static final String ITEM_QUANTITY_ID = "itemQuantity";
060:
061:            /**
062:             * Id for item desc in prototype row.
063:             */
064:            private static final String ITEM_DESC_ID = "itemDesc";
065:
066:            /**
067:             * Object being operated on.
068:             */
069:            private ContentsTable htmlObj;
070:
071:            /**
072:             * Prototype row for the table.
073:             */
074:            private HTMLTableRowElement protoRow;
075:
076:            /**
077:             * Is table dynamic?
078:             */
079:            private boolean dynamic;
080:
081:            /**
082:             * Cart to add to HTML object.
083:             */
084:            private Cart cart;
085:
086:            /**
087:             * Generate a table row for an item based on the prototype.
088:             *
089:             * @param rowNum Row number in table.
090:             * @param itemPair The item information to display.
091:             */
092:            private HTMLTableRowElement makeItemRow(int rowNum,
093:                    CartItemPair itemPair) {
094:                // Get necessary info.
095:                int quantity = itemPair.getQuantity();
096:                CartItem item = itemPair.getItem();
097:                BigDecimal price = new BigDecimal(item.getPrice());
098:                BigDecimal total = new BigDecimal(quantity * item.getPrice());
099:
100:                // Format price and total to have 2 digits past the decimal.
101:                price = price.setScale(2, BigDecimal.ROUND_HALF_UP);
102:                total = total.setScale(2, BigDecimal.ROUND_HALF_UP);
103:
104:                // Setup new row.
105:                HTMLTableRowElement newRow = (HTMLTableRowElement) protoRow
106:                        .cloneNode(true);
107:
108:                // Item number
109:                Element itemNumElem = XMLCUtil.getRequiredElementById(
110:                        ITEM_NUM_ID, newRow);
111:                XMLCUtil.getFirstText(itemNumElem).setData(
112:                        Integer.toString(rowNum));
113:
114:                // Quantity
115:                String quantityStr = Integer.toString(quantity);
116:                if (dynamic) {
117:                    // Update input node.
118:                    HTMLInputElement quantityElem = (HTMLInputElement) XMLCUtil
119:                            .getRequiredElementById(ITEM_QUANTITY_ID, newRow);
120:                    quantityElem.setName(QTY_PREFIX + item.getObjectId());
121:                    quantityElem.setValue(quantityStr);
122:                } else {
123:                    // The node will only be an input node if it came from the 
124:                    // cart HTML.  Figure it out and either replace input node or
125:                    // change the next node.
126:                    Element quantityElem = XMLCUtil.getRequiredElementById(
127:                            ITEM_QUANTITY_ID, newRow);
128:                    if (quantityElem instanceof  HTMLInputElement) {
129:                        Text text = htmlObj.createTextNode(quantityStr);
130:                        quantityElem.getParentNode().replaceChild(text,
131:                                quantityElem);
132:                    } else {
133:                        XMLCUtil.getFirstText(quantityElem)
134:                                .setData(quantityStr);
135:                    }
136:                }
137:
138:                // Description
139:                Element itemDescElem = XMLCUtil.getRequiredElementById(
140:                        ITEM_DESC_ID, newRow);
141:                XMLCUtil.getFirstText(itemDescElem).setData(item.getName());
142:
143:                // Price
144:                Element itemPriceElem = XMLCUtil.getRequiredElementById(
145:                        "itemPrice", newRow);
146:                XMLCUtil.getFirstText(itemPriceElem).setData(price.toString());
147:
148:                // Total
149:                Element itemTotalElem = XMLCUtil.getRequiredElementById(
150:                        "itemTotal", newRow);
151:                XMLCUtil.getFirstText(itemTotalElem).setData(total.toString());
152:
153:                // Delete checkbox, only in a dynamic table.
154:                if (dynamic) {
155:                    HTMLInputElement itemDeleteElem = (HTMLInputElement) XMLCUtil
156:                            .getRequiredElementById("itemDelete", newRow);
157:                    itemDeleteElem.setName(DEL_PREFIX + item.getObjectId());
158:                }
159:                return newRow;
160:            }
161:
162:            /**
163:             * Fill in table rows from cart and total cost.
164:             */
165:            private void fillInRows() {
166:
167:                try {
168:                    Enumeration items = cart.getContents();
169:                    HTMLTableRowElement costRow = htmlObj.getElementCostRow();
170:                    Node parent = costRow.getParentNode();
171:
172:                    int rowNum = 1;
173:                    while (items.hasMoreElements()) {
174:                        HTMLTableRowElement newRow = makeItemRow(rowNum++,
175:                                (CartItemPair) items.nextElement());
176:                        parent.insertBefore(newRow, costRow);
177:                    }
178:
179:                    BigDecimal total = new BigDecimal(cart.getTotal());
180:                    total = total.setScale(2, BigDecimal.ROUND_HALF_UP);
181:                    htmlObj.setTextCost(total.toString());
182:                    //same thing
183:                } catch (NullPointerException ex) {
184:                }
185:            }
186:
187:            /**
188:             * Recursively search for a row and deleted it if it doesn't have an id
189:             * or if its the prototype row.
190:             */
191:            private void deleteDemoRows(Node node) {
192:                if (node instanceof  HTMLTableRowElement) {
193:                    String id = ((HTMLTableRowElement) node).getId();
194:                    if ((id == null) || (id.length() == 0)
195:                            || (node == protoRow)) {
196:                        node.getParentNode().removeChild(node);
197:                    }
198:                } else {
199:                    // Not a row, search children.
200:                    Node child = node.getFirstChild();
201:                    while (child != null) {
202:                        Node next = child.getNextSibling();
203:                        deleteDemoRows(child);
204:                        child = next;
205:
206:                    }
207:                }
208:            }
209:
210:            /**
211:             * Constructor.  External instances are never created.
212:             *
213:             * @param tableHtmlObj Object with HTML table for items.
214:             * @param tableCart Cart used to generate the table.
215:             */
216:            private ContentsTableFormatter(ContentsTable tableHtmlObj,
217:                    Cart tableCart, boolean isDynamic) {
218:                htmlObj = tableHtmlObj;
219:                cart = tableCart;
220:                dynamic = isDynamic;
221:                protoRow = htmlObj.getElementProtoRow();
222:                deleteDemoRows(htmlObj.getElementItemTable());
223:            }
224:
225:            /**
226:             * File in the table object from a cart.
227:             */
228:            public static void fillInTable(ContentsTable tableHtmlObj,
229:                    Cart tableCart, boolean isDynamic) {
230:                ContentsTableFormatter table = new ContentsTableFormatter(
231:                        tableHtmlObj, tableCart, isDynamic);
232:                table.fillInRows();
233:            }
234:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.