KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jline > Terminal


1 /**
2  * jline - Java console input library
3  * Copyright (c) 2002,2003 Marc Prud'hommeaux mwp1@cornell.edu
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jline;
20
21 import java.io.*;
22 import java.util.*;
23
24
25 /**
26  * Representation of the input terminal for a platform. Handles
27  * any initialization that the platform may need to perform
28  * in order to allow the {@link ConsoleReader} to correctly handle
29  * input.
30  *
31  * @author <a HREF="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
32  */

33 public abstract class Terminal
34 {
35     private static Terminal term;
36
37
38     /**
39      * @see #setupTerminal
40      */

41     public static Terminal getTerminal ()
42     {
43         return setupTerminal ();
44     }
45
46
47     /**
48      * <p>Configure and return the {@link Terminal} instance for the
49      * current platform. This will initialize any system settings
50      * that are required for the console to be able to handle
51      * input correctly, such as setting tabtop, buffered input, and
52      * character echo.</p>
53      *
54      * <p>This class will use the Terminal implementation specified in the
55      * <code>jline.terminal</code> system property, or, if it is unset, by
56      * detecting the operating system from the <code>os.name</code>
57      * system property and instantiateing either the
58      * {@link WindowsTerminal} or {@link UnixTerminal}.
59      *
60      * @see #initializeTerminal
61      */

62     public static synchronized Terminal setupTerminal ()
63     {
64         if (term != null)
65             return term;
66
67         final Terminal t;
68
69         String JavaDoc os = System.getProperty ("os.name").toLowerCase ();
70         String JavaDoc termProp = System.getProperty ("jline.terminal");
71         if (termProp != null && termProp.length () > 0)
72         {
73             try
74             {
75                 t = (Terminal)Class.forName (termProp).newInstance ();
76             }
77             catch (Exception JavaDoc e)
78             {
79                 throw (IllegalArgumentException JavaDoc)new IllegalArgumentException JavaDoc (
80                     e.toString ()).fillInStackTrace ();
81             }
82         }
83         else if (os.indexOf ("windows") != -1)
84         {
85             t = new WindowsTerminal ();
86         }
87         else
88         {
89             t = new UnixTerminal ();
90         }
91
92         try
93         {
94             t.initializeTerminal ();
95         }
96         catch (Exception JavaDoc e)
97         {
98             e.printStackTrace ();
99             return term = new UnsupportedTerminal ();
100         }
101
102         return term = t;
103     }
104
105
106     /**
107      * Initialize any system settings
108      * that are required for the console to be able to handle
109      * input correctly, such as setting tabtop, buffered input, and
110      * character echo.
111      */

112     public abstract void initializeTerminal ()
113         throws Exception JavaDoc;
114
115
116     /**
117      * Returns the current width of the terminal (in characters)
118      */

119     public abstract int getTerminalWidth ();
120
121
122     /**
123      * Returns the current height of the terminal (in lines)
124      */

125     public abstract int getTerminalHeight ();
126
127
128     /**
129      * Returns true if this terminal is capable of initializing the
130      * terminal to use jline.
131      */

132     public abstract boolean isSupported ();
133
134
135     /**
136      * Returns true if the terminal will echo all characters type.
137      */

138     public abstract boolean getEcho ();
139 }
140
Popular Tags