gt: 		if (t != null)
context: /*
   * org.openmicroscopy.shoola.util.roi.io.OutputServerStrategy
   *
   *------------------------------------------------------------------------------
   *  Copyright (C) 2006-2014 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.util.roi.io;
  
  //Java imports
  import java.awt.Color;
  import java.awt.geom.AffineTransform;
  import java.awt.geom.Point2D;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.LinkedList;
  import java.util.List;
  import java.util.TreeMap;
  
  //Third-party libraries
  import org.jhotdraw.draw.AttributeKeys;
  import org.jhotdraw.geom.BezierPath;
  
  //Application-internal dependencies
  import org.openmicroscopy.shoola.util.roi.ROIComponent;
  import org.openmicroscopy.shoola.util.roi.exception.ParsingException;
  import org.openmicroscopy.shoola.util.roi.figures.MeasureBezierFigure;
  import org.openmicroscopy.shoola.util.roi.figures.MeasureEllipseFigure;
  import org.openmicroscopy.shoola.util.roi.figures.MeasurePointFigure;
  import org.openmicroscopy.shoola.util.roi.figures.MeasureRectangleFigure;
  import org.openmicroscopy.shoola.util.roi.figures.MeasureLineFigure;
  import org.openmicroscopy.shoola.util.roi.figures.MeasureMaskFigure;
  import org.openmicroscopy.shoola.util.roi.figures.MeasureTextFigure;
  import org.openmicroscopy.shoola.util.roi.figures.ROIFigure;
  import org.openmicroscopy.shoola.util.roi.model.ROI;
  import org.openmicroscopy.shoola.util.roi.model.ROIShape;
  import org.openmicroscopy.shoola.util.roi.model.annotation.AnnotationKeys;
  import org.openmicroscopy.shoola.util.roi.model.annotation.MeasurementAttributes;
  import org.openmicroscopy.shoola.util.roi.model.util.Coord3D;
  import org.openmicroscopy.shoola.util.ui.UIUtilities;
  import pojos.EllipseData;
  import pojos.ImageData;
  import pojos.LineData;
  import pojos.MaskData;
  import pojos.PointData;
  import pojos.PolygonData;
  import pojos.PolylineData;
  import pojos.ROIData;
  import pojos.ShapeData;
  import pojos.ShapeSettingsData;
  import pojos.TextData;
  import pojos.RectangleData;
  import omero.model.LengthI;
  import omero.model.enums.UnitsLength;
  
  /**
   * Handles ROI from server.
   *
   * @author  Jean-Marie Burel &nbsp;&nbsp;&nbsp;&nbsp;
   * 	<a href="mailto:j.burel@dundee.ac.uk">j.burel@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
   * <small>
   * (<b>Internal version:</b> $Revision: $Date: $)
   * </small>
   * @since 3.0-Beta4
   */
  class OutputServerStrategy 
  {
  	
  	/** The ROIComponent to serialize. */
  	private ROIComponent component;
  	
  	/** The list of ROI to be supplied to the server. */
  	private List<ROIData>  ROIList;
  	
  	/**
  	 * Parses the ROI in the ROIComponent to create the appropriate ROIData 
  	 * object to supply to the server.
  	 * 
  	 * @param image The image the ROI is on.
  	 * @param index One of the constants defined by {@link ROIComponent} class.
  	 * @param userID The id of the user currently logged in.
  	 * @throws Exception 
  	 */
  	private void parseROI(ImageData image, int index, long userID) 
  		throws Exception
  	{
  		TreeMap<Long, ROI> map = component.getROIMap();
  		Iterator<ROI> i = map.values().iterator();
  		ROI roi;
  		switch (index) {
  			case ROIComponent.ANNOTATE:
  				while (i.hasNext())
  				{
  					roi = i.next();
  					if (roi.canAnnotate())
  						ROIList.add(createServerROI(roi, image));
  				}
  				break;
  			case ROIComponent.EDIT:
  				while (i.hasNext())
  				{
  					roi = i.next();
  					if (roi.canEdit())
  						ROIList.add(createServerROI(roi, image));
  				}
  				break;
  			case ROIComponent.DELETE:
  				while (i.hasNext())
  				{
  					roi = i.next();
  					if (roi.canDelete()) 
  						ROIList.add(createServerROI(roi, image));
  				}
  				break;
  			case ROIComponent.DELETE_MINE:
  				while (i.hasNext())
  				{
  					roi = i.next();
  					if (roi.canDelete()) {
  						if (roi.getOwnerID() < 0 || roi.getOwnerID() == userID)
  							ROIList.add(createServerROI(roi, image));
  					}
  				}
  				break;
  			case ROIComponent.DELETE_OTHERS:
  				while (i.hasNext())
  				{
  					roi = i.next();
  					if (roi.canDelete() && roi.getOwnerID() >= 0 &&
  						roi.getOwnerID() != userID)
  						ROIList.add(createServerROI(roi, image));
  				}
  				break;	
  			case ROIComponent.ALL:
  				while (i.hasNext())
  					ROIList.add(createServerROI(i.next(), image));
  		}
  	}
  
  	/**
  	 * Creates the Shape object for the ROIShape figure object.
  	 * @param clientShape See above.
  	 * @return See above.
  	 * @throws Exception If an error occurred while creating the shape.
  	 */
  	private ShapeData createShapeData(ROIShape clientShape) 
  		throws Exception
  	{
  		ROIFigure fig = clientShape.getFigure();
  		ShapeData shape = null;
  		if (fig instanceof MeasureBezierFigure)
  			shape = createBezierFigure(clientShape);
  		else if (fig instanceof MeasureEllipseFigure)
  			shape = createEllipseFigure(clientShape);
  		else if (fig instanceof MeasureLineFigure)
  			shape = createLineFigure(clientShape);
  		else if (fig instanceof MeasureMaskFigure)
  			shape = createMaskFigure(clientShape);
  		else if (fig instanceof MeasurePointFigure)
  			shape = createPointFigure(clientShape);
  		else if (fig instanceof MeasureRectangleFigure)
  			shape = createRectangleFigure(clientShape);
  		else if (fig instanceof MeasureTextFigure)
  			shape = createTextFigure(clientShape);
  		if (shape == null)
  			throw new Exception("ROIShape not supported : " + 
  									clientShape.getClass().toString());
  		if (clientShape.getT() >= 0)
  			shape.setT(clientShape.getT());
  		if (clientShape.getZ() >= 0)
  			shape.setZ(clientShape.getZ());
  		shape.setDirty(fig.isDirty());
  		if (!fig.isClientObject())
  			shape.setId(clientShape.getROIShapeID());
  		return shape;
  	}
  	
  	/**
  	 * Creates an ROIData object from an ROI. 
  	 * 
  	 * @param roi The ROI to handle.
  	 * @param image The image the ROI is on.
  	 * @return See above.
  	 * @throws Exception If an error occurred while parsing the ROI.
  	 */
  	private ROIData createServerROI(ROI roi, ImageData image) 
  		throws Exception
  	{
  		ROIData roiData = new ROIData();
  		String ns = (String) roi.getAnnotation(AnnotationKeys.NAMESPACE);
  		List<String> list = UIUtilities.CSVToList(
  				(String) roi.getAnnotation(AnnotationKeys.KEYWORDS));
  		String[] kw = new String[list.size()];
  		list.toArray(kw);
  		roiData.setNamespaceKeywords(ns, kw);
  		roiData.setClientSide(roi.isClientSide());
  		if (!roi.isClientSide())
  			roiData.setId(roi.getID());
  		roiData.setImage(image.asImage());
  		TreeMap<Coord3D, ROIShape> shapes =  roi.getShapes();
  		Iterator<ROIShape> shapeIterator = shapes.values().iterator();
  		ROIShape roiShape;
  		ShapeData shape;
  		while (shapeIterator.hasNext())
  		{
  			roiShape = shapeIterator.next();
  			shape = createShapeData(roiShape);
  			addShapeAttributes(roiShape.getFigure(), shape);
  			roiData.addShapeData(shape);
  		}
  		return roiData;
  	}
  	
  	/**
  	 * Creates a Bezier figure server side object from a MeasureBezierFigure 
  	 * client side object.
  	 * 
  	 * @param shape See above.
  	 * @return See above.
  	 * @throws ParsingException If an error occurred while parsing.
  	 */
  	private ShapeData createBezierFigure(ROIShape shape) 
  		throws ParsingException
  	{
  		MeasureBezierFigure fig = (MeasureBezierFigure) shape.getFigure();
  		if (fig.isClosed()) return createPolygonFigure(shape);
  		return createPolylineFigure(shape);
  	}
  	
  	/**
  	 * Creates an ellipse figure server side object from a MeasureEllipseFigure 
  	 * client side object.
  	 * 
  	 * @param shape See above.
  	 * @return See above.
  	 * @throws ParsingException If an error occurred while parsing.
  	 */
  	private EllipseData createEllipseFigure(ROIShape shape) 
  		throws ParsingException
  	{
  		MeasureEllipseFigure fig = (MeasureEllipseFigure) shape.getFigure();
  		double rx = fig.getEllipse().getWidth()/2d;
  		double ry = fig.getEllipse().getHeight()/2d;
  		double cx = fig.getEllipse().getCenterX();
  		double cy = fig.getEllipse().getCenterY();
  		
  		EllipseData ellipse = new EllipseData(cx, cy, rx, ry); 
  		ellipse.setVisible(fig.isVisible());
  		String text = fig.getText();
  		if (text != null && text.trim().length() > 0 && 
  				!text.equals(ROIFigure.DEFAULT_TEXT))
  			ellipse.setText(text);
  		AffineTransform t = AttributeKeys.TRANSFORM.get(fig);
  