// $Id: MainFrame.java,v 1.25 2004/06/19 09:32:10 m31 Exp $
//
// This file is part of the program suite "Principia Mathematica II"
// which is a working prototype for the main project:
// "Hilbert II" - http://www.qedeq.org
//
// Copyright 2001-2004  Michael Meyling <mime@qedeq.org>.
//
// "Principia Mathematica II" 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.

package com.meyling.principia.gui;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;

import com.meyling.principia.AlreadyRunningException;
import com.meyling.principia.PmiiFacade;
import com.meyling.principia.gui.jtabbedpane.PmiiTabbedPaneView;
import com.meyling.principia.gui.jtree.PmiiTreeCtrl;
import com.meyling.principia.gui.jtree.PmiiTreeModel;
import com.meyling.principia.gui.jtree.PmiiTreeView;
import com.meyling.principia.io.DataDictionary;
import com.meyling.principia.log.Trace;


/**
 * Main dialog of prototype.
 *
 * @version $Revision: 1.25 $
 * @author  Michael Meyling
 */
public final class MainFrame extends JFrame {


    /**
     * Starts the dialog.
     *
     * @param   args    not used yet
     */
    public static final void main(final String[] args) {
        MainFrame frame = new MainFrame();
    }


    /**
     * Constuctor.
     */
    public MainFrame() {

        super("Principia Mathematica II");
        try {
            // initialize resource bundle, e.g. for menu entries
            DataDictionary.init("com.meyling.principia.resources.pmii");
            
            PmiiTreeModel treeModel = new PmiiTreeModel();
            PmiiFacade.init(treeModel);

            ImageIcon icon = new ImageIcon(
                this.getClass().getResource("/com/meyling/principia/pic/title.png"));
            setIconImage(icon.getImage());
            final JSplitPane splitPane = new JSplitPane(JSplitPane
                .HORIZONTAL_SPLIT);
            final PmiiTreeView treeView = new PmiiTreeView(treeModel);

            final PmiiTabbedPaneView tabbedView = new PmiiTabbedPaneView(null);
            final PmiiController controller = new PmiiController(this);
            final PmiiTreeCtrl treeCtrl = new PmiiTreeCtrl(treeView, treeModel, tabbedView, 
                controller);
            controller.setTreeCtrl(treeCtrl);
            splitPane.setLeftComponent(treeView);
            splitPane.setRightComponent(tabbedView);
            // Status Bar
            final JTextField statusField = new JTextField("");
            statusField.setEditable(false);
            controller.setStatusField(statusField);

            this.setJMenuBar(new PmiiMenu(controller, this, tabbedView));

            this.getContentPane().add(splitPane);
            this.getContentPane().add(statusField, BorderLayout.SOUTH);

            this.addWindowListener( new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    PmiiFacade.shutdown();
                    System.exit(0);
                }
            });
            
            this.pack();                   // TODO necessary?
            this.setSize(780, 455);
            this.show();
            treeView.setSelectionRow(1);   // TODO necessary?
            PmiiFacade.getInstance().getModuleContext().start();

        } catch (Throwable e){
            Trace.trace(MainFrame.class, "MainFrame", e);
            JOptionPane.showMessageDialog(
                null,
                e.getMessage(),
                "Failure during start of " + AboutDialog.aboutTitle,
                JOptionPane.ERROR_MESSAGE,
                null
            );
            PmiiFacade.shutdown();
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }

}
