gt:         if (view != null) {
context: /*
   *------------------------------------------------------------------------------
   *  Copyright (C) 2006-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.imviewer;
  
  import java.awt.Rectangle;
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.List;
  
  import javax.swing.JComponent;
  import ome.model.units.BigResult;
  
  import org.apache.commons.collections.CollectionUtils;
  
  import org.openmicroscopy.shoola.agents.events.FocusGainedEvent;
  import org.openmicroscopy.shoola.agents.events.iviewer.CopyRndSettings;
  import org.openmicroscopy.shoola.agents.events.iviewer.ImageViewport;
  import org.openmicroscopy.shoola.agents.events.iviewer.MeasurementTool;
  import org.openmicroscopy.shoola.agents.events.iviewer.RendererUnloadedEvent;
  import org.openmicroscopy.shoola.agents.events.iviewer.RndSettingsCopied;
  import org.openmicroscopy.shoola.agents.events.iviewer.RndSettingsSaved;
  import org.openmicroscopy.shoola.agents.events.iviewer.SaveRelatedData;
  import org.openmicroscopy.shoola.agents.events.iviewer.ViewImage;
  import org.openmicroscopy.shoola.agents.events.iviewer.ViewImageObject;
  import org.openmicroscopy.shoola.agents.events.iviewer.ViewerState;
  import org.openmicroscopy.shoola.agents.events.measurement.MeasurementToolLoaded;
  import org.openmicroscopy.shoola.agents.events.measurement.SelectChannel;
  import org.openmicroscopy.shoola.agents.events.measurement.SelectPlane;
  import org.openmicroscopy.shoola.agents.events.metadata.ChannelSavedEvent;
  import org.openmicroscopy.shoola.agents.events.treeviewer.DeleteObjectEvent;
  import org.openmicroscopy.shoola.agents.events.treeviewer.DisplayModeEvent;
  import org.openmicroscopy.shoola.agents.imviewer.view.ImViewer;
  import org.openmicroscopy.shoola.agents.imviewer.view.ImViewerFactory;
  import org.openmicroscopy.shoola.env.Agent;
  import org.openmicroscopy.shoola.env.Environment;
  import org.openmicroscopy.shoola.env.LookupNames;
  import org.openmicroscopy.shoola.env.config.Registry;
  import org.openmicroscopy.shoola.env.data.events.ReconnectedEvent;
  import org.openmicroscopy.shoola.env.data.events.ReloadRenderingEngine;
  import org.openmicroscopy.shoola.env.data.events.UserGroupSwitched;
  import org.openmicroscopy.shoola.env.data.events.ViewInPluginEvent;
  import org.openmicroscopy.shoola.env.data.util.AgentSaveInfo;
  import omero.gateway.SecurityContext;
  import org.openmicroscopy.shoola.env.event.AgentEvent;
  import org.openmicroscopy.shoola.env.event.AgentEventListener;
  import org.openmicroscopy.shoola.env.event.EventBus;
  import org.openmicroscopy.shoola.env.ui.UserNotifier;
  import org.openmicroscopy.shoola.env.ui.ViewObjectEvent;
  
  import omero.gateway.model.ChannelData;
  import omero.gateway.model.DataObject;
  import omero.gateway.model.ExperimenterData;
  import omero.gateway.model.ImageData;
  import omero.gateway.model.PixelsData;
  import omero.gateway.model.WellSampleData;
  
  /** 
   * The ImViewer agent. This agent displays an <code>Image</code> and the 
   * controls to modify the rendering settings.
   *
   * @author  Jean-Marie Burel &nbsp;&nbsp;&nbsp;&nbsp;
   * 				<a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a>
   * @author	Andrea Falconi &nbsp;&nbsp;&nbsp;&nbsp;
   * 				<a href="mailto:a.falconi@dundee.ac.uk">a.falconi@dundee.ac.uk</a>
   * @author	Donald MacDonald &nbsp;&nbsp;&nbsp;&nbsp;
   * 				<a href="mailto:donald@lifesci.dundee.ac.uk">
   *              donald@lifesci.dundee.ac.uk</a>
   * @version 3.0
   * @since OME2.2
   */
  public class ImViewerAgent
      implements Agent, AgentEventListener
  {
  
      /** The default error message. */
      public static final String ERROR = " An error occurred while modifying " +
              "the rendering settings.";
  
      /** Reference to the registry. */
      private static Registry registry; 
  
      /** The display mode.*/
      private int displayMode = -1;
  
      /**
       * Helper method.
       *
       * @return A reference to the <code>Registry</code>.
       */
      public static Registry getRegistry() { return registry; }
  
      /**
       * Helper method returning the current user's details.
       *
       * @return See above.
       */
      public static ExperimenterData getUserDetails()
      { 
          return (ExperimenterData) registry.lookup(
                  LookupNames.CURRENT_USER_DETAILS);
      }
  
      /**
       * Helper method returning <code>true</code> if the connection is fast,
       * <code>false</code> otherwise.
       *
       * @return See above.
       */
      public static boolean isFastConnection()
      {
          int value = (Integer) registry.lookup(LookupNames.CONNECTION_SPEED);
          return value == ImViewer.UNCOMPRESSED;
      }
  
      /** 
       * Returns <code>true</code> if the application is used as a plugin,
       * <code>false</code> otherwise.
       * 
       * @return See above.
       */
      public static boolean isRunAsPlugin()
      {
          Environment env = (Environment) registry.lookup(LookupNames.ENV);
          if (env == null) return false;
          return env.runAsPlugin() > 0;
      }
  
      /**
       * Handles the {@link ViewImage} event.
       *
       * @param evt The event to handle.
       */
      private void handleViewImage(ViewImage evt)
      {
          if (evt == null) return;
          Boolean available = (Boolean)
                  registry.lookup(LookupNames.BINARY_AVAILABLE);
          if (available != null && !available.booleanValue()) return;
          List<ViewImageObject> images = evt.getImages();
          ViewImageObject object = images.get(0);
          DataObject image = object.getImage();
          Rectangle r = evt.getRequesterBounds();
          ImViewer view = null;
          boolean b = evt.isSeparateWindow();
          if (image != null) {
              if (evt.getPlugin() > 0) {
                  ViewInPluginEvent event = new ViewInPluginEvent(
                          evt.getSecurityContext(), image, evt.getPlugin());
                  EventBus bus = registry.getEventBus();
                  bus.post(event);
                  return;
              }
              PixelsData pixels = null;
              if (image instanceof ImageData) {
                  pixels = ((ImageData) image).getDefaultPixels();
              } else if (image instanceof WellSampleData) {
                  pixels = ((WellSampleData) image).getImage().getDefaultPixels();
              }
              if (pixels != null)
                  view = ImViewerFactory.getImageViewer(evt.getSecurityContext(),
                          image, r, b);
          } else {
              if (evt.getPlugin() > 0) {
                  ViewInPluginEvent event = new ViewInPluginEvent(
                          evt.getSecurityContext(),object.getImageID(),
                          evt.getPlugin());
                  EventBus bus = registry.getEventBus();
                  bus.post(event);
              } else {
                  view = ImViewerFactory.getImageViewer(evt.getSecurityContext(),
                          object.getImageID(), r, b);
              }
          }
  
          if (view != null) {
              view.activate(object.getSettings(), object.getSelectedUserID(),
                      displayMode);
              view.setContext(object.getParent(), object.getGrandParent());
          }
      }
      
      /**
       * Convenience method for logging BigResult exceptions
       * 
       * @param src
       *            The origin of the exception
       * @param exception
       *            The exception
       * @param property
       *            The property which conversion triggered the exception
       */
      public static void logBigResultExeption(Object src, Object exception,
              String property) {
          if (exception instanceof BigResult) {
              ImViewerAgent
                      .getRegistry()
                      .getLogger()
                      .warn(src,
                              "Arithmetic overflow; "
                                      + property
                                      + " is "
                                      + ((BigResult) exception).result
                                              .doubleValue());
          }
      }
  
      /**
       * Handles the {@link SaveRelatedData} event.
       *
       * @param evt The event to handle.
       */
      private void handleSaveRelatedData(SaveRelatedData evt)
      {
          if (evt == null) return;
          ImViewerFactory.storeEvent(evt);
      }
  
      /**
       * Handles the {@link MeasurementToolLoaded} event.
       *
       * @param evt The event to handle.
       */
      private void handleMeasurementToolLoaded(MeasurementToolLoaded evt)
      {
          if (evt == null) return;
          MeasurementTool request = (MeasurementTool) evt.getACT();
          PixelsData pixels = request.getPixels();
          if (pixels == null) return;
          long pixelsID = pixels.getId();
          //TODO: review
          ImViewer view = ImViewerFactory.getImageViewer(evt.getSecurityContext(),
                  pixelsID);
  