java-shapes.js
0001 /*
0002 * Copyright 1999-2004 The Apache Software Foundation.
0003 *
0004 * Licensed under the Apache License, Version 2.0 (the "License");
0005 * you may not use this file except in compliance with the License.
0006 * You may obtain a copy of the License at
0007 *
0008 * http://www.apache.org/licenses/LICENSE-2.0
0009 *
0010 * Unless required by applicable law or agreed to in writing, software
0011 * distributed under the License is distributed on an "AS IS" BASIS,
0012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013 * See the License for the specific language governing permissions and
0014 * limitations under the License.
0015 */
0017 // Shape's area and perimeter calculation example.
0019 var calculator = null;
0021 function public_startShape() {
0022 var hint = "Calculate shape's area and perimeter using logic in java. ";
0024 // let user select shape
0025 cocoon.sendPageAndWait("java-shapes/views/select", {"hint" : hint});
0026 var shapeId = cocoon.request.get("shape");
0028 // send shape-specific view
0029 cocoon.sendPageAndWait("java-shapes/views/" + shapeId, {"shapeId" : shapeId});
0031 // get request parameters (of which some are null depending on shape, that's not a problem)
0032 var h = parseInt( cocoon.request.get("h") );
0033 var b = parseInt( cocoon.request.get("b") );
0034 var r = parseInt( cocoon.request.get("r") );
0036 // instantiate appropriate calculator
0037 if(shapeId == "square") {
0038 calculator = new Packages.org.apache.cocoon.samples.tour.shapes.Square(b);
0039 } else if(shapeId=="rectangular") {
0040 calculator = new Packages.org.apache.cocoon.samples.tour.shapes.Rectangular(b,h);
0041 } else if(shapeId=="circle") {
0042 calculator = new Packages.org.apache.cocoon.samples.tour.shapes.Circle(r);
0043 } else {
0044 throw new java.lang.Exception("No calculator found for shape '" + shapeId + "'");
0045 }
0047 // compute results
0048 // (accessing bean-like properties like "getArea()" using property names like "area")
0049 var a = calculator.area;
0050 var p = calculator.perimeter;
0052 cocoon.sendPage("java-shapes/views/results", {"area" : a, "perimeter" : p, "shape" : shapeId} );
0053 }