/////////////////////////////////////////////////////////////////////////////
// Name:        dbmodel.cc
// Purpose:     Database Objects
// Author:      Daniel Horak
// Modified by:
// RCS-ID:      $Id: dbmodel.cc,v 1.5 2004/01/04 18:32:16 horakdan Exp $
// Copyright:   (c) Daniel Horak
// Licence:     GPL
/////////////////////////////////////////////////////////////////////////////

// ============================================================================
// declarations
// ============================================================================

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWindows headers
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include <wx/notebook.h>
#include <wx/ogl/ogl.h>
#include "config.h"
#include "xml.h"
#include "dbobject.h"
#include "dbmodel.h"
#include "dbentity.h"
#include "dbmodelentity.h"
#include "dbmodelrelation.h"
#include "project.h"

DBModel::DBModel(DataDesignerProject *project, DataDesignerContainer *container)
	:DBObject(DBModelType, "model", project, container), m_entities(NULL), m_relations(NULL)
{
}

DBModel::~DBModel()
{
	if (m_entities) {
		m_project->DeleteChildren(m_entities->GetTreeItemId());
		delete m_entities;
	}

	if (m_relations) {
		m_project->DeleteChildren(m_relations->GetTreeItemId());
		delete m_relations;
	}
}

wxDialog *DBModel::Editor(bool edit)
{
	return new DBModelEditor(this, edit);
}

void DBModel::LoadXmlNode(wxXmlNode *node)
{
	if (node->GetName() == m_typestr) {
		m_name		= node->GetPropVal("name", wxEmptyString);
		
		wxXmlNode *child = node->GetChildren();
		wxString name;
		
		while (child) {
			name = child->GetName();
			
			if (name == "modelentities")
				m_entities->LoadXmlNode(child);
				
			child = child->GetNext();
		}
	} else {
		wxLogMessage("wrong type '%s'", node->GetName().c_str());
	}

	m_project->SetItemText(m_treeitemid, m_name);
}

wxXmlNode *DBModel::GetXmlNode()
{
	wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, m_typestr);
	
	node->AddProperty("name", m_name);
	
	node->AddChild(m_entities->GetXmlNode());
	
	return node;
}

wxTreeItemId DBModel::AppendItem()
{
	if (! m_appended) {
		m_treeitemid	= m_project->AppendItem(m_container->GetTreeItemId(), m_name, -1, -1, new DataDesignerItemData(this));
		m_entities	= new DBModelEntityContainer(m_project, m_project->AppendItem(m_treeitemid, _("Entities")));
		m_relations	= new DBModelRelationContainer(m_project, m_project->AppendItem(m_treeitemid, _("Relations")));
		m_appended	= TRUE;
	}

	return m_treeitemid;
}

/*
 * Editor
 */
BEGIN_EVENT_TABLE(DBModelEditor, DBObjectEditor)
	EVT_BUTTON(wxID_APPLY, DBModelEditor::OnApply)
END_EVENT_TABLE()
		
DBModelEditor::DBModelEditor(DBObject *object, bool edit)
	: DBObjectEditor(_("Model"), wxSize(500,300), object, edit)
{
	m_page_entities	= new wxPanel(m_notebook);
	
	m_list_entities	= new DBModelEntityListCtrl(m_page_entities, ((DBModel *)GetObject())->m_entities);
	
	if (m_edit) {
		((DBModel *)GetObject())->m_entities->SetList(m_list_entities);
		((DBModel *)GetObject())->m_entities->AddObjectsToList();
	}

	wxLayoutConstraints *c = new wxLayoutConstraints;
	c->top.SameAs   (m_page_entities, wxTop);
	c->left.SameAs  (m_page_entities, wxLeft);
	c->right.SameAs (m_page_entities, wxRight);
	c->bottom.SameAs(m_page_entities, wxBottom);
	m_list_entities->SetConstraints(c);
	
	m_page_entities->SetAutoLayout(TRUE);

	m_notebook->InsertPage(m_notebook->GetPageCount() - 1, m_page_entities, _("Entities"));

	m_button_apply = AddButton(wxID_APPLY, _("Apply"), wxSize(60,-1));
	
	if (m_edit) {
		m_button_ok->SetDefault();
	} else {
		m_button_apply->SetDefault();
		
		m_page_entities->Disable();
	}
}

DBModelEditor::~DBModelEditor()
{
}

void DBModelEditor::OnApply(wxCommandEvent& event)
{
	DBModel	*object = (DBModel *)GetObject();

	if (Validate() == FALSE)
		return;
		
	TransferDataFromWindow();
	
	object->AppendItem();
	m_list_entities->SetContainer(object->m_entities);
	
	m_page_entities->Enable();
	
	m_button_apply->Disable();
	m_button_ok->SetDefault();
	
	m_edit = TRUE;
}

/*
 * Container
 */
DBModelContainer::DBModelContainer(DataDesignerProject *project, const wxTreeItemId& parent)
	: DataDesignerContainer(project, parent, "models")
{
}

DBObject *DBModelContainer::CreateObject()
{
	return new DBModel(GetProject(), this);
}

void DBModelContainer::ShowList()
{
	SetList(new DBModelListCtrl(GetProject()->GetSplitter(), this));
	
	DataDesignerContainer::AddObjectsToListAndShow();
}

/*
 * ObjectList
 */
DBModelListCtrl::DBModelListCtrl(wxWindow *parent, DataDesignerContainer *container)
	: DBObjectListCtrl(parent, container)
{
}

DBModelListCtrl::~DBModelListCtrl()
{
}

void DBModelListCtrl::SetObject(long item, DBObject *object)
{
	DBModel *attribute = (DBModel *)object;
}


syntax highlighted by Code2HTML, v. 0.9.1