midterm.java

来自「j2me学习 简单例子」· Java 代码 · 共 470 行 · 第 1/2 页

JAVA
470
字号
/* License
 * 
 * Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  
 *  * Redistribution of source code must retain the above copyright notice,
 *	this list of conditions and the following disclaimer.
 * 
 *  * Redistribution in binary form must reproduce the above copyright notice,
 *	this list of conditions and the following disclaimer in the
 *	documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *  
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *  
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility. 
 */
package example.term.telnetfont;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

/**
* Connects to a telnet host using a user-specified host and port.
*/ 
public class MIDTerm extends MIDlet implements CommandListener, Runnable
{ 
    private Display display;
    private String host, port, user, pass;
    private TelnetCanvas canvas;
    private TelnetConnection connection;
    private InputStream input;
    private OutputStream output;
    private Command connectCommand, disconnectCommand, openCommand, closeCommand;
    private Command inputCommand, okCommand, cancelCommand, exitCommand;

    // form for login
    private Form loginForm;
    private TextField hostField;
    private TextField portField;
    
    // form for user input
    private Form inputForm;
    private TextField inputField;
    private ChoiceGroup inputOptions;
    private ChoiceGroup scrollOptions;
    
    // user-visible strings
    private final static String INPUT_ESC = "Prepend ESC";
    private final static String INPUT_CTRL = "Send as CTRL";
    private final static String INPUT_ENTER = "Append ENTER";
    private final static String INPUT_SCROLL = "Scroll Lock";
    
    public MIDTerm()
    {
        display = null;
    
        host = getAppProperty( "MIDTerm-Host" );
        if ( host == null ) host = "127.0.0.1";
        port = getAppProperty( "MIDTerm-Port" );
        if ( port == null ) port = "23";
        user = getAppProperty( "MIDTerm-User" );
        pass = getAppProperty( "MIDTerm-Pass" );
            
        canvas = new TelnetCanvas();
        connection = null;
        
        openCommand = new Command( "Open", Command.OK, 0 );
        exitCommand = new Command( "Exit", Command.EXIT, 0 );
        connectCommand = new Command( "Start", Command.OK, 0 );
        disconnectCommand = new Command( "Stop", Command.EXIT, 0 );
        closeCommand = new Command( "Close", Command.EXIT, 0 );
        inputCommand = new Command( "Input", Command.OK, 0 );
        okCommand = new Command( "OK", Command.OK, 0 );
        cancelCommand = new Command( "Cancel", Command.CANCEL, 0 );
        
        canvas.addCommand( closeCommand );
        canvas.addCommand( connectCommand );
        canvas.setCommandListener( this );
        
        // set up input form
        inputForm = new Form( "Input" );
        inputField = new TextField( null, "", 255, TextField.ANY );
        inputForm.append( inputField );
        inputOptions = new ChoiceGroup( null, Choice.MULTIPLE );
        inputOptions.append( INPUT_ENTER, null );
        inputOptions.append( INPUT_CTRL, null );
        inputOptions.append( INPUT_ESC, null );
        inputOptions.setSelectedIndex( 0, true ); // default ENTER to true
        inputForm.append( inputOptions );
        scrollOptions = new ChoiceGroup( null, Choice.MULTIPLE );
        scrollOptions.append( INPUT_SCROLL, null );
        inputForm.append( scrollOptions );
        inputForm.addCommand( okCommand );
        inputForm.addCommand( cancelCommand );
        inputForm.setCommandListener( this );
    }

    public void startApp()
    {
        if ( display == null )
        {
            display = Display.getDisplay( this );
            onShowLogin();
        }
    }

    public void pauseApp()
    {
        disconnect();
    }
    
    public void destroyApp( boolean unconditional )
    {
        disconnect();
    }
    
    private void connect()
    {
        disconnect();

        canvas.removeCommand( closeCommand );
        canvas.removeCommand( connectCommand );
        canvas.addCommand( disconnectCommand );
        canvas.addCommand( inputCommand );
        
        new Thread( this ).start();
    }
    
    private void disconnect()
    {
        canvas.removeCommand( disconnectCommand );
        canvas.removeCommand( inputCommand );
        canvas.addCommand( closeCommand );
        canvas.addCommand( connectCommand );
        
        if ( input != null )
        {
            try
            {
                input.close();
                input = null;
            }
            catch ( IOException ioe )
            {
                System.err.println( "Error while disconnecting input: " + ioe.toString() );
                canvas.receive( toASCII( "Error while disconnecting input: " + ioe.toString() ) );
            }
        }
        if ( output != null )
        {
            try
            {
                output.close();
                output = null;
            }
            catch ( IOException ioe )
            {
                System.err.println( "Error while disconnecting output: " + ioe.toString() );
                canvas.receive( toASCII( "Error while disconnecting output: " + ioe.toString() ) );
            }
        }
        if ( connection != null )
        {
            try
            {
                connection.close();
                connection = null;
            }
            catch ( IOException ioe )
            {
                System.err.println( "Error while disconnecting: " + ioe.toString() );
                canvas.receive( toASCII( "Error while disconnecting: " + ioe.toString() ) );
            }
        }
    }
    
    public void onShowLogin()
    {
        // create and populate login form
        loginForm = new Form( "Connect to" );
        hostField = new TextField( "Host", host, 50, TextField.URL );
        loginForm.append( hostField );
        portField = new TextField( "Port", port, 6, TextField.NUMERIC );
        loginForm.append( portField );
        loginForm.addCommand( exitCommand );
        loginForm.addCommand( openCommand );
        loginForm.setCommandListener( this );
        
        // show form
        display.setCurrent( loginForm );
    }

    public void onDoLogin()
    {
        host = hostField.getString().trim();
        port = portField.getString().trim();
        if ( host.length() == 0 ) host = "127.0.0.1";
        if ( port.length() == 0 ) port = "23";
        
        canvas.removeCommand( disconnectCommand );
        canvas.removeCommand( inputCommand );
        canvas.addCommand( closeCommand );
        canvas.addCommand( connectCommand );
        canvas.reset();
        
        display.setCurrent( canvas );
        
        connect();
        
        // dispose login form
        loginForm = null;
        hostField = null;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?