PageRenderTime 25ms CodeModel.GetById 18ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/SideKick/macros/SearchAsset.bsh

#
Unknown | 93 lines | 88 code | 5 blank | 0 comment | 0 complexity | 958a152f7dafce897db9bad7e50188fd MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
 1// This is a recorded macro. First, check over the
 2// commands to make sure this is what you intended. Then,
 3// save this buffer, and the macro should appear in the
 4// Macros menu.
 5
 6import javax.swing.tree.*;
 7import javax.swing.Timer;
 8
 9DefaultMutableTreeNode findNodeOfLine(DefaultMutableTreeNode node, int line)
10{
11	if (node.getUserObject() instanceof org.gjt.sp.jedit.search.HyperSearchFileNode)
12	{
13		DefaultMutableTreeNode match = node.getFirstChild();
14		while(match != null)
15		{
16			String text = match.getUserObject().str;
17			if (text.startsWith(String.valueOf(line + 1) + ":"))
18				return match;
19			match = match.getNextSibling();
20		}
21	}
22	else
23	{
24		for (Enumeration e = node.children(); e.hasMoreElements();)
25		{
26			DefaultMutableTreeNode n = findNodeOfLine(e.nextElement(), line);
27			if (n != null)
28				return n;
29		}
30	}
31	return null;
32}
33
34void selectNodeOfLine(View view, int line)
35{
36	HyperSearchResults hs = view.getDockableWindowManager().getDockableWindow(
37		"hypersearch-results");
38	if (hs == null)
39		return;
40	DefaultMutableTreeNode root = hs.getTreeModel().getRoot();
41	if (root == null || root.getDepth() == 0)
42		return;
43	// Check only the results of the last search
44	DefaultMutableTreeNode node = root.getLastChild();
45	if (node == null)
46		return;
47	Object object = node.getUserObject();
48	if (! (object instanceof org.gjt.sp.jedit.search.HyperSearchOperationNode))
49		continue;
50	DefaultMutableTreeNode n = findNodeOfLine(node, line);
51	if (n != null)
52	{
53		hs.getTree().setSelectionPath(new TreePath(n.getPath()));
54		return;
55	}
56}
57
58void SearchAsset(JEditTextArea textArea, boolean exact)
59{
60	int position = textArea.getCaretPosition();
61	int line = textArea.getCaretLine();
62	Selection[] selection = textArea.getSelection();
63	String s = null;
64	if (selection == null || selection.length == 0) {
65		s = Macros.input(view, "Enter text to search for:");
66		if (s == null)
67			return;
68	} else {
69		s = textArea.getSelectedText(selection[0]);
70	}
71	sidekick.SideKickActions.selectAsset(view);
72	if (exact)
73		SearchAndReplace.setSearchString("\\b" + s + "\\b");
74	else
75		SearchAndReplace.setSearchString(s);
76	SearchAndReplace.setAutoWrapAround(false);
77	SearchAndReplace.setReverseSearch(false);
78	SearchAndReplace.setIgnoreCase(false);
79	SearchAndReplace.setRegexp(exact);
80	SearchAndReplace.hyperSearch(view,true);
81	textArea.setCaretPosition(position);
82	textArea.setSelection(selection);
83	Timer timer = new Timer(1000, new ActionListener() {
84			public void actionPerformed(ActionEvent ae) {
85				setAccessibility(true);
86				selectNodeOfLine(view, line);
87				setAccessibility(false);
88			}
89	});
90	timer.setRepeats(false);
91	timer.start();
92}	
93