/* 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: SchlegelHelpFrame.java,v 1.6 2004/06/22 16:23:14 gawrilow Exp $
*/

package de.tuberlin.polymake.polytope;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import jv.object.PsConfig;

/**
 * This class is used to display the help of the interactive
 * Schlegel diagram application.
 *
 * @author Thilo Schr&ouml;der
 */
public class SchlegelHelpFrame extends Frame 
	implements ItemListener,  ActionListener {
    
    /** Area to display help text. */
    protected TextArea textArea = new TextArea();
    
    /** Button to close Help. */
    protected Button hideButton = new Button("Close");
    
    /** To choose between Javaview and Schlegel interactive help.*/
    protected Choice whichHelp = new Choice();
    
    /** Bottom Panel to display buttons. */
    protected Panel panel = new Panel();
    
    /** String holding the help text. */
    protected String text = new String();
    
    /** Panel holding the title of the help text. */
    protected Panel titlePanel = new Panel();
    
    /** Title of help. */
    protected Label titleLabel = new Label();

    /**
	 * Create a new frame to display the contents of
	 * the Help.txt file.
	 */
	public SchlegelHelpFrame() {
		super("SchegelInteractive and Javaview Help");
		setBounds(new Rectangle(30,30,450,500)); 
		addWindowListener( new WindowAdapter() {
			public void windowClosing( WindowEvent e ) {
			    setVisible(false);
			}
		});
		setLayout(new BorderLayout());
		textArea.setEditable(false);
	
		titlePanel.setLayout(new BorderLayout());
		titleLabel.setFont(new Font("Arial",Font.BOLD,15));
		titleLabel.setText("SchlegelInteractive Help");
		titlePanel.add(titleLabel,BorderLayout.CENTER);
	
		hideButton.setActionCommand("hide");
		hideButton.addActionListener(this);
		
		whichHelp.add("Schlegel interactive");
		whichHelp.add("Javaview");
		whichHelp.addItemListener(this);
	
		panel.setLayout(new BorderLayout());
		panel.add(hideButton, BorderLayout.EAST);
		panel.add(whichHelp,BorderLayout.WEST);
		try{
		    BufferedInputStream in = new BufferedInputStream(getClass().getResource("Help.txt").openStream());
		    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		    String line = new String();
		    while(( line = reader.readLine()) != null) text += line + "\n";
		} catch( IOException e ) {System.err.println(e.getMessage());}
		textArea.setText(text);
	
		add(titlePanel, BorderLayout.NORTH);
		add(textArea,BorderLayout.CENTER);
		add(panel,BorderLayout.SOUTH);
    }
    
    /** Process Button events. */
    public void actionPerformed(ActionEvent e) {
    	if(e.getActionCommand().equals("hide")) {
    		this.setVisible(false);
    	}
    }
    
    /** Process the changes of the Choice menu. */
	public void itemStateChanged( ItemEvent e ) {
		if(e.getItem().equals("Schlegel interactive")) {
			titleLabel.setText("SchlegelInteractive Help");
			textArea.setText(text);
		} else if (e.getItem().equals("Javaview")) {
			titleLabel.setText("Javaview Help (Keyboard Shortcuts)");
			textArea.setText(PsConfig.getMessage("28008"));//false,28008,"explanation"));
		}
	}

}
