gt:         if (images.size() > 0) {
context: /*
   *------------------------------------------------------------------------------
   *  Copyright (C) 2015 University of Dundee. All rights reserved.
   *
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation; either version 2 of the License, or
   *  (at your option) any later version.
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.
   *  
   *  You should have received a copy of the GNU General Public License along
   *  with this program; if not, write to the Free Software Foundation, Inc.,
   *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   *
   *------------------------------------------------------------------------------
   */
  package org.openmicroscopy.shoola.agents.treeviewer.util;
  
  //Java imports
  import ij.IJ;
  import ij.ImagePlus;
  import ij.WindowManager;
  
  import java.awt.BorderLayout;
  import java.awt.Container;
  import java.awt.FlowLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.ItemEvent;
  import java.awt.event.ItemListener;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  import javax.swing.Box;
  import javax.swing.BoxLayout;
  import javax.swing.ButtonGroup;
  import javax.swing.JButton;
  import javax.swing.JCheckBox;
  import javax.swing.JDialog;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JRadioButton;
  import javax.swing.JTextField;
  import javax.swing.event.ChangeEvent;
  import javax.swing.event.ChangeListener;
  
  import org.apache.commons.collections.CollectionUtils;
  import org.openmicroscopy.shoola.agents.events.treeviewer.SaveResultsEvent;
  import org.openmicroscopy.shoola.agents.treeviewer.TreeViewerAgent;
  import org.openmicroscopy.shoola.env.data.model.FileObject;
  import org.openmicroscopy.shoola.env.data.model.ResultsObject;
  import org.openmicroscopy.shoola.env.event.SaveEvent;
  import omero.gateway.SecurityContext;
  
  import org.openmicroscopy.shoola.util.CommonsLangUtils;
  
  import org.openmicroscopy.shoola.util.ui.MessageBox;
  import org.openmicroscopy.shoola.util.ui.UIUtilities;
  
  import pojos.ExperimenterData;
  
  
  /** 
   * Dialog used to save results from ImageJ.
   *
   * @author Jean-Marie Burel &nbsp;&nbsp;&nbsp;&nbsp;
   * <a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a>
   * @since 5.1.0
   */
  public class SaveResultsDialog
      extends JDialog
  {
  
      /** Select to save or not the ROI.*/
      private JCheckBox roi;
  
      /** Select to save or not the results table.*/
      private JCheckBox table;
      
      /** Flag indicating to select the image from the active window.*/
      private boolean activeWindow;
  
      /** Close the dialog.*/
      private JButton cancelButton;
  
      /** Close the dialog.*/
      private JButton saveButton;
  
      /** The field displayed the name to use of the measurement.*/
      private JTextField nameField;
  
      /** Initializes the components.
       * 
       * @param index The index indicating what to save.
       */
      private void initialize(int index)
      {
          activeWindow = true;
          cancelButton = new JButton("Cancel");
          cancelButton.addActionListener(new ActionListener() {
              
              @Override
              public void actionPerformed(ActionEvent e) {
                  cancel();
              }
          });
          saveButton = new JButton("Save");
          saveButton.addActionListener(new ActionListener() {
              
              @Override
              public void actionPerformed(ActionEvent e) {
                  save();
              }
          });
          ChangeListener l = new ChangeListener() {
              public void stateChanged(ChangeEvent changeEvent) {
                  saveButton.setEnabled(roi.isSelected() || table.isSelected());
                }
              };
          roi = new JCheckBox("ROI");
          roi.addChangeListener(l);
          table = new JCheckBox("Measurements");
          table.addChangeListener(l);
          nameField = new JTextField(15);
          if (index == SaveEvent.ALL) {
              roi.setSelected(true);
              table.setSelected(true);
          } else if (index == SaveEvent.ROIS) {
              roi.setSelected(true);
          } else if (index == SaveEvent.RESULTS) {
              table.setSelected(true);
          }
      }
  
      /** Closes the dialog.*/
      private void cancel()
      {
          setVisible(false);
          dispose();
      }
  
      /** Closes the dialog.*/
      private void save()
      {
          List<Object> images = new ArrayList<Object>();
          FileObject img;
          List<Object> toImport = new ArrayList<Object>();
          ImagePlus plus;
          if (activeWindow) {
              plus = WindowManager.getCurrentImage();
              if (plus != null) {
                  img = new FileObject(plus);
                  if (img.getOMEROID() < 0 || img.isNewImage()) {
                      toImport.add(img);
                    //check if there are associated files
                      int[] values = WindowManager.getIDList();
                      String path = img.getAbsolutePath();
                      if (path != null) {
                          FileObject ff;
                          for (int i = 0; i < values.length; i++) {
                              ff = new FileObject(WindowManager.getImage(values[i]));
                              if (path.equals(ff.getAbsolutePath())) {
                                  img.addAssociatedFile(ff);
                              }
                          }
                      }
                  } else images.add(img);
              }
          } else {
              int[] values = WindowManager.getIDList();
              if (values != null) {
                  List<String> paths = new ArrayList<String>();
                  FileObject ff;
                  for (int i = 0; i < values.length; i++) {
                      plus = WindowManager.getImage(values[i]);
                      img = new FileObject(plus);
                      if (img.getOMEROID() < 0 || img.isNewImage()) {
                          String path = img.getAbsolutePath();
                          if (!paths.contains(path)) {
                              paths.add(path);
                              //Check if the image has associated file
                              List<FileObject> l = img.getAssociatedFiles();
                              if (CollectionUtils.isEmpty(l)) {
                                  toImport.add(img);
                              }
                              for (int j = 0; j < values.length; j++) {
                                  ff = new FileObject(
                                          WindowManager.getImage(values[j]));
                                  if (path.equals(ff.getAbsolutePath())) {
                                      img.addAssociatedFile(ff);
                                  }
                              }
                          }
                      } else {
                          images.add(img);
                      }
                  }
              }
          }
          //Check if the images are OMERO images
          ResultsObject result;
          if (toImport.size() > 0) { //ask if they want to import the image
              StringBuffer buf = new StringBuffer();
              buf.append("Do you wish to import any selected images not already "
                      + CommonsLangUtils.LINE_SEPARATOR+
                      "saved in OMERO to the OMERO server?");
              MessageBox box = new MessageBox(this, "Import images", buf.toString());
              if (box.centerMsgBox() == MessageBox.YES_OPTION) {
                   result = new ResultsObject(toImport);
                   result.setROI(roi.isSelected());
                   result.setTable(table.isSelected());
                   result.setTableName(nameField.getText());
                   TreeViewerAgent.getRegistry().getEventBus().post(
                           new SaveResultsEvent(result, true));
              }
          }
  