package com.dbxml.db.admin.dialogs;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: ConnectDialog.java,v 1.9 2004/07/21 00:20:03 bradford Exp $
 */

import java.awt.*;
import javax.swing.*;

import com.dbxml.db.admin.Admin;
import com.dbxml.db.admin.swing.PropertyInfo;
import com.dbxml.db.admin.swing.PropertyPane;
import com.dbxml.db.client.dbXMLClient;
import com.dbxml.db.server.dbXML;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.border.Border;

/**
 * ConnectDialog
 */

public final class ConnectDialog extends JDialog {
   private static final String[] ColumnNames = new String[]{"Name", "Value"};
   private static final String[] DriverNames = new String[]{"XML-RPC", "Local"};
   private static final Class[] DriverClasses = new Class[]{
      com.dbxml.db.client.xmlrpc.dbXMLClientImpl.class,
      com.dbxml.db.client.local.dbXMLClientImpl.class
   };

   private List[] properties = new List[DriverNames.length];
   private boolean okClicked = false;

   ImageIcon imgDatabase;
   JLabel lblConnect = new JLabel();
   Border brdSearch;
   GridBagLayout thisLayout = new GridBagLayout();
   JPanel pnlButtons = new JPanel();
   JButton btnConnect = new JButton();
   JButton btnCancel = new JButton();
   JLabel lblDriver = new JLabel();
   JComboBox cmbDriver = new JComboBox(DriverNames);
   JLabel lblProps = new JLabel();
   JLabel lblLabel = new JLabel();
   JTextField txtLabel = new JTextField();
   BorderLayout pnlButtonsLayout = new BorderLayout();
   JPanel pnlDialog = new JPanel();
   private PropertyPane propertyPane = new PropertyPane() {
      public boolean checkValidity() {
         if ( super.checkValidity() )
            return testRequired();
         else {
            btnConnect.setEnabled(false);
            return false;
         }
      }
   };

   public ConnectDialog(Frame owner) {
      super(owner, true);

      PropertyInfo user = new PropertyInfo(dbXMLClient.USER, "User Name");
      PropertyInfo pass = new PropertyInfo(dbXMLClient.PASS, "Password", "", false, PropertyInfo.TYPE_PASSWORD);

      properties[0] = new ArrayList();
      properties[0].add(new PropertyInfo("connection", "Connection Type", "standard", true));
      properties[0].add(new PropertyInfo(dbXMLClient.HOST, "Server Host Name", dbXML.DEFAULT_HOST, true));
      properties[0].add(new PropertyInfo(dbXMLClient.PORT, "Server Port", Integer.toString(dbXML.DEFAULT_PORT), true, PropertyInfo.TYPE_INT));
      properties[0].add(user);
      properties[0].add(pass);

      properties[1] = new ArrayList();
      properties[1].add(user);
      properties[1].add(pass);

      propertyPane.addProperties(properties[0]);
      propertyPane.checkValidity();

      enableEvents(AWTEvent.WINDOW_EVENT_MASK);
      try {
         jbInit();

         cmbDriver.setMaximumRowCount(DriverNames.length);

         pack();

         // Center the Dialog
         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
         Dimension frameSize = getSize();
         if (frameSize.height > screenSize.height)
           frameSize.height = screenSize.height;
         if (frameSize.width > screenSize.width)
           frameSize.width = screenSize.width;
         setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
      }
      catch ( Exception e ) {
         Admin.getInstance().addMessage(e.toString());
      }
   }

