//---------------------------------------------------------------------------- // COMPONENT NAME: LPEX Editor // // All Rights Reserved. // // DESCRIPTION: // CopyLineAction - sample user-defined action (copyLine) //---------------------------------------------------------------------------- package com.ibm.lpex.samples; import com.ibm.lpex.core.LpexAction; import com.ibm.lpex.core.LpexView; /** * Sample action <b>copyLine</b> - copy selection / current line to clipboard. * This action is similar to the <b>copy</b> default editor action, but it * will copy the current element to the clipboard if there is no selection * in the current view. * * <p>Here is the CopyLineAction * <a href="doc-files/CopyLineAction.java.html">source code</a>.</p> * * <p>To run this sample: * <ul> * <li>Define the action via an editor preference page, where available, or * from the editor command line: * <pre>set actionClass.copyLine com.ibm.lpex.samples.CopyLineAction</pre> * <li>Run it from the editor command line: * <pre>action copyLine</pre> * or associate it with a key: * <pre>set keyAction.c-c copyLine</pre> * </ul></p> * * @see com.ibm.lpex.samples All the samples */ public class CopyLineAction implements LpexAction { public void doAction(LpexView lpexView) { int copyActionId = lpexView.actionId("copy"); // if there is a selection, in this view, do the default "copy" action if (lpexView.queryOn("block.inView")) { lpexView.doAction(copyActionId); return; } // 1.- first save any selection in another (view of the) document String blockType = null, forceAllVisible = null, cursorRow = null; int blockTopElement = 0, blockTopPosition = 0, blockBottomElement = 0, blockBottomPosition = 0; LpexView blockView = lpexView.blockView(); if (blockView != null) { // ensure all the elements are visible for the jumps and set blocks below cursorRow = blockView.query("cursorRow"); forceAllVisible = blockView.query("forceAllVisible"); blockView.doCommand("set forceAllVisible on"); blockType = blockView.query("block.type"); blockTopElement = blockView.queryInt("block.topElement"); blockBottomElement = blockView.queryInt("block.bottomElement"); blockTopPosition = blockView.queryInt("block.topPosition"); blockBottomPosition = blockView.queryInt("block.bottomPosition"); } // 2.- select the current element lpexView.doAction(lpexView.actionId("blockMarkElement")); // 3.- do a regular clipboard copy lpexView.doAction(copyActionId); // 4.- clear the selection lpexView.doAction(lpexView.actionId("blockUnmark")); // 5.- restore any original selection in another (view of the) document if (blockView != null) { int currentElement = blockView.currentElement(); int currentPosition = blockView.currentPosition(); // set in the right order, in case it's a stream block if (currentElement == blockTopElement && currentPosition == blockTopPosition) { blockView.jump(blockBottomElement, blockBottomPosition); blockView.doCommand("block set " + blockType); blockView.jump(blockTopElement, blockTopPosition); blockView.doCommand("block set"); } else { blockView.jump(blockTopElement, blockTopPosition); blockView.doCommand("block set " + blockType); blockView.jump(blockBottomElement, blockBottomPosition); blockView.doCommand("block set"); // may have to restore blockView's cursor position if (currentElement != blockBottomElement || currentPosition != blockBottomPosition) { blockView.jump(currentElement, currentPosition); } } blockView.doCommand("set forceAllVisible " + forceAllVisible); blockView.doCommand("set cursorRow " + cursorRow); } } public boolean available(LpexView lpexView) { // this action can be run whenever there are visible elements in the view return lpexView.currentElement() != 0; } }