/////////////////////////////////////////////////////////////////////////////
// Name:        dbrelattr.cc
// Purpose:     Database Objects
// Author:      Daniel Horak
// Modified by:
// RCS-ID:      $Id: dbrelattr.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 <wx/ogl/ogl.h>
#include "config.h"
#include "xml.h"
#include "dbobject.h"
#include "dbrelattr.h"
#include "dbrelation.h"
#include "dbentity.h"
#include "dbattribute.h"
#include "project.h"


DBRelationAttribute::DBRelationAttribute(DataDesignerProject *project, DataDesignerContainer *container)
	:DBObject(DBRelationAttrType, "attributebind", project, container)
{
}

wxTreeItemId DBRelationAttribute::AppendItem()
{
	wxString name;
	
	if (! m_appended) {
		name = m_parent + " <-> " + m_child;
		m_treeitemid = m_project->AppendItem(m_container->GetTreeItemId(), name, -1, -1, new DataDesignerItemData(this));
		m_appended = TRUE;
	}
	
	return m_treeitemid;
}

wxDialog *DBRelationAttribute::Editor(bool edit)
{
	return new DBRelationAttributeEditor(this, edit);
}

void DBRelationAttribute::LoadXmlNode(wxXmlNode *node)
{
	if (node->GetName() == m_typestr) {
		m_parent	= node->GetPropVal("parent", wxEmptyString);
		m_child		= node->GetPropVal("child", wxEmptyString);
	} else {
		wxLogMessage("wrong type '%s'", node->GetName().c_str());
	}
}

wxXmlNode *DBRelationAttribute::GetXmlNode()
{
	wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, m_typestr);
	
	node->AddProperty("parent", m_parent);
	node->AddProperty("child", m_child);
	
	return node;
}

/*
 * Editor
 */
DBRelationAttributeEditor::DBRelationAttributeEditor(DBObject *object, bool edit)
	: wxDialog(NULL, -1, _("Related Attributes"), wxDefaultPosition, wxSize(500,200)),
	m_object(object), m_edit(edit)
{
	wxString	**strings;
	int		idx;
	DBEntity	*entity;
	DataDesignerProject	*project;
	wxTreeItemId	treeid;
	DBRelation	*relation;
	
	m_panel_general = new wxPanel(this);
	m_panel_button = new wxPanel(this);
	
	wxLayoutConstraints *c = new wxLayoutConstraints;
	c->top.SameAs	(this, wxTop);
	c->left.SameAs	(this, wxLeft);
	c->right.SameAs	(this, wxRight);
	c->bottom.SameAs(m_panel_button, wxTop);
	m_panel_general->SetConstraints(c);
	
	c = new wxLayoutConstraints;
	c->height.Absolute(40);
	c->left.SameAs	(this, wxLeft);
	c->right.SameAs	(this, wxRight);
	c->bottom.SameAs(this, wxBottom);
	m_panel_button->SetConstraints(c);
	

	project = object->GetProject();
#ifdef ENABLE_DEBUG
	wxLogMessage("project=%p", project);
#endif
	
	treeid = object->GetContainer()->GetTreeItemId();	// list of attribute pairs for a relation
	if (!treeid.IsOk()) {
		wxLogMessage("1. parent is not OK");
		return;
	}
	treeid = project->GetParent(treeid);			// relation
	if (!treeid.IsOk()) {
		wxLogMessage("2. parent is not OK");
		return;
	}
	relation = (DBRelation *)(((DataDesignerItemData *)(project->GetItemData(treeid)))->GetObject());
	
	if (relation) {
		new wxStaticText(m_panel_general, -1, _("Parent"), wxPoint(10,10), wxSize(100,-1), wxALIGN_RIGHT);
		c1 = new wxComboBox(m_panel_general, -1, wxEmptyString, wxPoint(120,10), wxSize(100,-1), 0, NULL, wxCB_SORT | wxCB_READONLY);

		entity = (DBEntity *)(project->m_top_entities->GetObjectByName(relation->m_parent));
		if (entity) {
			idx = 0;
			strings = entity->m_attrs->ListNames();
			while (strings[idx]) {
				c1->Append(*strings[idx]);
				idx++;
			}
			delete strings;
		} else {
			wxLogMessage("cannot find parent entity");
		}

		new wxStaticText(m_panel_general, -1, _("Child"), wxPoint(10,35), wxSize(100,-1), wxALIGN_RIGHT);
		c2 = new wxComboBox(m_panel_general, -1, wxEmptyString, wxPoint(120,35), wxSize(100,-1), 0, NULL, wxCB_SORT | wxCB_READONLY);

		entity = (DBEntity *)(project->m_top_entities->GetObjectByName(relation->m_child));
		if (entity) {
			idx = 0;
			strings = entity->m_attrs->ListNames();
			while (strings[idx]) {
				c2->Append(*strings[idx]);
				idx++;
			}
			delete strings;
		} else {
			wxLogMessage("cannot find child entity");
		}
	} else {
		wxLogMessage("cannot find relation");
	}
	
	m_button_ok	= new wxButton(m_panel_button, wxID_OK, _("OK"), wxPoint(10,10), wxSize(60,-1));
	m_button_cancel	= new wxButton(m_panel_button, wxID_CANCEL, _("Cancel"), wxPoint(90,10), wxSize(60,-1));
	
	SetAutoLayout(TRUE);
	Layout();
	Center();
}

DBRelationAttributeEditor::~DBRelationAttributeEditor()
{
}

bool DBRelationAttributeEditor::TransferDataFromWindow()
{
	DBRelationAttribute	*object = (DBRelationAttribute *)GetObject();

	object->m_parent	= c1->GetValue();
	object->m_child		= c2->GetValue();
	
	return TRUE;
}

bool DBRelationAttributeEditor::TransferDataToWindow()
{
	DBRelationAttribute	*object = (DBRelationAttribute *)GetObject();
	
	c1->SetValue(object->m_parent);
	c2->SetValue(object->m_child);
	
	return TRUE;
}

bool DBRelationAttributeEditor::Validate()
{
	return TRUE;
}

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

DBObject *DBRelationAttributeContainer::CreateObject()
{
	return new DBRelationAttribute(GetProject(), this);
}

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

/*
 * ObjectList
 */
DBRelationAttributeListCtrl::DBRelationAttributeListCtrl(wxWindow *parent, DataDesignerContainer *container)
	: DBObjectListCtrl(parent, container)
{
	SetColumnTitle(0, _("Parent"));
	InsertColumn(1, _("Child"));
}

DBRelationAttributeListCtrl::~DBRelationAttributeListCtrl()
{
}

void DBRelationAttributeListCtrl::AddObject(long item, DBObject *object)
{
	InsertItem(item, "tmp");
	SetItemData(item, (long)object);
}

void DBRelationAttributeListCtrl::SetObject(long item, DBObject *object)
{
	DBRelationAttribute *attribute = (DBRelationAttribute *)object;
	
	SetItem(item, 0, attribute->m_parent);
	SetItem(item, 1, attribute->m_child);
}


syntax highlighted by Code2HTML, v. 0.9.1