/*
 * WANDORA
 * Knowledge Extraction, Management, and Publishing Application
 * http://wandora.org
 * 
 * Copyright (C) 2004-2023 Wandora Team
 * 
 * This program 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 3 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 
 * DatabaseConfigurationPanel.java
 *
 * Created on 21. marraskuuta 2005, 10:37
 */

package org.wandora.application.gui;


import static org.wandora.utils.Tuples.t2;

import java.awt.Cursor;
import java.util.Collection;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JDialog;

import org.wandora.application.Wandora;
import org.wandora.application.gui.simple.SimpleList;
import org.wandora.application.gui.simple.SimpleTextArea;
import org.wandora.topicmap.TopicMapConfigurationPanel;
import org.wandora.topicmap.database.DatabaseConfiguration;
import org.wandora.utils.Options;
import org.wandora.utils.Tuples.T2;
import org.wandora.utils.swing.GuiTools;

/**
 *
 * @author  olli
 */
public class DatabaseConfigurationPanel extends javax.swing.JPanel {
	
	private static final long serialVersionUID = 1L;
	
    public final static String GENERIC_TYPE = "Generic";
    
    
    private Wandora wandora;
    protected JDialog newConDialog;
    protected DefaultListModel listModel;
    protected int editingIndex=-1;
    
    
    
    /** Creates new form ConfigurationPanel */
    public DatabaseConfigurationPanel(Wandora wandora) {
        this.wandora = wandora;
        listModel = new DefaultListModel();
        initComponents();
        connectionsList.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        loadSaveButtonPanel.setVisible(false);
        // dbtypeComboBox.addItem("Microsoft SQL Server");
        // dbtypeComboBox.addItem("MySQL");
        dbtypeComboBox.addItem(GENERIC_TYPE);
        setDbConfPanel();
    }
    
