/////////////////////////////////////////////////////////////////////////////
// Name:        dbindex.cc
// Purpose:     Database Objects
// Author:      Daniel Horak
// Modified by:
// RCS-ID:      $Id: dbindex.cc,v 1.3 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 "config.h"
#include "xml.h"
#include "dbobject.h"
#include "dbindex.h"
#include "dbsimpleattr.h"


DBIndex::DBIndex(DataDesignerProject *project, DataDesignerContainer *container)
	:DBObject(DBIndexType, "index", project, container), m_unique(TRUE), m_attrs(NULL)
{
}

DBIndex::~DBIndex()
{
	if (m_attrs) {
		m_project->DeleteChildren(m_attrs->GetTreeItemId());
		delete m_attrs;
	}
}

wxDialog *DBIndex::Editor(bool edit)
{
	return new DBIndexEditor(this, edit);
}

void DBIndex::LoadXmlNode(wxXmlNode *node)
{
	wxXmlNode	*child;

	if (node->GetName() == m_typestr) {
		DBObject::LoadXmlNode(node);
	
		LoadBoolProperty(node, "unique", m_unique);
		
		child = node->GetChildren();
		while (child) {
			if (child->GetName() == "unique") {
				LoadBoolNode(child, "unique", m_unique);
			} else if (child->GetName() == "simpleattributes") {
				m_attrs->LoadXmlNode(child);
			}
			child = child->GetNext();
		}
	} else {
		wxLogMessage("wrong type '%s'", node->GetName().c_str());
	}
}

wxTreeItemId DBIndex::AppendItem()
{
	if (! m_appended) {
		m_treeitemid	= m_project->AppendItem(m_container->GetTreeItemId(), m_name, -1, -1, new DataDesignerItemData(this));
		m_attrs		= new DBSimpleAttributeContainer(m_project, m_project->AppendItem(m_treeitemid, _("Index's Attributes")));
		m_appended	= TRUE;
	}

	return m_treeitemid;
}

wxXmlNode *DBIndex::GetXmlNode()
{
	wxXmlNode *node = DBObject::GetXmlNode();
	
	node->AddChild(GetBoolNode("unique", m_unique));
	node->AddChild(m_attrs->GetXmlNode());
	
	return node;
}

/*
 * Editor
 */
BEGIN_EVENT_TABLE(DBIndexEditor, DBObjectEditor)
	EVT_BUTTON(wxID_APPLY, DBIndexEditor::OnApply)
END_EVENT_TABLE()

DBIndexEditor::DBIndexEditor(DBObject *object, bool edit)
	: DBObjectEditor(_("Index"), wxSize(500,200), object, edit)
{
	m_page_attrs = new wxPanel(m_notebook);

        m_list_attrs = new DBSimpleAttributeListCtrl(m_page_attrs,((DBIndex *)GetObject())->m_attrs);
	
	if (m_edit) {
		((DBIndex *)GetObject())->m_attrs->SetList(m_list_attrs);
		((DBIndex *)GetObject())->m_attrs->AddObjectsToList();
	}

	wxLayoutConstraints *c = new wxLayoutConstraints;
	c->top.SameAs   (m_page_attrs, wxTop);
	c->left.SameAs  (m_page_attrs, wxLeft);
	c->right.SameAs (m_page_attrs, wxRight);
	c->bottom.SameAs(m_page_attrs, wxBottom);
	m_list_attrs->SetConstraints(c);

	m_page_attrs->SetAutoLayout(TRUE);
	
	m_notebook->InsertPage(m_notebook->GetPageCount() - 1, m_page_attrs, _("Attributes"));
		
	c1 = new wxCheckBox(m_page_general, -1, _("Unique"), wxPoint(10,85), wxSize(100,-1));
	
	m_button_apply = AddButton(wxID_APPLY, _("Apply"), wxSize(60,-1));

	if (m_edit) {
		m_button_ok->SetDefault();
	} else {
		m_button_apply->SetDefault();
		
		m_page_attrs->Disable();
	}
}

DBIndexEditor::~DBIndexEditor()
{
}

bool DBIndexEditor::TransferDataFromWindow()
{
	DBIndex	*object = (DBIndex *)GetObject();

	DBObjectEditor::TransferDataFromWindow();
	
	object->m_unique = c1->GetValue();
	
	return TRUE;
}

bool DBIndexEditor::TransferDataToWindow()
{
	DBIndex	*object = (DBIndex *)GetObject();
	
	DBObjectEditor::TransferDataToWindow();
	
	c1->SetValue(object->m_unique);

	return TRUE;
}

void DBIndexEditor::OnApply(wxCommandEvent& event)
{
	DBIndex	*object = (DBIndex *)GetObject();
	
	if (Validate() == FALSE)
		return;
		
	TransferDataFromWindow();
	
	object->AppendItem();
	m_list_attrs->SetContainer(object->m_attrs);

	m_page_attrs->Enable();
	
	m_button_apply->Disable();
	m_button_ok->SetDefault();

	m_edit = TRUE;
}

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

DBObject *DBIndexContainer::CreateObject()
{
	return new DBIndex(GetProject(), this);
}

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

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

DBIndexListCtrl::~DBIndexListCtrl()
{
}

void DBIndexListCtrl::SetObject(long item, DBObject *object)
{
	DBIndex *index = (DBIndex *)object;
}


syntax highlighted by Code2HTML, v. 0.9.1