//---------------------------------------------------------------------------- // COMPONENT NAME: LPEX Editor // // (C) Copyright IBM Corporation 2004, 2005 // All Rights Reserved. // // DESCRIPTION: // FindsCommand - sample user-defined command (finds) //---------------------------------------------------------------------------- package com.ibm.lpex.samples; import com.ibm.lpex.core.LpexCommand; import com.ibm.lpex.core.LpexDocumentLocation; import com.ibm.lpex.core.LpexStringTokenizer; import com.ibm.lpex.core.LpexView; /** * Sample command <b>finds</b> - count the number of find-text matches in view. * This command uses the current <b>findText.</b> settings to determine the * number of occurrences in the document of either <b>findText.findText</b> * or the specified text string. * * <p>Here is the FindsCommand <a href="doc-files/FindsCommand.java.html">source * code</a>.</p> * * <p>To run this sample: * <ul> * <li>Define this user command via an editor preference page, where available, * or from the editor command line: * <pre>set commandClass.finds com.ibm.lpex.samples.FindsCommand</pre></li> * <li>Run it from the editor command line: * <pre>finds [<i>text</i>]</pre></li> * </ul></p> * * @see com.ibm.lpex.samples All the samples */ public class FindsCommand implements LpexCommand { /** * Runs this command. * Displays the number of find-text matches in the view. */ public boolean doCommand(LpexView lpexView, String parameters) { // build the "findText" command parameters, the options-used result message StringBuffer findTextParms = new StringBuffer(64); StringBuffer options = new StringBuffer(64); findTextParms.append("quiet noBeep noWrap noEmphasis "); if (lpexView.queryOn("current.findText.columns")) { String startColumn = lpexView.query("current.findText.startColumn"); String endColumn = lpexView.query("current.findText.endColumn"); findTextParms.append("columns ") .append(startColumn).append(' ').append(endColumn).append(' '); setOption(options, "in columns " + startColumn + ".." + endColumn); } if (lpexView.queryOn("current.findText.block")) { findTextParms.append("block "); setOption(options, "in selection"); } if (lpexView.queryOn("current.findText.wholeWord")) { findTextParms.append("wholeWord "); setOption(options, "whole word"); } if (lpexView.queryOn("current.findText.asis")) { findTextParms.append("asis "); setOption(options, "case sensitive"); } // the text to find - explicitly specified / view's String text; if (parameters.length() != 0) { text = parameters; findTextParms.append(LpexStringTokenizer.addQuotes(parameters)); } else { if (lpexView.queryOn("current.findText.regularExpression")) { findTextParms.append("regularExpression "); setOption(options, "regular expression"); } text = lpexView.query("current.findText.findText"); findTextParms.append(text); text = LpexStringTokenizer.removeQuotes(text); // for result } setOption(options, null); // close it up // walk-through document with TestUserProfile's redefined "findText" command extras disabled String oldShowFoundContext = lpexView.query("userParameter.view.findText.showFoundContext"); /**/ lpexView.doCommand("set userParameter.view.findText.showFoundContext off"); String findTextParmsString = findTextParms.toString(); String cmd0 = "findText checkStart " + findTextParmsString; String cmd = "findText " + findTextParmsString; int count = 0; for (LpexDocumentLocation loc = new LpexDocumentLocation(1, 1); ; count++) { lpexView.doCommand(loc, ((count == 0)? cmd0 : cmd)); if (lpexView.query("status") != null) { break; } } /**/ lpexView.doCommand("set userParameter.view.findText.showFoundContext " + ((oldShowFoundContext == null)? "" : oldShowFoundContext)); // clear status lpexView.doCommand("set status"); // display result - text: occurrences (options used). String countText; switch (count) { case 0: countText = "not found"; break; case 1: countText = "1 occurrence"; break; default: countText = count + " occurrences"; } lpexView.doCommand("set messageText " + text + ": " + countText + options.toString() + "."); return true; } /** * Add a setting to the 'options used' result message. */ private void setOption(StringBuffer sb, String option) { if (sb.length() == 0) { if (option != null) { sb.append(option); } } else { if (option != null) { sb.append(", ").append(option); } else { sb.insert(0, " (").append(')'); } } } }