Package ch.goodsolutions.demoextension.controller

Source Code of ch.goodsolutions.demoextension.controller.DemoController

/**
* 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) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package ch.goodsolutions.demoextension.controller;

import java.util.Arrays;

import org.olat.admin.user.UserSearchController;
import org.olat.basesecurity.events.SingleIdentityChosenEvent;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.Component;
import org.olat.core.gui.components.htmlheader.jscss.JSAndCSSComponent;
import org.olat.core.gui.components.link.Link;
import org.olat.core.gui.components.link.LinkFactory;
import org.olat.core.gui.components.panel.Panel;
import org.olat.core.gui.components.velocity.VelocityContainer;
import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.Event;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.control.controller.BasicController;
import org.olat.core.gui.control.generic.closablewrapper.CloseableModalController;
import org.olat.core.gui.control.generic.modal.DialogBoxController;
import org.olat.core.gui.control.generic.modal.DialogBoxUIFactory;
import org.olat.core.gui.control.navigation.DefaultNavElement;
import org.olat.core.id.Identity;
import org.olat.core.id.User;
import org.olat.core.id.UserConstants;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.user.HomePageConfig;
import org.olat.user.HomePageConfigManagerImpl;
import org.olat.user.HomePageDisplayController;

/**
* Description:<br>
* Demo for a simple extension using some of the provided functionality of OLAT
* and the component framework. To switch on this extenstion go to
* /webapp/WEB-INF/olat-extensions.xml and uncomment the line containing
* "demoextension". The demo extension shows how to generate a simple form, user
* can fill in. It also shows how to listen to events (e.g. user request
* generated by clicking in the browser on a link or a button). Further is shows
* how to use and react onto events generated by other controller used in this
* example.
*
* @author Felix Jost, Initial Date: Jul 13, 2005
*/
public class DemoController extends BasicController {
 
  OLog log = Tracing.createLoggerFor(this.getClass());

  private VelocityContainer myContent;
  private final DefaultNavElement navElem;
 
  private DialogBoxController dialogC;
  private UserSearchController searchC;
  private HomePageDisplayController homepageC;
 
  private Panel homepagePanel;
 
  private Link modalButton;
  private Link searchButton;
  private Link iconButton;

  private SampleFlexiForm sampleFlexiForm;
 
  private CloseableModalController cmc;
 
  /**
   * @param ureq
   * @param wControl
   * @param navElem
   */
  public DemoController(UserRequest ureq, WindowControl wControl, DefaultNavElement navElem) {
    //it is mandatory to set the WindowControl to the superclass
    super(ureq, wControl);
    this.navElem = navElem;

    // set up the main layout
    myContent = createVelocityContainer("index");
   
    modalButton = LinkFactory.createButton("command.modal", myContent, this);
    searchButton = LinkFactory.createButton("command.search", myContent, this);
   
    iconButton = LinkFactory.createCustomLink("sonne", "cmd.sonne", "", Link.NONTRANSLATED, myContent, this);
    iconButton.setCustomEnabledLinkCSS("demoext_bild");
    iconButton.setCustomDisabledLinkCSS("demoext_bild");
   
    // let the scripts (.js files) and css files be included when this controller's main component is rendered
    JSAndCSSComponent jscss = new JSAndCSSComponent("jsAndCssForDemo", this.getClass(), new String[] {"script.js"}, "style.css", false);
    myContent.put("jsAndCssForDemo", jscss); // we include it in the render tree, so that the custom js and css are included
   
    // prepare an empty panel for displaying the selected user's home-page
    homepagePanel = new Panel("hpPanel");
    myContent.put("homepagepanel", homepagePanel);
   

    sampleFlexiForm = new SampleFlexiForm(ureq, getWindowControl());
    listenTo(sampleFlexiForm);
    // listenTo is the convenience method and ensures that the dispose chain
    // is guaranteed -> see doPreDispose of BasicController
    myContent.put("sampleform", sampleFlexiForm.getInitialComponent());
   
    // lookup the current user info to display a personalized welcome message
    Identity curIdentity = ureq.getIdentity();
    User curUser = curIdentity.getUser();
    String userinfo = (curUser==null? "n/a" :getTranslator().translate("show.entered.data", new String[] { curUser.getProperty(UserConstants.LASTNAME, ureq.getLocale()), curUser.getProperty(UserConstants.FIRSTNAME, ureq.getLocale())}));
    // simple variable that can be referred in the velocity context by
    // "$userinfo"
    myContent.contextPut("userinfo", userinfo);

    putInitialPanel(myContent);
  }

