/////////////////////////////////////////////////////////////////////////////
// Name:        dbattribute.cc
// Purpose:     Database Objects
// Author:      Daniel Horak
// Modified by:
// RCS-ID:      $Id: dbsimpleattr.cc,v 1.4 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 "dbsimpleattr.h"
#include "dbentity.h"
#include "dbattribute.h"
#include "project.h"

DBSimpleAttribute::DBSimpleAttribute(DataDesignerProject *project, DataDesignerContainer *container)
	:DBObject(DBSimpleAttrType, "simpleattribute", project, container), m_attr(NULL)
{
}

wxDialog *DBSimpleAttribute::Editor(bool edit)
{
	return new DBSimpleAttributeEditor(this, edit);
}

void DBSimpleAttribute::LoadXmlNode(wxXmlNode *node)
{
	if (node->GetName() == m_typestr) {
		m_name		= node->GetPropVal("name", wxEmptyString);
	} else {
		wxLogMessage("wrong type '%s'", node->GetName().c_str());
	}
	
	// must set pointer to the real attribute
	m_attr = (DBAttribute *)GetEntity()->GetAttributes()->GetObjectByName(m_name);
	if (m_attr && m_attr->GetType() == DBAttributeType)
		m_attr->AddSimpleAttribute(this);
	else
		wxLogMessage("DBSimpleAttribute::LoadXmlNode - cannot find real attribute for " + m_name);

	m_project->SetItemText(m_treeitemid, m_name);
}

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

DBEntity *DBSimpleAttribute::GetEntity()
{
	DBEntity	*entity = NULL;
	wxTreeItemId	treeid;

	treeid = GetContainer()->GetTreeItemId();			// list of this attrs
	treeid = GetProject()->GetParent(treeid);			// parent
	treeid = GetProject()->GetParent(treeid);			// list of foo for an entity
	treeid = GetProject()->GetParent(treeid);			// entity
	entity = (DBEntity *)(((DataDesignerItemData *)(GetProject()->GetItemData(treeid)))->GetObject());
	
	return entity;
}

/*
 * Editor
 */
DBSimpleAttributeEditor::DBSimpleAttributeEditor(DBObject *object, bool edit)
	: wxDialog(NULL, -1, _("Simple attribute"), wxDefaultPosition, wxSize(400,120)),
	m_object(object), m_edit(edit)
{
	wxString	**strings;
	int		idx;
	
	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);

	
	idx = 0;

	m_entity = ((DBSimpleAttribute *)object)->GetEntity();
	
	strings = m_entity->m_attrs->ListNames();
	new wxStaticText(m_panel_general, -1, _("Attribute"), 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);
	while (strings[idx]) {
		if (m_object->GetContainer()->GetObjectByName(*strings[idx]) == NULL)
			c1->Append(*strings[idx]);
		idx++;
	}
	delete strings;
	
	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();
}

DBSimpleAttributeEditor::~DBSimpleAttributeEditor()
{
}

bool DBSimpleAttributeEditor::TransferDataFromWindow()
{
	DBSimpleAttribute	*object = (DBSimpleAttribute *)GetObject();
	DBAttribute		*attr_old, *attr_new;

	object->m_name 		= c1->GetValue();
	attr_old 		= object->GetRealAttribute();
	attr_new		= (DBAttribute *)m_entity->m_attrs->GetObjectByName(object->m_name);
	
	if (attr_old != attr_new) {
		if (attr_old) {
			attr_old->DeleteSimpleAttribute(object);
		}
		attr_new->AddSimpleAttribute(object);
		
		object->SetRealAttribute(attr_new);
	}
	
	return TRUE;
}

bool DBSimpleAttributeEditor::TransferDataToWindow()
{
	DBSimpleAttribute	*object = (DBSimpleAttribute *)GetObject();
	
	c1->SetValue(object->m_name);
	
	return TRUE;
}

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

DBObject *DBSimpleAttributeContainer::CreateObject()
{
	return new DBSimpleAttribute(GetProject(), this);
}

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

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

DBSimpleAttributeListCtrl::~DBSimpleAttributeListCtrl()
{
}

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


syntax highlighted by Code2HTML, v. 0.9.1