/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 1991-2005 OpenCFD Ltd.
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM 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.

    OpenFOAM 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.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM; if not, write to the Free Software Foundation,
    Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

\*---------------------------------------------------------------------------*/
package FoamX.Reporting;

import java.util.Hashtable;
import java.util.Enumeration;

import FoamX.CaseManagement.CaseManagerListener;
import FoamX.CaseManagement.CaseManagerEvent;

import FoamX.App;

public class ReportingManager
    extends javax.swing.JPanel
    implements CaseManagerListener
{
    //--------------------------------------------------------------------------

    protected ReportingWindow foamXWindow_;    // Reporting window for FoamX application.
    protected Hashtable windowMap_;      // Map of ReportingWindow objects.

    //--------------------------------------------------------------------------
    /** ReportingManager constructor. */
    public ReportingManager()
    {
        try
        {
            // Initialise the GUI components.
            initComponents();

            windowMap_ = new Hashtable();

            // Add FoamX reporting window.
            foamXWindow_ = new ReportingWindow();
            tabbedPanel_.addTab("FoamX", foamXWindow_);
            windowMap_.put("FoamX", foamXWindow_);
        }
        catch (Exception ex)
        {
            App.handleAllExceptions(ex);
        }
    }

    //--------------------------------------------------------------------------

    public void shutdown()
    {}

    //--------------------------------------------------------------------------

    public ReportingWindow getReportingWindow(String windowName)
    {
        ReportingWindow reportingWindow = null;

        if (windowMap_.containsKey(windowName))
        {
            // Return the reporting window
            reportingWindow = (ReportingWindow)windowMap_.get(windowName);
        }
        return reportingWindow;
    }

    //--------------------------------------------------------------------------
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        tabbedPanel_ = new javax.swing.JTabbedPane();
        
        setLayout(new java.awt.BorderLayout());
        
        tabbedPanel_.setTabPlacement(3);
        tabbedPanel_.setFont(new java.awt.Font("Dialog", 0, 10));
        add(tabbedPanel_, java.awt.BorderLayout.CENTER);
        
    }//GEN-END:initComponents

    //--------------------------------------------------------------------------

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane tabbedPanel_;
    // End of variables declaration//GEN-END:variables

    //--------------------------------------------------------------------------
    //---- CaseManagerListener Interface
    //--------------------------------------------------------------------------

    public void caseOpened(CaseManagerEvent evt)
    {
        try
        {
            // Make sure the event object has a case server reference.
            if (evt.caseServer() != null)
            {
                // See if we already have this case.
                String key = evt.caseRoot() + "/" + evt.caseName();
                if (!windowMap_.containsKey(key))
                {
                    // Create a new reporting window object.
                    ReportingWindow repWin = new ReportingWindow();

                    // Add new tab.
                    tabbedPanel_.addTab(evt.caseName(), repWin);
                    windowMap_.put(key, repWin);

                    // Select the case report window.
                    tabbedPanel_.setSelectedComponent(repWin);
                }
            }
        }
        catch (Exception ex)
        {
            App.handleAllExceptions(ex);
        }
    }

    //--------------------------------------------------------------------------

    public void casePostOpened(CaseManagerEvent evt)
    {
        try
        {
            // Make sure the event object has a case server reference.
            if (evt.casePostServer() != null)
            {
                // See if we already have this case.
                String key = evt.caseRoot() + "/" + evt.caseName();
                if (!windowMap_.containsKey(key))
                {
                    // Create a new reporting window object.
                    ReportingWindow repWin = new ReportingWindow();

                    // Add new tab.
                    tabbedPanel_.addTab(evt.caseName(), repWin);
                    windowMap_.put(key, repWin);

                    // Select the case report window.
                    tabbedPanel_.setSelectedComponent(repWin);
                }
            }
        }
        catch (Exception ex)
        {
            App.handleAllExceptions(ex);
        }
    }

    //--------------------------------------------------------------------------

    public void caseClosed(CaseManagerEvent evt)
    {
        try
        {
            // See if we have this case.
            String key = evt.caseRoot() + "/" + evt.caseName();
            if (windowMap_.containsKey(key))
            {
                // Get reporting window object for this case.
                ReportingWindow repWin = (ReportingWindow)windowMap_.get(key);

                // Select the main report window.
                tabbedPanel_.setSelectedComponent(foamXWindow_);

                // Remove tab.
                tabbedPanel_.remove(repWin);
                windowMap_.remove(key);
            }
        }
        catch (Exception ex)
        {
            App.handleAllExceptions(ex);
        }
    }

    //--------------------------------------------------------------------------

    public void casePostClosed(CaseManagerEvent evt)
    {
        try
        {
            // See if we have this case.
            String key = evt.caseRoot() + "/" + evt.caseName();
            if (windowMap_.containsKey(key))
            {
                // Get reporting window object for this case.
                ReportingWindow repWin = (ReportingWindow)windowMap_.get(key);

                // Select the main report window.
                tabbedPanel_.setSelectedComponent(foamXWindow_);

                // Remove tab.
                tabbedPanel_.remove(repWin);
                windowMap_.remove(key);
            }
        }
        catch (Exception ex)
        {
            App.handleAllExceptions(ex);
        }
    }

    //--------------------------------------------------------------------------

    public void caseDeleted(CaseManagerEvent evt)
    {
        try
        {
            // See if we have this case.
            String key = evt.caseRoot() + "/" + evt.caseName();
            if (windowMap_.containsKey(key))
            {
                // Get reporting window object for this case.
                ReportingWindow repWin = (ReportingWindow)windowMap_.get(key);

                // Select the main report window.
                tabbedPanel_.setSelectedComponent(foamXWindow_);

                // Remove tab.
                tabbedPanel_.remove(repWin);
                windowMap_.remove(key);
            }
        }
        catch (Exception ex)
        {
            App.handleAllExceptions(ex);
        }
    }

    //--------------------------------------------------------------------------

    public void casePanelSelected(CaseManagerEvent evt)
    {
        try
        {
            // See if we have this case.
            String key = evt.caseRoot() + "/" + evt.caseName();
            if (windowMap_.containsKey(key))
            {
                // Get reporting window object for this case.
                ReportingWindow repWin = (ReportingWindow)windowMap_.get(key);

                // Select the case tab.
                tabbedPanel_.setSelectedComponent(repWin);
            }
        }
        catch (Exception ex)
        {
            App.handleAllExceptions(ex);
        }
    }

    //--------------------------------------------------------------------------

    public void caseBrowserSelected(CaseManagerEvent evt)
    {
        try
        {
            // Select the main report window.
            tabbedPanel_.setSelectedComponent(foamXWindow_);
        }
        catch (Exception ex)
        {
            App.handleAllExceptions(ex);
        }
    }

    //--------------------------------------------------------------------------
    //--------------------------------------------------------------------------
    //--------------------------------------------------------------------------
    //--------------------------------------------------------------------------
    //--------------------------------------------------------------------------
    //--------------------------------------------------------------------------
    //--------------------------------------------------------------------------
}


