//----------------------------------------------------------------------------
// COMPONENT NAME: LPEX Editor
//
// © Copyright IBM Corporation 2003, 2006
// 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> - copy selection to new lines.
 * This action is similar to the <b>blockCopy</b> default editor action,
 * but 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 this user 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 (here, <b>Alt+T</b>):
 *   <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
{
 /**
  * Runs the action.
  * Transfers the selected text.
  */
 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");
 }

 /**
  * Returns the availability of this action.  This action can run in
  * a visible, writable view, whenever there is a visible selection.
  */
 public boolean available(LpexView lpexView)
 {
  return lpexView.currentElement() > 0 &&
         !lpexView.queryOn("readonly") &&
         lpexView.queryOn("block.anythingSelected");
 }
}