/* Reads image to be displayed through the input port
 * 
 * Copyright (c) 2010 FP7 EU EUFORIA (211804) & POZNAN SUPERCOMPUTING AND
 * NETWORKING CENTER All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without license
 * or royalty fees, to use, copy, modify, and distribute this software and its
 * documentation for any purpose, provided that the above copyright notice and
 * the following two paragraphs appear in all copies of this software.
 *
 * IN NO EVENT SHALL THE FP7 EU EUFORIA (211804) & POZNAN SUPERCOMPUTING AND
 * NETWORKING CENTER BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE
 * AND ITS DOCUMENTATION, EVEN IF THE FP7 EU EUFORIA (211804) & POZNAN
 * SUPERCOMPUTING AND NETWORKING CENTER HAS BEEN ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * THE FP7 EU EUFORIA (211804) & POZNAN SUPERCOMPUTING AND NETWORKING CENTER
 * SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE FP7 EU
 * EUFORIA (211804) & POZNAN SUPERCOMPUTING AND NETWORKING CENTER HAS NO
 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
 * MODIFICATIONS.
 *
*/

package pl.psnc.kepler.common.actor;

import java.awt.Image;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;

import pl.psnc.kepler.common.helper.Constants;
import ptolemy.actor.TypedIOPort;
import ptolemy.data.AWTImageToken;
import ptolemy.data.StringToken;
import ptolemy.kernel.CompositeEntity;
import ptolemy.kernel.util.IllegalActionException;
import ptolemy.kernel.util.NameDuplicationException;

//////////////////////////////////////////////////////////////////////////
////ImageReaderPort

/**
* Reads image from the input port and outputs Image object
*
*
* Actor accepts:
* 
* <p> Input ports
* <it>imagePort</it> - name of the input file containing image
* </p>
* 
* <p> Output ports
* <it>outputImagePort</it> - image to be displayed
* </p>
*
* <p> Parameters
* <it></it> - 
* </p>
*
* @author Michal Owsiak - michalo [at] man.poznan.pl
* @version $Id: ImageReaderPort.java 2289 2010-09-20 13:43:41Z tzok $
* @see KAR:ImageReaderPort.kar
*
*/

@SuppressWarnings("serial")
public class ImageReaderPort extends ErrorEnabledActor {

   /** Construct an actor with the given container and name.
    *  @param container The container.
    *  @param name The name of this actor.
    *  @exception IllegalActionException If the actor cannot be contained
    *   by the proposed container.
    *  @exception NameDuplicationException If the container already has an
    *   actor with this name.
    */
   public ImageReaderPort(CompositeEntity container, String name)
           throws IllegalActionException, NameDuplicationException {
       super(container, name);

       imagePort = new TypedIOPort(this, "imageFile", true, false);
       imagePort.setTypeEquals(ptolemy.data.type.BaseType.STRING);
       
       output.setTypeEquals(ptolemy.data.type.BaseType.OBJECT);

   }

   ///////////////////////////////////////////////////////////////////
   ////                         public variables                  ////
 	
   //revision value used for getting current class revision
   public static final String REVISION = "$Revision: 2289 $";

   ///////////////////////////////////////////////////////////////////
   ////                         ports and parameters              ////

   /** output port - image object */
   public TypedIOPort imagePort;

   ///////////////////////////////////////////////////////////////////
   ////                         public methods                    ////
   
   /**
    * Read image from the file and send it to the output
    */

   public void fire() throws IllegalActionException {
     super.fire();
     
     String imageString = null;
     if(imagePort.getWidth() != 0 && imagePort.hasToken(0)) {
       StringToken imagePortToken = (StringToken) imagePort.get(0);
       imageString = imagePortToken.stringValue();
     }
     
     if (imageString == null) {
       sendErrorCode(Constants.ERROR_CODE_FAILED);
       sendErrorMessage(Constants.ERROR_MESSAGE_FAILED + " image URL is null");
       
       output.broadcast(new AWTImageToken(new ImageIcon().getImage()));
     } else {
       try {
         _url = new URL(imageString);
         
         _fileRoot = _url.getFile();
         _image = new ImageIcon(_fileRoot).getImage();

         if ((_image.getWidth(null) == -1) && (_image.getHeight(null) == -1)) {
           sendErrorCode(Constants.ERROR_CODE_FAILED);
           sendErrorMessage(Constants.ERROR_MESSAGE_FAILED + " image size is (-1,-1); failed to open: " + _fileRoot);
           
           output.broadcast(new AWTImageToken(new ImageIcon().getImage()));
         } else {
           sendErrorCode(Constants.ERROR_CODE_OK);
           sendErrorMessage(Constants.ERROR_MESSAGE_OK);
           
           output.broadcast(new AWTImageToken(_image));
         }
         
       } catch (MalformedURLException e) {
         sendErrorCode(Constants.ERROR_CODE_EXCEPTION);
         sendErrorMessage(Constants.ERROR_MESSAGE_FAILED,e);
         
         output.broadcast(new AWTImageToken(new ImageIcon().getImage()));
       }
     }
   }

   ///////////////////////////////////////////////////////////////////
   ////                         private members                   ////
   
   // The URL as a string.
   private String _fileRoot;

   // Image that is read in.
   private Image _image;

   // The URL of the file.
   private URL _url;
}

