/* Copyright (c) 1997-2004
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de

   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, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   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.

   $Project: polymake $$Id: PolymakeFrame.java,v 1.6 2005/10/31 15:56:35 wotzlaw Exp $
*/

package de.tuberlin.polymake.common;

import java.awt.Frame;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Properties;

import jv.object.PsViewerIf;
import jv.project.PvDisplayIf;
import jv.viewer.PvControlMenu;
import jv.viewer.PvViewer;

/**
 * Frame used to display interactive polymake content using Javaview.
 * 
 * @author  Thilo Schr&ouml;der
 *			
 */
public abstract class PolymakeFrame extends Frame implements WindowListener {

    public static PvViewer polymakeViewer = new PvViewer();

    /** Contains non Javaview parameters for display */
    protected Properties parameters;

    /** Contains interactive parameters for display */
    protected Properties iparameters;

    /** Contains interactive sliders */
    protected HashMap sliderMap = new HashMap();

    /** Manager of the display and control panel. */
    //    protected PvViewer viewer;

    protected Label statusBar = null;

    /** Display embedded into this frame. */
    protected PvDisplayIf disp;

    /** Main geometry updated in intervals by external program. */
    protected EmbeddedGeometries geom;

    //protected PolymakeControl parent;
    
    protected JavaviewControl parent;

    protected ByteBuffer bbuf = ByteBuffer.allocate(32);

    /** title of Frame */
    protected String title = "NoName";

    /**
     * Create a frame displaying the given geometry with given parameters.
     * The output of any event is written into a pipe.
     *
     * @param i_geom   		geometry to be displayed
     * @param sink 			pipe for output
     *  @param params 		additional non Javaview parameters controlling display 
     */
    public PolymakeFrame(EmbeddedGeometries i_geom,
			 Properties params, Properties iparams, JavaviewControl parent) {

	super(i_geom.getName());

	title = i_geom.getName();
	
	statusBar = new Label();

	this.parent = parent;

	this.addWindowListener(this);
	
	geom = (EmbeddedGeometries)i_geom.clone();
	if(params != null) {
	    parameters = (Properties)params.clone();
	}

	if(iparams != null) {
	    iparameters = (Properties)iparams.clone();
	}
	
	this.setBounds(new Rectangle(30, 30, 500, 400));
	
	// Create viewer for viewing 3d geometries
	// polymakeViewer.init();
	polymakeViewer.showPanel(jv.object.PsViewerIf.DISPLAY);
	
	// Create viewer for viewing 3d geometries, and register applet.
	disp = polymakeViewer.newDisplay(geom.getName(),false);//new PvDisplay(viewer);
	disp.setFrame(this);
	for(int i = 0; i < geom.getNumberOfGeometries(); ++i) {
	    disp.addGeometry(geom.getGeometry(i));
	}
	disp.selectGeometry(geom.getGeometry(0));
	disp.fit();
	//	polymakeViewer.addDisplay(geom.getName(),disp);
	polymakeViewer.selectDisplay(disp);
	
	PvControlMenu jvMenuBar = polymakeViewer.newMenuBar(this);
	jvMenuBar.setEnabledMenu(PsViewerIf.MENU_FILE_NEW_PROJECT,false);
	jvMenuBar.setEnabledMenu(PsViewerIf.MENU_FILE_NEW_GEOMETRY,false);
	jvMenuBar.setEnabledMenu(PsViewerIf.MENU_METHOD,true);
	this.setMenuBar(jvMenuBar);
    }

    /** update the frame with new values for the vertices and parameters*/
    public abstract void update(PointSet ps, Properties params);

    /** setStatusBar */
    public void setStatus(String status) {
	statusBar.setText(status);
    }


    /** WindowListenerInterface */
    public void windowActivated(WindowEvent event) { }
    public void windowDeactivated(WindowEvent event) {}
    public void windowDeiconified(WindowEvent event) {}
    public void windowIconified(WindowEvent event) {}
    public void windowOpened(WindowEvent event) {}

    public void windowClosing(WindowEvent e) {
	this.dispose();
    }

    public void windowClosed(WindowEvent e) {
	try {
	    parent.removeFrame(title);
	    parent.putMessage(SimpleGeometryParser.write(geom.getName(),"close",1.),false);
	} catch(IOException ex) {
	    ex.printStackTrace();
	}
    }



}
