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

package com.ibm.lpex.samples;

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

/**
 * Sample action <b>zoom</b> - zoom in / out a segment of the document.
 * This action lets you set a zoom-in segment to the current selection in the
 * document, and then zoom out / in this segment.
 *
 * <p>Here is the ZoomAction
 * <a href="doc-files/ZoomAction.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.zoom com.ibm.lpex.samples.ZoomAction</pre>
 *  <li>Run it from the editor command line:
 *   <pre>action zoom</pre>
 *   or associate it with a key (here, <code>Alt+.</code>):
 *   <pre>set keyAction.a-period zoom</pre>
 * </ul></p>
 *
 * @see com.ibm.lpex.samples.TestUserProfile
 * @see com.ibm.lpex.samples All the samples
 */
public class ZoomAction extends LpexBaseAction
{
 /**
  * Zoom in / out.
  */
 public void doAction(LpexView lpexView)
 {
  /*----------------------------------------------------------------------*/
  /*  1.- if no selection in this view, toggle any previous zoom setting  */
  /*----------------------------------------------------------------------*/
  if (!lpexView.queryOn("block.inView"))
   {
    // is there a previously-set zoom mark?
    int zoomMark = lpexView.queryInt("markId.@ZoomAction");
    if (zoomMark > 0)
     {
      boolean wasZoomedIn = lpexView.queryOn("markIncluded.@ZoomAction");

      // zoom out - restore original view's visibility of the expand/hide area
      if (wasZoomedIn)
       {
        restoreExpandHide(lpexView);
        // lpexView.doCommand("set mark.@ZoomAction clear");
       }
      // zoom in - ensure the expand/hide area is hidden
      else
       {
        hideExpandHide(lpexView);
       }

      // toggle zoom in/out state
      lpexView.doCommand("set markIncluded.@ZoomAction " + (wasZoomedIn? "off" : "on"));
     }
    return;
   }

  /*------------------------------------------------------------------*/
  /*  2.- [re]set our mark to zoom in on the currently selected text  */
  /*------------------------------------------------------------------*/
  // zoom in onto the selection
  zoomIn(lpexView, lpexView.queryInt("block.topElement"),
                   lpexView.queryInt("block.bottomElement"));

  // clear the zoom-defining selection
  lpexView.doCommand("block clear");
 }

 public String getToolTipText(LpexView lpexView)
 {
  return "Zoom in / out a segment of the document";
 }

 /**
  * Zooms in on the segment bounded by the given elements.
  */
 static void zoomIn(LpexView lpexView, int topElement, int bottomElement)
 {
  // hide the expand/hide area for a crystal-clear zoomed-in view
  hideExpandHide(lpexView);

  // [re]define the zoom mark to the given segment, set it as included
  lpexView.doCommand("set mark.@ZoomAction sticky element " +
                      topElement + ' ' + bottomElement);
  lpexView.doCommand("set markIncluded.@ZoomAction on");
 }

 /**
  * Hides the expand/hide area, shows all filtered-out elements in the given view.
  */
 static void hideExpandHide(LpexView lpexView)
 {
  // unless already zoomed in, remember original expand/hide area,
  // includedClasses, and excludedClasses settings
  if (!lpexView.queryOn("markIncluded.@ZoomAction"))
   {
    lpexView.doCommand("set userParameter.view.ZoomAction.expandHide " +
                       lpexView.query("current.expandHide"));

    String classes = lpexView.query("includedClasses");
    if (classes != null)
     {
      lpexView.doCommand("set userParameter.view.ZoomAction.includedClasses " + classes);
     }
    classes = lpexView.query("excludedClasses");
    if (classes != null)
     {
      lpexView.doCommand("set userParameter.view.ZoomAction.excludedClasses " + classes);
     }
   }

  // hide expand/hide area, show entire zoomed-in segment
  lpexView.doCommand("set expandHide off");
  lpexView.doAction(lpexView.actionId("showAll"));
 }

 /**
  * Restores the expand/hide area and view-filter classes in the given view
  * to the settings in effect prior to the last zoom in.
  */
 static void restoreExpandHide(LpexView lpexView)
 {
  lpexView.doCommand("set expandHide " +
                     lpexView.query("userParameter.view.ZoomAction.expandHide"));

  String classes = lpexView.query("userParameter.view.ZoomAction.includedClasses");
  if (classes != null)
   {
    lpexView.doCommand("set includedClasses " + classes);
   }
  classes = lpexView.query("userParameter.view.ZoomAction.excludedClasses");
  if (classes != null)
   {
    lpexView.doCommand("set excludedClasses " + classes);
   }
 }
}