PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/macros/Misc/Write_HyperSearch_Results.bsh

#
Unknown | 274 lines | 256 code | 18 blank | 0 comment | 0 complexity | af5fbf3f1758215fabd9b412cf114b8b MD5 | raw file
  1/*
  2 * Write_HyperSearch_Results.bsh -  a BeanShell macro script
  3 * for the jEdit text editor - writes the contents of the
  4 * "HyperSearch Results" window to a new text buffer
  5 * Copyright (C) 2001 John Gellene
  6 * jgellene@nyc.rr.com
  7 * http://community.jedit.org
  8 *
  9 * This program is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU General Public License
 11 * as published by the Free Software Foundation; either version 2
 12 * of the License, or any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with the jEdit program; if not, write to the Free Software
 21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 22 *
 23 * Notes on use:
 24 *
 25 * The macro operates by dumping the contents of the HyperSearch Results
 26 * window into a simple text report format.  Because the HyperSearchResults
 27 * object does not know the search parameters that produced its data, the
 28 * parameters must be retrieved from the SearchAndReplace object.  If those
 29 * parameters have changed since the HyperSearch, the report header will not be
 30 * accurate (although the body of the report will not be affected).  A simple
 31 * test checks whether the HyperSearch flag is set before running the dumping
 32 * routines.  To be completely reliable, the macro should be run immediately
 33 * after a HyperSearch.
 34 *
 35 * $Id: Write_HyperSearch_Results.bsh 4422 2003-01-10 19:48:25Z spestov $
 36 *
 37 * Checked for jEdit 4.0 API
 38 * Adapted and checked for jEdit 4.1 API by Rudi Widmann
 39 *
 40 */
 41
 42import java.text.SimpleDateFormat;
 43import javax.swing.tree.*;
 44
 45void writeHyperSearchResults()
 46{
 47    title = "Write HyperSearch results";
 48    hsearch = view.getDockableWindowManager()
 49        .getDockable("hypersearch-results");
 50    if(hsearch == null)
 51    {
 52        JOptionPane.showMessageDialog(view,
 53            "The \"HyperSearch Results\" window is not open.",
 54            title, JOptionPane.ERROR_MESSAGE);
 55        return;
 56    }
 57
 58    if(jEdit.getBooleanProperty("search.hypersearch.toggle") == false)
 59    {
 60        answer = JOptionPane.showConfirmDialog(view,
 61            "Search settings have changed;\nthe report may not be accurate."
 62            + "\nDo you wish to proceed?",
 63            title, JOptionPane.YES_NO_OPTION);
 64        if(answer != JOptionPane.YES_OPTION) return;
 65    }
 66    /*******************************************************************
 67    * Hyper search result tree structure
 68    * root
 69    * + searchNode
 70    *   + fileNode
 71    *     + results 
 72    *   + fileNode
 73    *     + results 
 74    * + searchNode
 75    *   + fileNode
 76    *     + results 
 77    *******************************************************************/
 78    jEdit.newFile(view);
 79    writeHeader();
 80    treeModel = hsearch.getTreeModel();
 81    root = treeModel.getRoot();
 82    rootChildCount = root.getChildCount();
 83    if(rootChildCount == 0)
 84    {
 85        textArea.setSelectedText(
 86            "Search items not found\n\nEnd of report\n");
 87    }
 88    else
 89    {
 90        searchNode = root.getFirstChild();
 91        for(int i = 0; i < rootChildCount; ++i)
 92        {
 93	    super.fileCount = 0;
 94	    super.hitCount = 0;
 95	    writeSearchHeader(searchNode);
 96	    if (i == rootChildCount-1)
 97		    writeSearchParameters();  // write only for last result
 98	    super.searchCount++;
 99	    searchChildCount = searchNode.getChildCount();
100	    if(searchChildCount == 0)
101	    {
102		textArea.setSelectedText(
103			"Search term not found\n\nEnd of report\n");
104	    }
105	    fileNode = searchNode.getFirstChild();
106	    for(int j = 0; j < searchChildCount; ++j)
107	    {
108		writeResultsForFile(fileNode);
109		fileNode = fileNode.getNextSibling();
110	    }
111		searchNode = searchNode.getNextSibling();
112						    writeFileFooter();
113        }
114	writeFooter();
115    }
116}
117
118void writeSearchHeader( node)
119{
120    node = (DefaultMutableTreeNode)node;
121    if(node == null) return;
122    childCount = node.getChildCount();
123    if( childCount == 0) return;
124    sb.setLength(0);
125    sb.append("Results for search item: \"");
126    sb.append(node.getUserObject().toString());
127    sb.append("\"\n\n");
128    textArea.setSelectedText(sb.toString());
129}
130
131void writeResultsForFile( node)
132{
133    node = (DefaultMutableTreeNode)node;
134    if(node == null) return;
135    childCount = node.getChildCount();
136    if( childCount == 0) return;
137    ++super.fileCount;
138    super.hitCount += childCount;
139    obj = node.getUserObject();
140    sb.setLength(0);
141    sb.append("\tMatched file:\n\t");
142    sb.append(node.getUserObject().toString());
143    sb.append("\n\n");
144    lineNode = (DefaultMutableTreeNode)node.getFirstChild();
145	if(lineNode == null) return;
146    for( int i = 0; i < childCount; ++i)
147    {
148	if(lineNode == null)
149	{
150		sb.append("\t\tNull node for i = " + String.valueOf(i));
151	}
152	else
153	{
154	    sb.append("\t\tline ");
155	    sb.append(lineNode.getUserObject().toString());
156	}
157        sb.append('\n');
158        lineNode = lineNode.getNextSibling();
159    }
160    sb.append("\n\tNumber of occurrences: ");
161    sb.append(String.valueOf(childCount));
162    sb.append("\n\n");
163
164    textArea.setSelectedText(sb.toString());
165}
166
167void writeHeader()
168{
169    sb.append("Hypersearch report written on ");
170    SimpleDateFormat f = new SimpleDateFormat("EE MMM d, yyyy h:mm a z");
171    sb.append(f.format( new Date()));
172    sb.append("\n\n");
173    textArea.setSelectedText(sb.toString());
174}
175void writeSearchParameters()
176{
177    sb.setLength(0);
178    sb.append("\tUsed search parameters for ");
179    if(SearchAndReplace.getRegexp())
180        sb.append("regular expression:  ");
181    else
182        sb.append("search term:  ");
183    sb.append(SearchAndReplace.getSearchString());
184    sb.append("  (case ");
185    if(SearchAndReplace.getIgnoreCase())
186        sb.append("in");
187    sb.append("sensitive)\n");
188    sb.append("\tSearch conducted on ");
189    sb.append(writeSearchFileSetType());
190    sb.append('\n');
191    sb.append('\n');
192    textArea.setSelectedText(sb.toString());
193}
194
195void writeFileFooter()
196{
197    sb.setLength(0);
198    sb.append("\tTotal of ");
199    sb.append(String.valueOf(hitCount));
200    sb.append(" occurrences found in ");
201    sb.append(String.valueOf(fileCount));
202    sb.append(" files\n\n");
203
204    textArea.setSelectedText(sb.toString());
205}
206void writeFooter()
207{
208    sb.setLength(0);
209    sb.append("Total of ");
210    sb.append(String.valueOf(searchCount));
211    sb.append(" search results reported \n\nEnd of report\n");
212    textArea.setSelectedText(sb.toString());
213}
214
215String writeSearchFileSetType()
216{
217    result = new StringBuffer();
218    fileSet = SearchAndReplace.getSearchFileSet();
219    if(fileSet instanceof CurrentBufferSet)
220        result.append("current buffer");
221    else if(fileSet instanceof AllBufferSet)
222        result.append("all open buffers with file mask '")
223              .append(((AllBufferSet)fileSet).getFileFilter())
224              .append('\'');
225    else if(fileSet instanceof DirectoryListSet)
226    {
227        fileSet = (DirectoryListSet)fileSet;
228        result.append("all files in \n")
229              .append(fileSet.getDirectory())
230              .append('\n');
231        if(fileSet.isRecursive())
232            result.append("(and subdirectories) ");
233        result.append("with file mask '")
234              .append(fileSet.getFileFilter())
235              .append('\'');
236    }
237    else
238        result.append("unknown file set");
239    return result.toString();
240}
241
242sb = new StringBuffer();
243searchCount = 0;
244fileCount = 0;
245hitCount = 0;
246writeHyperSearchResults();
247
248/*
249	Macro index data (in DocBook format)
250
251<listitem>
252    <para><filename>Write_HyperSearch_Results.bsh</filename></para>
253    <abstract><para>
254        This macro writes the contents of the <quote>HyperSearch
255        Results</quote> window to a new text buffer.
256    </para></abstract>
257    <para>
258        The macro employs a simple text report format. Since
259        the HyperSearch window's object does not maintain the search
260        settings that produced the displayed results, the macro examines the
261        current settings in the <classname>SearchAndReplace</classname> object.
262        It confirms that the HyperSearch option is selected before writing
263        the report. However, the only way to be sure that the report's contents
264        are completely accurate is to run the macro immediately after a
265        HyperSearch.
266    </para>
267</listitem>
268
269*/
270
271
272// end Write_HyperSearch_Results.bsh
273
274