/*=========================================================================

  Program:   Orbit Visualization Tool
  Source:    $Source: /stor/devel/ovt2g/ovt/XYZWindow.java,v $
  Date:      $Date: 2001/04/24 15:23:58 $
  Version:   $Revision: 1.52 $


Copyright (c) 2000 OVT Team (Kristof Stasiewicz, Mykola Khotyaintsev, 
Yuri Khotyaintsev)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification is permitted provided that the following conditions are met:

 * No part of the software can be included in any commercial package without
written consent from the OVT team.

 * Redistributions of the source or binary code must retain the above
copyright notice, this list of conditions and the following disclaimer.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT OR
INDIRECT DAMAGES  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE.

OVT Team (http://ovt.irfu.se)   K. Stasiewicz, M. Khotyaintsev, Y.
Khotyaintsev

=========================================================================*/

package ovt;

//import ovt.*;
import ovt.gui.*;
import ovt.util.*;
import ovt.event.*;
import ovt.object.*;
import ovt.datatype.*;
import ovt.interfaces.*;

import vtk.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.IOException;

import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.event.*;



public class XYZWindow extends JFrame implements ActionListener, CoreSource {

  protected OVTCore core;
  protected SplashWindow splashWindow;

  // VTK stuff
  protected VisualizationPanel renPanel;
  protected vtkRenderer ren;
  // GUI
  public static StatusLine statusLine = new StatusLine();
  protected TreePanel treePanel;
  public boolean windowResizable = true;
  protected XYZMenuBar menuBar;
  protected JSplitPane splitPane;
  protected XYZToolBar toolBar;
  //protected HelpWindow helpWindow;
  protected HTMLBrowser htmlBrowser;
  
  private static final String SETTING_VISUALIZATION_PANEL_WIDTH = "VisualizationPanel.width";
  private static final String SETTING_VISUALIZATION_PANEL_HEIGHT = "VisualizationPanel.height";
  
  public XYZWindow() {
    super("Orbit Visualization Tool (version " + OVTCore.VERSION + ")");

    setIconImage (Toolkit.getDefaultToolkit().getImage(core.getImagesDir()+"ovt.gif"));

    //if(System.getProperty("os.name").equalsIgnoreCase("Windows 95")) windowResizable = false;

    // show splashscreen
    splashWindow = new SplashWindow();
    splashWindow.setVisible(true);

//------- create the vtkPanel ----------
    addNotify();
    renPanel = new VisualizationPanel(this);
    addOriginActor();

//------- create the OVTCore ----------
    //core = new OVTCore(renPanel.getRenderWindow(), renPanel.getRenderer());
    core = new OVTCore(this);

    int width = 400;
    int height = 400;
    try{
      width = new Integer(OVTCore.getGlobalSetting(SETTING_VISUALIZATION_PANEL_WIDTH)).intValue();
      height = new Integer(OVTCore.getGlobalSetting(SETTING_VISUALIZATION_PANEL_HEIGHT)).intValue();
    } catch(NumberFormatException ignore){  }
    
    renPanel.setSize(width, height);
    // avoid crush on some win 95 computers
    if (System.getProperty("os.name").equalsIgnoreCase("Windows 95")) setResizable(false);
    //renPanel.re
    // set the renderer
    ren = renPanel.getRenderer();
    float[] rgb = ovt.util.Utils.getRGB(core.getBackgroundColor());
    ren.SetBackground(rgb[0], rgb[1], rgb[2]);
    

    
//------- create the PolarWindow -------
    //polarWindow = new PolarWindow();


    //System.out.println("core done.");
    
//------- set up the menu -------------

    JPopupMenu.setDefaultLightWeightPopupEnabled(false);

    menuBar = new XYZMenuBar(getCore(), this);
    setJMenuBar(menuBar);

    Container contentPane = getContentPane();
    contentPane.setLayout(new BorderLayout());
  
//-------- create the tree panel -----
    treePanel = new TreePanel(getCore());
    Dimension ddd = new Dimension(200, height);
    treePanel.setPreferredSize(ddd);
    /*if (windowResizable) {
      treePanel.setMinimumSize(ddd);
      treePanel.setMaximumSize(ddd);
    }*/
  
//--------Create a split pane with the two scroll panes in it
    splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,windowResizable);

    splitPane.setLeftComponent(treePanel);
    splitPane.setRightComponent(renPanel);
    splitPane.setOneTouchExpandable(windowResizable);
    //splitPane.res
  //Provide minimum sizes for the two components in the split pane
    //Dimension minimumSize = new Dimension(130, 400);
    //renPanel.setMinimumSize(minimumSize);
    //treePanel.setMinimumSize(minimumSize);
    treePanel.setPreferredSize(new Dimension(200, height));

  //Set the initial location and size of the divider
  //splitPane.setDividerLocation(400);
    splitPane.setDividerSize(7);

  //Provide a preferred size for the split pane
  //splitPane.setPreferredSize(new Dimension(500, 400));
    // add a split pane
    contentPane.add(splitPane, BorderLayout.CENTER);
  
    // add main ToolBar
    toolBar = new XYZToolBar(getCore(), this);
    contentPane.add(toolBar, BorderLayout.SOUTH);
    
    // add Status Line
    //contentPane.add(statusLine, BorderLayout.NORTH);
    
    //  enableEvents(AWTEvent.WINDOW_EVENT_MASK);

