/**
   * Gets a study object for a given name from a list of study objects.
   *
   * @param name the name of the study to look for.
   * @param studies the list of study objects.
   * @return the study object that matches the given name.
   */
  private StudyDef getStudy(String name, List<StudyDef> studies) {
    for (StudyDef study : studies) {
      if (study.getName().equals(name)) return study;
    }

    return null;
  }
  /**
   * 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);
    }
  }
  /**
   * Loads a study and its contents in this view.
   *
   * @param studyDef the study definition object.
   */
  public static void loadStudy(StudyDef studyDef) {

    TreeItem studyRoot =
        new CompositeTreeItem(new TreeItemWidget(images.note(), studyDef.getName(), popup));
    studyRoot.setUserObject(studyDef);
    tree.addItem(studyRoot);

    if (studyDef.getForms() != null) {
      for (FormDef def : studyDef.getForms()) loadForm(def, studyRoot, false);
    }

    Utilities.selectFirstItemOnTreeView(tree);
  }
  /**
   * Constructs a <code>StudyDef Tree item root</code> to bind other items to.
   *
   * @param xDef <code>StudyDef</code> for whom to create root.
   * @return Constructed <code>Tree Item.</code>
   */
  private static TreeItem constructTreeItem(StudyDef xDef) {
    TreeItem root = new CompositeTreeItem(new TreeItemWidget(images.note(), xDef.getName(), popup));
    if (root.getUserObject() != null) {
      if (!root.getUserObject().equals(xDef)) {
        root.setUserObject(xDef);
        tree.addItem(root);
      }
    } else {
      root.setUserObject(xDef);
      tree.addItem(root);
    }

    return root;
  }
  /**
   * 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);
    }
  }
  /**
   * Sets the Properties of the selected Item on the <tt>Tree View.</tt>
   *
   * @param item <tt>Tree Item</tt> to set properties for.
   * @param tree <tt>Tree View</tt> to set properties on.
   * @param popup <tt>Popup</tt> for the <tt>Tree View.</tt>
   */
  public static void changeEditableProperties(Object item, Tree tree, PopupPanel popup) {
    TreeItem treeItem = tree.getSelectedItem();
    if (item == null) return; // How can this happen?

    if (item instanceof StudyDef) {
      StudyDef studyDef = (StudyDef) item;
      treeItem.setWidget(new TreeItemWidget(images.note(), studyDef.getName(), popup));
      treeItem.setTitle(studyDef.getDescription());
      studyDef.setDirty(true);
    } else if (item instanceof FormDef) {
      FormDef formDef = (FormDef) item;
      treeItem.setWidget(new TreeItemWidget(images.drafts(), formDef.getName(), popup));
      treeItem.setTitle(formDef.getDescription());
      formDef.setDirty(true);
    } else if (item instanceof FormDefVersion) {
      FormDefVersion formDefVersion = (FormDefVersion) item;
      treeItem.setWidget(new TreeItemWidget(images.markRead(), formDefVersion.getName(), popup));
      treeItem.setTitle(formDefVersion.getDescription());
      formDefVersion.setDirty(true);
    }
  }