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

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

import FoamX.App;
import FoamX.Editors.DictionaryEntryEditor.DictionaryEntry;
import FoamX.Editors.DictionaryEntryEditor.DictionaryEntryCellEditor;
import FoamX.Editors.DictionaryEntryEditor.DictionaryEntryCellRenderer;
import FoamX.Editors.DictionaryEntryEditor.DictionaryNameCellRenderer;
import FoamX.Editors.DictionaryEntryEditor.DictionaryEntryTableModel;
import FoamX.Editors.TypeEditor.TypeDescriptorCache;

import FoamXServer.FoamXError;
import FoamXServer.FoamXIOError;
import FoamXServer.IDictionaryEntry;

public class FixedListEditor
    extends javax.swing.JDialog
{
    //--------------------------------------------------------------------------

    private static final int DEFAULT_WIDTH = 400;

    private VectorTableModel tableModel_;

    //--------------------------------------------------------------------------
    /** FixedListEditor constructor. */
    public FixedListEditor
    (
        java.awt.Frame parent,
        IDictionaryEntry entry,
        TypeDescriptorCache typeDescriptor
    )
    {
        super(parent, "Vector Editor", true);     // Modal.

        // Initialise the table model.
        tableModel_ = new VectorTableModel(entry, typeDescriptor);

        initComponents();

        // Use 'standard' renderer for name
        elementTable_.getColumn
        (
            tableModel_.getNameColumnTitle()
        ).setCellRenderer(new DictionaryNameCellRenderer());

        // Set cell renderer and editor for value column.
        elementTable_.getColumn
        (
            tableModel_.getValueColumnTitle()
        ).setCellRenderer(new DictionaryEntryCellRenderer());
        elementTable_.getColumn
        (
            tableModel_.getValueColumnTitle()
        ).setCellEditor(new DictionaryEntryCellEditor());

        // Resize to show the defined rows only.
        // Calculate size: table + some extra (= 1 row height) or
        //                 space needed for panel itself (so with empty table)
        int height =
            elementTable_.getRowHeight() * (tableModel_.getRowCount() + 2)
            + (int)getMinimumSize().getHeight();

        setSize(new java.awt.Dimension(DEFAULT_WIDTH, height));
    }

    //--------------------------------------------------------------------------
    /** 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
    {
        tablePanel_ = new javax.swing.JPanel();
        tableScrollPane_ = new javax.swing.JScrollPane();
        elementTable_ = new javax.swing.JTable();
        buttonPanel_ = new javax.swing.JPanel();
        btnOK_ = new javax.swing.JButton();
        
        getContentPane().setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints1;
        
        setName("Vector Space Editor");
        setModal(true);
        setFont(new java.awt.Font("Dialog", 0, 10));
        addWindowListener(new java.awt.event.WindowAdapter()
        {
            public void windowClosing(java.awt.event.WindowEvent evt)
            {
                closeDialog(evt);
            }
        });
        
        tablePanel_.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints2;
        
        tablePanel_.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(10, 10, 10, 10)));
        tableScrollPane_.setFont(new java.awt.Font("Dialog", 0, 10));
        elementTable_.setModel(tableModel_);
        elementTable_.setFont(new java.awt.Font("Dialog", 0, 10));
        elementTable_.setPreferredScrollableViewportSize(new java.awt.Dimension(200, 50));
        elementTable_.setMinimumSize(new java.awt.Dimension(100, 100));
        tableScrollPane_.setViewportView(elementTable_);
        
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 0;
        gridBagConstraints2.gridy = 0;
        gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints2.weightx = 1.0;
        gridBagConstraints2.weighty = 1.0;
        tablePanel_.add(tableScrollPane_, gridBagConstraints2);
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 0;
        gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints1.insets = new java.awt.Insets(5, 5, 5, 5);
        gridBagConstraints1.weightx = 1.0;
        gridBagConstraints1.weighty = 1.0;
        getContentPane().add(tablePanel_, gridBagConstraints1);
        
        buttonPanel_.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
        
        buttonPanel_.setBorder(new javax.swing.border.EtchedBorder());
        btnOK_.setFont(new java.awt.Font("Dialog", 0, 10));
        btnOK_.setText("Close");
        btnOK_.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                OnClose(evt);
            }
        });
        
        buttonPanel_.add(btnOK_);
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 1;
        gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints1.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints1.weightx = 1.0;
        getContentPane().add(buttonPanel_, gridBagConstraints1);
        
        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setSize(new java.awt.Dimension(350, 300));
        setLocation((screenSize.width-350)/2,(screenSize.height-300)/2);
    }//GEN-END:initComponents

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

  private void shapeTypeComboActionPerformed (java.awt.event.ActionEvent evt)
    {//GEN-FIRST:event_shapeTypeComboActionPerformed
        // Update the table to reflect the new shape type.
  }//GEN-LAST:event_shapeTypeComboActionPerformed

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

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

        // Commit any pending edits.
        elementTable_.editingStopped(new ChangeEvent(this));

        setVisible(false);
        dispose();
  }//GEN-LAST:event_OnClose

    //--------------------------------------------------------------------------
    /** Closes the dialog */
  private void closeDialog(java.awt.event.WindowEvent evt)
    {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
  }//GEN-LAST:event_closeDialog

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

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JPanel tablePanel_;
  private javax.swing.JScrollPane tableScrollPane_;
  private javax.swing.JTable elementTable_;
  private javax.swing.JPanel buttonPanel_;
  private javax.swing.JButton btnOK_;
  // End of variables declaration//GEN-END:variables

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

    private class VectorTableModel
    extends DictionaryEntryTableModel
    {
        //--------------------------------------------------------------------------

        private TypeDescriptorCache typeDescriptor_;

        //--------------------------------------------------------------------------
        /** VectorTableModel constructor for fixedList types.
         * @param compoundEntry Reference to the fixedList entry object.
         */
        public VectorTableModel
        (
            IDictionaryEntry compoundEntry,
            TypeDescriptorCache typeDescriptor
        )
        {
            super(compoundEntry);
            setNameColumnTitle("Component");
            typeDescriptor_ = typeDescriptor;
        }

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

        public java.lang.Object getValueAt(int rowIndex, int columnIndex)
        {
            java.lang.Object obj = null;

            try
            {
                // Check for valid row index.
                if (rowIndex>= getRowCount()) throw new Exception("Invalid row index");

                if (columnIndex == NAME_COLUMN_INDEX)
                {
                    // Return a new string containing the component name.
                    obj = typeDescriptor_.getElementLabels()[rowIndex];
                }
                else
                {
                    // Return the DictionaryEntry object for this entry.
                    DictionaryEntry dictEntry = (DictionaryEntry)dictEntryList_.get(rowIndex);
                    obj = dictEntry;
                }
            }
            catch (FoamXIOError ioErr)
            {
                App.handleException(ioErr);
            }
            catch (FoamXError fxErr)
            {
                App.handleException(fxErr);
            }
            catch (Exception ex)
            {
                App.handleAllExceptions(ex);
            }

            return obj;
        }
    }

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




