#include "stdafx.h"

#include "wb_overview_connections.h"
#include "wb_context.h"
#include "grtpp_undo_manager.h"

using namespace wb;
using namespace bec;


class ConnectionNode : public OverviewBE::ObjectNode
{
public:
  ConnectionNode(const db_mgmt_ConnectionRef &conn)
  {
    object= conn;

    type= OverviewBE::OItem;
    label= conn->name();
  }

  virtual bool activate(WBContext *wb)
  {
    g_message("activate connection");
    return true;
  }

  virtual bool is_deletable() 
  {
    return false;
  }

  virtual void delete_object(WBContext *wb)
  {
    //wb->delete_diagram(db_mgmt_ConnectionRef::cast_from(object));
  }


  virtual bool rename(WBContext *wb, const std::string &name)
  {
    grt::AutoUndo undo(wb->get_grt());
    
    object->name(name);
    
    undo.end(strfmt(_("Rename Connection to '%s'"), name.c_str()));
    
    return true;
  }


  virtual bool is_renameable() 
  { 
    return true;
  }

  virtual int get_popup_menu_items(WBContext *wb, bec::MenuItemList &items)
  {
    return 0;
  }
};


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


ConnectionListNode::ConnectionListNode(const grt::ListRef<db_mgmt_Connection> &connections)
: ContainerNode(OverviewBE::OItem), _connections(connections)
{
  id= "connectionlist";

  type= OverviewBE::ODivision;
  label= _("Database Connections");
  small_icon= 0;
  large_icon= 0;
  expanded= true;
  display_mode= OverviewBE::MLargeIcon;

  refresh_children();
}


bool ConnectionListNode::add_new(WBContext *wb)
{
  return false;
}


void ConnectionListNode::refresh_children()
{
  clear_children();

  for (size_t c= _connections.count(), i= 0; i < c; i++)
  {
    ConnectionNode *node= new ConnectionNode(_connections[i]);

    std::string icon_name= _connections.content_class_name() + ".$.png";
    node->small_icon= IconManager::get_instance()->get_icon_id(icon_name, Icon16);
    node->large_icon= IconManager::get_instance()->get_icon_id(icon_name, Icon48);

    children.push_back(node);
  }

  OverviewBE::AddObjectNode *node= new OverviewBE::AddObjectNode(sigc::mem_fun(this, &ConnectionListNode::add_new));

  node->type= OverviewBE::OItem;
  node->label= _("Add Connection");

  std::string icon_name= _connections.content_class_name() + ".$.png";
  node->small_icon= IconManager::get_instance()->get_icon_id(icon_name, Icon16, "add");
  node->large_icon= IconManager::get_instance()->get_icon_id(icon_name, Icon48, "add");

  children.insert(children.begin(), node);
}

