Package org.olat.core.gui.components.velocity

Source Code of org.olat.core.gui.components.velocity.VelocityContainer

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) 1999-2006 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.core.gui.components.velocity;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.context.Context;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.ComponentRenderer;
import org.olat.core.gui.components.Container;
import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.Event;
import org.olat.core.gui.translator.Translator;
import org.olat.core.util.Util;
import org.olat.core.util.WebappHelper;

/**
* Description: <br>
*
* @author Felix Jost
*/
public class VelocityContainer extends Container {
  private static final ComponentRenderer RENDERER = new VelocityContainerRenderer(null);
 
  /**
   * Comment for <code>COMMAND_ID</code>
   */
  public static final String COMMAND_ID = "cid";

  private String page;
  private Context context = new VelocityContext();

  // access for the renderer only
  String markingCommandString;

  /**
   * Constructor to create a VC container from a given file path
   * @param componentName Name of component, displayed in debug mode
   * @param pagePath Full path to velocity page
   * @param trans Translator to be used
   * @param listeningController the listenenController; may be null if the
   *          caller has no need to register for events
   */
  public VelocityContainer(String componentName, String pagePath, Translator trans, Controller listeningController) {
    super(componentName, trans);
    setPage(pagePath);
    if (listeningController != null) addListener(listeningController);
  }
 
  /**
   * Constructor to create a VC container from a given base class and the page name
   * @param componentName Name of component, displayed in debug mode
   * @param baseClass
   * @param pageName page name that is available within this base class package
   * @param trans Translator to be used
   * @param listeningController the listenenController; may be null if the
   *          caller has no need to register for events
   */
  public VelocityContainer(String componentName, Class baseClass, String pageName, Translator trans, Controller listeningController) {
    this(componentName, Util.getPackageVelocityRoot(baseClass) + "/" + pageName + ".html", trans, listeningController);
  }

  /**
   * @see org.olat.core.gui.components.Component#dispatchRequest(org.olat.core.gui.UserRequest)
   */
  protected void doDispatchRequest(UserRequest ureq) {
    // called when a page-link is clicked ($r.renderCommand)
    String commandString = ureq.getParameter(COMMAND_ID);
   
   
    // WAS: set component dirty by default.
    // if you are in ajax mode, and the link resulted in a external mediaresource, then you can (in your controller) manually reset dirty to false.
    // we decided that there are only very few cases for this, and otherwise that the chance that a developers forgets to call vc.setDirty(true) is
    // much higher
   
    // now: normal links are created using the Link component, which takes care of setting dirty itself (everytime it is dispatched)
    //setDirty(true);
   
    // notify listening controllers
    fireEvent(ureq, new Event(commandString));
 
 
  /**
   * @return the Velocity Context
   */
  public Context getContext() {
    return context;
  }

  /**
   * Add a variable to the velocity context. The methods of the objects accessed
   * from the velocity file must be public. If not public you will get an error
   * message in the olat.log that the reference is not valid.
   *
   * @param key
   * @param value
   */
  public void contextPut(String key, Object value) {
    context.put(key, value);
    setDirty(true);
  }
 
 

  /**
   * Remove a variable from the velocity context
   *
   * @param key
   */
  public void contextRemove(String key) {
    if (context.containsKey(key)) {
      context.remove(key);
      setDirty(true);
    }
  }

  /**
   * @return String
   */
  public String getPage() {
    return page;
  }

  /**
   * Sets the page.
   * @deprecated Rather use panels to swap views
   * @param page The page to set
   */
  public void setPage(String page) {
    this.page = page;
    setDirty(true);
  }

  /**
   * @see org.olat.core.gui.components.Container#getExtendedDebugInfo()
   */
  public String getExtendedDebugInfo() {
    String webappContext = WebappHelper.getServletContextPath();
    String url = "page:"+page+" "+super.getExtendedDebugInfo();
    //url="<a href=&#34;gugus.html&#34;>halldfsdfolll</a>";
    //ServletUtils.
    return url;//"page:" + page + " ," + super.getExtendedDebugInfo();
  }

  public ComponentRenderer getHTMLRendererSingleton() {
    return RENDERER;
  }

  /**
   * Sets the translator for this VC. Note that this should be called before
   * adding any child components using put(component).
   * <p>
   * put(component) will set the current translator on the component if the
   * component itself does not yet have a translator set. The translator of
   * the components can't be modified afterwards.
   * <p>
   * This method overrides the protected setTranslator() method from the
   * component interface and make it public to support workflows where the
   * velocity container is created in the constructor (e.g.
   * FormBasicController) but the translator might be modified later (using
   * some special fallback translator or such)
   *
   * @param translator
   *            The translator to set
   */
  @Override
  public void setTranslator(Translator newTranslator) {
    super.setTranslator(newTranslator);
  }


}
TOP

Related Classes of org.olat.core.gui.components.velocity.VelocityContainer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.