 /* 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: SchlegelControl.java,v 1.7 2005/11/18 15:10:53 thilosch Exp $
 */

package de.tuberlin.polymake.polytope;

import java.awt.Frame;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.channels.Pipe;

import de.tuberlin.polymake.common.JavaviewControl;
import de.tuberlin.polymake.common.PointSet;
import de.tuberlin.polymake.common.SimpleGeometryParser;

/**
 * This class ???
 *
 * @author Thilo Schr&ouml;der
 */
public class SchlegelControl extends JavaviewControl {
	
	public SchlegelControl(
			BufferedReader psReader, 
			BufferedReader clientReader, 
			Pipe.SinkChannel sink) throws IOException{
		super(psReader,clientReader,sink);
		parser = new SimpleGeometryParser(new String[]{"zoom"});
		try {
			putMessage("read " + geom.getName() + "\n",true);
		} catch( IOException ex ) {
			ex.printStackTrace();
		}
	}
	
	public void update() throws IOException {
		PointSet newGeom = parser.parse(clientReader);
		synchronized(clientQueue){
			if(parser.getError() != null) {
				clientQueue.clear();
			} else {
				clientQueue.popFront();
			}
			if(!clientQueue.isEmpty()) {
				bbuf.flip();
				pipeSink.write(bbuf);
			}
		}
		if (!(frameMap.containsKey(newGeom.getName())) ||
				!(((SchlegelFrame)frameMap.get(newGeom.getName())).isDisplayable())) {
			SchlegelFrame tmpFrame = new SchlegelFrame(geom,parser.getParameters(),parser.getInteractiveParameters(),this);
			tmpFrame.update(newGeom, parser.getParameters());
			//			tmpFrame.addPickListener(this);
			frameMap.put(newGeom.getName(),tmpFrame);
			tmpFrame.setVisible(true);
		} else {
			SchlegelFrame tmpFrame = (SchlegelFrame)frameMap.get(newGeom.getName());
			if (tmpFrame.isVisible() || tmpFrame.getState() == Frame.ICONIFIED) {
				tmpFrame.setState(Frame.NORMAL);
			}
			tmpFrame.setVisible(true);
			if(parser.getError() != null) {
				tmpFrame.setStatus(parser.getError());
			}
			if(parser.getWarning() != null) {
				tmpFrame.setStatus(parser.getWarning());
			}
			tmpFrame.update(newGeom, parser.getParameters(), parser.getError() != null);
		}
		
	}

}
