/**
   * Handles events.
   *
   * @see ActionListener#actionPerformed(ActionEvent)
   */
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() instanceof ScriptMenuItem) {
      ScriptMenuItem item = (ScriptMenuItem) e.getSource();
      if (item.isScriptWithUI()) {
        switch (item.getIndex()) {
          case ScriptMenuItem.MOVIE_FIGURE_SCRIPT:
            model.createFigure(FigureDialog.MOVIE);
            break;
          case ScriptMenuItem.ROI_FIGURE_SCRIPT:
            model.createFigure(FigureDialog.SPLIT_ROI);
            break;
          case ScriptMenuItem.THUMBNAIL_FIGURE_SCRIPT:
            model.createFigure(FigureDialog.THUMBNAILS);
            break;
          case ScriptMenuItem.SPLIT_VIEW_FIGURE_SCRIPT:
            model.createFigure(FigureDialog.SPLIT);
            break;
          case ScriptMenuItem.MOVIE_EXPORT_SCRIPT:
            view.makeMovie(-1, null);
            break;
          case ScriptMenuItem.FLIM_SCRIPT:
            openFLIM();
        }
      } else {
        ScriptObject object = item.getScript();
        if (!object.isParametersLoaded()) model.loadScript(object.getScriptID());
        else model.setScript(object);
      }
      return;
    }

    int index = Integer.parseInt(e.getActionCommand());

    switch (index) {
      case ADD_LOCAL_DOCS:
        selectFileToAttach();
        break;
      case ADD_UPLOADED_DOCS:
        model.loadExistingAttachments();
        break;
      case SAVE:
        view.saveData(true);
        break;
      case DOWNLOAD:
        download();
        break;
      case ADD_TAGS:
        loadExistingTags();
        break;
      case CREATE_NEW_EXPERIMENT:
        view.createNewExperiment();
        break;
      case CREATE_MOVIE:
        view.makeMovie(-1, null);
        break;
      case RENDERER:
        model.loadRenderingControl(RenderingControlLoader.LOAD);
        break;
      case ANALYSE_FLIM:
        view.analyse(AnalysisParam.FLIM);
        break;
      case ANALYSE_FRAP:
        view.analyse(AnalysisParam.FRAP);
        break;
      case REFRESH:
        model.refresh();
        break;
      case EXPORT_AS_OMETIFF:
        export();
        break;
      case SPLIT_VIEW_FIGURE:
        model.createFigure(FigureDialog.SPLIT);
        break;
      case SPLIT_VIEW_ROI_FIGURE:
        model.createFigure(FigureDialog.SPLIT_ROI);
        break;
      case THUMBNAILS_FIGURE:
        model.createFigure(FigureDialog.THUMBNAILS);
        break;
      case MOVIE_FIGURE:
        model.createFigure(FigureDialog.MOVIE);
        break;
      case UPLOAD_SCRIPT:
        view.uploadScript();
        break;
      case RELOAD_SCRIPT:
        view.reloadScript();
        break;
        /*
        case REMOVE_TAGS:
        	view.removeTags();
        	break;
        case REMOVE_DOCS:
        	view.removeAttachedFiles();
        	break;
        	*/
      case SAVE_AS:
        saveAsJPEG();
        break;
      case VIEW_IMAGE:
        Object refObject = view.getRefObject();
        ImageData img = null;
        if (refObject instanceof ImageData) {
          img = (ImageData) refObject;
        } else if (refObject instanceof WellSampleData) {
          img = ((WellSampleData) refObject).getImage();
        }
        if (img != null) {
          ViewImageObject vio = new ViewImageObject(img);
          MetadataViewerAgent.getRegistry()
              .getEventBus()
              .post(new ViewImage(model.getSecurityContext(), vio, null));
        }
        break;
      case VIEW_IMAGE_IN_IJ:
        Object object = view.getRefObject();
        ImageData image = null;
        if (object instanceof ImageData) {
          image = (ImageData) object;
        } else if (object instanceof WellSampleData) {
          image = ((WellSampleData) object).getImage();
        }
        if (image != null) {
          ViewInPluginEvent event =
              new ViewInPluginEvent(
                  model.getSecurityContext(), (DataObject) object, MetadataViewer.IMAGE_J);
          MetadataViewerAgent.getRegistry().getEventBus().post(event);
        }
    }
  }
 /**
  * Reacts to property change.
  *
  * @see PropertyChangeListener#propertyChange(PropertyChangeEvent)
  */
 public void propertyChange(PropertyChangeEvent evt) {
   String name = evt.getPropertyName();
   if (SAVE_PROPERTY.equals(name)
       || DataComponent.DATA_MODIFIED_PROPERTY.equals(name)
       || PreviewPanel.PREVIEW_EDITED_PROPERTY.equals(name)) {
     view.setDataToSave(view.hasDataToSave());
   } else if (MetadataViewer.SAVE_DATA_PROPERTY.equals(name)) {
     Boolean b = (Boolean) evt.getNewValue();
     view.saveData(b.booleanValue());
   } else if (MetadataViewer.CLEAR_SAVE_DATA_PROPERTY.equals(name)
       || MetadataViewer.ON_DATA_SAVE_PROPERTY.equals(name)
       || MetadataViewer.ADMIN_UPDATED_PROPERTY.equals(name)) {
     view.clearData();
   } else if (UIUtilities.COLLAPSED_PROPERTY_JXTASKPANE.equals(name)) {
     view.handleTaskPaneCollapsed((JXTaskPane) evt.getSource());
   } else if (FileChooser.APPROVE_SELECTION_PROPERTY.equals(name)) {
     File[] files = (File[]) evt.getNewValue();
     view.attachFiles(files);
   } else if (AnnotationUI.REMOVE_ANNOTATION_PROPERTY.equals(name)) {
     Object object = evt.getNewValue();
     if (object instanceof DocComponent) {
       DocComponent doc = (DocComponent) object;
       Object data = doc.getData();
       if (data instanceof File) view.removeAttachedFile(data);
       else if (data instanceof FileAnnotationData) view.removeAttachedFile(data);
       else if (data instanceof TagAnnotationData) view.removeObject((DataObject) data);
     }
   } else if (AnnotationUI.DELETE_ANNOTATION_PROPERTY.equals(name)) {
     Object object = evt.getNewValue();
     if (object instanceof DocComponent) {
       DocComponent doc = (DocComponent) object;
       Object data = doc.getData();
       if (data instanceof FileAnnotationData) {
         view.deleteAnnotation((FileAnnotationData) data);
         view.removeAttachedFile(data);
       }
     } else if (object instanceof TextualAnnotationComponent) {
       TextualAnnotationComponent doc = (TextualAnnotationComponent) object;
       TextualAnnotationData data = doc.getData();
       // view.deleteAnnotation((TextualAnnotationData) data);
       view.removeObject(data);
     }
   } else if (AnnotationUI.EDIT_TAG_PROPERTY.equals(name)) {
     Object object = evt.getNewValue();
     if (object instanceof DocComponent) {
       // Save the tag w/o update.
       DataObject d = (DataObject) ((DocComponent) object).getData();
       // Save the tag
       OmeroMetadataService svc = MetadataViewerAgent.getRegistry().getMetadataService();
       long id = MetadataViewerAgent.getUserDetails().getId();
       try {
         svc.saveData(model.getSecurityContext(), Arrays.asList(d), null, null, id);
       } catch (Exception e) {
         Logger l = MetadataViewerAgent.getRegistry().getLogger();
         LogMessage msg = new LogMessage();
         msg.print("Saving object");
         msg.print(e);
         l.error(this, msg);
       }
     }
   } else if (OMEWikiComponent.WIKI_DATA_OBJECT_PROPERTY.equals(name)) {
     WikiDataObject object = (WikiDataObject) evt.getNewValue();
     long id;
     switch (object.getIndex()) {
       case WikiDataObject.IMAGE:
         id = object.getId();
         if (id < 0) viewImage(object.getName());
         else viewImage(id);
         break;
       case WikiDataObject.PROTOCOL:
         viewProtocol(object.getId());
         break;
     }
   } else if (SelectionWizard.SELECTED_ITEMS_PROPERTY.equals(name)) {
     Map m = (Map) evt.getNewValue();
     if (m == null || m.size() != 1) return;
     Set set = m.entrySet();
     Entry entry;
     Iterator i = set.iterator();
     Class type;
     while (i.hasNext()) {
       entry = (Entry) i.next();
       type = (Class) entry.getKey();
       view.handleObjectsSelection(type, (Collection) entry.getValue());
     }
   } else if (PreviewPanel.OPEN_FILE_PROPERTY.equals(name)) {
     Long id = (Long) evt.getNewValue();
     if (id != null) viewProtocol(id.longValue());
   } else if (MetadataViewer.SETTINGS_APPLIED_PROPERTY.equals(name)) {
     model.loadRenderingControl(RenderingControlLoader.RELOAD);
     view.onSettingsApplied(true);
   } else if (MetadataViewer.ACTIVITY_OPTIONS_PROPERTY.equals(name)) {
     List l = (List) evt.getNewValue();
     view.activityOptions((Component) l.get(0), (Point) l.get(1), (Integer) l.get(2));
   } else if (FigureDialog.CREATE_FIGURE_PROPERTY.equals(name)) {
     view.createFigure(evt.getNewValue());
   } else if (FigureDialog.CLOSE_FIGURE_PROPERTY.equals(name)) {
     figureDialog = null;
   } else if (MetadataViewer.CLOSE_RENDERER_PROPERTY.equals(name)) {
     view.discardRenderer(evt.getNewValue());
   } else if (MetadataViewer.RELATED_NODES_PROPERTY.equals(name)) {
     view.onRelatedNodesSet();
   } else if (ScriptingDialog.RUN_SELECTED_SCRIPT_PROPERTY.equals(name)) {
     // view.manageScript((ScriptObject) evt.getNewValue(),
     //		MetadataViewer.RUN);
   } else if (ScriptingDialog.DOWNLOAD_SELECTED_SCRIPT_PROPERTY.equals(name)) {
     Object value = evt.getNewValue();
     if (value instanceof ScriptObject)
       view.manageScript((ScriptObject) value, MetadataViewer.DOWNLOAD);
     else if (value instanceof String) {
       ScriptObject script = view.getScriptFromName((String) value);
       if (script != null) view.manageScript(script, MetadataViewer.DOWNLOAD);
     }
   } else if (ScriptingDialog.VIEW_SELECTED_SCRIPT_PROPERTY.equals(name)) {
     Object value = evt.getNewValue();
     if (value instanceof ScriptObject)
       view.manageScript((ScriptObject) value, MetadataViewer.VIEW);
     else if (value instanceof String) {
       ScriptObject script = view.getScriptFromName((String) value);
       if (script != null) view.manageScript(script, MetadataViewer.VIEW);
     }
   } else if (AnalysisResultsItem.ANALYSIS_RESULTS_DELETE.equals(name)) {
     AnalysisResultsItem item = (AnalysisResultsItem) evt.getNewValue();
     List<FileAnnotationData> list = item.getAttachments();
     view.fireAnnotationsDeletion(list);
   } else if (AnalysisResultsItem.ANALYSIS_RESULTS_VIEW.equals(name)) {
     AnalysisResultsItem item = (AnalysisResultsItem) evt.getNewValue();
     if (view.getRndIndex() == MetadataViewer.RND_GENERAL) {
       model.displayAnalysisResults(item);
       /*
       ViewImage event = new ViewImage(item.getData(), null);
       event.setAnalysis(item);
       EventBus bus = MetadataViewerAgent.getRegistry().getEventBus();
       bus.post(event);
       */
     } else {
       model.displayAnalysisResults(item);
     }
   } else if (AnalysisResultsItem.ANALYSIS_RESULTS_CANCEL.equals(name)) {
     AnalysisResultsItem item = (AnalysisResultsItem) evt.getNewValue();
     view.cancelAnalysisResultsLoading(item);
   }
 }
 /**
  * Posts an event to view the image.
  *
  * @param imageID The id of the image to view.
  */
 private void viewImage(long imageID) {
   EventBus bus = MetadataViewerAgent.getRegistry().getEventBus();
   bus.post(new ViewImage(model.getSecurityContext(), new ViewImageObject(imageID), null));
 }
 /**
  * Posts an event to view the protocol.
  *
  * @param protocolID The id of the protocol to view.
  */
 private void viewProtocol(long protocolID) {
   EventBus bus = MetadataViewerAgent.getRegistry().getEventBus();
   bus.post(new EditFileEvent(model.getSecurityContext(), protocolID));
 }
 /**
  * Posts an event to view the image.
  *
  * @param imageID The id of the image to view.
  */
 private void viewImage(long imageID) {
   EventBus bus = MetadataViewerAgent.getRegistry().getEventBus();
   ViewImage evt = new ViewImage(model.getSecurityContext(), new ViewImageObject(imageID), null);
   evt.setPlugin(MetadataViewerAgent.runAsPlugin());
   bus.post(evt);
 }