/* gvSIG. Geographic Information System of the Valencian Government
*  osgVP. OSG Virtual Planets.
*
* Copyright (C) 2007-2008 Infrastructures and Transports Department
* of the Valencian Government (CIT)
*
* 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.
*
*/
/*
* AUTHORS (In addition to CIT):
* 2008 Instituto de Automática e Informática Industrial, UPV.
*/



#ifndef _AI2_LIBOSGPLANETS_JNICALLBACK_H_
#define _AI2_LIBOSGPLANETS_JNICALLBACK_H_


#include <osgvpplanets/Export.hpp>
#include <osg/ref_ptr>
#include <osg/Referenced>

#include <osg/Vec2>
#include <jni.h>
#include <vector>
#include <osg/Notify>

namespace osgvp {
	namespace planets {
		/** 
		 * Defines a JNI parameter.
		 * @author Rafael Gait�n <rgaitan@dsic.upv.es>
		 */
		class JNIParam: public osg::Referenced
		{
		public:
			virtual jobject operator()(JNIEnv *env)=0;
		protected:
			virtual ~JNIParam();
			JNIEnv *_env;
			jobject _obj;
		};

		/** 
		 * Defines a template type of JNI parameter.
		 * @author Rafael Gait�n <rgaitan@dsic.upv.es>
		 */
		template<class T>
		class JNIParamType:public JNIParam
		{
		public:
			typedef T value_type;
			JNIParamType(value_type value)
			{
				_value = value;
			}
			virtual jobject operator()(JNIEnv *env);
		protected:
			virtual ~JNIParamType() { }
			value_type _value;

		};

		/** 
		 * Defines a template type of JNI callback.
		 * @author Rafael Gait�n <rgaitan@dsic.upv.es>
		 */
		class JNICallback: public osg::Referenced
		{
		public:
			/// Constructor. Creates a new callback.
			JNICallback(JavaVM *vm, jobject cb, jmethodID method);

			/// Constructor. Creates a new callback like a copy of other one.
			JNICallback(const JNICallback &jnicallback);

			/// Execute the callback.
			void execute();

			/// Add a param to the callback.
			void addParam(JNIParam *param);

		protected:
			virtual ~JNICallback();
			/// Callback attributes.
			jobject _callback;
			jmethodID _method;
			JavaVM *_vm;
			std::vector< osg::ref_ptr<JNIParam> > _params;
		};
	} // libjosgplanets
} // ai2

#endif /* _AI2_LIBOSG_UTILS_JNICALLBACK_H_ */

