You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by po...@apache.org on 2006/05/09 22:45:59 UTC

svn commit: r405527 - in /jakarta/commons/proper/jelly/trunk/jelly-tags/interaction: project.xml src/java/org/apache/commons/jelly/tags/interaction/AskTag.java src/test/org/apache/commons/jelly/tags/interaction/sample.jelly

Author: polx
Date: Tue May  9 13:45:57 2006
New Revision: 405527

URL: http://svn.apache.org/viewcvs?rev=405527&view=rev
Log:
Applying patch at JELLY-229 of Lukas Theussl.
Complementing with a better sample script and a method
to ignore the history in the completor if need be.
paul

Modified:
    jakarta/commons/proper/jelly/trunk/jelly-tags/interaction/project.xml
    jakarta/commons/proper/jelly/trunk/jelly-tags/interaction/src/java/org/apache/commons/jelly/tags/interaction/AskTag.java
    jakarta/commons/proper/jelly/trunk/jelly-tags/interaction/src/test/org/apache/commons/jelly/tags/interaction/sample.jelly

Modified: jakarta/commons/proper/jelly/trunk/jelly-tags/interaction/project.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jelly/trunk/jelly-tags/interaction/project.xml?rev=405527&r1=405526&r2=405527&view=diff
==============================================================================
--- jakarta/commons/proper/jelly/trunk/jelly-tags/interaction/project.xml (original)
+++ jakarta/commons/proper/jelly/trunk/jelly-tags/interaction/project.xml Tue May  9 13:45:57 2006
@@ -36,8 +36,9 @@
     <dependency>
       <groupId>jline</groupId>
       <artifactId>jline</artifactId>
-      <version>0.9.0</version>
-    </dependency> 
+      <version>0.9.5</version>
+      <type>jar</type>
+    </dependency>  
     <dependency>
       <groupId>commons-cli</groupId>
       <artifactId>commons-cli</artifactId>

Modified: jakarta/commons/proper/jelly/trunk/jelly-tags/interaction/src/java/org/apache/commons/jelly/tags/interaction/AskTag.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jelly/trunk/jelly-tags/interaction/src/java/org/apache/commons/jelly/tags/interaction/AskTag.java?rev=405527&r1=405526&r2=405527&view=diff
==============================================================================
--- jakarta/commons/proper/jelly/trunk/jelly-tags/interaction/src/java/org/apache/commons/jelly/tags/interaction/AskTag.java (original)
+++ jakarta/commons/proper/jelly/trunk/jelly-tags/interaction/src/java/org/apache/commons/jelly/tags/interaction/AskTag.java Tue May  9 13:45:57 2006
@@ -19,6 +19,9 @@
 import java.io.IOException;
 import java.io.InputStreamReader;
 
+import java.util.List;
+import java.util.ArrayList;
+
 import jline.ConsoleReader;
 import jline.History;
 import jline.SimpleCompletor;
@@ -35,12 +38,11 @@
   * 
   * @author <a href="mailto:smor@hasgard.net">Stéphane Mor </a>
    */