    /** 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 Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        knownDBPanel = new javax.swing.JPanel();
        jLabel3 = new org.wandora.application.gui.simple.SimpleLabel();
        jLabel4 = new org.wandora.application.gui.simple.SimpleLabel();
        jLabel5 = new org.wandora.application.gui.simple.SimpleLabel();
        userTextField = new org.wandora.application.gui.simple.SimpleField();
        jLabel6 = new org.wandora.application.gui.simple.SimpleLabel();
        passwordTextField = new org.wandora.application.gui.simple.SimpleField();
        jPanel2 = new javax.swing.JPanel();
        serverTextField = new org.wandora.application.gui.simple.SimpleField();
        databaseTextField = new org.wandora.application.gui.simple.SimpleField();
        jLabel1 = new org.wandora.application.gui.simple.SimpleLabel();
        nameTextField = new org.wandora.application.gui.simple.SimpleField();
        generalDBPanel = new javax.swing.JPanel();
        jLabel7 = new org.wandora.application.gui.simple.SimpleLabel();
        jLabel8 = new org.wandora.application.gui.simple.SimpleLabel();
        jLabel9 = new org.wandora.application.gui.simple.SimpleLabel();
        userGenTextField = new org.wandora.application.gui.simple.SimpleField();
        driverTextField = new org.wandora.application.gui.simple.SimpleField();
        conTextField = new org.wandora.application.gui.simple.SimpleField();
        jLabel2 = new org.wandora.application.gui.simple.SimpleLabel();
        nameGenTextField = new org.wandora.application.gui.simple.SimpleField();
        jScrollPane2 = new javax.swing.JScrollPane();
        scriptTextArea = new SimpleTextArea();
        jLabel12 = new org.wandora.application.gui.simple.SimpleLabel();
        jLabel10 = new org.wandora.application.gui.simple.SimpleLabel();
        jPanel1 = new javax.swing.JPanel();
        passwordGenTextField = new org.wandora.application.gui.simple.SimpleField();
        newConPanel = new javax.swing.JPanel();
        jLabel11 = new org.wandora.application.gui.simple.SimpleLabel();
        dbtypeComboBox = new org.wandora.application.gui.simple.SimpleComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        dbConfContainerPanel = new javax.swing.JPanel();
        newConButtonsPanel = new javax.swing.JPanel();
        newConOKButton = new org.wandora.application.gui.simple.SimpleButton();
        newConCancelButton = new org.wandora.application.gui.simple.SimpleButton();
        loadDialog = new javax.swing.JDialog();
        loadContainerPanel = new javax.swing.JPanel();
        loadButtonsPanel1 = new javax.swing.JPanel();
        loadCancelButton = new org.wandora.application.gui.simple.SimpleButton();
        loadOKButton = new org.wandora.application.gui.simple.SimpleButton();
        loadSaveButtonPanel = new javax.swing.JPanel();
        loadButton = new org.wandora.application.gui.simple.SimpleButton();
        saveButton = new org.wandora.application.gui.simple.SimpleButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        connectionsList = new SimpleList(listModel);
        jPanel3 = new javax.swing.JPanel();
        newConnectionButton = new org.wandora.application.gui.simple.SimpleButton();
        editButton = new org.wandora.application.gui.simple.SimpleButton();
        deleteConnectionButton = new org.wandora.application.gui.simple.SimpleButton();

        knownDBPanel.setLayout(new java.awt.GridBagLayout());

        jLabel3.setText("Server");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        knownDBPanel.add(jLabel3, gridBagConstraints);

        jLabel4.setText("Database");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        knownDBPanel.add(jLabel4, gridBagConstraints);

        jLabel5.setText("User");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        knownDBPanel.add(jLabel5, gridBagConstraints);

        userTextField.setMargin(new java.awt.Insets(3, 3, 3, 3));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 7);
        knownDBPanel.add(userTextField, gridBagConstraints);

        jLabel6.setText("Password");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        knownDBPanel.add(jLabel6, gridBagConstraints);

        passwordTextField.setMargin(new java.awt.Insets(3, 3, 3, 3));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 7);
        knownDBPanel.add(passwordTextField, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.weighty = 1.0;
        knownDBPanel.add(jPanel2, gridBagConstraints);

        serverTextField.setMargin(new java.awt.Insets(3, 3, 3, 3));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 7);
        knownDBPanel.add(serverTextField, gridBagConstraints);

        databaseTextField.setMargin(new java.awt.Insets(3, 3, 3, 3));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 7);
        knownDBPanel.add(databaseTextField, gridBagConstraints);

        jLabel1.setText("Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        knownDBPanel.add(jLabel1, gridBagConstraints);

        nameTextField.setMargin(new java.awt.Insets(3, 3, 3, 3));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 7);
        knownDBPanel.add(nameTextField, gridBagConstraints);

        generalDBPanel.setLayout(new java.awt.GridBagLayout());

        jLabel7.setText("Driver");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        generalDBPanel.add(jLabel7, gridBagConstraints);

        jLabel8.setText("Connection string");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        generalDBPanel.add(jLabel8, gridBagConstraints);

        jLabel9.setText("User");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        generalDBPanel.add(jLabel9, gridBagConstraints);

        userGenTextField.setMargin(new java.awt.Insets(3, 3, 3, 3));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 7);
        generalDBPanel.add(userGenTextField, gridBagConstraints);

        driverTextField.setMargin(new java.awt.Insets(3, 3, 3, 3));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 7);
        generalDBPanel.add(driverTextField, gridBagConstraints);

        conTextField.setMargin(new java.awt.Insets(3, 3, 3, 3));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 7);
        generalDBPanel.add(conTextField, gridBagConstraints);

        jLabel2.setText("Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        generalDBPanel.add(jLabel2, gridBagConstraints);

        nameGenTextField.setMargin(new java.awt.Insets(3, 3, 3, 3));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 7);
        generalDBPanel.add(nameGenTextField, gridBagConstraints);

        scriptTextArea.setMargin(new java.awt.Insets(3, 3, 3, 3));
        jScrollPane2.setViewportView(scriptTextArea);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 3, 7);
        generalDBPanel.add(jScrollPane2, gridBagConstraints);

        jLabel12.setText("Run script");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 3, 5);
        generalDBPanel.add(jLabel12, gridBagConstraints);

        jLabel10.setText("Password");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 0, 6);
        generalDBPanel.add(jLabel10, gridBagConstraints);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        passwordGenTextField.setMargin(new java.awt.Insets(3, 3, 3, 3));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        jPanel1.add(passwordGenTextField, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 7);
        generalDBPanel.add(jPanel1, gridBagConstraints);

        newConPanel.setLayout(new java.awt.GridBagLayout());

        jLabel11.setText("Type");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 7, 5, 5);
        newConPanel.add(jLabel11, gridBagConstraints);

        dbtypeComboBox.setEditable(false);
        dbtypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dbtypeComboBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 7);
        newConPanel.add(dbtypeComboBox, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        newConPanel.add(jSeparator1, gridBagConstraints);

        dbConfContainerPanel.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(3, 7, 3, 7);
        newConPanel.add(dbConfContainerPanel, gridBagConstraints);

        newConButtonsPanel.setLayout(new java.awt.GridBagLayout());

        newConOKButton.setText("OK");
        newConOKButton.setMaximumSize(new java.awt.Dimension(70, 23));
        newConOKButton.setMinimumSize(new java.awt.Dimension(70, 23));
        newConOKButton.setPreferredSize(new java.awt.Dimension(70, 23));
        newConOKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newConOKButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        newConButtonsPanel.add(newConOKButton, gridBagConstraints);

        newConCancelButton.setText("Cancel");
        newConCancelButton.setMargin(new java.awt.Insets(2, 6, 2, 6));
        newConCancelButton.setMaximumSize(new java.awt.Dimension(70, 23));
        newConCancelButton.setMinimumSize(new java.awt.Dimension(70, 23));
        newConCancelButton.setPreferredSize(new java.awt.Dimension(70, 23));
        newConCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newConCancelButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        newConButtonsPanel.add(newConCancelButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 7, 5, 0);
        newConPanel.add(newConButtonsPanel, gridBagConstraints);

        loadDialog.setModal(true);
        loadDialog.getContentPane().setLayout(new java.awt.GridBagLayout());

        loadContainerPanel.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(3, 7, 3, 7);
        loadDialog.getContentPane().add(loadContainerPanel, gridBagConstraints);

        loadButtonsPanel1.setLayout(new java.awt.GridBagLayout());

        loadCancelButton.setText("Cancel");
        loadCancelButton.setMargin(new java.awt.Insets(2, 6, 2, 6));
        loadCancelButton.setMaximumSize(new java.awt.Dimension(65, 23));
        loadCancelButton.setPreferredSize(new java.awt.Dimension(65, 23));
        loadCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadCancelButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 3);
        loadButtonsPanel1.add(loadCancelButton, gridBagConstraints);

        loadOKButton.setText("OK");
        loadOKButton.setMaximumSize(new java.awt.Dimension(65, 23));
        loadOKButton.setPreferredSize(new java.awt.Dimension(65, 23));
        loadOKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadOKButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 5);
        loadButtonsPanel1.add(loadOKButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 7, 5, 0);
        loadDialog.getContentPane().add(loadButtonsPanel1, gridBagConstraints);

        loadButton.setText("Load");
        loadButton.setMaximumSize(new java.awt.Dimension(70, 23));
        loadButton.setMinimumSize(new java.awt.Dimension(70, 23));
        loadButton.setPreferredSize(new java.awt.Dimension(70, 23));
        loadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadButtonActionPerformed(evt);
            }
        });
        loadSaveButtonPanel.add(loadButton);

        saveButton.setText("Save");
        saveButton.setMaximumSize(new java.awt.Dimension(70, 23));
        saveButton.setMinimumSize(new java.awt.Dimension(70, 23));
        saveButton.setPreferredSize(new java.awt.Dimension(70, 23));
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        loadSaveButtonPanel.add(saveButton);

        setLayout(new java.awt.GridBagLayout());

        connectionsList.setFont(org.wandora.application.gui.UIConstants.plainFont);
        connectionsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        connectionsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                proposeNameForLayer(evt);
            }
        });
        jScrollPane1.setViewportView(connectionsList);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jScrollPane1, gridBagConstraints);

        jPanel3.setLayout(new java.awt.GridLayout(3, 0, 0, 5));

        newConnectionButton.setText("New");
        newConnectionButton.setMaximumSize(new java.awt.Dimension(57, 21));
        newConnectionButton.setMinimumSize(new java.awt.Dimension(57, 21));
        newConnectionButton.setPreferredSize(new java.awt.Dimension(57, 21));
        newConnectionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newConnectionButtonActionPerformed(evt);
            }
        });
        jPanel3.add(newConnectionButton);

        editButton.setText("Edit");
        editButton.setMaximumSize(new java.awt.Dimension(53, 21));
        editButton.setMinimumSize(new java.awt.Dimension(53, 21));
        editButton.setPreferredSize(new java.awt.Dimension(53, 21));
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });
        jPanel3.add(editButton);

        deleteConnectionButton.setText("Delete");
        deleteConnectionButton.setMaximumSize(new java.awt.Dimension(65, 21));
        deleteConnectionButton.setMinimumSize(new java.awt.Dimension(65, 21));
        deleteConnectionButton.setPreferredSize(new java.awt.Dimension(65, 21));
        deleteConnectionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteConnectionButtonActionPerformed(evt);
            }
        });
        jPanel3.add(deleteConnectionButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 5);
        add(jPanel3, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void deleteConnectionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteConnectionButtonActionPerformed
        StoredConnection sc=(StoredConnection)connectionsList.getSelectedValue();
        if(sc==null){
            WandoraOptionPane.showMessageDialog(wandora, "Select connection to be deleted!");
            return;
        }
        else {
            if(WandoraOptionPane.showConfirmDialog(wandora,"Are you sure you want to delete selected layer configuration '" + sc.name + "'?") == WandoraOptionPane.YES_OPTION) {
                listModel.remove(connectionsList.getSelectedIndex());
                connectionsList.clearSelection();
            }
        }
    }//GEN-LAST:event_deleteConnectionButtonActionPerformed

    private void proposeNameForLayer(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_proposeNameForLayer
        try {
            NewTopicMapPanel ntp = (NewTopicMapPanel) UIBox.getParentComponentByClass("org.wandora.application.gui.NewTopicMapPanel", this, 5);
            if(ntp != null) {
                ntp.proposeName(connectionsList.getSelectedValue().toString());
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_proposeNameForLayer

    boolean loadCancelled=true;
    private void loadOKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadOKButtonActionPerformed
        loadCancelled=false;
        loadDialog.setVisible(false);
    }//GEN-LAST:event_loadOKButtonActionPerformed

    private void loadCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadCancelButtonActionPerformed
        loadCancelled=true;
        loadDialog.setVisible(false);
    }//GEN-LAST:event_loadCancelButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        StoredConnection sc=makeNewStoredConnection();
        if(sc==null) return;

        setConnections(DatabaseConfiguration.parseConnections(wandora.getOptions()));
        listModel.addElement(sc);
        DatabaseConfiguration.writeOptions(wandora.getOptions(),DatabaseConfigurationPanel.this);
        
        WandoraOptionPane.showMessageDialog(wandora,"Connection settings saved!");
    }//GEN-LAST:event_saveButtonActionPerformed

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

        DatabaseConfiguration dc=new DatabaseConfiguration(wandora, wandora.getOptions());
        loadContainerPanel.removeAll();
        loadContainerPanel.add(dc);
        
        loadDialog.setSize(600,550);
        GuiTools.centerWindow(loadDialog,wandora);
        loadCancelled=true;
        loadDialog.setVisible(true);
        
        if(loadCancelled) return;
        
        Object params=dc.getParameters();
        
        initNewConPanel((StoredConnection)params);
    }//GEN-LAST:event_loadButtonActionPerformed

    
    public TopicMapConfigurationPanel getEditConfigurationPanel(Object params){
        newConButtonsPanel.setVisible(false);
        loadSaveButtonPanel.setVisible(true);
        final StoredConnection sc=(StoredConnection)params;
        initNewConPanel(sc);
        
        return new TopicMapConfigurationPanel() {
            {
                this.setLayout(new java.awt.BorderLayout());
                this.add(newConPanel);
                this.revalidate();
            }
            
            public Object getParameters(){
                StoredConnection sc=makeNewStoredConnection();
                return sc;
            }
        };
    }
    
    
    
    
    private void initNewConPanel(StoredConnection sc){
        clearNewConPanel();
        dbtypeComboBox.setSelectedItem(sc.type);
        setDbConfPanel();
        if(sc.type.equals(GENERIC_TYPE)){
            nameGenTextField.setText(sc.name);
            driverTextField.setText(sc.driver);
            conTextField.setText(sc.conString);
            userGenTextField.setText(sc.user);
            if(sc.pass.length()>0){
                //rememberPassGenCheckBox.setSelected(true);
                passwordGenTextField.setText(sc.pass);
                passwordGenTextField.setEnabled(true);
            }
            else {
                //rememberPassGenCheckBox.setSelected(true);
                passwordGenTextField.setText("");
                passwordGenTextField.setEnabled(true);                
            }
            scriptTextArea.setText(sc.script);
            scriptTextArea.setCaretPosition(0);
        }
        else{
            nameTextField.setText(sc.name);
            serverTextField.setText(sc.server);
            databaseTextField.setText(sc.database);
            userTextField.setText(sc.user);
            if(sc.pass.length()>0){
                //rememberPassCheckBox.setSelected(true);
                passwordTextField.setText(sc.pass);
                passwordTextField.setEnabled(true);
            }
            else {
                //rememberPassCheckBox.setSelected(true);
                passwordTextField.setText("");
                passwordTextField.setEnabled(true);                
            }
        }        
    }
    
    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
        StoredConnection sc=(StoredConnection)connectionsList.getSelectedValue();
        if(sc==null) {
            WandoraOptionPane.showMessageDialog(wandora, "Select connection first!");
            return;
        }
        initNewConPanel(sc);
        editingIndex=connectionsList.getSelectedIndex();
        openNewConPanel();
    }//GEN-LAST:event_editButtonActionPerformed

    public static T2<String,String> getConnectionDriverAndString(StoredConnection sc){
        if(sc == null) return null;
        if(sc.type.equals(GENERIC_TYPE)){
            return t2(sc.driver,sc.conString);
        }
        else if(sc.type.equals("MySQL")){
            return t2("com.mysql.jdbc.Driver","jdbc:mysql://"+sc.server+"/"+sc.database);
        }
        else if(sc.type.equals("Microsoft SQL Server")){
            return t2("com.microsoft.jdbc.sqlserver.SQLServerDriver",
                      "jdbc:microsoft:sqlserver://"+sc.server+";DatabaseName="+sc.database);            
        }
        else return null;
    }
    
    private StoredConnection makeNewStoredConnection(){
        StoredConnection sc=null;
        if(dbtypeComboBox.getSelectedItem().toString().equals(GENERIC_TYPE)){
            String name=nameGenTextField.getText().trim();
            String driver=driverTextField.getText().trim();
            String conString=conTextField.getText().trim();
            String user=userGenTextField.getText().trim();
            String pass="";
            String script=scriptTextArea.getText().trim();
            //if(rememberPassGenCheckBox.isSelected()) pass=passwordGenTextField.getText().trim();
            pass=passwordGenTextField.getText().trim();
            if(name.length()==0) {WandoraOptionPane.showMessageDialog(wandora,"Enter connection name"); return null;}
            if(driver.length()==0) {WandoraOptionPane.showMessageDialog(wandora,"Enter connection driver"); return null;}
            if(conString.length()==0) {WandoraOptionPane.showMessageDialog(wandora,"Enter connection string"); return null;}
            if(user.length()==0) {WandoraOptionPane.showMessageDialog(wandora,"Enter user name"); return null;}
            sc=StoredConnection.generic(name,GENERIC_TYPE, driver, conString, user, pass, script);
        }
        else{
            String name=nameTextField.getText().trim();
            String server=serverTextField.getText().trim();
            String database=databaseTextField.getText().trim();
            String user=userTextField.getText().trim();
            String pass="";
            //if(rememberPassCheckBox.isSelected()) pass=passwordTextField.getText().trim();
            pass=passwordTextField.getText().trim();
            if(name.length()==0) {WandoraOptionPane.showMessageDialog(wandora,"Enter connection name"); return null;}
            if(server.length()==0) {WandoraOptionPane.showMessageDialog(wandora,"Enter server address"); return null;}
            if(database.length()==0) {WandoraOptionPane.showMessageDialog(wandora,"Enter database name"); return null;}
            if(user.length()==0) {WandoraOptionPane.showMessageDialog(wandora,"Enter user name"); return null;}            
            sc=StoredConnection.known(name, dbtypeComboBox.getSelectedItem().toString(), server, database, user, pass);
        }
        return sc;
    }
    
    private void newConOKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newConOKButtonActionPerformed
        StoredConnection sc=makeNewStoredConnection();
        if(sc==null) return;
        if(editingIndex!=-1) listModel.setElementAt(sc, editingIndex);
        else listModel.addElement(sc);
        newConDialog.setVisible(false);
    }//GEN-LAST:event_newConOKButtonActionPerformed

    private void newConCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newConCancelButtonActionPerformed
        newConDialog.setVisible(false);
    }//GEN-LAST:event_newConCancelButtonActionPerformed

    protected void clearNewConPanel(){
        dbtypeComboBox.setSelectedIndex(0);
        nameGenTextField.setText("");
        driverTextField.setText("");
        conTextField.setText("");
        userGenTextField.setText("");
        passwordGenTextField.setText("");
        //rememberPassGenCheckBox.setSelected(false);
        passwordGenTextField.setEnabled(true);
        nameTextField.setText("");
        serverTextField.setText("");
        databaseTextField.setText("");
        userTextField.setText("");
        passwordTextField.setText("");
        //rememberPassCheckBox.setSelected(false);
        passwordTextField.setEnabled(true);        
        scriptTextArea.setText("");
    }
    
    protected void openNewConPanel(){
        java.awt.Container parent=this.getParent();
        while( parent!=null && !(parent instanceof java.awt.Frame) && !(parent instanceof java.awt.Dialog) ){
            parent=parent.getParent();
        }
        if(parent==null) newConDialog=new JDialog();
        else if(parent instanceof java.awt.Frame) newConDialog=new JDialog((java.awt.Frame)parent,true);
        else newConDialog=new JDialog((java.awt.Dialog)parent,true);
        newConDialog.getContentPane().add(newConPanel);
        newConDialog.setSize(600,550);
        if(wandora != null) wandora.centerWindow(newConDialog, 0, +30);
        newConDialog.setTitle("Edit database layer configuration");
        newConDialog.setVisible(true);
    }
    
    private void newConnectionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newConnectionButtonActionPerformed
        clearNewConPanel();
        editingIndex=-1;
        openNewConPanel();
    }//GEN-LAST:event_newConnectionButtonActionPerformed

    protected void setDbConfPanel(){
        String type=dbtypeComboBox.getSelectedItem().toString();
        dbConfContainerPanel.removeAll();
        if(type.equals(GENERIC_TYPE)){
            dbConfContainerPanel.add(generalDBPanel);
        }
        else if(type.equals("MySQL") || type.equals("Microsoft SQL Server")){
            dbConfContainerPanel.add(knownDBPanel);            
        }
        else{
            throw new RuntimeException("Unknown database type");
        }
        dbConfContainerPanel.revalidate();
        dbConfContainerPanel.repaint();
    }
    
    
    private void dbtypeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dbtypeComboBoxActionPerformed
        setDbConfPanel();
    }//GEN-LAST:event_dbtypeComboBoxActionPerformed
    
    
    public StoredConnection getSelectedConnection(){
        StoredConnection sc=(StoredConnection)connectionsList.getSelectedValue();
        return sc; // can be null if nothing selected
    }
    
    
    public Collection<StoredConnection> getAllConnections(){
        Vector<StoredConnection> ret=new Vector<StoredConnection>();
        for(int i=0;i<listModel.getSize();i++){
            ret.add((StoredConnection)listModel.getElementAt(i));
        }
        return ret;
    }
    
    public void setConnections(Collection<StoredConnection> cs){
        listModel.clear();
        for(StoredConnection c : cs){
            listModel.addElement(c);
        }
    }
    

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField conTextField;
    private javax.swing.JList connectionsList;
    private javax.swing.JTextField databaseTextField;
    private javax.swing.JPanel dbConfContainerPanel;
    private javax.swing.JComboBox dbtypeComboBox;
    private javax.swing.JButton deleteConnectionButton;
    private javax.swing.JTextField driverTextField;
    private javax.swing.JButton editButton;
    private javax.swing.JPanel generalDBPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPanel knownDBPanel;
    private javax.swing.JButton loadButton;
    private javax.swing.JPanel loadButtonsPanel1;
    private javax.swing.JButton loadCancelButton;
    private javax.swing.JPanel loadContainerPanel;
    private javax.swing.JDialog loadDialog;
    private javax.swing.JButton loadOKButton;
    private javax.swing.JPanel loadSaveButtonPanel;
    private javax.swing.JTextField nameGenTextField;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JPanel newConButtonsPanel;
    private javax.swing.JButton newConCancelButton;
    private javax.swing.JButton newConOKButton;
    private javax.swing.JPanel newConPanel;
    private javax.swing.JButton newConnectionButton;
    private javax.swing.JTextField passwordGenTextField;
    private javax.swing.JTextField passwordTextField;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextArea scriptTextArea;
    private javax.swing.JTextField serverTextField;
    private javax.swing.JTextField userGenTextField;
    private javax.swing.JTextField userTextField;
    // End of variables declaration//GEN-END:variables

    
    public static class StoredConnection {
        public String name;
        public String type;
        public String server;
        public String database;
        public String driver;
        public String conString;
        public String user;
        public String pass;
        public String script;
        
        
        public StoredConnection(){

        }
        
        
        public static StoredConnection known(String name,String type,String server,String database,String user,String password){
            StoredConnection c=new StoredConnection();
            c.name=name;
            c.type=type;
            c.server=server;
            c.database=database;
            c.user=user;
            c.pass=password;
            return c;
        }
        
        
        public static StoredConnection generic(String name,String type,String driver,String conString,String user,String password){
            return generic(name,type,driver,conString,user,password,"");
        }
        
        
        public static StoredConnection generic(String name,String type,String driver,String conString,String user,String password,String script){
            StoredConnection c=new StoredConnection();
            c.name=name;
            c.type=type;
            c.driver=driver;
            c.conString=conString;
            c.user=user;
            c.pass=password;
            c.script=script;
            return c;
        }
        
        
        public String toString(){
            return name;
        }
        
        
        public void writeOptions(Options options,String prefix){
            if(name!=null) options.put(prefix+"name",name);
            if(type!=null) options.put(prefix+"type",type);
            if(user!=null) options.put(prefix+"user",user);
            if(pass!=null) options.put(prefix+"pass",pass);
            if(server!=null) options.put(prefix+"server",server);
            if(database!=null) options.put(prefix+"database",database);
            if(driver!=null) options.put(prefix+"driver",driver);
            if(conString!=null) options.put(prefix+"conString",conString);
            if(script!=null) options.put(prefix+"script",script);
        }
        
        
        public void readOptions(Options options,String prefix){
            name=options.get(prefix+"name");
            type=options.get(prefix+"type");
            server=options.get(prefix+"server");
            database=options.get(prefix+"database");
            driver=options.get(prefix+"driver");
            conString=options.get(prefix+"conString");
            user=options.get(prefix+"user");
            pass=options.get(prefix+"pass");
            script=options.get(prefix+"script");
        }
    }

}

