PageRenderTime 237ms CodeModel.GetById 232ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/macros/Misc/Write_HyperSearch_Results.bsh

#
Unknown | 215 lines | 198 code | 17 blank | 0 comment | 0 complexity | 32fedb88ccd3a36c4276998ca2624d1c 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 3912 2001-11-24 03:56:51Z jgellene $
 36 *
 37 * Checked for jEdit 4.0 API
 38 *
 39 */
 40
 41import java.text.SimpleDateFormat;
 42import javax.swing.tree.*;
 43
 44void writeHyperSearchResults()
 45{
 46    title = "Write HyperSearch results";
 47    hsearch = view.getDockableWindowManager()
 48        .getDockableWindow("hypersearch-results");
 49    if(hsearch == null)
 50    {
 51        JOptionPane.showMessageDialog(view,
 52            "The \"HyperSearch Results\" window is not open.",
 53            title, JOptionPane.ERROR_MESSAGE);
 54        return;
 55    }
 56
 57    if(jEdit.getBooleanProperty("search.hypersearch.toggle") == false)
 58    {
 59        answer = JOptionPane.showConfirmDialog(view,
 60            "Search settings have changed;\nthe report may not be accurate."
 61            + "\nDo you wish to proceed?",
 62            title, JOptionPane.YES_NO_OPTION);
 63        if(answer != JOptionPane.YES_OPTION) return;
 64    }
 65
 66    jEdit.newFile(view);
 67    writeHeader();
 68    treeModel = hsearch.getTreeModel();
 69    root = treeModel.getRoot();
 70    childCount = root.getChildCount();
 71    if(childCount == 0)
 72    {
 73        textArea.setSelectedText(
 74            "Search term not found\n\nEnd of report\n");
 75    }
 76    else
 77    {
 78        fileNameNode = root.getFirstChild();
 79        for(int i = 0; i < childCount; ++i)
 80        {
 81            writeResultsForFile(fileNameNode);
 82            fileNameNode = fileNameNode.getNextSibling();
 83        }
 84        writeFooter();
 85    }
 86}
 87
 88void writeResultsForFile( node)
 89{
 90    node = (DefaultMutableTreeNode)node;
 91    if(node == null) return;
 92    childCount = node.getChildCount();
 93    if( childCount == 0) return;
 94    ++super.fileCount;
 95    super.hitCount += childCount;
 96    obj = node.getUserObject();
 97    sb.setLength(0);
 98    sb.append("Results for file:\n");
 99    sb.append(node.getUserObject().toString());
100    sb.append("\n\n");
101    lineNode = (DefaultMutableTreeNode)node.getFirstChild();
102	if(lineNode == null) return;
103    for( int i = 0; i < childCount; ++i)
104    {
105		if(lineNode == null)
106		{
107			sb.append(" Null node for i = " + String.valueOf(i));
108		}
109		else
110		{
111        sb.append("\tline ");
112        sb.append(lineNode.getUserObject().toString());
113		}
114        sb.append('\n');
115        lineNode = lineNode.getNextSibling();
116    }
117    sb.append("\nNumber of occurrences: ");
118    sb.append(String.valueOf(childCount));
119    sb.append("\n\n");
120
121    textArea.setSelectedText(sb.toString());
122}
123
124void writeHeader()
125{
126    sb.append("Search results for ");
127    if(SearchAndReplace.getRegexp())
128        sb.append("regular expression:  ");
129    else
130        sb.append("search term:  ");
131    sb.append(SearchAndReplace.getSearchString());
132    sb.append("  (case ");
133    if(SearchAndReplace.getIgnoreCase())
134        sb.append("in");
135    sb.append("sensitive)\n");
136    sb.append("Search conducted on ");
137    sb.append(writeSearchFileSetType());
138    sb.append('\n');
139    sb.append("Report written on ");
140    SimpleDateFormat f = new SimpleDateFormat("EE MMM d, yyyy h:mm a z");
141    sb.append(f.format( new Date()));
142    sb.append("\n\n");
143    textArea.setSelectedText(sb.toString());
144}
145
146void writeFooter()
147{
148    sb.setLength(0);
149    sb.append("Total of ");
150    sb.append(String.valueOf(hitCount));
151    sb.append(" occurrences found in ");
152    sb.append(String.valueOf(fileCount));
153    sb.append(" files\n\nEnd of report\n");
154
155    textArea.setSelectedText(sb.toString());
156}
157
158String writeSearchFileSetType()
159{
160    result = new StringBuffer();
161    fileSet = SearchAndReplace.getSearchFileSet();
162    if(fileSet instanceof CurrentBufferSet)
163        result.append("current buffer");
164    else if(fileSet instanceof AllBufferSet)
165        result.append("all open buffers with file mask '")
166              .append(((AllBufferSet)fileSet).getFileFilter())
167              .append('\'');
168    else if(fileSet instanceof DirectoryListSet)
169    {
170        fileSet = (DirectoryListSet)fileSet;
171        result.append("all files in \n")
172              .append(fileSet.getDirectory())
173              .append('\n');
174        if(fileSet.isRecursive())
175            result.append("(and subdirectories) ");
176        result.append("with file mask '")
177              .append(fileSet.getFileFilter())
178              .append('\'');
179    }
180    else
181        result.append("unknown file set");
182    return result.toString();
183}
184
185sb = new StringBuffer();
186fileCount = 0;
187hitCount = 0;
188writeHyperSearchResults();
189
190/*
191	Macro index data (in DocBook format)
192
193<listitem>
194    <para><filename>Write_HyperSearch_Results.bsh</filename></para>
195    <abstract><para>
196        This macro writes the contents of the <quote>HyperSearch
197        Results</quote> window to a new text buffer.
198    </para></abstract>
199    <para>
200        The macro employs a simple text report format. Since
201        the HyperSearch window's object does not maintain the search
202        settings that produced the displayed results, the macro examines the
203        current settings in the <classname>SearchAndReplace</classname> object.
204        It confirms that the HyperSearch option is selected before writing
205        the report. However, the only way to be sure that the report's contents
206        are completely accurate is to run the macro immediately after a
207        HyperSearch.
208    </para>
209</listitem>
210
211*/
212
213
214// end Write_HyperSearch_Results.bsh
215