/**
   * Adds a new Item according to selected item on the <tt>Tree View.</tt>
   *
   * <p>If a <Study</tt> is selected, a new <tt>Study</tt> will be added.
   *
   * <p>If a <tt>Form</tt> is selected, a new <tt>Form</tt> will be added.
   *
   * <p>If a <tt>Form Version</tt> is selected, a new <tt>Form Version</tt> will be added.
   */
  public static void addNewItem(String xForm, Tree tree, List<StudyDef> studies, PopupPanel popup) {

    TreeItem item = tree.getSelectedItem();
    if (item == null || item.getUserObject() instanceof StudyDef) {
      StudyDef studyDef = new StudyDef(0, "New Study" + (tree.getItemCount() + 1));
      studyDef.setCreator(Context.getAuthenticatedUser());
      studyDef.setDateCreated(new Date());
      studyDef.setDirty(true);

      TreeItem root =
          new CompositeTreeItem(new TreeItemWidget(images.note(), studyDef.getName(), popup));
      root.setUserObject(studyDef);
      tree.addItem(root);
      studies.add(studyDef);
      tree.setSelectedItem(root);

      // Automatically add a new form
      addNewChildItem(tree, studies, popup);

    } else if (item.getUserObject() instanceof FormDef) {
      TreeItem parent = item.getParentItem();
      FormDef formDef =
          new FormDef(
              0, "New Form" + (parent.getChildCount() + 1), (StudyDef) parent.getUserObject());
      formDef.setCreator(Context.getAuthenticatedUser());
      formDef.setDateCreated(new Date());
      formDef.setDirty(true);

      item = addImageItem(parent, formDef.getName(), images.drafts(), formDef, null, popup);
      ((StudyDef) parent.getUserObject()).addForm(formDef);
      tree.setSelectedItem(item);
      parent.setState(true);

      // Automatically add a new form version
      addNewChildItem(tree, studies, popup);
    } else if (item.getUserObject() instanceof FormDefVersion) {
      TreeItem parent = item.getParentItem();
      FormDefVersion formDefVersion =
          new FormDefVersion(
              0, "v" + (parent.getChildCount() + 1), (FormDef) parent.getUserObject());
      formDefVersion.setCreator(Context.getAuthenticatedUser());
      formDefVersion.setDateCreated(new Date());
      formDefVersion.getFormDef().turnOffOtherDefaults(formDefVersion);
      formDefVersion.setDirty(true);

      if (xForm != null) formDefVersion.setXform(xForm);

      item =
          addImageItem(
              parent, formDefVersion.getName(), images.markRead(), formDefVersion, null, popup);
      ((FormDef) parent.getUserObject()).addVersion(formDefVersion);
      tree.setSelectedItem(item);
      parent.setState(true);
    }
  }
  /**
   * Load all the returned <code>Studies</code>.
   *
   * @param studies <code>List</code> of <code>Studies</code> to load.
   */
  public static void loadAllStudies(List<StudyDef> studies) {

    if (studies == null) studies = Context.getStudies();

    for (StudyDef def : studies) {
      loadStudy(def);
    }
  }
  /**
   * Adds a Child Item to the selected item on the <tt>Tree View.</tt>
   *
   * <p>If a <tt>Study</tt> is selected, a <tt>Form</tt> will be added,
   *
   * <p>If a <tt>Form</tt> is selected, a <tt>Form Version</tt> will be added.
   *
   * <p>
   */
  public static void addNewChildItem(Tree tree, List<StudyDef> studies, PopupPanel popup) {
    String studyName = ""; // Name of study

    TreeItem item = tree.getSelectedItem();
    if (item == null) {
      addNewItem(
          null, tree, studies,
          popup); // Automatically add a new study/form/version to better usability
      return;
    }

    if (item.getUserObject() instanceof StudyDef) {
      studyName =
          ((StudyDef) item.getUserObject()).getName(); // set the value of the study from here

      FormDef formDef =
          new FormDef(
              0, studyName + " Form" + (item.getChildCount() + 1), (StudyDef) item.getUserObject());
      formDef.setCreator(Context.getAuthenticatedUser());
      formDef.setDateCreated(new Date());
      formDef.setDirty(true);

      item = addImageItem(item, formDef.getName(), images.drafts(), formDef, null, popup);
      ((StudyDef) item.getParentItem().getUserObject()).addForm(formDef);
      tree.setSelectedItem(item);
      item.getParentItem().setState(true);

      // Automatically add a new form version
      addNewChildItem(tree, studies, popup);
    } else if (item.getUserObject() instanceof FormDef) {
      FormDefVersion formDefVersion =
          new FormDefVersion(0, "v" + (item.getChildCount() + 1), (FormDef) item.getUserObject());
      formDefVersion.setCreator(Context.getAuthenticatedUser());
      formDefVersion.setDateCreated(new Date());
      formDefVersion.getFormDef().turnOffOtherDefaults(formDefVersion);
      formDefVersion.setDirty(true);

      item =
          addImageItem(
              item, formDefVersion.getName(), images.markRead(), formDefVersion, null, popup);
      ((FormDef) item.getParentItem().getUserObject()).addVersion(formDefVersion);
      tree.setSelectedItem(item);
      item.getParentItem().setState(true);
    } else addNewItem(null, tree, studies, popup);
  }
  /**
   * Bind <tt>StudyDef</tt> contents.
   *
   * @param tree <tt>Tree View</tt> to bind <tt>StudyDef</tt> to.
   * @param popup <tt>Popup</tt> to bind to the <tt>Tree View.</tt>
   * @param editable <tt>Editable</tt> we are checking contents for.
   */
  private static void setStudyContent(final Tree tree, final PopupPanel popup, Editable editable) {
    StudyDef studyDef = (StudyDef) editable;
    studyDef.setCreator(Context.getAuthenticatedUser());
    studyDef.setDateCreated(new Date());
    studyDef.setDirty(true);

    TreeItem root =
        new CompositeTreeItem(new TreeItemWidget(images.note(), studyDef.getName(), popup));
    root.setUserObject(studyDef);
    tree.addItem(root);
    Context.getStudies().add(studyDef);
    tree.setSelectedItem(root);

    List<FormDef> forms = studyDef.getForms();
    if (forms != null) {
      for (FormDef formDef : forms) importForm(tree, root, formDef, popup);
    }
  }
 /**
  * Loads the <code>User Mapped Forms.</code>
  * @param mappedForms <code>List of <code>Mapped Forms</code> to Load.
  */
 public static void loadMappedForms(User user, List<UserFormMap> mappedForms) {
   tree.clear();
   if (mappedForms != null && mappedForms.size() > 0) {
     List<UserFormMap> userMappedForms =
         RolesListUtil.getPermissionResolver().getUserMappedForms(user, mappedForms);
     for (UserFormMap map : userMappedForms) {
       for (FormDef xForm : Context.getForms()) {
         if (map.getFormId() == xForm.getFormId()) {
           bindMappedFormToTreeView(xForm);
         }
       }
     }
   }
 }
  /**
   * Imports a Form Version.
   *
   * @param tree
   * @param parent
   * @param formDefVersion
   * @param popup
   */
  public static void importFormVersion(
      Tree tree, TreeItem parent, FormDefVersion formDefVersion, PopupPanel popup) {
    formDefVersion.setFormDef((FormDef) parent.getUserObject());

    formDefVersion.setCreator(Context.getAuthenticatedUser());
    formDefVersion.setDateCreated(new Date());
    formDefVersion.setDirty(true);

    TreeItem item =
        addImageItem(
            parent, formDefVersion.getName(), images.markRead(), formDefVersion, null, popup);
    tree.setSelectedItem(item);
    item.getParentItem().setState(true);
  }
  /**
   * Imports a <tt>FormDef.</tt>
   *
   * @param tree <tt>Tree View</tt> we importing too.
   * @param parent <tt>FormDef</tt> parent. Should be a <tt>StudyDef.</tt>
   * @param formDef <tt>FormDef</tt> we are importing.
   * @param popup <tt>Popup</tt> for the <tt>Tree View.</tt>
   */
  public static void importForm(Tree tree, TreeItem parent, FormDef formDef, PopupPanel popup) {
    formDef.setStudy((StudyDef) parent.getUserObject());

    formDef.setCreator(Context.getAuthenticatedUser());
    formDef.setDateCreated(new Date());
    formDef.setDirty(true);

    TreeItem item = addImageItem(parent, formDef.getName(), images.drafts(), formDef, null, popup);
    tree.setSelectedItem(item);
    item.getParentItem().setState(true);

    TreeItem versionParent = item;
    List<FormDefVersion> versions = formDef.getVersions();
    if (versions != null) {
      for (FormDefVersion formDefVersion : versions)
        importFormVersion(tree, versionParent, formDefVersion, popup);
    }
  }
 /**
  * Fired when an option is selected on the <tt>FormVersionOpenDialogListener.</tt>
  *
  * @param option the selected option.
  * @param tree <tt>Tree View</tt> we checking items from.
  * @param item <tt>Tree Item</tt> we checking.
  * @param eventBus <tt>ItemSelectionListener</tt> for the data check operation.
  * @param popup <tt>Popup</tt> for the <tt>Tree View.</tt>
  */
 public static void onOptionSelected(
     int option, Tree tree, TreeItem item, EventBus eventBus, PopupPanel popup) {
   if (option == 0) { // Create new form verson
     if (item.getUserObject() instanceof FormDefVersion) {
       FormDefVersion copyVersion = (FormDefVersion) item.getUserObject();
       String xForm = copyVersion.getXform();
       addNewItem(xForm, tree, Context.getStudies(), popup);
     }
   } else if (option == 1) { // Open the dialog as read only
     final Object userObject = tree.getSelectedItem().getUserObject();
     // ((StudyView)eventBus).designItem(true, tree.getSelectedItem().getUserObject());
     if (userObject instanceof FormDefVersion)
       eventBus.fireEvent(new DesignFormEvent((FormDefVersion) userObject));
   } else if (option == 2) { // Cancel option
     return;
   } else {
     // ((StudyView)eventBus).designItem(false, tree.getSelectedItem().getUserObject());
     final Object userObject = tree.getSelectedItem().getUserObject();
     if (userObject instanceof FormDefVersion)
       eventBus.fireEvent(new DesignFormEvent((FormDefVersion) userObject, true));
   }
 }