/**
   * This method is required if the AlgorithmPerformed interface is implemented. It is called by the
   * algorithms when it has completed or failed to to complete, so that the dialog can be display
   * the result image and/or clean up.
   *
   * @param algorithm Algorithm that caused the event.
   */
  public void algorithmPerformed(AlgorithmBase algorithm) {

    if (algorithm instanceof AlgorithmNonlocalMeansFilter) {
      image.clearMask();

      if ((nlMeansFilterAlgo.isCompleted() == true) && (resultImage != null)) {

        updateFileInfo(image, resultImage);
        resultImage.clearMask();

        // The algorithm has completed and produced a new image to be displayed.
        try {
          new ViewJFrameImage(resultImage, null, new Dimension(610, 200));
        } catch (OutOfMemoryError error) {
          MipavUtil.displayError("Out of memory: unable to open new frame");
        }
      } else if (resultImage == null) {

        // These next lines set the titles in all frames where the source image is displayed to
        // image name so as to indicate that the image is now unlocked!
        // The image frames are enabled and then registed to the userinterface.
        Vector<ViewImageUpdateInterface> imageFrames = image.getImageFrameVector();

        for (int i = 0; i < imageFrames.size(); i++) {
          ((Frame) (imageFrames.elementAt(i))).setTitle(titles[i]);
          ((Frame) (imageFrames.elementAt(i))).setEnabled(true);

          if (((Frame) (imageFrames.elementAt(i))) != parentFrame) {
            userInterface.registerFrame((Frame) (imageFrames.elementAt(i)));
          }
        }

        if (parentFrame != null) {
          userInterface.registerFrame(parentFrame);
        }

        image.notifyImageDisplayListeners(null, true);
      } else if (resultImage != null) {

        // algorithm failed but result image still has garbage
        resultImage.disposeLocal(); // clean up memory
        resultImage = null;
      }
    }

    if (algorithm.isCompleted()) {
      insertScriptLine();
    }
    // save the completion status for later
    setComplete(algorithm.isCompleted());

    nlMeansFilterAlgo.finalize();
    nlMeansFilterAlgo = null;
    dispose();
  }