001: /*
002: * $Author: rahul_kumar $
003: * $Id: CommandHandler.java,v 1.1 2004/02/01 07:48:22 rahul_kumar Exp rahul $
004: * This is free software, as software should be; you can redistribute
005: * it and/or modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008:
009: * See LICENSE.txt for the full license covering this software/program/code.
010: */
011:
012: package isql;
013:
014: import java.util.*;
015:
016: /**
017: * Class documentation.
018: * @author rahul kumar <rahul_kumar@yahoo.com>
019: * @see XXX
020: */
021:
022: public class CommandHandler {
023:
024: /** ctor/constructor.
025: */
026: public CommandHandler(SQLForm form) {
027: _form = form;
028: commandMap = new TreeMap();
029: commands = Commands.getCommands();
030: }
031:
032: /** register the given command, by putting it in the command map
033: * along with its command list.
034: */
035: public void register(Command com) {
036: // fetch the string for given command and then put in map.
037: String commandlist[] = com.getCommandList();
038: for (int i = 0; i < commandlist.length; i++) {
039: commandMap.put(commandlist[i], com);
040: }
041: }
042:
043: /** register all the commands in the commands array.
044: */
045: public void registerAllCommands() {
046: for (int i = 0; i < commands.length; i++) {
047: register(commands[i]);
048: }
049: }
050:
051: /** execute the given command.
052: * fetches the command object from the map, and calls execute()
053: * passing the SQLForm object.
054: */
055: public void execute(String command, String line) {
056: Command com = getCommandFor(command);
057: if (com == null) {
058: String serr = ("No command class was setup to handle this command. ["
059: + command + "]");
060: System.err.println(serr);
061: _form.popup(serr);
062: return;
063: }
064: com.execute(_form, command, line);
065: }
066:
067: /** returns the command object for a key.
068: * required by help() in MetadataCommand also.
069: */
070: public Command getCommandFor(String key) {
071: Command com = (Command) commandMap.get(key);
072: // hack for default command - which handles all else.
073: if (com == null) {
074: com = (Command) commandMap.get("");
075: }
076: return com;
077:
078: }
079:
080: /** returns commands.
081: * used by MetadataCommand help().
082: */
083: public Command[] getCommands() {
084: return commands;
085: }
086:
087: ////// START INSTANCE VARIABLES //////
088:
089: /** a map of strings and command objects. */
090: Map /*<String><Command>*/commandMap;
091: /** array of commands */
092: Command[] commands;
093: /** instance of form object
094: */
095: SQLForm _form;
096:
097: ////// START CONSTANTS AND CLASS LEVEL VARIABLES //////
098: static final String P = "CommandHandler"; // used in exception strings
099:
100: } // end of class
|