KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > editwizard > WizardCommand


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.applications.editwizard;
11
12 import java.util.*;
13 import org.mmbase.util.logging.*;
14
15 /**
16  * The wizardcommands are used to store information received from the clients about commands.
17  * Eg.: add-item, delete commands are stored here.
18  *
19  * @javadoc
20  * @author Kars Veling
21  * @author Pierre van Rooden
22  * @since MMBase-1.6
23  * @version $Id: WizardCommand.java,v 1.12 2005/10/05 10:41:38 michiel Exp $
24  */

25 public class WizardCommand {
26
27     public final static short UNKNOWN_COMMAND = -1;
28     public final static short ADD_ITEM = 0;
29     public final static short CANCEL = 1;
30     public final static short COMMIT = 2;
31     public final static short DELETE_ITEM = 3;
32     public final static short GOTO_FORM = 4;
33     public final static short MOVE_DOWN = 5;
34     public final static short MOVE_UP = 6;
35     public final static short SAVE = 7;
36     public final static short START_WIZARD = 8;
37     public final static short UPDATE_ITEM = 9;
38
39     /**
40      * Array with the command strings, as they are parsed.
41      * The accompanying command constant can be derived by using the
42      * index in the array.
43      */

44     private final static String JavaDoc[] COMMANDS =
45     {"add-item", "cancel", "commit", "delete-item", "goto-form", "move-down", "move-up", "save", "start-wizard", "update-item"};
46
47     private static final Logger log = Logging.getLoggerInstance(WizardCommand.class);
48
49     private String JavaDoc commandName="unknown";
50     private int type = UNKNOWN_COMMAND;
51     private List params = null;
52
53     private String JavaDoc value = null;
54
55     // the original command as it was passed
56
private String JavaDoc command;
57
58     /**
59      * Creates a wizard command object with the given command and value.
60      * The command parsed should be of the format:
61      * <code>
62      * cmd/type/fid/did/otherdid/
63      * </code>
64      * 'type' is the command itself (i.e. 'add-item'), fid, did, and otherdid are possible
65      * parameters to the command.
66      *
67      * @todo should use StringTokenizer here
68      * @param acommand The full command
69      * @param avalue The value of the command
70      */

71     public WizardCommand(String JavaDoc acommand, String JavaDoc avalue) {
72         command = acommand.toLowerCase();
73         value = avalue;
74         if (log.isDebugEnabled()) {
75             log.debug("command: " + command + " : "+value);
76         }
77
78         StringTokenizer st= new StringTokenizer(command,"/",true);
79         // skip first token ('cmd') and delimiter
80
st.nextToken();
81         st.nextToken();
82
83         // second token is command name (aka type)
84
commandName= st.nextToken();
85         st.nextToken(); // delimiter
86

87         // attempt to determine type from the (ordered) array of known commands.
88
type = Arrays.binarySearch(COMMANDS, commandName);
89         if (type<0) type =UNKNOWN_COMMAND;
90
91         int paramcount=st.countTokens();
92         if (paramcount>0) {
93             params = new ArrayList(paramcount);
94             // get optional other parameters: fid, did, and otherdid, possible others...
95
while (st.hasMoreTokens()) {
96                 String JavaDoc tok=st.nextToken();
97                 if (!tok.equals("/")) {
98                     params.add(tok);
99                     st.nextToken();
100                 } else {
101                     params.add("");
102                 }
103             }
104         }
105     }
106
107     /**
108      * Returns the type of the parsed command.
109      * @return one of the WizardCommand constants, or UNKNOWN_COMMAND if the type cannot be determined
110      */

111     public int getType() {
112         return type;
113     }
114
115     /**
116      * Returns the value passed to the parsed command.
117      * @return the value as a string
118      */

119     public String JavaDoc getValue() {
120         return value;
121     }
122    /**
123      * Returns the parameter with the indicated index of the parsed command.
124      * @return teh parameter as string, or an empty string if it doesn't exist.
125      */

126     public String JavaDoc getParameter(int i) {
127         if ((params==null) || (i>=params.size()))
128             return "";
129         else
130             return (String JavaDoc)params.get(i);
131     }
132
133     /**
134      * Returns the 'fid' (field id) parameter of the parsed command.
135      * This is always the parameter with index 0
136      * @return the fid as string, or an empty string if it doesn't exist.
137      */

138     public String JavaDoc getFid() {
139         return getParameter(0);
140     }
141
142     /**
143      * Returns the 'did' (data id) parameter of the parsed command.
144      * This is always the parameter with index 1
145      * @return the did as string, or an empty string if it doesn't exist.
146      */

147     public String JavaDoc getDid() {
148         return getParameter(1);
149     }
150
151     public String JavaDoc toString() {
152         return command;
153     }
154
155 }
156
Popular Tags