//----------------------------------------------------------------------------
// COMPONENT NAME: LPEX Editor
//
// © Copyright IBM Corporation 2004, 2007
// All Rights Reserved.
//
// DESCRIPTION:
// FindNextDefaultAction - sample user-defined action (findNextDefault)
//----------------------------------------------------------------------------

package com.ibm.lpex.samples;

import com.ibm.lpex.core.LpexAction;
import com.ibm.lpex.core.LpexView;

/**
 * Sample action <b>findNextDefault</b> - find next default text occurrence.
 * This action is similar to the <b>findNext</b> built-in editor action, but will find
 * the next occurrence of the global-scoped find text (<b>default.findText.findText</b>),
 * rather than the current view-scoped find text. The global find text is usually set
 * to the text lately searched from the live-find dialog of <i>any</i> view.
 *
 * <p>Here is the FindNextDefaultAction
 * <a href="doc-files/FindNextDefaultAction.java.html">source code</a>.</p>
 *
 * <p>To run this sample:
 * <ul>
 *  <li>Define this user action via an editor preference page, where available,
 *   or from the editor command line:
 *   <pre>set actionClass.findNextDefault com.ibm.lpex.samples.FindNextDefaultAction</pre></li>
 *  <li>Run it from the editor command line:
 *   <pre>action findNextDefault</pre>
 *   or associate it with a key (here, <b>Ctrl+N</b> in all window contexts):
 *   <pre>set keyAction.c-n.t.c.p findNextDefault</pre></li>
 * </ul></p>
 *
 * @see com.ibm.lpex.samples All the samples
 */
public class FindNextDefaultAction implements LpexAction
{
 /**
  * Runs the action.
  * Finds the next occurrence of the default find text.
  */
 public void doAction(LpexView lpexView)
 {
  // get the default text to find and its associated regular-expression setting
  String findText = LpexView.globalQuery("current.findText.findText");
  if (findText == null || findText.length() == 0)
   {
    return; // nothing to find.
   }
  String regex = LpexView.globalQuery("current.findText.regularExpression");

  // built-in "findNext" editor action uses the "actionRepeat" argument
  int repeat = lpexView.queryInt("actionRepeat");
  boolean reverse = repeat < 0;
  if (reverse)
   {
    repeat = -repeat;
   }

  // build the "findText" command string
  StringBuilder findTextCommand = new StringBuilder(32);
  findTextCommand.append("findText ");
  if (reverse)
   {
    findTextCommand.append("up ");
   }
  // 1.- view-scoped settings
  if (lpexView.queryOn("current.findText.mark"))
   {
    findTextCommand.append("mark ");
   }
  if (lpexView.queryOn("current.findText.columns"))
   {
    findTextCommand.append("columns ")
     .append(lpexView.query("current.findText.startColumn")).append(' ')
     .append(lpexView.query("current.findText.endColumn")).append(' ');
   }
  if (lpexView.queryOn("current.findText.block"))
   {
    findTextCommand.append("block ");
   }
  if (lpexView.queryOn("current.findText.wholeWord"))
   {
    findTextCommand.append("wholeWord ");
   }
  if (!lpexView.queryOn("current.findText.wrap"))
   {
    findTextCommand.append("noWrap ");
   }
  if (lpexView.queryOn("current.findText.asis"))
   {
    findTextCommand.append("asis ");
   }
  if (!lpexView.queryOn("current.findText.emphasis"))
   {
    findTextCommand.append("noEmphasis ");
   }
  // 2.- global text & regular expression settings
  if ("on".equals(regex))
   {
    findTextCommand.append("regularExpression ");
   }
  findTextCommand.append(findText);
  String findTextCommandString = findTextCommand.toString();

  // finally, do the find
  for (int i = 0; i < repeat; i++)
   {
    lpexView.doCommand(findTextCommandString);
   }
 }

 /**
  * Returns the availability of this action.
  * This action is enabled whenever there is text to find.
  */
 public boolean available(LpexView lpexView)
 {
  String findText = LpexView.globalQuery("current.findText.findText");
  return findText != null && findText.length() != 0;
 }
}