        // create Help Window
    htmlBrowser = new HTMLBrowser(core);
    
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
          quit();
      }
    });
}

  public void start() {
    //refreshGUI();
    pack();
    Dimension scrnSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension windowSize = getSize();
    /*setLocation(scrnSize.width/2 - windowSize.width/2, 
                 scrnSize.height/2 - windowSize.height/2);*/
    setLocation(scrnSize.width/2 - windowSize.width/2, scrnSize.height/2 - windowSize.height/2);
    splashWindow.dispose();
    this.setResizable(windowResizable);
    setVisible(true);
    //polarWindow.setVisible(true);
    //getCore().start(); 
    
  }

  public OVTCore getCore()
  { return core; }

  public void actionPerformed(ActionEvent e) {
  }


  public void Render() { 
      renPanel.Render(); 
  }

  public vtkRenderer getRenderer() { 
    return renPanel.getRenderer(); 
  }
   
  public vtkRenderWindow getRenderWindow() { 
    return renPanel.getRenderWindow(); 
  }


  /** Is executed when the window closes */
public void quit() {
    
  try {
    getCore().saveSettings();
  } catch (IOException e2) {
    getCore().sendErrorMessage("Error Saving Settings", e2.getMessage());
  }
  // save VisualizationPanel's size
  Dimension d = renPanel.getSize();
  OVTCore.setGlobalSetting(SETTING_VISUALIZATION_PANEL_WIDTH, ""+d.width);
  OVTCore.setGlobalSetting(SETTING_VISUALIZATION_PANEL_HEIGHT, ""+d.height);
  try {
    OVTCore.saveGlobalSettings();
  } catch (IOException e2) {
    core.sendErrorMessage("Error Saving Settings", e2.getMessage());
  }
  System.exit(0);
}


  public static void setStatus(String statusMessage) {
    statusLine.setStatus(statusMessage);
  }

/**
 * Returns <code>true</code> if user pressed <code>Yes</code> 
 * else <code>false</code>.
 * @param none
 * @return see upp!
 * @see javax.swing.JOptionPane#showOptionDialog
 */

  private boolean quitConfirmed() {
        Object[] options = {"Yes", "No"};
        int n = JOptionPane.showOptionDialog(this,"Do you really want to exit?", "Exit", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,"No");
        if (n == JOptionPane.YES_OPTION) {
            return true;
        } else {
            return false;
        }
  }

  /** 
   * Main method. Here we launch OVT
   */

  public static void main(String[] arg) {
    XYZWindow XYZwin = new XYZWindow();
    XYZwin.start();
  }

  public HTMLBrowser getHTMLBrowser() {
    return htmlBrowser;
  }

  protected void addOriginActor() {
    vtkVectorText atext = new vtkVectorText();
        atext.SetText(". (0, 0, 0)");
    vtkPolyDataMapper mapper = new vtkPolyDataMapper();
        mapper.SetInput(atext.GetOutput());
    vtkFollower actor = new vtkFollower();
        actor.SetMapper(mapper);
        actor.SetScale(0.02);
        actor.AddPosition(0, 0, 0);
        actor.SetCamera(getRenderer().GetActiveCamera());
        actor.GetProperty().SetColor(0, 0, 0);
        getRenderer().AddActor(actor);
  }
  
  /* 
  public void setRenWinSize(int width, int height) {
    renPanel.setSize(width, height);
    treePanel.setSize((int)treePanel.getSize().width,height+50);
    pack();
  }
   */

  public boolean isWindowResizable() {
    return windowResizable;
  }

  public VisualizationPanel getVisualizationPanel() {
    return renPanel;
  }
  
}

class SplashWindow extends JWindow {
    JLabel imageLabel;
    public SplashWindow() {
        super();
        imageLabel = new JLabel(new ImageIcon(OVTCore.getImagesDir()+"splash.gif"));
            imageLabel.setBorder(BorderFactory.createRaisedBevelBorder());
            Dimension labelSize = imageLabel.getPreferredSize();
            imageLabel.setBounds(0, 0, labelSize.width, labelSize.height);
        
        JLabel label = new JLabel("Version " + OVTCore.VERSION + ", " + OVTCore.RELEASE_DAY );
            label.setFont(new Font("Arial", Font.PLAIN, 12));
            label.setForeground(Color.yellow);
            labelSize = label.getPreferredSize();
            label.setBounds(230, 255, labelSize.width, labelSize.height);
            
        JLabel copyrightLabel = new JLabel("Copyright (c) OVT Team, 2000-2001");
            copyrightLabel.setFont(new Font("Arial", Font.PLAIN, 10));
            copyrightLabel.setForeground(Color.white);
            labelSize = copyrightLabel.getPreferredSize();
            copyrightLabel.setBounds(
                (imageLabel.getPreferredSize().width - labelSize.width)/2,
                290, labelSize.width, labelSize.height);
        
        JLayeredPane layeredPane = new JLayeredPane();
            layeredPane.setPreferredSize(imageLabel.getPreferredSize());
            layeredPane.add(imageLabel, new Integer(0), 1);
            layeredPane.add(label, new Integer(1), 0);
            layeredPane.add(copyrightLabel, new Integer(2));
        
        setSize(imageLabel.getPreferredSize());
        getContentPane().add(layeredPane, BorderLayout.CENTER);
        //getContentPane().add(imageLabel, BorderLayout.CENTER);
        //getContentPane().add(new JLabel("Thank you, oh MAN!"), BorderLayout.NORTH);
        pack();
        
        // senter splash window
        Dimension scrnSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = getSize();
        setLocation(scrnSize.width/2 - windowSize.width/2,
                 scrnSize.height/2 - windowSize.height/2);
    }
    
/*
    public void paint(Graphics g) {
        imageLabel.paint(g);
        label.paint(g);
    }*/
    
}