//---------------------------------------------------------------------------- // COMPONENT NAME: LPEX Editor // // © Copyright IBM Corporation 2005, 2006 // All Rights Reserved. // // DESCRIPTION: // FindTextContextCommand - sample user-defined command (redefined findText) //---------------------------------------------------------------------------- package com.ibm.lpex.samples; import com.ibm.lpex.core.LpexCommand; import com.ibm.lpex.core.LpexView; /** * Sample command <b>findText</b> - redefines editor's to show context for the * found text. Repositions the result of a successful find-text command or action * in the specified view so that its context is shown - two lines above and below. * * <p>Here is the FindTextContextCommand * <a href="doc-files/FindTextContextCommand.java.html">source code</a>.</p> * * <p>To redefine the default <b>findText</b> command: * <ul> * <li>Register this new command implementation via an editor preference page, * where available, or from the editor command line: * <pre>set commandClass.findText com.ibm.lpex.samples.FindTextContextCommand</pre></li> * </ul></p> * * <p>The repositioning of the found text can be disabled by setting * <b>userParameter.view.findTextContext</b> to off. This is useful when running * the <b>findText</b> command without the need to reposition the result on the * screen (see, for example, the sample {@link FindsCommand}).</p> * * @see com.ibm.lpex.samples.TestUserProfile * @see com.ibm.lpex.samples All the samples */ public class FindTextContextCommand implements LpexCommand { /** * Runs this command. * Issues the default <b>findText</b> editor command, and then * repositions the found text in order to show a few lines around it. * * @param lpexView the document view in which the command was issued * @param parameters the findText command parameters */ public boolean doCommand(LpexView lpexView, String parameters) { boolean success = lpexView != null && lpexView.doDefaultCommand("findText " + parameters); if (success) { showFoundContext(lpexView); } return success; } /** * Shows the context for a successful find-text command or action in the * specified view. * * Called after running the default editor <b>findText</b> command. */ static void showFoundContext(LpexView lpexView) { // allow user to disable this (for example, in the long-running "finds" sample command) if (!"off".equals(lpexView.query("userParameter.view.findTextContext"))) { String status = LpexView.globalQuery("status"); if (status == null || (!status.equals("findText.notFound") && !status.equals("findText.invalidPattern"))) { // only bother if there are at least five lines showing int rows = lpexView.queryInt("rows"); if (rows >= 5) { // rebuild this view's screen lpexView.doCommand("screenShow view"); // reposition at least two lines away from the edge int row = lpexView.queryInt("cursorRow"); if (row < 3) { lpexView.doCommand("set cursorRow 3"); } else if (row > rows - 2) { lpexView.doCommand("set cursorRow " + (rows - 2)); } } } } } }