// QuickConvert.java // by Gregory C. Wilcox // August 22, 1997 /* bugs: fix layout implement molecular weight input make tweakFont into a class add online help */ import java.awt.*; // import java.applet.*; import java.awt.event.*; import java.applet.Applet; import java.applet.AppletContext; // QuickConvert classes // import UnitData; import ChoicePanel; // // import HelpFrame; // import HelpDialog; // QuickConvert interfaces // import DialogClient; // can't extend Choice, // since pItems and selectedIndex are not visible by method 'clear' // so, use ChoicePanel instead // and wait for 'remove' method in JDK 1.1 /* class ClearChoice extends Choice { public synchronized void clear() { // pItems = new Vector(); // selectedIndex = -1; } } */ /** * DialogClients are notified when the Dialog with which they * are associated is dismissed. A reference to the dismissed * Dialog is passed as a parameter of dialogDismissed() in case * a DialogClient is a client of more than one Dialog.

* * @version 1.0, Apr 1 1996 * @author David Geary * @see MessageDialog * @see QuestionDialog * @see YesNoDialog * @see gjt.test.DialogTest */ interface DialogClient { abstract public void dialogDismissed(Dialog d); } // header panel containing information about QuickConvert class Header extends Panel { Label title = new Label("QuickConvert", Label.CENTER); Label version = new Label("Version 2.0", Label.CENTER); Label copyright = new Label("Copyright 2000 by Arthur D. Little, Inc.", Label.CENTER); int titleX = 0, titleY = 0; // 'wide' could be 192, but jdk1.0.3 can't shrink fonts - so copyright is big int wide = 256; public Header(Graphics g) { Font currentFont = g.getFont(); Font titleFont = tweakFont(currentFont, Font.BOLD, 8); title.setFont(titleFont); Font copyrightFont = tweakFont(currentFont, Font.ITALIC, -2); copyright.setFont(copyrightFont); setSize( wide, 64 ); // lay the header out by hand for a custom look setLayout(null); add(title); add(version); add(copyright); title.setBounds(titleX, titleY, wide, 24); version.setBounds(titleX, titleY + 20, wide, 16); copyright.setBounds(titleX, titleY + 40, wide, 16); } // increment the font size, and optionally set the style Font tweakFont(Font font, int style, int sizeInc) { String fontName = font.getName(); int fontStyle = style >= 0 ? style : font.getStyle(); int fontSize = font.getSize(); return new Font(fontName, fontStyle, fontSize + sizeInc); } } class OKDialog extends Dialog implements ActionListener { Panel p1 = new Panel(); Panel p2 = new Panel(); Button OKButton = new Button("OK"); public OKDialog(Frame parent, String title) { super(parent, title, true); p2.add(OKButton); OKButton.addActionListener(this); add("South", p2); } public void actionPerformed(ActionEvent event) { if (event.getSource() == OKButton) dispose(); } public void windowClosing(WindowEvent event) { dispose(); } } class AboutDialog extends OKDialog { public AboutDialog(Frame parent) { super(parent, "About QuickConvert"); p1.add(new Label("QuickConvert")); p1.add(new Label("Copyright 2000 by Arthur D. Little, Inc.")); p1.add(new Label("Acorn Park, Cambridge, MA 02140")); add("Center", p1); setSize(288, 192); } } class MessageDialog extends OKDialog { public MessageDialog(Frame parent, String title, String message) { super(parent, title); p1.add(new Label(message)); add("Center", p1); setSize(288, 128); } public static void error(Frame parent, String string) { MessageDialog md = new MessageDialog(parent, "Error: ", string); md.setVisible(true); } public static void caution(Frame parent, String string) { // message("Caution: ", string); MessageDialog md = new MessageDialog(parent, "Caution: ", string); md.setVisible(true); } } // get an integer from user class GetIntegerDialog extends OKDialog implements ActionListener { int inputValue = 1; TextField inputField = new TextField("", 8); protected DialogClient client; // constructor with input value public GetIntegerDialog(Frame parent, DialogClient client, String label, int inputValue) { this(parent, client, label); inputField.setText(Integer.toString(inputValue)); } // standard constructor public GetIntegerDialog(Frame parent, DialogClient client, String label) { super(parent, "Enter value"); this.client = client; p1.add(new Label(label)); p1.add(inputField); add("Center", p1); setSize(256, 128); OKButton.addActionListener(this); } public void setVisible(boolean visible) { inputField.requestFocus(); super.setVisible(visible); } public void actionPerformed(ActionEvent event) { if (event.getSource() == OKButton) { String inputString = inputField.getText(); try { inputValue = Integer.parseInt(inputString); } catch (NumberFormatException e) { MessageDialog.error((Frame)this.getParent().getParent(), inputString + " is not a number."); System.err.println('\7' + inputString + " is not an integer."); // this only works in the context of an applet // showStatus(inputString + " is not a number."); } dispose(); client.dialogDismissed(this); } } } public class QuickConvert extends Applet implements DialogClient, ActionListener, ItemListener { int i, digits = 16, molWeight = 0; int kindValue = 0; Double inputValue, outputValue; String inputString = "1"; String outputString = inputString; String javaVersion = System.getProperty("java.version"); GetIntegerDialog molWeightDialog; Frame QCFrame; Header header; Label kindLabel = new Label("Kind:"); TextField input = new TextField(inputString, digits); TextField output = new TextField(outputString, digits); Label baseLabel = new Label("SI base units: "); Label baseValue = new Label("Java version: " + javaVersion); Label fromLabel = new Label("From:"); Label toLabel = new Label("To:"); Label blankLabel = new Label(); Choice kind = new Choice(); // ClearChoice fromUnit = new ClearChoice(); // ClearChoice toUnit = new ClearChoice(); // ChoicePanel fromUnit = new ChoicePanel(256 + 128, 32); // ChoicePanel toUnit = new ChoicePanel(256 + 128, 32); Choice fromUnit = new Choice(); Choice toUnit = new Choice(); Panel checkPanel = new Panel(); Checkbox abbrev = new Checkbox("Abbreviate"); Checkbox round = new Checkbox("Round"); Panel buttonPanel = new Panel(); Button convertButton = new Button("Convert"); Button flipButton = new Button("Flip"); Button aboutButton = new Button("About"); Button helpButton = new Button("Help"); Button molWeightButton = new Button("Mol. Weight"); // use beep (ASCII code 7) to signal message void message(String label, String string) { System.err.println('\7' + label + string); } // not used public static Frame getFrame(Component component) { Component c = component; if (c instanceof Frame) return (Frame)c; while((c = c.getParent()) != null) if (c instanceof Frame) return (Frame)c; return null; } void getMolWeight() { molWeightDialog = new GetIntegerDialog(QCFrame, this, "Molecular weight:", molWeight); molWeightDialog.setVisible(true); } public void dialogDismissed(Dialog d) { if (d == molWeightDialog) { molWeight = molWeightDialog.inputValue; // do conversion and show result showOutput(); } } // count the number of significant digits in this number int signifDigits (String string) { int digits = 0; char c; for (int i=0; i