/*
 * HyperSearch_Results_to_Buffer.bsh - a Beanshell macro
 * for jEdit text that writes HyperSearch results
 * matches to a new buffer in the format:
 *
 * * <search string>
 * <filename> :<linenum>: <line text>
 *
 * Copyright (C) 2002, 2003 Ollie Rutherfurd <oliver@rutherfurd.net>
 * 2008: Shlomy Reinstein - added search string and include all searches
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: HyperSearch_Results_to_Buffer.bsh 21353 2012-03-14 09:46:51Z jojaba_67 $
 */

//Localization
final static String WindowNotOpenError = jEdit.getProperty("macro.rs.HyperSearchResults.WindowNotOpen.error", "The 'HyperSearch Results' window is not open.");
final static String NoMatchError = jEdit.getProperty("macro.rs.HyperSearchResults.NotMatch.error", "No HyperSearch matches.");
 
traverseTree(javax.swing.tree.DefaultMutableTreeNode node, StringBuffer results){
	if (node.getUserObject() instanceof org.gjt.sp.jedit.search.HyperSearchFileNode)
	{
		path = node.getUserObject().path;
		match = node.getFirstChild();
		while(match != null)
		{
			text = match.getUserObject().str;
			results.append(path + " :" + text + "\n");
			match = match.getNextSibling();
		}
	}
	else
	{
		for (Enumeration e = node.children(); e.hasMoreElements();)
			traverseTree(e.nextElement(), results);
	}
}

writeHypersearchResultsToBuffer(View view){
        hs = view.getDockableWindowManager().getDockableWindow("hypersearch-results");
	if (hs == null)
	{
		Macros.error(view, WindowNotOpenError);
		return;
	}

	root = hs.getTreeModel().getRoot();
	if(root == null || root.getDepth() == 0)
	{
		Macros.error(view, NoMatchError);
		return;
	}

	results = new StringBuffer();
	node = root.getFirstChild();
	while(node != null)
	{
	    	Object object = node.getUserObject();
	    	if (! (object instanceof org.gjt.sp.jedit.search.HyperSearchOperationNode))
		    continue;
	    	results.append("* " + object.getSearchMatcher() + "\n");
		traverseTree(node, results);
		node = node.getNextSibling();
	}

	text = results.toString();
	if(text.length() > 0)
	{
		jEdit.newFile(view);
		view.getTextArea().setText(text);
	}
}

setAccessibility(true);
writeHypersearchResultsToBuffer(view);
setAccessibility(false);
/*
	Macro index data (in DocBook format)

<listitem>
	<para><filename>HyperSearch_Results_to_Buffer.bsh</filename></para>
	<abstract><para>
	Writes HyperSeach results to a new buffer.
	</para></abstract>
</listitem>

*/

// :wrap=none:collapseFolds=0:noTabs=false:lineSeparator=\n:maxLineLen=80:indentSize=4:deepIndent=false:folding=none:
