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