package telnet;

/* This file is part of "Telnet Floyd".
 *
 * (c) Radek Polak 2003-2004. All Rights Reserved.
 *
 * Please visit project homepage at http://phoenix.inf.upol.cz/~polakr
 *
 * --LICENSE NOTICE--
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * --LICENSE NOTICE--
 *
 */

import javax.microedition.lcdui.*;

/**
 * Dialog where user can specify parameters for given action. Action type is
 * specified by calling function setAction. E.g to set up parameters for new
 * connection use this code:
 *     ActionDlg dlg = new ActionDlg();
 *     Telnet.setDisplay(dlg);
 *     dlg.setAction(Action.CONNECT);
 */

public class FuncParamsDlg
    extends Form
    implements CommandListener {

  String funct;
  TextField tfArg0;
  TextField tfArg1;
  TextField tfArg2;

  private final String LINES_TO_SCROLL = "lines to scroll";

  public FuncParamsDlg() {
    super("Function args");
    setCommandListener(this);
    addCommand(new Command("OK", Command.OK, 1));
  }

  public void setAction( String funct )
  {
    this.funct = funct;

    switch (funct.hashCode()) {

      case Action.BG_COLOR_HASH:
        add3("r", "g", "b",  "255", "255", "255");
        break;
      case Action.CONNECT_HASH:
        add3("host", "login", "password", "158.194.80.13", "polakr", "pwd");
        break;
      case Action.ENTER_STRING_HASH:
        add1("string to enter", "");
        break;
      case Action.FG_COLOR_HASH:
        add3("r", "g", "b", "0", "0", "0");
        break;
      case Action.INPUT_DIALOG_HASH:
        add1("default text", "" );
        break;
      case Action.KEEP_CONNECTION_TIME_HASH:
        add1("seconds after it dies", "300");
        break;
      case Action.KEY_PRESSED_HASH:
        add3("keycode", "keychar", "modifiers", "37", "65535", "8");
        break;
      case Action.KEY_TYPED_HASH:
        add3("key or code", "(keychar)", "(modifiers)", "ctrl+c", "", "");
        break;
      case Action.SCOLLBACK_DOWN_HASH:
        add1( LINES_TO_SCROLL, "5" );
        break;
      case Action.SCOLLBACK_UP_HASH:
        add1( LINES_TO_SCROLL, "5" );
        break;
      case Action.SCROLL_DOWN_HASH:
        add1( LINES_TO_SCROLL, "5" );
        break;
      case Action.SCROLL_LEFT_HASH:
        add1( LINES_TO_SCROLL, "5" );
        break;
      case Action.SCROLL_RIGH_HASH:
        add1( LINES_TO_SCROLL, "5" );
        break;
      case Action.SCROLL_UP_HASH:
        add1( LINES_TO_SCROLL, "5" );
        break;
      case Action.SET_SCOLLBACK_SIZE_HASH:
        add1( LINES_TO_SCROLL, "100" );
        break;
      case Action.SET_SCREEN_SIZE_HASH:
        add2( "width", "height", "80", "24" );
        break;
      case Action.SET_SLEEP_TIME_HASH:
        add1( "time in ms", "1000" );
        break;
      case Action.TYPE_STRING_HASH:
        add1( "string", "" );
        break;
      default:
        done();
    }
  }

  private void add1(String label0, String val0) {
    tfArg0 = new TextField(label0, val0, 64, TextField.ANY);
    append(tfArg0);
  }

  private void add2(String label0, String label1, String val0, String val1) {
    add1( label0, val0 );
    tfArg1 = new TextField(label1, val1, 64, TextField.ANY);
    append(tfArg1);
  }

  private void add3(String label0, String label1, String label2, String val0,
                    String val1, String val2) {
    add2( label0, label1, val0, val1 );
    tfArg2 = new TextField(label2, val2, 64, TextField.ANY);
    append(tfArg2);
  }

  private void done() {
    Action action = null;

    if (tfArg0 == null) {
      action = new Action(funct);
    }
    else
    if (tfArg1 == null) {
      action = new Action(funct, tfArg0.getString());
    }
    else
    if (tfArg2 == null) {
      action = new Action(funct, tfArg0.getString(), tfArg1.getString());
    }
    else {
      action = new Action(funct, tfArg0.getString(), tfArg1.getString(),
                 tfArg2.getString());
    }

    Telnet.setDisplay( (Displayable)new BindKey(action));
  }

  public void commandAction(Command command, Displayable displayable) {
    done();
  }
}

