Package org.olat.repository

Source Code of org.olat.repository.DetailsForm

/**
* 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 org.olat.repository;

import java.util.Locale;

import org.olat.ControllerFactory;
import org.olat.core.gui.components.form.Form;
import org.olat.core.gui.formelements.CheckBoxElement;
import org.olat.core.gui.formelements.StaticHTMLTextElement;
import org.olat.core.gui.formelements.StaticSingleSelectionElement;
import org.olat.core.gui.formelements.StaticTextElement;
import org.olat.core.gui.formelements.TextAreaElement;
import org.olat.core.gui.formelements.TextElement;
import org.olat.core.gui.translator.Translator;
import org.olat.repository.handlers.RepositoryHandler;
import org.olat.repository.handlers.RepositoryHandlerFactory;

/**
* Initial Date:  08.07.2003
*
* @author Mike Stock
*
* Comment: 
*
*/
public class DetailsForm extends Form {

  private RepositoryEntry entry;
  private String typeName;
  private boolean enableAuthorView = false;
 
  private TextElement displayName;
  final public static int MAX_DISPLAYNAME = 100;
  private StaticTextElement resourceName;
  private StaticTextElement initialAuthor;
  private StaticHTMLTextElement type;
  private TextAreaElement description;
  private CheckBoxElement canCopy;
  private CheckBoxElement canReference;
  private CheckBoxElement canLaunch;
  private CheckBoxElement canDownload;
  private StaticSingleSelectionElement access;
  private Locale locale;
 
  /**
   * The details form is initialized with data collected from entry and typeName. Handler is looked-up by
   * the given handlerName and not by the entry's resourceableType. This is to allow for an entry with no resourceable
   * to initialize correctly (c.f. RepositoryAdd workflow). The typeName may be null.
   *
   * @param name
   * @param t
   * @param entry
   * @param typeName
   * @param enableAuthorView
   * @param doDisplayOnly
   */
  public DetailsForm(String name, Translator translator, RepositoryEntry entry, String typeName, boolean enableAuthorView, boolean doDisplayOnly) {
    super(name, translator);
    this.entry = entry;
    this.typeName = typeName;
    this.enableAuthorView = enableAuthorView;
    this.locale = translator.getLocale();
    init();
    setDisplayOnly(doDisplayOnly);
  }

  /**
   * Initialize form data based on repository entry.
   */
  public void init() {
    if (enableAuthorView) {
      addFormElement("cif_id", new StaticTextElement("cif.id", entry.getKey() != null ? entry.getKey().toString() : translate("cif.id.na")));
    }
    displayName = new TextElement("cif.displayname", entry.getDisplayname(), true, MAX_DISPLAYNAME);
    displayName.setMaxLength(100);
    addFormElement("cif_displayname", displayName);
    initialAuthor = new StaticTextElement("cif.initialAuthor", entry.getInitialAuthor());
    addFormElement("cif_initialAuthor", initialAuthor);
   
    // for empty TextAreas, put at least a space so that TextArea gets displayed correctly
    description = new TextAreaElement("cif.description", 4, 40, (entry.getDescription() != null) ? entry.getDescription() : " ");
    description.setMandatory(true);
    addFormElement("cif_description", description);
    resourceName = new StaticTextElement("cif.resourcename", (entry.getResourcename() == null) ? "-" : entry.getResourcename());
    resourceName.setMaxLength(100);
    addFormElement("cif_resourcename", resourceName);
   
    StringBuilder typeDisplayText = new StringBuilder(100);
    if (typeName != null) { // add image and typename code
      RepositoryEntryIconRenderer reir = new RepositoryEntryIconRenderer(this.locale);
      typeDisplayText.append("<span class=\"b_with_small_icon_left ");
      typeDisplayText.append(reir.getIconCssClass(entry));
      typeDisplayText.append("\">");
      String tName = ControllerFactory.translateResourceableTypeName(typeName, getLocale());
      typeDisplayText.append(tName);
      typeDisplayText.append("</span>");
    } else {
      typeDisplayText.append(translate("cif.type.na"));
    }
    type = new StaticHTMLTextElement("cif.type", typeDisplayText.toString(), 255);
    addFormElement("cif_type", type);
   
    RepositoryHandler handler = null;
    if (typeName != null) handler = RepositoryHandlerFactory.getInstance().getRepositoryHandler(typeName);
   
    canCopy = new CheckBoxElement("cif.canCopy", entry.getCanCopy());
    if (enableAuthorView)
      addFormElement("cif_canCopy", canCopy);

    canReference = new CheckBoxElement("cif.canReference", entry.getCanReference());
    if (enableAuthorView)
      addFormElement("cif_canReference", canReference);

    if (handler != null && handler.supportsLaunch()) {
      canLaunch = new CheckBoxElement("cif.canLaunch", entry.getCanLaunch());
      addFormElement("cif_canLaunch", canLaunch);
    } else { // launch not supported
      canLaunch = null;
      addFormElement("cif_canLaunch", new StaticHTMLTextElement("cif.canLaunch", translate("cif.canLaunch.na"), 255));
    }

    if (handler != null && handler.supportsDownload()) {
      canDownload = new CheckBoxElement("cif.canDownload", entry.getCanDownload());
      addFormElement("cif_canDownload", canDownload);
    } else { // download not supported
      canDownload = null;
      addFormElement("cif_canDownload", new StaticHTMLTextElement("cif.canDownload", translate("cif.canDownload.na"), 255));
    }
   
    if (enableAuthorView) {
      String[] keys = new String[] {
          "" + RepositoryEntry.ACC_OWNERS,
          "" + RepositoryEntry.ACC_OWNERS_AUTHORS,
          "" + RepositoryEntry.ACC_USERS,
          "" + RepositoryEntry.ACC_USERS_GUESTS
        };
      String[] values = new String[] {
          translate("cif.access.owners"),
          translate("cif.access.owners_authors"),
          translate("cif.access.users"),
          translate("cif.access.users_guests"),
        };
      access = new StaticSingleSelectionElement("cif.access", keys, values);
      access.select("" + entry.getAccess(), true);
      addFormElement("cif_access", access)
    }
   
    addSubmitKey("submit", "submit");
    setCancelButton();
  }

