//----------------------------------------------------------------------------
// COMPONENT NAME: LPEX Editor
//
// All Rights Reserved.
//
// DESCRIPTION:
// BlockTransferAction - sample user-defined action (blockTransfer)
//----------------------------------------------------------------------------
package com.ibm.lpex.samples;
import com.ibm.lpex.core.LpexAction;
import com.ibm.lpex.core.LpexView;
/**
* Sample action <b>blockTransfer</b> - transfer selection.
* This action is similar to the <b>blockCopy</b> default editor action, but
* it will copy the selection to new lines.
*
* <p>Here is the BlockTransferAction
* <a href="doc-files/BlockTransferAction.java.html">source code</a>.</p>
*
* <p>To run this sample:
* <ul>
* <li>Define the action from an editor preference page, where available, or
* from the editor command line:
* <pre>set actionClass.blockTransfer com.ibm.lpex.samples.BlockTransferAction</pre></li>
* <li>Run it from the editor command line:
* <pre>action blockTransfer</pre>
* or associate it with a key:
* <pre>set keyAction.a-t blockTransfer</pre></li>
* </ul></p>
*
* @see com.ibm.lpex.samples.TestUserProfile
* @see com.ibm.lpex.samples All the samples
*/
public class BlockTransferAction implements LpexAction
{
public void doAction(LpexView lpexView)
{
String blockType = lpexView.query("block.type");
if (blockType.equals("element"))
{
lpexView.doCommand("block copy");
return;
}
// determine number of elements > 1 to make room for
int elements = 0;
if (blockType.equals("rectangle"))
{
String blockText = lpexView.query("block.text");
for (int i = blockText.indexOf('\n'); i >= 0; i = blockText.indexOf('\n', i+1))
{
elements++;
}
}
// remember the cursor column position
int position = lpexView.queryInt("displayPosition");
// ensure we're not attempting to transfer into selection itself
int currentElement = lpexView.currentElement();
int blockTopElement = lpexView.queryInt("block.topElement");
int blockBottomElement = lpexView.queryInt("block.bottomElement");
if (currentElement >= blockTopElement && currentElement < blockBottomElement)
{
lpexView.jump(blockBottomElement, position);
}
// insert new lines for the transferred block
lpexView.doCommand("add");
int element = lpexView.currentElement();
if (elements > 0)
{
lpexView.doCommand("add " + elements);
}
// do the block copy from the first new line
lpexView.jump(element, position);
lpexView.doCommand("block copy");
}
public boolean available(LpexView lpexView)
{
// can run action whenever there is a selection in a visible, writable view
return !lpexView.query("block.type").equals("none") &&
lpexView.currentElement() > 0 && !lpexView.queryOn("readonly");
}
}