package telnet;

import javax.microedition.lcdui.*;

/* This file is part of "Telnet Floyd".
 *
 * (c) Radek Polak 2003-2004. All Rights Reserved.
 * The file was changed by Radek Polak to work as midlet in MIDP 1.0
 *
 * 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--
 *
 */

/**
 * User can use this dialog to enter specified text or special sequences like
 * ctrl+d etc.. To invoke this special just enter "ctrl+d" or "ctrld".
 */

public class InputDialog extends TextBox implements CommandListener {
  /**Construct the displayable*/
  public InputDialog() {
    super("", "", 1024, TextField.ANY);
    setCommandListener(this);
    addCommand(new Command("Enter", Command.OK, 1 ));
    addCommand(new Command("Type", Command.ITEM, 2 ));
  }

  /**Handle command events*/
  public void commandAction(Command command, Displayable displayable) {
    Telnet.setDisplay( (Displayable) Telnet.terminal);
    String str = this.getString();
    int modifiers = Telnet.terminal.getModifiers(str);
    if (modifiers != 0) { // special keys - like ctrl+d
      char keyChar = (char) (str.charAt(str.length() - 1) - 'a' + 1);
      Telnet.emulation.keyTyped(0, keyChar, modifiers); // key identified by specific string (CTRL+x)
    }
    else {
      for (int i = 0; i < str.length(); i++)
        Telnet.emulation.keyTyped(0, str.charAt(i), 0);
      if (command.getCommandType() == Command.OK)
        Telnet.emulation.keyTyped(0, '\n', 0);
    }
  }
}