  /**
   * @see org.olat.core.gui.components.Form#validate(org.olat.core.gui.UserRequest)
   */
  public boolean validate() {
    boolean ok = resourceName.notEmpty("cif.error.resourcename.empty");
    ok = ok && displayName.notEmpty("cif.error.displayname.empty");
    ok = ok && description.notEmpty("cif.error.description.empty");
    ok = ok && description.notLongerThan(255, "input.toolong");

    return ok;
  }

  /**
   * @return Resource name filed.
   */
  public String getResourceName() {
    return resourceName.getValue();
  }

  /**
   * @return Display name field.
   */
  public String getDisplayName() {
    return displayName.getValue();
  }

  /**
   * @return Author field.
   */
  public String getAuthor() {
    return initialAuthor.getValue();
  }

  /**
   * @return Typoe field.
   */
  public String getType() {
    return type.getValue();
  }

  /**
   * @return Descritpion field.
   */
  public String getDescription() {
    return description.getValue().trim();
  }

  /**
   * Set entry and re-initialize the form based on data of this entry.
   * @param newEntry
   */
  public void setEntry(RepositoryEntry newEntry) {
    entry = newEntry;
    init();
  }
 
  /**
   * Update the given repository entry with data available from this form.
   * @param repositoryEntry
   */
  public void updateEntry(RepositoryEntry repositoryEntry) {
    String displayNameValue = getDisplayName();
    if (displayNameValue == null) displayNameValue = "";
    repositoryEntry.setDisplayname(displayNameValue);
    repositoryEntry.setDescription(getDescription());
    if (canCopy != null) repositoryEntry.setCanCopy(canCopy.isChecked());
    if (canReference != null) repositoryEntry.setCanReference(canReference.isChecked());
    if (canLaunch != null) repositoryEntry.setCanLaunch(canLaunch.isChecked());
    if (canDownload != null) repositoryEntry.setCanDownload(canDownload.isChecked());
    if (enableAuthorView) { // only changeable in authors view
      repositoryEntry.setAccess(Integer.parseInt(access.getSelectedKey()));
    }
  }

  /**
   * Get an updated repository entry with data from this form.
   * @return Updated repository entry.
   */
  public RepositoryEntry getUpdatedEntry() {
    String displayNameValue = getDisplayName();
    if (displayNameValue == null) displayNameValue = "";
    entry.setDisplayname(displayNameValue);
    entry.setDescription(getDescription());
    if (canCopy != null) entry.setCanCopy(canCopy.isChecked());
    if (canReference != null) entry.setCanReference(canReference.isChecked());
    if (canLaunch != null) entry.setCanLaunch(canLaunch.isChecked());
    if (canDownload != null) entry.setCanDownload(canDownload.isChecked());
    if (enableAuthorView) { // only changeable in authors view
      entry.setAccess(Integer.parseInt(access.getSelectedKey()));
    }
    return entry;
  }

}
TOP

Related Classes of org.olat.repository.DetailsForm

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.