/**
  * Removes the passed file from the display.
  *
  * @param file The file to remove.
  */
 void removeAttachedFile(Object file) {
   if (file == null) return;
   FileAnnotationData fData = (FileAnnotationData) file;
   List<FileAnnotationData> attachments = getCurrentAttachmentsSelection();
   Iterator<FileAnnotationData> i = attachments.iterator();
   FileAnnotationData data;
   List<FileAnnotationData> toKeep = new ArrayList<FileAnnotationData>();
   while (i.hasNext()) {
     data = i.next();
     if (data.getId() != fData.getId()) toKeep.add(data);
   }
   if (filesDocList.size() > 0) {
     Iterator<DocComponent> j = filesDocList.iterator();
     DocComponent doc;
     Object fa;
     while (j.hasNext()) {
       doc = j.next();
       fa = doc.getData();
       if (fa instanceof FileAnnotationData) {
         data = (FileAnnotationData) fa;
         if (data.getId() <= 0 && !data.equals(file)) {
           toKeep.add(data);
         }
       }
     }
   }
   handleObjectsSelection(FileAnnotationData.class, toKeep, true);
 }
 /**
  * Handles the selection of objects via the selection wizard.
  *
  * @param type The type of objects to handle.
  * @param objects The objects to handle.
  * @param fire Pass <code>true</code> to notify, <code>false</code> otherwise.
  */
 void handleObjectsSelection(Class type, Collection objects, boolean fire) {
   if (objects == null) return;
   if (TagAnnotationData.class.equals(type)) {
     layoutTags(objects);
     List<Long> ids = new ArrayList<Long>();
     Iterator i = objects.iterator();
     TagAnnotationData tag;
     tagFlag = false;
     Collection tags = model.getTags();
     if (tags == null || tags.size() != objects.size()) {
       tagFlag = true;
     } else {
       while (i.hasNext()) {
         tag = (TagAnnotationData) i.next();
         if (tag != null && !model.isAnnotationUsedByUser(tag)) {
           ids.add(tag.getId());
         }
       }
       i = tags.iterator();
       while (i.hasNext()) {
         tag = (TagAnnotationData) i.next();
         if (tag != null && !ids.contains(tag.getId())) {
           tagFlag = true;
           break;
         }
       }
     }
   } else if (FileAnnotationData.class.equals(type)) {
     layoutAttachments(objects);
     List<Long> ids = new ArrayList<Long>();
     Iterator i = objects.iterator();
     FileAnnotationData data;
     docFlag = false;
     Collection attachments = model.getAttachments();
     if (attachments == null || attachments.size() != objects.size()) {
       docFlag = true;
     } else {
       while (i.hasNext()) {
         data = (FileAnnotationData) i.next();
         if (data != null) ids.add(data.getId());
       }
       i = attachments.iterator();
       while (i.hasNext()) {
         data = (FileAnnotationData) i.next();
         if (data != null && !ids.contains(data.getId())) {
           docFlag = true;
           break;
         }
       }
     }
   }
   buildGUI();
   if (fire)
     firePropertyChange(
         EditorControl.SAVE_PROPERTY, Boolean.valueOf(false), Boolean.valueOf(true));
 }
  /**
   * Starts an asynchronous call to save the passed file back to the server.
   *
   * @param file The file to save.
   * @param asynch Pass <code>true</code> to save asynchronously, <code>false</code> otherwise.
   */
  void fireFileSaving(File file, boolean asynch) {
    boolean fileIsExp = browser.isModelExperiment();
    int fileType = (fileIsExp ? FileSaver.EXPERIMENT : FileSaver.PROTOCOL);

    saveFile(file);
    FileAnnotationData data = null;
    DataObject linkTo = parent;
    if (fileAnnotation != null) data = fileAnnotation;
    else data = new FileAnnotationData(file);
    if (data.getId() > 0) linkTo = null;
    String description = CPEsummaryExport.export(browser.getTreeModel());
    if (description != null) data.setDescription(description);

    if (asynch) {
      currentLoader = new FileSaver(component, ctx, file, data, fileType, linkTo);
      currentLoader.load();
      state = Editor.SAVING;
    } else {
      OmeroMetadataService svc = EditorAgent.getRegistry().getMetadataService();
      try {
        svc.archivedFile(ctx, fileAnnotation, file, fileType, linkTo);
      } catch (Exception e) {
        LogMessage msg = new LogMessage();
        msg.print("State: " + state);
        msg.print("Cannot save file back to server");
        msg.print(e);
        EditorAgent.getRegistry().getLogger().error(this, msg);
      }
    }
  }
 /**
  * Returns the collection of attachments.
  *
  * @return See above.
  */
 List<FileAnnotationData> removeAttachedFiles() {
   List<FileAnnotationData> list = new ArrayList<FileAnnotationData>();
   if (filesDocList.size() == 0) {
     docFlag = false;
     return list;
   }
   List<FileAnnotationData> toKeep = new ArrayList<FileAnnotationData>();
   FileAnnotationData data;
   DocComponent doc;
   Object object;
   Iterator<DocComponent> i = filesDocList.iterator();
   while (i.hasNext()) {
     doc = i.next();
     object = doc.getData();
     if (doc.canUnlink()) {
       if (object instanceof FileAnnotationData) {
         data = (FileAnnotationData) object;
         if (data.getId() > 0) list.add(data);
       }
     } else {
       toKeep.add((FileAnnotationData) object);
     }
   }
   handleObjectsSelection(FileAnnotationData.class, toKeep, false);
   if (list.size() == 0) docFlag = false;
   return list;
 }
  /**
   * Attaches the passed file. Returns <code>true</code> if the file does not already exist, <code>
   * false</code> otherwise.
   *
   * @param files The files to attach.
   * @return See above
   */
  boolean attachFiles(File[] files) {
    List<FileAnnotationData> list = getCurrentAttachmentsSelection();
    DocComponent doc;
    List<File> toAdd = new ArrayList<File>();
    Object data = null;
    if (filesDocList.size() > 0) {
      Iterator<DocComponent> i = filesDocList.iterator();
      FileAnnotationData fa;
      while (i.hasNext()) {
        doc = i.next();
        data = doc.getData();
        if (data instanceof FileAnnotationData) {
          fa = (FileAnnotationData) data;
          /*
          for (int j = 0; j < files.length; j++) {
          	if (fa.getId() <= 0) {
          		if (!fa.getFilePath().equals(
          				files[j].getAbsolutePath()))
          			toAdd.add(files[j]);
          		list.add(fa);
          	} else {
          		if (fa.getFileName().equals(files[j].getName())) {
          			toReplace.add(fa);
          		} else toAdd.add(files[j]);
          	}
          }
          */
          for (int j = 0; j < files.length; j++) {
            if (fa.getId() >= 0 && fa.getFileName().equals(files[j].getName())) {
              toReplace.add(fa);
            }
          }
        }
      }
    }
    // if (data == null) {
    for (int i = 0; i < files.length; i++) {
      toAdd.add(files[i]);
    }
    // }
    if (toAdd.size() > 0) {
      data = null;
      try {
        docFlag = true;
        Iterator<File> j = toAdd.iterator();
        while (j.hasNext()) {
          list.add(new FileAnnotationData(j.next()));
        }

      } catch (Exception e) {
      }
      firePropertyChange(
          EditorControl.SAVE_PROPERTY, Boolean.valueOf(false), Boolean.valueOf(true));
    }
    layoutAttachments(list);
    return toAdd.size() > 0;
  }
  /**
   * Modifies the text of the component.
   *
   * @see ActivityComponent#notifyActivityEnd()
   */
  protected void notifyActivityEnd() {
    // Download the file.
    if (result instanceof FileAnnotationData) {
      FileAnnotationData data = (FileAnnotationData) result;
      String name = "";
      if (data.isLoaded()) name = data.getFileName();
      else name = "Annotation_" + data.getId();
      download("", result, new File(parameters.getFolder(), name));
    }

    type.setText(DESCRIPTION_CREATED + " " + parameters.getFolder().getName());
  }
 /**
  * Returns the list of attachments currently selected by the user.
  *
  * @return See above.
  */
 List<FileAnnotationData> getCurrentAttachmentsSelection() {
   List<FileAnnotationData> list = new ArrayList<FileAnnotationData>();
   if (filesDocList.size() == 0) return list;
   DocComponent doc;
   Object object;
   FileAnnotationData data;
   Iterator<DocComponent> i = filesDocList.iterator();
   while (i.hasNext()) {
     doc = i.next();
     object = doc.getData();
     if (object instanceof FileAnnotationData) {
       data = (FileAnnotationData) object;
       if (data.getId() > 0) list.add(data);
     }
   }
   return list;
 }
 /**
  * Gets a reference to the Original File if this file has been saved to the server. Otherwise
  * returns 0.
  *
  * @return See above
  */
 long getAnnotationId() {
   if (fileAnnotation == null) return annotationID;
   return fileAnnotation.getId();
 }
  /**
   * Returns the collection of annotation to remove.
   *
   * @see AnnotationUI#getAnnotationToRemove()
   */
  protected List<Object> getAnnotationToRemove() {
    List<Object> l = new ArrayList<Object>();
    if (selectedValue != initialValue && selectedValue == 0) {
      RatingAnnotationData rating = model.getUserRatingAnnotation();
      if (rating != null) l.add(rating);
    }
    List<Long> idsToKeep;
    DocComponent doc;
    long id;

    Object object;
    Iterator<DocComponent> i;
    Collection original;
    Iterator j;
    if (tagFlag && !model.isMultiSelection()) {
      idsToKeep = new ArrayList<Long>();

      TagAnnotationData tag;
      i = tagsDocList.iterator();
      while (i.hasNext()) {
        doc = i.next();
        object = doc.getData();
        if (object instanceof TagAnnotationData) {
          tag = (TagAnnotationData) object;
          id = tag.getId();
          if (id > 0) idsToKeep.add(id);
        }
      }

      original = model.getTags();
      j = original.iterator();
      while (j.hasNext()) {
        tag = (TagAnnotationData) j.next();
        id = tag.getId();
        if (!idsToKeep.contains(id)) // && model.isAnnotationToDelete(tag))
        l.add(tag);
      }
    }
    if (docFlag) {
      idsToKeep = new ArrayList<Long>();
      i = filesDocList.iterator();
      FileAnnotationData fa;
      while (i.hasNext()) {
        doc = i.next();
        object = doc.getData();
        if (object instanceof FileAnnotationData) {
          fa = (FileAnnotationData) object;
          id = fa.getId();
          if (id > 0) idsToKeep.add(id);
        }
      }
      original = model.getAttachments();
      j = original.iterator();
      while (j.hasNext()) {
        fa = (FileAnnotationData) j.next();
        id = fa.getId();
        if (!idsToKeep.contains(id)) //  && model.isAnnotationToDelete(fa))
        l.add(fa);
      }
    }
    if (model.hasBeenPublished()) {
      if (!publishedBox.isSelected()) {
        BooleanAnnotationData b = model.getPublishedAnnotation();
        if (b.getValue().booleanValue()) l.add(b);
      }
    }
    return l;
  }