Skip to content

Commit 4809d99

Browse files
committed
Implement search feature in treeview
1 parent 1da6d55 commit 4809d99

File tree

5 files changed

+93
-4
lines changed

5 files changed

+93
-4
lines changed

NppJSONViewer/NppJsonViewer/Define.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ const TCHAR STR_INI_OTHER_AUTO_FORMAT[] = TEXT("AUTO_FORMAT");
6565
const TCHAR STR_INI_OTHER_IGNORE_COMMENT[] = TEXT("IGNORE_COMMENT");
6666
const TCHAR STR_INI_OTHER_IGNORE_COMMA[] = TEXT("IGNORE_TRAILLING_COMMA");
6767

68+
const TCHAR STR_SRCH_SEARCHING[] = TEXT("Searching for: ");
69+
const TCHAR STR_SRCH_NOTFOUND[] = TEXT("Not found: ");
70+
const TCHAR STR_SRCH_NOMOREFOUND[] = TEXT("No more found: ");
71+
6872
enum class LineEnding
6973
{
7074
AUTO,

NppJSONViewer/NppJsonViewer/JsonViewDlg.cpp

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,86 @@ void JsonViewDlg::UpdateNodePath(HTREEITEM htiNode)
221221
CUtility::SetEditCtrlText(::GetDlgItem(_hSelf, IDC_EDT_NODEPATH), nodePath);
222222
}
223223

224+
void JsonViewDlg::SearchInTree()
225+
{
226+
std::wstring itemToSearch = CUtility::GetEditCtrlText(::GetDlgItem(_hSelf, IDC_EDT_SEARCH));
227+
CUtility::SetEditCtrlText(::GetDlgItem(_hSelf, IDC_EDT_NODEPATH), STR_SRCH_SEARCHING + itemToSearch);
228+
m_hTreeView->SetSelection(nullptr);
229+
230+
static int foundCount = 0;
231+
static std::wstring previousSearch;
232+
static HTREEITEM nextNode = m_hTreeView->NextItem(m_hTreeView->GetRoot());
233+
234+
// New search, hence search from begining
235+
if (previousSearch != itemToSearch)
236+
{
237+
previousSearch = itemToSearch;
238+
nextNode = m_hTreeView->NextItem(m_hTreeView->GetRoot());
239+
foundCount = 0;
240+
}
241+
else
242+
{
243+
nextNode = m_hTreeView->NextItem(nextNode);
244+
if (nextNode == m_hTreeView->GetRoot())
245+
{
246+
nextNode = m_hTreeView->NextItem(nextNode);
247+
foundCount = 0;
248+
}
249+
}
250+
251+
// Check if this is an empty json
252+
std::wstring nodeText = m_hTreeView->GetNodeName(nextNode);
253+
if (nodeText.empty() || wcscmp(nodeText.c_str(), JSON_ERR_PARSE) == 0)
254+
{
255+
CUtility::SetEditCtrlText(::GetDlgItem(_hSelf, IDC_EDT_NODEPATH), STR_SRCH_NOTFOUND + itemToSearch);
256+
}
257+
else
258+
{
259+
bool bFound = false;
260+
while (!bFound && nextNode)
261+
{
262+
nodeText = m_hTreeView->GetNodeName(nextNode);
263+
auto nodeKey = m_hTreeView->GetNodeKey(nextNode);
264+
auto nodeVal = m_hTreeView->GetNodeValue(nextNode);
265+
266+
// Search in node value
267+
if (nodeKey != nodeVal)
268+
bFound = StringHelper::Contains(nodeVal, itemToSearch);
269+
270+
// If both are euaal, but not all three
271+
// { "[i]": "[i]"} => Search for '[i]'
272+
//
273+
if (!bFound && nodeKey == nodeVal && nodeKey != nodeText)
274+
bFound = StringHelper::Contains(nodeVal, itemToSearch);
275+
276+
// If all three are same, then key does not start with '[' and end with ']'
277+
// "num": [12.148681171238422,42.835353759876654] => Search for 'num'
278+
//
279+
if (!bFound && nodeKey == nodeVal && nodeKey == nodeText && !nodeKey.starts_with(L"[") && !nodeKey.ends_with(L"]"))
280+
bFound = StringHelper::Contains(nodeText, itemToSearch);
281+
282+
if (bFound)
283+
break;
284+
285+
nextNode = m_hTreeView->NextItem(nextNode);
286+
}
287+
288+
if (bFound)
289+
{
290+
UpdateNodePath(nextNode);
291+
m_hTreeView->SetSelection(nextNode);
292+
++foundCount;
293+
}
294+
else
295+
{
296+
if (foundCount)
297+
CUtility::SetEditCtrlText(::GetDlgItem(_hSelf, IDC_EDT_NODEPATH), STR_SRCH_NOMOREFOUND + itemToSearch);
298+
else
299+
CUtility::SetEditCtrlText(::GetDlgItem(_hSelf, IDC_EDT_NODEPATH), STR_SRCH_NOTFOUND + itemToSearch);
300+
}
301+
}
302+
}
303+
224304
void JsonViewDlg::PrepareButtons()
225305
{
226306
// Refresh Button
@@ -651,7 +731,7 @@ INT_PTR JsonViewDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam)
651731
break;
652732

653733
case IDC_BTN_SEARCH:
654-
ShowMessage(L"OK", L"IDC_BTN_SEARCH", MB_OK);
734+
SearchInTree();
655735
break;
656736

657737
// Handle context menu entries
@@ -702,9 +782,6 @@ INT_PTR JsonViewDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam)
702782
}
703783

704784
default:
705-
// TODO: Temporarily hide controls which are not implemented
706-
std::vector<DWORD> toHide = {IDC_BTN_SEARCH, IDC_EDT_SEARCH};
707-
ShowControls(toHide, false);
708785
return DockingDlgInterface::run_dlgProc(message, wParam, lParam);
709786
}
710787
}

NppJSONViewer/NppJsonViewer/JsonViewDlg.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class JsonViewDlg : public DockingDlgInterface
3838

3939
void UpdateNodePath(HTREEITEM htiNode);
4040

41+
void SearchInTree();
42+
4143
void PrepareButtons();
4244
void SetIconAndTooltip(eButton ctrlType, const std::wstring &toolTip);
4345

NppJSONViewer/NppJsonViewer/TreeViewCtrl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ HTREEITEM TreeViewCtrl::GetSelection() const
223223
return TreeView_GetSelection(m_hTree);
224224
}
225225

226+
void TreeViewCtrl::SetSelection(HTREEITEM hItem) const
227+
{
228+
TreeView_SelectItem(m_hTree, hItem);
229+
}
230+
226231
bool TreeViewCtrl::IsItemVisible(HTREEITEM hti)
227232
{
228233
RECT rect = {};

NppJSONViewer/NppJsonViewer/TreeViewCtrl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class TreeViewCtrl
3737
bool HasChild(HTREEITEM hti) const;
3838

3939
HTREEITEM GetSelection() const;
40+
void SetSelection(HTREEITEM hItem) const;
4041

4142
bool IsItemVisible(HTREEITEM hti);
4243

0 commit comments

Comments
 (0)