//---------------------------------------------------------------------------- // COMPONENT NAME: LPEX Editor // // All Rights Reserved. // // DESCRIPTION: // SetDateAction - sample user-defined action (setDate) //---------------------------------------------------------------------------- package com.ibm.lpex.samples; import com.ibm.lpex.core.LpexAction; import com.ibm.lpex.core.LpexCommand; import com.ibm.lpex.core.LpexDocumentLocation; import com.ibm.lpex.core.LpexStringTokenizer; import com.ibm.lpex.core.LpexView; import java.text.SimpleDateFormat; import java.util.Date; /** * Sample action <b>setDate</b> - set the sequence-numbers date in selected range. * Use this action to modify the textual part of the sequence numbers in all the * currently-selected visible lines. The user is prompted for the new date. * * <p>Here is the SetDateAction * <a href="doc-files/SetDateAction.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.setDate com.ibm.lpex.samples.SetDateAction</pre></li> * <li>Run it from the editor command line: * <pre>action setDate</pre> * or associate it with a key: * <pre>set keyAction.c-t setDate</pre></li> * </ul></p> * * @see com.ibm.lpex.samples All the samples */ public class SetDateAction implements LpexAction { /** * Runs the action. * Prompts the user for a new date, then updates the sequence numbers for all * the visible selected lines. Uses the <b>input</b> default editor command, * and the <b>doSetDate</b> command defined in here. */ public void doAction(LpexView lpexView) { // ensure our set-date command is defined in the view in which this action runs lpexView.defineCommand("doSetDate", setDateCommand); // prompt user for the new date, update visible selected lines lpexView.doDefaultCommand(getInputCommandString(lpexView)); } /** * Returns the availability of this action. * The action can be run in any visible, writable view of a document with * sequence numbers which have a textual part. */ public boolean available(LpexView lpexView) { return canSetSequenceNumbersText(lpexView); } /** * Variation of SetDateAction that can be used when our <b>doSetDate</b> * command has already been defined in that view. */ public static LpexAction setDateAction = new LpexAction() { public void doAction(LpexView lpexView) { lpexView.doDefaultCommand(getInputCommandString(lpexView)); } public boolean available(LpexView lpexView) { return canSetSequenceNumbersText(lpexView); } }; /** * Command to set the sequence-numbers text, passed in parameters, in all * the visible selected lines, or in the current line if no selection. */ public static LpexCommand setDateCommand = new LpexCommand() { public boolean doCommand(LpexView lpexView, String parameters) { // determine the range of elements to set int firstElement, lastElement; // 1.- visible selection in this view if (lpexView.queryOn("block.inView") && lpexView.queryOn("block.anythingSelected")) { firstElement = lpexView.queryInt("block.topElement") - lpexView.linesBeforeStart(); lastElement = lpexView.queryInt("block.bottomElement") - lpexView.linesBeforeStart(); } // 2.- no selected text (is visible) in this view else { firstElement = lastElement = lpexView.currentElement(); } // go through all visible lines in range LpexDocumentLocation loc = new LpexDocumentLocation(1, 1); for (int i = firstElement; i <= lastElement; i++) { if (!lpexView.show(i)) { loc.element = i; if (lpexView.queryOn("visible", loc)) { lpexView.doCommand(loc, "set sequenceText " + parameters); } } } return true; } }; // Returns the input command to use for prompting and setting the date (see // the "input" editor command), e.g., input "New date:" "040630" "doSetDate ". private static String getInputCommandString(LpexView lpexView) { int sequenceNumbersTextLength = sequenceNumbersTextLength(lpexView); // prompt - specify YYMMDD if the sequence numbers are iSeries style String prompt = (sequenceNumbersTextLength == 6)? "New date (YYMMDD):" : "New date:"; prompt = LpexStringTokenizer.addQuotes(prompt); // prompt initialization - default text if set / today's date if room for it / 0s String initialText = lpexView.query("current.sequenceDefaultText"); if (initialText == null || initialText.trim().length() == 0) { // iSeries style - use today's date, in YYMMDD format if (sequenceNumbersTextLength == 6) { initialText = new SimpleDateFormat("yyMMdd").format(new Date()); } // otherwise - initialize to 0s else { initialText = "0"; while (initialText.length() < sequenceNumbersTextLength) { initialText += '0'; } } } initialText = LpexStringTokenizer.addQuotes(initialText); // build the entire input command return "input " + prompt + ' ' + initialText + " \"doSetDate \""; } // Checks whether we can set sequence-numbers text in the given view: // this should be the writable, visible view of a document with sequence // numbers which have a textual part. private static boolean canSetSequenceNumbersText(LpexView lpexView) { return lpexView.currentElement() != 0 && !lpexView.queryOn("readonly") && sequenceNumbersTextLength(lpexView) != 0; } // Returns the length of the document's sequence-numbers text part. private static int sequenceNumbersTextLength(LpexView lpexView) { return Integer.parseInt(lpexView.query("current.sequenceNumbers").split(" ")[3]); } }