#include "stdafx.h"

#include "wb_overview_diagram.h"
#include "wb_context.h"
#include "grt/icon_manager.h"

using namespace bec;
using namespace wb;

class AddDiagramNode : public OverviewBE::Node
{
public:
  model_ModelRef model;

  virtual bool activate(WBContext *wb)
  {
    wb->add_new_diagram(model);
    return true;
  }
};


class DiagramNode : public OverviewBE::ObjectNode
{
public:
  DiagramNode(const model_DiagramRef &view) { object= view; }

  virtual bool activate(WBContext *wb)
  {
    wb->switch_diagram(model_DiagramRef::cast_from(object));
    return true;
  }

  virtual bool is_deletable() 
  {
    return true;
  }

  virtual void delete_object(WBContext *wb)
  {
    wb->delete_diagram(model_DiagramRef::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 Diagram 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;
  }
};


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


DiagramListNode::DiagramListNode(model_ModelRef model)
: ContainerNode(OverviewBE::OItem), _model(model)
{
  id= model.id()+"/modellist";

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

  refresh_children();
}


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

  if (_model->diagrams().is_valid())
  {
    for (size_t c= _model->diagrams().count(), i= 0; i < c; i++)
    {
      DiagramNode *node= new DiagramNode(_model->diagrams()[i]);

      node->type= OverviewBE::OItem;
      node->label= *_model->diagrams()[i]->name();

      std::string icon_name= _model->diagrams().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);
    }

    AddDiagramNode *node= new AddDiagramNode();

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

    std::string icon_name= _model->diagrams().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");
    node->model= _model;

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