by June 30, 2004 0 comments



If you use RedHat or PCQLinux, you may be familiar with neat configuration tools, such as ntsysv, netconfig and redhat-config-securitylevel-tui, which use a TUI (Text User Interface) for configuring startup services, the network and firewall. These are quite handy if you don’t have X Window, which is required to launch jazzy graphical-configuration tools.
On the development side, such TUI applications are usually written using curses and ncurses (the
latest incarnation), a popular library for text-based interfaces. The languages used are usually C or Python. You can also use Java to code rich TUI applications. We use CHARVA
(CHARacter-mode jaVA) to show you how.

For Java programmers, CHARVA provides a set of Java classes and a native library to code TUIs. What’s more, the CHARVA classes are almost the same as the Swing and AWT classes (used for writing GUI applications). This means, you have a JFrame for displaying a screen on the Linux console and even a JFileChooser to browse the directories on the Linux machine. You can set the background color using the setBackground( ) method of the Jframe. The Java classes for event handling are packaged as java.awt.event and javax.swing.event. Without emphasizing further on how easy it will be for a Java programmer to get started, let’s look at a small Hello World program for a proof of the concept. 

A Hello World program

Direct
Hit!
Applies
to:
Linux/Unix-based application developers
USP:
No need to learn extra languages to write TUI apps
Links:
www.pitman.co.za/projects/charva/

import charvax.swing.*;
import charva.awt.*;
import charva.awt.event.*;

public class HelloWorld{
public static void main(String[] args){
System.setProperty(“charva.color”,””);
JFrame fr = new JFrame();
fr.setTitle(“Hello World”);
fr.setBackground(Color.blue);
fr.setSize(80,24);
Container contentPane = fr.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(new JLabel(“Hello World”),BorderLayout.CENTER);

JButton exitButton = new JButton(“Exit”);
exitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});

contentPane.add(exitButton,BorderLayout.SOUTH);
fr.show();
}
}

Very familiar, isn’t it? Note the import statements. javax.swing and java.awt for GUI applications have been very intuitively replaced by charvax.swing and charva.awt. Note that the size of the JFrame is set to 80X24, which is the typical dimension of a text-based terminal. Hence, the frame will show up fullscreen. The statement:

System.setProperty(“charva.color”,””);

is required to display colors in the TUI applications. 

Execute the code

First, download the file www.pitman.co.za/
projects/download/charva-1.0.2.tar.gz and extract it as:

tar -zxvf charva-1.0.2.tar.gz

A text-based DNS configuration screen written in Java

This will produce a directory called charva-1.0.1. Create a directory called myprograms under your home directory. Copy the file named charva.jar (found in the directory charva-1.0.1/java/lib) to this directory. Next, copy the file libTerminal.so (found in the directory charva-1.0.1/c/src) to
myprograms. 

Type the above Hello World program in a text editor and save it as HelloWorld.java in the myprograms directory. Switch to a Linux console or a terminal window in X. Change to the myprograms directory and compile the Java program as:

javac -classpath charva.jar HelloWorld.java

Before executing the programs, issue the following command.

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/myprograms

This is required to point the library path to the file libTerminal.so. This file is the native C library of CHARVA which talks to ncurses library. Execute the program as:

java -cp charva.jar:. HelloWorld

Pressing Enter will drop you back to the terminal. 

DNSConfigure Tool
Now let’s write a full-fledged program to set up the information of a DNS server for a client machine. Linux stores the IP address of the DNS server (to resolve the website names) in a file named resolv.conf found in /etc directory. The entries are in the form:

nameserver <ip-address-of-the-DNS-server>

The DNSConfigure programs show up a text field where the IP address of the DNS server is to filled. Pressing OK will write the IP on the resolv.conf in the above-mentioned format. 

Code for DNSConfigure

import charvax.swing.*;
import charva.awt.*;
import charva.awt.event.*;
import java.io.*;

public class DNSConfig extends JFrame{
public static void main(String[] args){ System.setProperty(“charva.color”,””);
JFrame DNSConfigFrame = new DNSConfig();
DNSConfigFrame.show();
}

public DNSConfig(){
setTitle(“Configure DNS”);
setBackground(Color.blue);
setupComponents();
setSize(80,24);
getContentPane().setLayout(null);
}

public void setupComponents(){

JPanel centerPanel = new JPanel();
centerPanel.setSize(40,10);
centerPanel.setBackground(Color.white);
centerPanel.setLocation(20,5);

JPanel shadowPanel = new JPanel();
shadowPanel.setSize(40,10);
shadowPanel.setBackground(Color.black);
shadowPanel.setLocation(21,6);
JPanel inputPanel = new JPanel();
inputPanel.setLocation(3,4);

JLabel caption = new JLabel(“Enter the IP address of your DNS server:”);
caption.setForeground(Color.red);

JLabel textFieldLabel = new JLabel(“DNS Server:”);
textFieldLabel.setForeground(Color.black);
dnsInput = new JTextField(15);
dnsInput.setForeground(Color.black);
dnsInput.setBackground(Color.blue);
dnsInput.setLocation(12,0);

JPanel buttonPanel = new JPanel();
buttonPanel.setLocation(0,7);

JButton okButton = new JButton(“Ok”);
okButton.setLocation(3,0);
okButton.setForeground(Color.blue);
okButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
writeToDNSFile();
System.exit(0);
}
});

JButton exitButton = new JButton(“Exit”);
exitButton.setLocation(32,0);
exitButton.setForeground(Color.blue);
exitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
buttonPanel.add(okButton);
buttonPanel.add(exitButton);

inputPanel.add(textFieldLabel);
inputPanel.add(dnsInput);

centerPanel.add(caption);
centerPanel.add(inputPanel);
centerPanel.add(buttonPanel);

getContentPane().add(shadowPanel);
getContentPane().add(centerPanel);
}

private void writeToDNSFile(){
try{
PrintWriter out = new PrintWriter(new FileOutputStream(“/etc/resolv.conf”));
out.println(“nameserver “+dnsInput.getText());
out.close();
}
catch(IOException ioe){
System.out.println(“Error writing to DNS file: “+ioe);
}
}
private JTextField dnsInput;
}

The setupComponents( ) methods lay out the UI components, such as panels, textfields, labels and buttons onto the application’s
JFrame. 

An interesting part of the code is the use of shadowPanel to add a shadow effect. Also note that all components are laid out using the setLocation( ) method which takes in the X and Y co-ordinates for the top-left corner of the component. The co-ordinates are relative to the container in which the component is added. For example, the co-ordinates of the shadowPanel are relative to the main frame while the co-ordinates of textfield are relative to the inputPanel. On an 80X24 pixel screen, using setLocation( ) method is definitely easier than using one of the layout components. CHARVA supports the familiar layout components, such as FlowLayout, BorderLayout and GridBagLayout. A peculiar thing is that the setForeground( ) method for the button actually sets its background color. 

To further explore CHARVA refer to the site www.pitman.co.za/projects/charva/. You can find the API documentation of CHARVA under the directory charva-1.0.1/java/doc. 

Shekhar Govindarajan
IT4Enterprise

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.

Your data will be safe!Your e-mail address will not be published. Also other data will not be shared with third person.