/**
   * 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);
    }
  }
  /**
   * Removes the given <tt>Form Def Tree Item</tt> from the Parent.
   *
   * @param item <tt>TreeItem</tt> to remove.
   * @param parent <tt>Parent</tt> of <tt>Tree Item.</tt>
   */
  public static void removeFormDefItem(TreeItem item, TreeItem parent) {
    Object userObj = item.getUserObject();
    Object parentUserObj = parent.getUserObject();

    if (userObj instanceof FormDefVersion) {
      ((FormDef) parentUserObj).removeVersion((FormDefVersion) userObj);
      ((FormDef) parentUserObj).setDirty(true);
      ((StudyDef) parent.getParentItem().getUserObject()).setDirty(true);
    } else if (userObj instanceof FormDef) {
      ((StudyDef) parentUserObj).removeForm((FormDef) userObj);
      ((StudyDef) parentUserObj).setDirty(true);
    }
  }
  /**
   * 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);
    }
  }