TreeNode* GetLastCommonParent(TreeNode* pRoot,TreeNode* pNode1,TreeNode* pNode2){ if(pRoot==NULL || pNode1==NULL || pNode2==NULL) return NULL; list<TreeNode*> path1; GetNodePath(pRoot,pNode1,path1); list<TreeNode*> path2; GetNodePath(pRoot,pNode2,path2); return GetLastCommonNode(path1,path2); }
// Attempt to reselect the node with the given path bool frmMain::SetCurrentNode(wxTreeItemId node, const wxString &origPath) { wxString path = origPath.Lower(); wxTreeItemIdValue cookie; wxTreeItemId child = browser->GetFirstChild(node, cookie); while (child.IsOk()) { wxString actNodePath = GetNodePath(child).Lower(); if(path.StartsWith(actNodePath)) { if(!browser->IsExpanded(child)) { browser->SelectItem(child, true); browser->Expand(child); } if (actNodePath == path) { browser->SelectItem(child, true); return true; } else if (SetCurrentNode(child, path)) return true; } child = browser->GetNextChild(node, cookie); } return false; }
//找出根结点到所要查询的结点的路径存储到链表中 bool GetNodePath(BinTree r,int v,std::vector<BinNode*> &list){ if (ToInt(r) == v) { return true; } list.push_back(r); bool found = false; if(r->lchild != NULL){ found = GetNodePath(r->lchild,v,list); } if(!found && r->rchild != NULL){ found = GetNodePath(r->rchild,v,list); } if (!found){ list.pop_back(); } return found; }
void __fastcall TfrmOpenFTPDialog::tvFolderChange(TObject *Sender, TTreeNode *Node) { AnsiString Path; GetNodePath(Node,Path); if(Path != IdFTP1->RetrieveCurrentDir()) { IdFTP1->ChangeDir(Path); ListDir(Node); } }
//--------------------------------------------------------------------------- void __fastcall TfrmOpenFTPDialog::GetNodePath(TTreeNode * Node,AnsiString & Path) { if(Node->Text.SubString(Node->Text.Length(),1) != "/") Path = Node->Text + "/" + Path; else Path = Node->Text + Path; if(Node->Parent != NULL && Node->Text != "/") { GetNodePath(Node->Parent,Path); } }
//找出二叉树中两个结点的最近公共祖先 BinNode *LastCommonParent(BinTree r, int v1, int v2) { BinNode *pLast = NULL; // 最近的公共祖先结点 std::vector<BinNode*> list1, list2; // 根结点到v1(v2)结点的路径链表 GetNodePath(r, v1, list1); GetNodePath(r, v2, list2); //问题转化为求两个链表的首个公共结点 if (list1.empty() || list2.empty()) return NULL; std::vector<BinNode*>::iterator iter1, iter2; iter1 = list1.begin(); iter2 = list2.begin(); while (iter1 != list1.end() && iter2 != list2.end()) { if (*iter1 == *iter2) { pLast = *iter1; } iter1++; iter2++; } return pLast; }
void frmMain::ExpandChildNodes(wxTreeItemId node, wxArrayString &expandedNodes) { wxTreeItemIdValue cookie; wxTreeItemId child = browser->GetFirstChild(node, cookie); while (child.IsOk()) { if (expandedNodes.Index(GetNodePath(child)) != wxNOT_FOUND) { browser->Expand(child); ExpandChildNodes(child, expandedNodes); } child = browser->GetNextChild(node, cookie); } }
void frmMain::GetExpandedChildNodes(wxTreeItemId node, wxArrayString &expandedNodes) { wxTreeItemIdValue cookie; wxTreeItemId child = browser->GetFirstChild(node, cookie); while (child.IsOk()) { if (browser->IsExpanded(child)) { GetExpandedChildNodes(child, expandedNodes); expandedNodes.Add(GetNodePath(child)); } child = browser->GetNextChild(node, cookie); } }
//生成要列出文件的目录列表 void __fastcall TfrmOpenFTPDialog::CreateListPaths(TTreeNode * ParentNode, TStringList * PathList, bool Recursion/*是否递归列出子目录下的文件*/ ) { AnsiString APath; GetNodePath(ParentNode,APath); if(PathList->IndexOf(APath) < 0) PathList->Add(APath); if(Recursion) { for(int n = 0;n < ParentNode->Count;n++) { CreateListPaths(ParentNode->Item[n],PathList,Recursion); } } }
bool GetNodePath(TreeNode* pRoot,TreeNode* pNode,list<TreeNode*> &path){ if(pRoot==pNode) return true; path.push_back(pRoot); vector<TreeNode*>::iterator it=pRoot->children.begin(); bool found = false; while(!found && it!=pRoot->children.end()){ found=GetNodePath(*it,pNode,path); ++it; } if(!found) path.pop_back(); return found; }
// Attempt to reselect the node with the given path bool frmMain::SetCurrentNode(wxTreeItemId node, const wxString &path) { wxTreeItemIdValue cookie; wxTreeItemId child = browser->GetFirstChild(node, cookie); while (child.IsOk()) { if (GetNodePath(child) == path) { browser->SelectItem(child, true); return true; } else if (SetCurrentNode(child, path)) return true; child = browser->GetNextChild(node, cookie); } return false; }
void frmMain::ExecDrop(bool cascaded) { wxTreeItemId item = browser->GetSelection(); pgCollection *collection = (pgCollection *)browser->GetObject(item); // Get any table object for later update wxTreeItemId owneritem; pgObject *node = (pgObject *)browser->GetObject(item); int metatype = node->GetMetaType(); switch (metatype) { case PGM_COLUMN: owneritem = node->GetTable()->GetId(); break; case PGM_CHECK: case PGM_CONSTRAINT: case PGM_EXCLUDE: case PGM_FOREIGNKEY: case PGM_INDEX: case PGM_PRIMARYKEY: case PGM_UNIQUE: case PGM_TRIGGER: case PGM_RULE: // Rules are technically table objects! Yeuch case EDB_PACKAGEFUNCTION: case EDB_PACKAGEVARIABLE: case PGM_SCHEDULE: case PGM_STEP: if (node->IsCollection()) owneritem = browser->GetParentObject(node->GetId())->GetId(); else owneritem = browser->GetParentObject(browser->GetParentObject(node->GetId())->GetId())->GetId(); break; default: break; } // Grab the parent item to re-focus on. wxString parent = GetNodePath(item).BeforeLast('/'); bool success = false; if (collection == currentObject) success = dropSingleObject(currentObject, true, cascaded); else { if (collection && collection->IsCollection()) { long index = properties->GetFirstSelected(); if (index >= 0) { pgObject *data = collection->FindChild(browser, index); if (!data || !data->CanDrop()) return; if (properties->GetSelectedItemCount() == 1) { success = dropSingleObject(data, true, cascaded); } else { if (cascaded || data->RequireDropConfirm() || settings->GetConfirmDelete()) { wxString text, caption; if (cascaded) { text = _("Are you sure you wish to drop multiple objects including all objects that depend on them?"); caption = _("Drop multiple objects cascaded?"); } else { text = _("Are you sure you wish to drop multiple objects?"); caption = _("Drop multiple objects?"); } wxMessageDialog msg(this, text, caption, wxYES_NO | wxICON_QUESTION | wxNO_DEFAULT); if (msg.ShowModal() != wxID_YES) { return; } } bool done = true; long count = 0; while (done && data && index >= 0) { if (data->GetSystemObject()) { wxMessageDialog msg(this, data->GetTranslatedMessage(CANNOTDROPSYSTEM), _("Trying to drop system object"), wxICON_EXCLAMATION); msg.ShowModal(); return; } done = dropSingleObject(data, false, cascaded); if (done) { properties->DeleteItem(index); count++; index = properties->GetFirstSelected(); if (index >= 0) data = collection->FindChild(browser, index); success = true; } } } } } } if (success) { // If the collection has a table, refresh that as well. if (owneritem) { ObjectBrowserRefreshing(true); Refresh(browser->GetObject(owneritem)); ObjectBrowserRefreshing(false); } // Now re-focus on the parent of the deleted node if (!parent.IsEmpty()) SetCurrentNode(browser->GetRootItem(), parent); } }
// Return the path for the current node wxString frmMain::GetCurrentNodePath() { return GetNodePath(currentObject->GetId()); }