/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  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.Modules.CaseEditor;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

import FoamX.App;
import FoamXServer.CaseServer.ICaseServer;

public class PatchEditorWindow
    extends javax.swing.JPanel
{
    //--------------------------------------------------------------------------

    private static final int DEFAULT_WIDTH = 400;

    protected EventListenerList listenerList_;
    protected PatchEditorModel patchModel_;
    protected String patchName_;

    //--------------------------------------------------------------------------
    /** Creates a new PatchEditorWindow object. */
    public PatchEditorWindow(ICaseServer caseServer, String patchName)
    {
        try
        {
            patchName_  = patchName;
            patchModel_ = new PatchEditorModel(caseServer, patchName);

            // Create listener list.
            listenerList_ = new EventListenerList();

            // Initialise the GUI.
            initComponents();

            // Resize to show the defined rows only.
            // Calculate size: table + panel + extra(=1 row height)
            int height =
                table_.getRowHeight() * (patchModel_.getRowCount() + 2)
                + (int)getMinimumSize().getHeight();  // panel size
 
            table_.setPreferredScrollableViewportSize
            (
                new java.awt.Dimension(DEFAULT_WIDTH, height)
            );

            // Initialise the boundary type edit box.
            patchPhysicalTypeEdit_.setText
            (
                patchModel_.getBoundaryDefinition().getDisplayName()
            );
        }
        catch (Exception ex)
        {
            App.handleAllExceptions(ex);
        }
    }

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

    public String toString()
    {
        // Used to set the title of the host internal frame window.
        return "Patch : " + patchName_;
    }

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

    public void addPatchStatusListener(PatchStatusListener l)
    {
        listenerList_.add(PatchStatusListener.class, l);
    }

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

    public void removePatchStatusListener(PatchStatusListener l)
    {
        listenerList_.remove(PatchStatusListener.class, l);
    }

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

    protected void firePatchPhysicalTypeChanged()
    {
        String patchPhysicalTypeName = patchModel_.getBoundaryDefinition().getName();

        // Create event object.
        PatchStatusEvent evt = new PatchStatusEvent(this, patchName_, patchPhysicalTypeName);

        // Process the listeners last to first, notifying those that are interested in this event.
        Object[] listeners = listenerList_.getListenerList();
        for (int i = listeners.length - 2; i>= 0; i -= 2)
        {
            if (listeners[i] == PatchStatusListener.class)
            {
                ((PatchStatusListener)listeners[i + 1]).patchPhysicalTypeChanged(evt);
            }
        }
    }

    //--------------------------------------------------------------------------
    /** 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
    {
        comboPanel_ = new javax.swing.JPanel();
        patchPhysicalTypeLabel_ = new javax.swing.JLabel();
        patchPhysicalTypeEdit_ = new javax.swing.JTextField();
        selectPatchPhysicalTypeButton_ = new javax.swing.JButton();
        scrollPane_ = new javax.swing.JScrollPane();
        table_ = new javax.swing.JTable();
        
        setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints1;
        
        setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(10, 10, 10, 10)));
        setFont(new java.awt.Font("Dialog", 0, 10));
        comboPanel_.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints2;
        
        comboPanel_.setFont(new java.awt.Font("Dialog", 0, 10));
        patchPhysicalTypeLabel_.setText("Boundary Type");
        patchPhysicalTypeLabel_.setForeground(java.awt.Color.black);
        patchPhysicalTypeLabel_.setFont(new java.awt.Font("Dialog", 0, 10));
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 0;
        gridBagConstraints2.gridy = 0;
        comboPanel_.add(patchPhysicalTypeLabel_, gridBagConstraints2);
        
        patchPhysicalTypeEdit_.setEditable(false);
        patchPhysicalTypeEdit_.setFont(new java.awt.Font("SansSerif", 0, 10));
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 1;
        gridBagConstraints2.gridy = 0;
        gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints2.ipady = 5;
        gridBagConstraints2.insets = new java.awt.Insets(0, 5, 0, 5);
        gridBagConstraints2.weightx = 1.0;
        comboPanel_.add(patchPhysicalTypeEdit_, gridBagConstraints2);
        
        selectPatchPhysicalTypeButton_.setFont(new java.awt.Font("Dialog", 0, 10));
        selectPatchPhysicalTypeButton_.setText("...");
        selectPatchPhysicalTypeButton_.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                OnSelectPatchPhysicalType(evt);
            }
        });
        
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 2;
        gridBagConstraints2.gridy = 0;
        comboPanel_.add(selectPatchPhysicalTypeButton_, gridBagConstraints2);
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 0;
        gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints1.insets = new java.awt.Insets(5, 5, 5, 5);
        gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTH;
        add(comboPanel_, gridBagConstraints1);
        
        table_.setModel(patchModel_);
        table_.setFont(new java.awt.Font("Dialog", 0, 10));
        scrollPane_.setViewportView(table_);
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 1;
        gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints1.insets = new java.awt.Insets(5, 5, 5, 5);
        gridBagConstraints1.weightx = 1.0;
        gridBagConstraints1.weighty = 1.0;
        add(scrollPane_, gridBagConstraints1);
        
    }//GEN-END:initComponents

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

  private void OnSelectPatchPhysicalType (java.awt.event.ActionEvent evt)
    {//GEN-FIRST:event_OnSelectPatchPhysicalType

        // Initialise and show the boundary type selector dialog.
        PatchPhysicalTypeSelectorDlg dlg = new PatchPhysicalTypeSelectorDlg(null, patchModel_);
        dlg.show();

        // If the dialog wasn't cancelled, update the boundary type name.
        if (!dlg.wasCancelled())
        {
            patchPhysicalTypeEdit_.setText(patchModel_.getBoundaryDefinition().getDisplayName());
            table_.updateUI();
            firePatchPhysicalTypeChanged();
        }
  }//GEN-LAST:event_OnSelectPatchPhysicalType

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

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JPanel comboPanel_;
  private javax.swing.JLabel patchPhysicalTypeLabel_;
  private javax.swing.JTextField patchPhysicalTypeEdit_;
  private javax.swing.JButton selectPatchPhysicalTypeButton_;
  private javax.swing.JScrollPane scrollPane_;
  private javax.swing.JTable table_;
  // End of variables declaration//GEN-END:variables

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




