/* 
 * © 2007-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifndef _GRTDBCONNECTPANEL_H_
#define _GRTDBCONNECTPANEL_H_

#include "wb_config.h"

#include <mforms/mforms.h>

#include "db_conn_be.h"

namespace grtui
{

class WBPUBLICBACKEND_PUBLIC_FUNC DbConnectPanel : public mforms::Box
{
public:
  DbConnectPanel(bool show_connection_combo= true);
  virtual ~DbConnectPanel();

  void init(const db_mgmt_ManagementRef &mgmt);
  void init(DbConnection *conn);
  
  void set_enabled(bool flag);
  
  mforms::TextEntry *get_name_entry() { return  &_name_entry; }

  DbConnection *get_be() const { return _connection; }

  void set_active_stored_conn(int stored_conn_index);

  void get_connection_details(int &rdbms_index, int &driver_index);
  
  db_mgmt_ConnectionRef get_default_connection() { return _anonymous_connection; }
  
  sigc::signal<void,std::string,bool> signal_validation_state_changed() { return _signal_validation_state_changed; }

protected:
  DbConnection *_connection;
  db_mgmt_ConnectionRef _anonymous_connection;

  mforms::Table _table;
  mforms::Label _label1;
  mforms::Label _label2;
  mforms::Label _label3;
  
  mforms::TextEntry _name_entry;
  mforms::Selector _stored_connection_sel;
  mforms::Selector _rdbms_sel;
  mforms::Selector _driver_sel;
  mforms::Label _desc1;
  mforms::Label _desc2;
  mforms::Label _desc3;
  
  mforms::TabView _tab;


  mforms::Table _params_table;
  std::vector<mforms::Box*> _param_rows;

  mforms::Table _advanced_table;
  std::vector<mforms::Box*> _advanced_rows;

  std::list<mforms::View*> _controls;
  
private:
  void save_param(const std::string& name, const grt::StringRef& param);
  std::string get_saved_param(const std::string& name);
  
  sigc::signal<void,std::string,bool> _signal_validation_state_changed;
  
  int _rdbms_index;
  int _driver_index;
  bool _initialized;
  bool _delete_connection_be;
  bool _show_connection_combo;
  bool _updating;
  std::string _last_validation;
  
  void clear_param_controls();
  void create_control(DbDriverParam *driver_param, ControlType ctrl_type, const Position &pos, const Size &size, const std::string &caption);

  void set_active_rdbms(int rdbms_index,
                        int driver_index);

  void set_active_driver(int driver_index);
  
  void change_active_rdbms();
  void change_active_driver();
  
  void param_value_changed(mforms::Control *sender);
  
  void refresh_stored_connections();
  
  void change_active_stored_conn();
  void reset_stored_conn_list();
  
  int open_editor();
};

};


#endif /* _GRTDBCONNECTFORM_H_ */