   private void jbInit() throws Exception {
      imgDatabase = new ImageIcon(ConnectDialog.class.getResource("database.gif"));
      brdSearch = BorderFactory.createEmptyBorder(5, 5, 5, 5);
      lblConnect.setIcon(imgDatabase);
      this.setModal(true);
      this.setTitle("Connect to Database");
      this.setResizable(false);
      propertyPane.setMinimumSize(new Dimension(250, 250));
      propertyPane.setPreferredSize(new Dimension(250, 250));
      this.getContentPane().add(pnlDialog);
      pnlDialog.setLayout(thisLayout);

      pnlButtons.setLayout(pnlButtonsLayout);
      btnConnect.setFont(new Font("Dialog", 0, 12));
      btnConnect.setActionCommand("connect");
      btnConnect.setSelected(true);
      btnConnect.setText("connect");
      btnConnect.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            btnConnect_actionPerformed(e);
         }
      });
      btnCancel.setFont(new Font("Dialog", 0, 12));
      btnCancel.setActionCommand("cancel");
      btnCancel.setText("cancel");
      btnCancel.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            btnCancel_actionPerformed(e);
         }
      });
      lblDriver.setText("Driver");
      lblProps.setText("Properties");


      cmbDriver.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(ActionEvent e) {
            cmbDriver_actionPerformed(e);
         }
      });
      lblLabel.setText("Label");
      txtLabel.setText("");
      txtLabel.addKeyListener(new java.awt.event.KeyAdapter() {
         public void keyReleased(KeyEvent e) {
            txtLabel_keyReleased(e);
         }
      });
      pnlButtonsLayout.setHgap(5);
      pnlDialog.add(pnlButtons, new GridBagConstraints(0, 3, 3, 1, 0.0, 0.0
            ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 10, 10, 10), 0, 0));
      pnlButtons.add(btnConnect, BorderLayout.CENTER);
      pnlButtons.add(btnCancel, BorderLayout.EAST);
      pnlDialog.add(lblDriver, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 10, 10, 0), 0, 0));
      pnlDialog.add(cmbDriver, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
         , GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 10), 0, 0));
      pnlDialog.add(lblProps, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 10, 10, 0), 0, 0));
      pnlDialog.add(lblLabel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(10, 10, 10, 0), 0, 0));
      pnlDialog.add(txtLabel, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
         , GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0));
      pnlDialog.add(lblConnect, new GridBagConstraints(0, 0, 1, 4, 0.0, 0.0
         , GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(10, 10, 10, 10), 0, 0));
      pnlDialog.add(propertyPane,    new GridBagConstraints(2, 2, 1, 1, 1.0, 1.0
            ,GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 10, 10, 10), 0, 0));
   }

   protected void processWindowEvent(WindowEvent e) {
      super.processWindowEvent(e);
      if ( e.getID() == WindowEvent.WINDOW_CLOSING ) {
         hide();
      }
   }

   public boolean isOkClicked() {
      return okClicked;
   }

   void btnConnect_actionPerformed(ActionEvent e) {
      try {
         okClicked = true;
         dbXMLClient client = (dbXMLClient)DriverClasses[cmbDriver.getSelectedIndex()].newInstance();

         PropertyInfo[] props = propertyPane.getProperties();
         for ( int i = 0; i < props.length; i++ ) {
            PropertyInfo pi = props[i];
            String value = pi.getValue();
            if ( value.trim().length() > 0 )
               client.setProperty(pi.getName(), value);
         }

         String label = txtLabel.getText();

         Admin.getInstance().browser.addClient(client, label);
         Admin.getInstance().addClientConfig(client, label);
         hide();
      }
      catch ( Exception ex ) {
         Admin.getInstance().addMessage(ex.getMessage());
      }
   }

   void btnCancel_actionPerformed(ActionEvent e) {
      hide();
   }

   void cmbDriver_actionPerformed(ActionEvent e) {
      List l = properties[cmbDriver.getSelectedIndex()];
      propertyPane.clearProperties();
      propertyPane.addProperties(l);
      propertyPane.checkValidity();
   }

   void txtLabel_keyReleased(KeyEvent e) {
      propertyPane.checkValidity();
   }

   boolean testRequired() {
      if ( txtLabel.getText().trim().length() == 0 ) {
         btnConnect.setEnabled(false);
         return false;
      }

      btnConnect.setEnabled(true);
      return true;
   }
}

