import java.awt.*;
import java.awt.event.*;
import java.util.*;
final public class ir extends WindowAdapter
implements ActionListener {
// Main Frame
private Frame mainFrame;
private static TextArea area;
//Menu items - file -
private Menu fileMenu;
private MenuItem exitMI;
private MenuItem openMI;
private MenuItem saveMI;
private MenuItem saveasMI;
private MenuItem printMI;
private MenuItem closeMI;
//Menu items - help -
private Menu helpMenu;
private MenuItem aboutMI;
// Panel
private Panel mainPanel;
//Window Size
private int windowWidth = 240;
private int windowHeight = 320;
private int titleHeight = 13;
private int taskbarHeight = 19;
private int margin = 2;
public ir (String[] menu_prin, String[] menu_arch, String[]
menu_ayuda) {
// Create frame
mainFrame = new Frame("Impresion via IrDa");
Button b = new Button("Imprimir CB");
area = new TextArea("01234567890",1,2);
mainFrame.setSize(240,280);
mainFrame.setLayout( new BorderLayout() );
b.addActionListener(this);
mainFrame.add(area, BorderLayout.NORTH);
mainFrame.add(b, BorderLayout.SOUTH);
// Create menu bar
MenuBar mb = new MenuBar();
mainFrame.setMenuBar(mb);
//File Menu
fileMenu = new Menu(menu_prin[0]);
openMI = new MenuItem(menu_arch[0]);
saveMI = new MenuItem(menu_arch[1]);
saveasMI = new MenuItem(menu_arch[2]);
printMI = new MenuItem(menu_arch[3]);
closeMI = new MenuItem(menu_arch[4]);
exitMI = new MenuItem(menu_arch[5]); //"Exit"
exitMI.addActionListener(this);
fileMenu.add(openMI);
fileMenu.add(saveMI);
fileMenu.add(saveasMI);
fileMenu.add(printMI);
fileMenu.add(closeMI);
fileMenu.add(exitMI);
mb.add(fileMenu);
//Help Menu
helpMenu = new Menu(menu_prin[1]);
aboutMI = new MenuItem(menu_ayuda[0]); //"Acerca");
aboutMI.addActionListener(this);
helpMenu.add(aboutMI);
mb.add(helpMenu);
//setup panel
mainPanel = new Panel();
mainFrame.add(mainPanel);
mainPanel.setVisible(true);
// setup frame
mainFrame.addWindowListener(this);
mainFrame.setSize((windowWidth - margin + 2 ), (windowHeight -
titleHeight - taskbarHeight - margin * 2 ));
mainFrame.setVisible( true );
mainFrame.setResizable(false);
mainFrame.show();
}
// Event handling
// windows event
public void windowClosing (WindowEvent we) {
if(we.getWindow() == mainFrame) {
System.exit(0);
}
}
// action event
public void actionPerformed (ActionEvent ae) {
Object o = ae.getSource();
if (o instanceof MenuItem) {
MenuItem mi = (MenuItem)o;
if (mi == exitMI) {
System.exit(0);
} else if (mi ==aboutMI) {
new l10nAbout();
} else if (mi == printMI) {
try { convert();
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}
}
} else if( o instanceof Button ) {
Button bi = (Button) o;
if( o == bi ) {
try { convert();
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}
}
}
}
public static String locale_en_Z() throws IOException
{
StringTokenizer st1;
String quote = "";
FileReader infile = new FileReader("locale.conf");
BufferedReader buff = new BufferedReader(infile);
boolean eof = false;
boolean siguiente = false;
while( !eof ) {
String line = buff.readLine();
if( line == null )
eof = true;
else {
if( siguiente ) {
quote = line;
siguiente = false;
}
line = line.trim();
if( line.equals("[Language]") ) siguiente = true;
}
}
buff.close();
st1 = new StringTokenizer( quote, "=");
st1.nextToken();
return st1.nextToken();
}
public static void main (String args[]) {
String[][] menu_prin = { {"Archivo","Ayuda"}, {"File","Help"},
{"Akte","Hilfe"} };
String[][] menu_arch = { {"Abrir","Guardar","Guardar Como",
"Imprimir","Cerrar","Salir"},
{"Open","Save","Save as",
"Print","Close","Exit"},
{"Geoffnet","AuBer","AuBer wie",
"Druck","Ende","Auf Wiedersehen"} };
String[][] menu_ayuda = { {"Acerca"}, {"About"}, {"Uber"} };
int arreglo; // 0 = sp, 1 = en, 2 = de
String localiza = "sp";
try { localiza = locale_en_Z(); }
catch (IOException e) {
System.err.println(e);
return;
}
localiza = localiza.trim();
if( args.length == 1 ) localiza = args[0];
arreglo = 0; // sp por defecto
if(localiza.equals("sp")) arreglo = 0;
else if(localiza.equals("en")) arreglo = 1;
else if(localiza.equals("de")) arreglo = 2;
ir foo = new ir(menu_prin[arreglo], menu_arch[arreglo],
menu_ayuda[arreglo]);
}
public static void convert() throws IOException,
UnsupportedEncodingException {
try {
String bc;
char[] bc_user = new char[11];
String texto = "";
InputStream in;
OutputStream out;
String infile = "holaz.lbl";
String outfile = "/dev/irlpt0";
int ubicacion, larea;
if( infile != null) in = new FileInputStream(infile);
else return;
if( outfile != null) out = new FileOutputStream(outfile);
else return;
Reader r = new BufferedReader( new
InputStreamReader(in));
Writer w = new BufferedWriter( new
OutputStreamWriter(out));
bc = area.getText();
larea = bc.length();
for( int j = 0; j < larea; j++ )
bc_user[j] = bc.charAt(j);
texto = "! 0 200 200 400 1\n\r";
texto += "LABEL\n\r";
texto += "CONTRAST 0\n\r";
texto += "TONE 0\n\r";
texto += "SPEED 5\n\r";
texto += "PAGE-WIDTH 600\n\r";
texto += "BAR-SENSE\n\r";
texto += ";// PAGE 0000000006000400\n\r";
texto += "T90 5 3 21 296 Hola Mundo\n\r";
texto += "T 5 3 157 89 Desde la ZAURUS\n\r";
texto += "BT 0 2 10\n\r";
texto += "B UPCA 2 1 90 164 176 ";
ubicacion = texto.length();
texto += bc;
texto += " \n\r";
texto += "BT OFF\n\r";
texto += "BOX 24 90 129 308 1\n\r";
texto += "LINE 279 322 279 338 21\n\r";
texto += "BOX 262 314 322 350 1\n\r";
texto += "FORM\n\r";
texto += "PRINT\n\r";
char[] buffer = new char[4096];
int len;
while( (len = r.read(buffer)) != -1 ) {
for( int k = 0; k < larea; k++ )
buffer[ubicacion+k] = bc_user[k];
w.write(buffer, 0, len);
System.err.print(buffer[ubicacion]);
ubicacion++;
System.err.print(buffer[ubicacion]);
}
r.close();
w.close();
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}
}
}