  /**
   * @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest,
   *      org.olat.core.gui.components.Component, org.olat.core.gui.control.Event)
   */
  public void event(UserRequest ureq, Component source, Event event) {
   
    if (source == myContent) {
      if (event.getCommand().equals("doThis")) {
        //do something here
        if(log.isDebug()){
          log.debug("link clicked, log something to the olat.log file");
        }
      }
     
    } else if (source == modalButton || source == iconButton){
      // The button "Click here for a modal dialog" was clicked
      // create a Ok/Cancel confirmation dialog and let the user answer the
      // question
      dialogC = activateOkCancelDialog(ureq, null, translate("question"), dialogC);
     
    } else if (source == searchButton){
      // the user clicked the search button.
      // Create a UserSearchController and display it in a modal dialogue box
      searchC = new UserSearchController(ureq, getWindowControl(), true);
      listenTo(searchC);
     
      //show it in the deprecated manner for now: TODO
      cmc = new CloseableModalController(
          getWindowControl(),
          translate("close"),
          searchC.getInitialComponent()
      );
      // listen to changes from cmc, e.g. "CLOSE_MODAL_EVENT"
      listenTo(cmc);
     
      //needs activate() to "open" and deactivate() to "close"
      cmc.activate();
    }
  }

  protected void event(UserRequest ureq, Controller source, Event event) {
    if (source == dialogC) {
      // there is an answer concerning the dialog
      if (DialogBoxUIFactory.isOkEvent(event)) {
        showError("clicked.ok");
      } else if (DialogBoxUIFactory.isClosedEvent(event)) {
        showInfo("clicked.close");
      } else {
        showInfo("clicked.cancel"); // nothing else left
      }
     
    } else if (source == searchC) {
      // it is the UserSearchController talking to us
      if (event instanceof SingleIdentityChosenEvent) {
        // some user was chosen
        // (UserSearchController's javadoc shows when which event is fired)
        SingleIdentityChosenEvent uce = (SingleIdentityChosenEvent) event;

        // prepare the homepage configuration for this user
        Identity identity = uce.getChosenIdentity();
        HomePageConfig homePageConfig = HomePageConfigManagerImpl.getInstance().loadConfigFor(
            identity.getName()
        );

        // create the controller with this configuration
        homepageC = new HomePageDisplayController(ureq, getWindowControl(), homePageConfig);
        listenTo(homepageC);
        // insert the homepage in the area below the button (for layout, see the
        // index.html file in the "content" subfolder
        homepagePanel.setContent(homepageC.getInitialComponent());
      } // else ignore other events (e.g. Event.CANCELLED_EVENT)
 
      //to "close" the modal dialogue
      cmc.deactivate();
     
      //cmc was added using listenTo(), so it can be removed like this:
      removeAsListenerAndDispose(cmc);
      cmc = null;
      //but it would have been disposed automatically anyway
     
    } else if (source == cmc && event.getCommand().equals("CLOSE_MODAL_EVENT")) {
     
      showInfo("clicked.close");
     
    } else if (source == sampleFlexiForm) {
     
      if (event == Event.DONE_EVENT) {
        String last = sampleFlexiForm.getLastName();
        String first = sampleFlexiForm.getFirstName();
        //the special key "placeholder" can be used for strings which
        //do not need translation or for strings which are already translated.
        showInfo("placeholder", translate("show.entered.data", new String[]{last,first}));
      }
    }
  }
 
  /**
   * @see org.olat.core.gui.control.DefaultController#doDispose(boolean)
   */
  protected void doDispose() { 
    //no need to dispose any controller because each one was
    //registered for cleanup in listenTo(controller)
   
    //no other stuff to clean up
  }
}
TOP

Related Classes of ch.goodsolutions.demoextension.controller.DemoController

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.