/////////////////////////////////////////////////////////////////////////////
// Name:        dbdomain.cc
// Purpose:     Database Objects
// Author:      Daniel Horak
// Modified by:
// RCS-ID:      $Id: dbdomain.cc,v 1.2 2004/01/01 13:56:19 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 "config.h"
#include "xml.h"
#include "dbobject.h"
#include "dbdomain.h"
#include "servers/dbserver.h"


DBDomain::DBDomain(DataDesignerProject *project, DataDesignerContainer *container)
	:DBObject(DBDomainType, "domain", project, container), m_nullable(TRUE)
{
}

wxDialog *DBDomain::Editor(bool edit)
{
	return new DBDomainEditor(this, edit);
}

void DBDomain::LoadXmlNode(wxXmlNode *node)
{
	if (node->GetName() == m_typestr) {
		DBObject::LoadXmlNode(node);
		
		m_basetype	= node->GetPropVal("basetype", wxEmptyString);
		m_length	= node->GetPropVal("length", wxEmptyString);
		m_decimals	= node->GetPropVal("decimals", wxEmptyString);
		m_default	= node->GetPropVal("default", wxEmptyString);
		m_check		= node->GetPropVal("check", wxEmptyString);
		LoadBoolProperty(node, "nullable", m_nullable);
		
		wxXmlNode *child = node->GetChildren();
		wxString name;
		while (child) {
			name = child->GetName();
			if (name == "basetype")
				LoadTextNode(child, "basetype", m_basetype);
			else if (name == "length")
				LoadTextNode(child, "length", m_length);
			else if (name == "decimals")
				LoadTextNode(child, "decimals", m_decimals);
			else if (name == "default")
				LoadTextNode(child, "default", m_default);
			else if (name == "check")
				LoadTextNode(child, "check", m_check);
			else if (name == "nullable")
				LoadBoolNode(child, "nullable", m_nullable, TRUE);
			child = child->GetNext();
		}
	} else {
		wxLogMessage("wrong type '%s'", node->GetName().c_str());
	}
}

wxXmlNode *DBDomain::GetXmlNode()
{
	wxXmlNode *node = DBObject::GetXmlNode();
	
	node->AddChild(GetTextNode("basetype",	m_basetype));
	node->AddChild(GetTextNode("length",	m_length));
	node->AddChild(GetTextNode("decimals",	m_decimals));
	node->AddChild(GetTextNode("default",	m_default));
	node->AddChild(GetTextNode("check",	m_check));
	node->AddChild(GetBoolNode("nullable",	m_nullable));

	return node;
}

/*
 * Editor
 */
DBDomainEditor::DBDomainEditor(DBObject *object, bool edit)
	: DBObjectEditor(_("Domain"), wxSize(500,250), object, edit)
{
	DBServer	*server;

	server = GetObject()->GetProject()->GetServer();
	new wxStaticText(m_page_general, -1, _("Base type"), wxPoint(10,60), wxSize(80,-1), wxALIGN_RIGHT);
        c1 = new wxComboBox(m_page_general, -1, wxEmptyString, wxPoint(100,60), wxSize(150,-1),
		server->GetTypesCount(), server->GetTypes(), wxCB_SORT /*| wxCB_READONLY*/);

	new wxStaticText(m_page_general, -1, _("Length"), wxPoint(10,85), wxSize(80,-1), wxALIGN_RIGHT);
	t2 = new wxTextCtrl(m_page_general, -1, wxEmptyString, wxPoint(100,85), wxSize(50,-1));

	new wxStaticText(m_page_general, -1, _("Decimals"), wxPoint(160,85), wxSize(130,-1), wxALIGN_RIGHT);
	t3 = new wxTextCtrl(m_page_general, -1, wxEmptyString, wxPoint(300,85), wxSize(50,-1));
	
	c4 = new wxCheckBox(m_page_general, -1, _("Nullable"), wxPoint(100,110), wxSize(100,-1));

	new wxStaticText(m_page_general, -1, _("Default"), wxPoint(10,135), wxSize(80,-1), wxALIGN_RIGHT);
	t5 = new wxTextCtrl(m_page_general, -1, wxEmptyString, wxPoint(100,135), wxSize(100,-1));

	new wxStaticText(m_page_general, -1, _("Check"), wxPoint(210,135), wxSize(80,-1), wxALIGN_RIGHT);
	t6 = new wxTextCtrl(m_page_general, -1, wxEmptyString, wxPoint(300,135), wxSize(170,-1));
}

DBDomainEditor::~DBDomainEditor()
{
}

bool DBDomainEditor::TransferDataFromWindow()
{
	DBDomain	*object = (DBDomain *)GetObject();
	
	DBObjectEditor::TransferDataFromWindow();

	object->m_basetype 	= c1->GetValue();
	object->m_length 	= t2->GetValue();
	object->m_decimals	= t3->GetValue();
	object->m_nullable	= c4->GetValue();
	
	return TRUE;
}

bool DBDomainEditor::TransferDataToWindow()
{
	DBDomain	*object = (DBDomain *)GetObject();
	
	DBObjectEditor::TransferDataToWindow();

	c1->SetValue(object->m_basetype);
	t2->SetValue(object->m_length);
	t3->SetValue(object->m_decimals);
	c4->SetValue(object->m_nullable);
	
	return TRUE;
}

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

DBObject *DBDomainContainer::CreateObject()
{
	return new DBDomain(GetProject(), this);
}

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

/*
 * ObjectList
 */
DBDomainListCtrl::DBDomainListCtrl(wxWindow *parent, DataDesignerContainer *container)
	: DBObjectListCtrl(parent, container)
{
	InsertColumn(1, _("Basetype"));
	InsertColumn(2, _("Length"));
	InsertColumn(3, _("Decimals"));
}

DBDomainListCtrl::~DBDomainListCtrl()
{
}

void DBDomainListCtrl::SetObject(long item, DBObject *object)
{
	DBDomain *domain = (DBDomain *)object;
	
	SetItem(item, 1, domain->m_basetype);
	SetItem(item, 2, domain->m_length);
	SetItem(item, 3, domain->m_decimals);
}


syntax highlighted by Code2HTML, v. 0.9.1