Example #1
0
void DbViewerPanel::RefreshDbView()
{
    // Refresh all connections
    wxTreeItemId root = m_treeDatabases->GetRootItem();
    if(root.IsOk()) {
        wxTreeItemIdValue cookie;
        wxTreeItemId child = m_treeDatabases->GetFirstChild(root, cookie);
        while(child.IsOk()) {
            DbItem* data = (DbItem*)m_treeDatabases->GetItemData(child);
            if(data) {
                DbConnection* pCon = wxDynamicCast(data->GetData(), DbConnection);
                if(pCon) pCon->RefreshChildren();
                Database* pDb = wxDynamicCast(data->GetData(), Database);
                if(pDb) pDb->RefreshChildren(false);
                Table* pTab = wxDynamicCast(data->GetData(), Table);
                if(pTab) pTab->RefreshChildren();
            }
            child = m_treeDatabases->GetNextChild(root, cookie);
        }
    }
    // clear items from tree
    m_treeDatabases->DeleteAllItems();
    // create imageList for icons
    wxImageList* pImageList = new wxImageList(16, 16, true, 3);
    pImageList->Add(m_mgr->GetStdIcons()->LoadBitmap(wxT("toolbars/16/standard/file_open"))); // folder icon
    pImageList->Add(m_mgr->GetStdIcons()->LoadBitmap(wxT("db-explorer/16/table")));           // table icon
    pImageList->Add(m_mgr->GetStdIcons()->LoadBitmap(wxT("toolbars/16/search/find")));        // view icon
    pImageList->Add(m_mgr->GetStdIcons()->LoadBitmap(wxT("db-explorer/16/database")));        // database
    pImageList->Add(m_mgr->GetStdIcons()->LoadBitmap(wxT("db-explorer/16/column")));          // column

    m_treeDatabases->AssignImageList(pImageList);

    wxTreeItemId totalRootID = m_treeDatabases->AddRoot(wxString::Format(wxT("Databases")), -1);

    // ---------------- load connections ----------------------------
    SerializableList::compatibility_iterator connectionNode = m_pConnections->GetFirstChildNode();
    while(connectionNode) {
        DbConnection* pDbCon = (DbConnection*)wxDynamicCast(connectionNode->GetData(), DbConnection);
        if(pDbCon) {
            wxTreeItemId rootID =
                m_treeDatabases->AppendItem(totalRootID,
                                            wxString::Format(wxT("Databases (%s)"), pDbCon->GetServerName().c_str()),
                                            3,
                                            3,
                                            new DbItem(pDbCon));

            // ----------------------- load databases -------------------------------
            SerializableList::compatibility_iterator dbNode = pDbCon->GetFirstChildNode();
            while(dbNode) {
                Database* pDatabase = wxDynamicCast(dbNode->GetData(), Database);
                if(pDatabase) {
                    wxTreeItemId dbID =
                        m_treeDatabases->AppendItem(rootID, pDatabase->GetName(), 3, 3, new DbItem(pDatabase));
                    m_treeDatabases->Expand(rootID);
                    wxTreeItemId idFolder = m_treeDatabases->AppendItem(dbID, wxT("Tables"), 0, 0, NULL);
                    // m_treeDatabases->Expand(dbID);

                    // ----------------------------- load tables ----------------------------------
                    SerializableList::compatibility_iterator tabNode = pDatabase->GetFirstChildNode();
                    while(tabNode) {
                        Table* pTable = wxDynamicCast(tabNode->GetData(), Table);
                        if(pTable) {
                            // wxTreeItemId tabID = m_treeDatabases->AppendItem(idFolder,pTable->getName(),1,-1,new
                            // DbItem(NULL,pTable)); //NULL);
                            wxTreeItemId tabID = m_treeDatabases->AppendItem(idFolder,
                                                                             pTable->GetName(),
                                                                             1,
                                                                             1,
                                                                             new DbItem(pTable)); // NULL);

                            ///////////////////////////////////////////////////////////
                            // Add the columns
                            ///////////////////////////////////////////////////////////
                            SerializableList::compatibility_iterator columnNode = pTable->GetFirstChildNode();
                            while(columnNode) {
                                Column* col = wxDynamicCast(columnNode->GetData(), Column);
                                if(col) {
                                    m_treeDatabases->AppendItem(
                                        tabID, col->FormatName().c_str(), 4, 4, new DbItem(col));
                                }
                                columnNode = columnNode->GetNext();
                            }
                        }
                        tabNode = tabNode->GetNext();
                    }
                    // ----------------------------------------------------------------------------

                    idFolder = m_treeDatabases->AppendItem(dbID, wxT("Views"), 0, 0, NULL);
                    // m_treeDatabases->Expand(dbID);

                    // ----------------------------- load views ----------------------------------
                    tabNode = pDatabase->GetFirstChildNode();
                    while(tabNode) {
                        View* pView = wxDynamicCast(tabNode->GetData(), View);
                        if(pView) {
                            // wxTreeItemId tabID = m_treeDatabases->AppendItem(idFolder,pTable->getName(),1,-1,new
                            // DbItem(NULL,pTable)); //NULL);
                            m_treeDatabases->AppendItem(idFolder, pView->GetName(), 2, 2, new DbItem(pView)); // NULL);
                        }
                        tabNode = tabNode->GetNext();
                    }
                    // ----------------------------------------------------------------------------
                }
                dbNode = dbNode->GetNext();
            }
            // -----------------------------------------------------------------------
        }
        connectionNode = connectionNode->GetNext();
    }
}