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: FilesystemDialog.java,v 1.2 2004/02/12 00:17:54 bradford Exp $
 */

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

import com.dbxml.db.admin.Admin;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import javax.swing.border.Border;

/**
 * FilesystemDialog
 */

public final class FilesystemDialog extends JDialog {
   ImageIcon imgFilesystem;
   JLabel lblConnect = new JLabel();
   Border brdSearch;
   GridBagLayout thisLayout = new GridBagLayout();
   JPanel pnlButtons = new JPanel();
   JButton btnAttach = new JButton();
   JButton btnCancel = new JButton();
   JLabel lblDirectory = new JLabel();
   JLabel lblLabel = new JLabel();
   JTextField txtLabel = new JTextField();
   BorderLayout pnlButtonsLayout = new BorderLayout();
   JPanel pnlDialog = new JPanel();
   JTextField txtDirectory = new JTextField();
   JButton btnBrowse = new JButton();

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

      testRequired();

      enableEvents(AWTEvent.WINDOW_EVENT_MASK);
      try {
         jbInit();
         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 {
      imgFilesystem = new ImageIcon(FilesystemDialog.class.getResource("filesystem.gif"));
      brdSearch = BorderFactory.createEmptyBorder(5, 5, 5, 5);
      lblConnect.setIcon(imgFilesystem);
      this.setModal(true);
      this.setTitle("Attach Filesystem");
      this.setResizable(false);
      btnBrowse.setFont(new Font("Dialog", 0, 12));
      btnBrowse.setActionCommand("browse");
      btnBrowse.setText("browse");
      btnBrowse.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(ActionEvent e) {
            btnBrowse_actionPerformed(e);
         }
      });
      this.getContentPane().add(pnlDialog);
      pnlDialog.setLayout(thisLayout);

      pnlButtons.setLayout(pnlButtonsLayout);
      btnAttach.setFont(new Font("Dialog", 0, 12));
      btnAttach.setActionCommand("connect");
      btnAttach.setSelected(true);
      btnAttach.setText("attach");
      btnAttach.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            btnAttach_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);
         }
      });
      lblDirectory.setText("Directory");


      lblLabel.setText("Label");
      txtLabel.setText("");
      txtLabel.addKeyListener(new java.awt.event.KeyAdapter() {
         public void keyReleased(KeyEvent e) {
            txtLabel_keyReleased(e);
         }
      });

      txtDirectory.setEditable(false);

      pnlButtonsLayout.setHgap(5);
      pnlDialog.add(pnlButtons, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0
            ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 10, 10, 10), 0, 0));
      pnlButtons.add(btnAttach, BorderLayout.CENTER);
      pnlButtons.add(btnCancel,  BorderLayout.EAST);
      pnlDialog.add(lblDirectory, new GridBagConstraints(1, 1, 1, 2, 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(btnBrowse, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 10, 10, 10), 0, 0));
      pnlDialog.add(txtDirectory, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
   }

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

   void btnBrowse_actionPerformed(ActionEvent e) {
      Admin admin = Admin.getInstance();
      JFileChooser fc = new JFileChooser();
      fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
      fc.setMultiSelectionEnabled(false);
      int result = fc.showDialog(admin, "Select");
      if ( result == JFileChooser.APPROVE_OPTION ) {
         txtDirectory.setText(fc.getSelectedFile().getPath());
         if ( txtLabel.getText().trim().length() == 0 )
            txtLabel.setText(fc.getSelectedFile().getName());
         testRequired();
      }
   }

   void btnAttach_actionPerformed(ActionEvent e) {
      try {
         Admin admin = Admin.getInstance();

         String label = txtLabel.getText();
         String path = txtDirectory.getText();

         admin.browser.addFileSystem(path, label);
         admin.addFileSystem(path, label);

         hide();
      }
      catch ( Exception ex ) {
         Admin.getInstance().addMessage(ex.getMessage());
      }
   }

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

   void txtLabel_keyReleased(KeyEvent e) {
      testRequired();
   }

   void testRequired() {
      if ( txtLabel.getText().trim().length() == 0
        || txtDirectory.getText().trim().length() == 0 ) {
         btnAttach.setEnabled(false);
         return;
      }

      btnAttach.setEnabled(true);
   }
}
