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

import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

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

    private ApplicationModel appModel_;
    private DefaultListModel listModel_;

    //--------------------------------------------------------------------------
    /** Creates a new AppClassPanel form. */
    public ApplicationDetailsTab(ApplicationModel appModel)
    {
        appModel_ = appModel;
        listModel_     = new DefaultListModel();

        // Initialise the gui.
        initComponents();

        // Initialise the argument list.
        Vector argVec = appModel_.getAppParameters();
        for (int i=0; i <argVec.size(); i++)
        {
            listModel_.addElement(argVec.get(i));
        }

        // Don't allow the application class name to be edited.
        appNameEdit_.setEditable(false);
    }

    //--------------------------------------------------------------------------
    /** Updates the application class model object. */
    public void updateModel()
    {
        // Update the application name and description.
        appModel_.setAppClassName(appNameEdit_.getText());
        appModel_.setAppDescription(appDescriptionEdit_.getText());
        appModel_.setAppExeName(appNameEdit_.getText());

        // Update the application arguments vector.
        Vector argVec = appModel_.getAppParameters();
        argVec.clear();
        for (int i=0; i <listModel_.getSize(); i++)
        {
            argVec.add(i, (String)listModel_.getElementAt(i));
        }
    }

    //--------------------------------------------------------------------------
    /** 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
        subPanel_ = new javax.swing.JPanel();
        classNameLabel_ = new javax.swing.JLabel();
        appNameEdit_ = new javax.swing.JTextField();
        appDescriptionLabel_ = new javax.swing.JLabel();
        appDescriptionEdit_ = new javax.swing.JTextField();
        appNameLabel_ = new javax.swing.JLabel();
        appNameEdit_ = new javax.swing.JTextField();
        argumentListLabel_ = new javax.swing.JLabel();
        argPanel_ = new javax.swing.JPanel();
        argListScroll_ = new javax.swing.JScrollPane();
        argumentList_ = new javax.swing.JList();
        argButtonPanel_ = new javax.swing.JPanel();
        addButton_ = new javax.swing.JButton();
        removeButton_ = new javax.swing.JButton();
        
        setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints1;
        
        subPanel_.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints2;
        
        classNameLabel_.setText("Application Class Name");
        classNameLabel_.setForeground(java.awt.Color.black);
        classNameLabel_.setFont(new java.awt.Font("Dialog", 0, 10));
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 0;
        gridBagConstraints2.gridy = 0;
        gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints2.insets = new java.awt.Insets(5, 5, 5, 5);
        gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTH;
        subPanel_.add(classNameLabel_, gridBagConstraints2);
        
        appNameEdit_.setEditable(false);
        appNameEdit_.setFont(new java.awt.Font("SansSerif", 0, 10));
        appNameEdit_.setText(appModel_.getAppClassName());
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 1;
        gridBagConstraints2.gridy = 0;
        gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints2.insets = new java.awt.Insets(5, 5, 5, 5);
        gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints2.weightx = 1.0;
        subPanel_.add(appNameEdit_, gridBagConstraints2);
        
        appDescriptionLabel_.setText("Description");
        appDescriptionLabel_.setForeground(java.awt.Color.black);
        appDescriptionLabel_.setFont(new java.awt.Font("Dialog", 0, 10));
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 0;
        gridBagConstraints2.gridy = 1;
        gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints2.insets = new java.awt.Insets(5, 5, 5, 5);
        gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTH;
        subPanel_.add(appDescriptionLabel_, gridBagConstraints2);
        
        appDescriptionEdit_.setFont(new java.awt.Font("SansSerif", 0, 10));
        appDescriptionEdit_.setText(appModel_.getAppDescription());
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 1;
        gridBagConstraints2.gridy = 1;
        gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints2.insets = new java.awt.Insets(5, 5, 5, 5);
        gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints2.weightx = 1.0;
        subPanel_.add(appDescriptionEdit_, gridBagConstraints2);
        
        appNameLabel_.setText("Executable Name");
        appNameLabel_.setForeground(java.awt.Color.black);
        appNameLabel_.setFont(new java.awt.Font("Dialog", 0, 10));
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 0;
        gridBagConstraints2.gridy = 2;
        gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints2.insets = new java.awt.Insets(5, 5, 5, 5);
        gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTH;
        subPanel_.add(appNameLabel_, gridBagConstraints2);
        
        appNameEdit_.setFont(new java.awt.Font("SansSerif", 0, 10));
        appNameEdit_.setText(appModel_.getAppExeName());
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 1;
        gridBagConstraints2.gridy = 2;
        gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints2.insets = new java.awt.Insets(5, 5, 5, 5);
        gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints2.weightx = 1.0;
        subPanel_.add(appNameEdit_, gridBagConstraints2);
        
        argumentListLabel_.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        argumentListLabel_.setText("Application Arguments");
        argumentListLabel_.setForeground(java.awt.Color.black);
        argumentListLabel_.setFont(new java.awt.Font("Dialog", 0, 10));
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 0;
        gridBagConstraints2.gridy = 3;
        gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints2.insets = new java.awt.Insets(5, 5, 5, 5);
        gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTH;
        subPanel_.add(argumentListLabel_, gridBagConstraints2);
        
        argPanel_.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints3;
        
        argumentList_.setModel(listModel_);
        argumentList_.setFont(new java.awt.Font("Dialog", 0, 10));
        argListScroll_.setViewportView(argumentList_);
        
        gridBagConstraints3 = new java.awt.GridBagConstraints();
        gridBagConstraints3.gridx = 0;
        gridBagConstraints3.gridy = 0;
        gridBagConstraints3.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints3.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints3.weightx = 1.0;
        gridBagConstraints3.weighty = 1.0;
        argPanel_.add(argListScroll_, gridBagConstraints3);
        
        argButtonPanel_.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints4;
        
        addButton_.setFont(new java.awt.Font("Dialog", 0, 10));
        addButton_.setText("Add");
        addButton_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed_(evt);
            }
        });
        
        gridBagConstraints4 = new java.awt.GridBagConstraints();
        gridBagConstraints4.gridx = 0;
        gridBagConstraints4.gridy = 0;
        gridBagConstraints4.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints4.insets = new java.awt.Insets(0, 0, 5, 0);
        gridBagConstraints4.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints4.weightx = 1.0;
        argButtonPanel_.add(addButton_, gridBagConstraints4);
        
        removeButton_.setFont(new java.awt.Font("Dialog", 0, 10));
        removeButton_.setText("Remove");
        removeButton_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed_(evt);
            }
        });
        
        gridBagConstraints4 = new java.awt.GridBagConstraints();
        gridBagConstraints4.gridx = 0;
        gridBagConstraints4.gridy = 1;
        gridBagConstraints4.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints4.insets = new java.awt.Insets(5, 0, 0, 0);
        gridBagConstraints4.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints4.weightx = 1.0;
        argButtonPanel_.add(removeButton_, gridBagConstraints4);
        
        gridBagConstraints3 = new java.awt.GridBagConstraints();
        gridBagConstraints3.gridx = 1;
        gridBagConstraints3.gridy = 0;
        gridBagConstraints3.insets = new java.awt.Insets(0, 10, 0, 0);
        gridBagConstraints3.anchor = java.awt.GridBagConstraints.NORTH;
        argPanel_.add(argButtonPanel_, gridBagConstraints3);
        
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 1;
        gridBagConstraints2.gridy = 3;
        gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints2.insets = new java.awt.Insets(5, 5, 5, 5);
        gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints2.weightx = 1.0;
        gridBagConstraints2.weighty = 1.0;
        subPanel_.add(argPanel_, gridBagConstraints2);
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 0;
        gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints1.insets = new java.awt.Insets(10, 10, 10, 10);
        gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints1.weightx = 1.0;
        gridBagConstraints1.weighty = 1.0;
        add(subPanel_, gridBagConstraints1);
        
    }//GEN-END:initComponents

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

private void removeButtonActionPerformed_ (java.awt.event.ActionEvent evt)
    {//GEN-FIRST:event_removeButtonActionPerformed_
        // Remove the selected application arguments.
        if (!argumentList_.getSelectionModel().isSelectionEmpty())
        {
            int minIndex = argumentList_.getSelectionModel().getMinSelectionIndex();
            int maxIndex = argumentList_.getSelectionModel().getMaxSelectionIndex();
            // Need to remove from the top so that it doesn't get its knickers in a twist.
            for (int i=maxIndex; i>= minIndex; i--)
            {
                listModel_.removeElementAt(i);
            }
        }
  }//GEN-LAST:event_removeButtonActionPerformed_

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

private void addButtonActionPerformed_ (java.awt.event.ActionEvent evt)
    {//GEN-FIRST:event_addButtonActionPerformed_
        // Add a new program argument.
        String arg = (String)JOptionPane.showInputDialog(this,
                                                          "Enter Application Argument",
                                                          "FoamX...",
                                                          JOptionPane.PLAIN_MESSAGE, null, null, null);
        if (arg != null && arg.trim().length()> 0)
        {
            listModel_.addElement(arg);
        }

}//GEN-LAST:event_addButtonActionPerformed_

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

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel subPanel_;
private javax.swing.JLabel classNameLabel_;
private javax.swing.JTextField appNameEdit_;
private javax.swing.JLabel appDescriptionLabel_;
private javax.swing.JTextField appDescriptionEdit_;
private javax.swing.JLabel appNameLabel_;
private javax.swing.JTextField appNameEdit_;
private javax.swing.JLabel argumentListLabel_;
private javax.swing.JPanel argPanel_;
private javax.swing.JScrollPane argListScroll_;
private javax.swing.JList argumentList_;
private javax.swing.JPanel argButtonPanel_;
private javax.swing.JButton addButton_;
private javax.swing.JButton removeButton_;
// End of variables declaration//GEN-END:variables

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



