/* * Copyright (c) 1998 The Regenstrief Institute. All rights reserved. * * 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. * * Written by Gunther Schadow. * * $Id: UnitsApplet.java,v 1.3 1999/05/23 19:13:59 schadow Exp schadow $ */ import java.lang.*; import java.io.*; import units.Unit; import units.UnitTab; import units.ParseException; import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.text.NumberFormat; public class UnitsApplet extends Applet { Frame win = null; NumberFormat numForm = NumberFormat.getInstance(); public static void main(String argv[]) { UnitsApplet applet = new UnitsApplet(); applet.application_init(); } public UnitsApplet() { super(); } private void application_init() { win = new Frame("Units"); win.setLayout(new FlowLayout()); win.setSize(120,180); win.setResizable(false); win.add(this); init(); start(); win.pack(); win.show(); try { Thread.sleep(10000); } catch(Exception x) {}; stop(); destroy(); } Input from_value = null; Input from_unit = null; Input to_value = null; Input to_unit = null; TextField status = null; Choice dbchoice = null; static String table_names[] = { "Unified Code for Units of Measure (UCUM) c/s", "Unified Code for Units of Measure (UCUM) c/i", "SI plain", "ISO 1000", "ISO 2955 case sensitive (original)", "ISO 2955 case sensitive (fixed)", "ISO 2955 case insensitive", "ANSI X 3.50 case sensitive (original)", "ANSI X 3.50 case sensitive (fixed)", "ANSI X 3.50 case insensitive (original)", "ANSI X 3.50 case insensitive (fixed)", "HL7 ISO+", "HL7 ISO+ANSI+", }; static String file_names[] = { "ucum-cs", "ucum-ci", "SI", "ISO1000", "ISO2955-cs", "ISO2955-cs-fixed", "ISO2955-ci", "ANSI-X3.50-cs", "ANSI-X3.50-cs-fixed", "ANSI-X3.50-ci", "ANSI-X3.50-ci-fixed", "HL7-ISO+", "HL7-ISO+ANSI+", }; int current_table = 0; void read() throws IOException { String url = getCodeBase() + file_names[current_table] + ".units"; UnitTab.read(url); status.setText("read: `" + url + "'"); } void change_table() throws IOException { int new_table = dbchoice.getSelectedIndex(); // System.out.println("change table " + current_table + " -> " + new_table); if(new_table != current_table) { current_table = new_table; read(); } } public void init() { numForm.setMaximumFractionDigits(4); from_value = new Input(this, "From Value:", 8, true); from_unit = new Input(this, "Unit:", 8, true); to_unit = new Input(this, "To: Unit", 8, true); to_value = new Input(this, "Value:", 8, false); Button okbutton = new Button("OK"); // JDK-1.1 okbutton.addActionListener(new ConvertListener(this)); add(okbutton); status = new TextField(); status.setEditable(false); status.setFont(new Font("helvetica", Font.PLAIN, 12)); add(status); status.setText("Copyright (c) 1998 Regenstrief Institute. NO WARRANTY!"); dbchoice = new Choice(); for(int i = 0; i < table_names.length; i++) dbchoice.addItem(table_names[i]); add(dbchoice); dbchoice.select(current_table); try { read(); } catch(IOException x) { status.setText("error :" + x.getMessage()); } } public void start() { } public void stop() { } public void destroy() { } public boolean action(Event evt, Object arg) { if("OK".equals(arg)) { doit(); return true; } return false; } void doit() { try { // change table? change_table(); // read a value double mu1 = Double.valueOf(from_value.getText()).doubleValue(); // read from unit Unit u1 = new Unit(from_unit.getText()); // u1.dump(); // read to unit Unit u2 = new Unit(to_unit.getText()); // u2.dump(); // convert double mu2 = u1.convertTo(mu1, u2); status.setText("" + numForm.format(mu1) + " " + u1 + " = " + numForm.format(mu2) + " " + u2); to_value.setText("" + numForm.format(mu2)); } catch(Exception x) { if(! (x instanceof java.lang.NoSuchMethodException)) status.setText("" + x.getClass().getName() + ": " + x.getMessage()); } } } // A TextField in a Panel with a Label class Input { Label label = null; TextField text = null; public Input(Container w, String s, int width, boolean editable) { label = new Label(s,Label.RIGHT); Font font = new Font("helvetica", Font.PLAIN, 10); label.setFont(font); text = new TextField("",width); text.setEditable(editable); w.add(label); w.add(text); } public String getText() { return text.getText(); } public void setText(String s) { text.setText(s); /** don't want to reposition to the beginning */ // text.setCaretPosition(0); } } /* This is JDK-1.1 stuff that won't work in most current browsers class ConvertListener implements ActionListener { UnitsApplet app; public ConvertListener(UnitsApplet a) { app = a; } public void actionPerformed(ActionEvent e) { app.doit(); } } */