/*==========================================================================*\ | $Id: JavascriptProxy.java,v 1.1 2010/05/11 14:51:58 aallowat Exp $ |*-------------------------------------------------------------------------*| | Copyright (C) 2009 Virginia Tech | | This file is part of Web-CAT. | | Web-CAT is free software; you can redistribute it and/or modify | it under the terms of the GNU Affero General Public License as published | by the Free Software Foundation; either version 3 of the License, or | (at your option) any later version. | | Web-CAT is distributed in the hope that it will be useful, | but WITHOUT ANY WARRANTY; without even the implied warranty of | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | GNU General Public License for more details. | | You should have received a copy of the GNU Affero General Public License | along with Web-CAT; if not, see <http://www.gnu.org/licenses/>. \*==========================================================================*/ package org.webcat.ui.generators; import java.util.List; //------------------------------------------------------------------------- /** * <p> * A basic proxy object for chaining method calls together on a Javascript * object. * </p><p> * This class is not intended to and cannot be instantiated by users. These * proxy objects are created by {@link JavascriptGenerator} when chaining * calls together that involve different types of objects. * </p> * * @author Tony Allevato * @version $Id: JavascriptProxy.java,v 1.1 2010/05/11 14:51:58 aallowat Exp $ */ public class JavascriptProxy { //~ Constructors .......................................................... // ---------------------------------------------------------- /** * Initializes a new JavascriptProxy object. * * @param generator the generator used to generate code * @param root the root expression for this proxy */ /*package*/ JavascriptProxy(JavascriptGenerator generator, String root) { this.generator = generator; if (root != null) { generator.append(root); } } //~ Methods ............................................................... // ---------------------------------------------------------- /** * Assigns a value to a property at the end of a call chain. * * @param variable the name of the property/field to assign * @param value the value to assign to the variable * @return this proxy, for chaining */ public JavascriptProxy assign(String variable, Object value) { appendToFunctionChain(variable + " = " + generator.javascriptObjectFor(value)); return this; } // ---------------------------------------------------------- /** * Appends a method call to the end of a call chain. * * @param method the method to call * @param arguments the arguments to pass to the method * @return this proxy, for chaining */ public JavascriptProxy call(String method, Object... arguments) { appendToFunctionChain(method + "(" + generator.argumentsForCall(arguments) + ")"); return this; } // ---------------------------------------------------------- /** * Gets (and initializes, if necessary) the current function chain. * * @return the list of lines currently generated by the generator */ protected List<String> functionChain() { if (functionChain == null) { functionChain = generator.lines; } return functionChain; } // ---------------------------------------------------------- /** * Appends the specified method call (with arguments) to the end of the * current function chain. * * @param call the method call to append */ protected void appendToFunctionChain(String call) { if (functionChain().size() > 0) { int index = functionChain().size() - 1; String line = functionChain().get(index).trim(); line = line.replaceAll(";\\z", ""); line = line + '.' + call + ';'; functionChain().set(index, line); } } //~ Static/instance variables ............................................. protected JavascriptGenerator generator; private List<String> functionChain; }