-public class AskTag extends TagSupport
-{
+public class AskTag extends TagSupport {
 
     private static Log logger = LogFactory.getLog(AskTag.class);
 
-    /** The question to ask to the user */
+    /** The question to ask to the user. */
     private String question;
 
     /**
@@ -49,15 +51,21 @@
      */
     private String answer = "interact.answer";
 
-    /** The default value, if the user doesn't answer */
+    /** The default value, if the user doesn't answer. */
     private String defaultInput;
 
     /** The user's input */
     private String input = "";
 
-    /** The prompt to display before the user input */
+    /** The prompt to display before the user input. */
     private String prompt = ">";
 
+    /** A list of predefined commands for tab completion. */
+    private List completor;
+    
+    /** Whether to complete with previous completions as well. */
+    private boolean useHistoryCompletor = true;
+
     private static History consoleHistory = new History();
 
     /**
@@ -72,8 +80,8 @@
     }
 
     /**
-     * Sets the name of the variable that will hold the answer This defaults to
-     * "interact.answer".
+     * Sets the name of the variable that will hold the answer. 
+     * This defaults to "interact.answer".
      * 
      * @param answer
      *            the name of the variable that will hold the answer
@@ -86,7 +94,7 @@
      * Sets the default answer to the question. If it is present, it will appear
      * inside [].
      * 
-     * @param default
+     * @param defaultInput
      *            the default answer to the question
      */
     public void setDefault(String defaultInput) {
@@ -96,7 +104,7 @@
     /**
      * Sets the prompt that will be displayed before the user's input.
      * 
-     * @param promt
+     * @param prompt
      *            the prompt that will be displayed before the user's input.
      */
     public void setPrompt(String prompt) {
@@ -104,7 +112,26 @@
     }
 
     /**
-     * Perform functionality provided by the tag
+     * Sets the list of predefined commands.
+     * 
+     * @param list
+     *            the list of commands used for tab completion.
+     */
+    public void setCompletor(List list) {
+        this.completor = list;
+    }
+    
+    /**
+     * Whether the completion should also happen on previously
+     * entered lines (default true).
+     * @param should whether it should
+     */
+    public void setUseHistoryCompletor(boolean should) {
+        this.useHistoryCompletor = should;
+    }
+
+    /**
+     * Perform functionality provided by the tag.
      * 
      * @param output
      *            the place to write output
@@ -145,10 +172,15 @@
                 consoleReader.setBellEnabled(false);
 
                 // add old commands as tab completion history
-                String[] oldCommands = new String[consoleHistory
-                        .getHistoryList().size()];
-                consoleHistory.getHistoryList().toArray(oldCommands);
-                consoleReader.addCompletor(new SimpleCompletor(oldCommands));
+                ArrayList oldCommandsAsList = useHistoryCompletor ?
+                    new ArrayList(consoleHistory.getHistoryList()) : new ArrayList(0);
+                // add predefined commands if given
+                if (completor != null && !completor.isEmpty()) {
+                    oldCommandsAsList.addAll(completor);
+                }
+                String[] oldCommands = new String[oldCommandsAsList.size()];
+                oldCommandsAsList.toArray(oldCommands);
+                consoleReader.addCompletor (new SimpleCompletor (oldCommands));
 
                 // read the input!
                 input = consoleReader.readLine();
@@ -171,4 +203,4 @@
 
         context.setVariable(answer, input);
     }
-}
\ No newline at end of file
+}

Modified: jakarta/commons/proper/jelly/trunk/jelly-tags/interaction/src/test/org/apache/commons/jelly/tags/interaction/sample.jelly
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jelly/trunk/jelly-tags/interaction/src/test/org/apache/commons/jelly/tags/interaction/sample.jelly?rev=405527&r1=405526&r2=405527&view=diff
==============================================================================
--- jakarta/commons/proper/jelly/trunk/jelly-tags/interaction/src/test/org/apache/commons/jelly/tags/interaction/sample.jelly (original)
+++ jakarta/commons/proper/jelly/trunk/jelly-tags/interaction/src/test/org/apache/commons/jelly/tags/interaction/sample.jelly Tue May  9 13:45:57 2006
@@ -19,10 +19,20 @@
 
     <!-- some sample scripts would be a good idea -->
 
-    BLOP
+    <j:new className="java.util.LinkedList" var="x"/>
+    <j:mute>${x.add('Newton')}${x.add('Galileo')}${x.add('Faraday')}
+      ${x.add('Pascal')}${x.add('Einstein')}</j:mute>
     
-    <interaction:ask question="Say something: " answer="answer"/>
-    
-    ${answer}
+    <j:while test='${true}'>
+      <interaction:ask question="Name a physicist before the 20th century : " answer="answer"
+        completor="${x}"/>
+Your answer was: ${answer}
+      <j:choose>
+<j:when test="${x.contains(answer)}">Congratulations!
+<j:break/></j:when>
+<j:otherwise>- I don't know a physicist by the name of ${answer}, please try again.
+</j:otherwise>
+      </j:choose>
+    </j:while>
     
 </j:jelly>



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org