home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-05-08 | 10.4 KB | 394 lines |
- package COM.odi.demo.OSDraw;
-
- /**
- * <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
- */
-
- import java.awt.*;
- import java.util.Vector;
-
- /*
- * The class AppletIOSupport handles various dialog boxes.
- * Dialog boxes are: single text line message boxes,
- * multiple text line boxes, input boxes, and list boxes.
- * First call the method registerFontMetrics(FontMetrics metrics)
- * Each box must be initialized by methods:
- * initializeMessageBox, initializeInputBox, initializeListBox
- */
-
- public class AppletIOSupport {
-
- // Record FontMetrics in order to determine physical dimensions of a box
-
- private FontMetrics metrics;
-
- private Dialog simpleBox;
- private Label simpleLabel;
-
- private Dialog messageBox;
- private Vector messageLabels;
- private int messageWidth;
- private int messageHeight;
-
- private DialogBox inputBox;
- private TextField inputField;
- private Label inputLabel;
- private int inputCols;
-
- private DialogBox listBox;
- private Label listLabel;
- private List list;
- private int listWidth;
-
- public AppletIOSupport() {
- metrics = null;
-
- messageBox = null;
- inputBox = null;
- listBox = null;
- }
-
- // MUST: Call registerFontMetrics method before using AppletIOSuport dialogs
-
- public void registerFontMetrics(FontMetrics metrics) {
- this.metrics = metrics;
- }
-
- // Return the FontMetrics instance
-
- public FontMetrics getFontMetrics() {
- return metrics;
- }
-
- // Method to initialize a simple message box with one line of text
- // Point (x,y) is the top left point of the box relative to the parent
- // i.e. (0,0) is at the top left of the parent window
-
- public void initializeMessageBox(Frame parent, int x, int y) {
- simpleBox = new DialogBox(parent, true, x, y);
- GridBagLayout gridbag = new GridBagLayout();
- GridBagConstraints constraints = new GridBagConstraints();
- simpleBox.setLayout(gridbag);
- constraints.weightx = 1.0;
- constraints.weighty = 1.0;
- constraints.fill = GridBagConstraints.NONE;
- constraints.gridwidth = constraints.REMAINDER;
- simpleLabel = new Label("", Label.CENTER);
- gridbag.setConstraints(simpleLabel, constraints);
- simpleBox.add(simpleLabel);
- Button ok = new Button("OK");
- gridbag.setConstraints(ok, constraints);
- simpleBox.add(ok);
- simpleBox.setSize(200, 115);
- simpleBox.setLocation(x, y);
- }
-
- // Method to initialize a message box with multiple lines of text
- // multipleLines must be true
- // Point (x,y) is the top left point of the box relative to the parent
- // i.e. (0,0) is at the top left of the parent window
-
- public void initializeMessageBox(Frame parent, int x, int y,
- boolean multipleLines) {
- if (!multipleLines) {
- initializeMessageBox(parent, x, y);
- return;
- }
- messageWidth = 100;
- messageHeight = 100;
- messageBox = new DialogBox(parent, true, x, y);
- messageBox.setSize(200, 115);
- messageBox.setLocation(x, y);
- messageLabels = new Vector();
- }
-
- // Reset lines of text in the message box
-
- public void resetMessageBox() {
- messageWidth = 100;
- messageHeight = 100;
- messageLabels = new Vector();
- messageBox.removeAll(); // Remove all components from this container
- }
-
- // Add a line of text to the message box
- // variable alignment is Label.RIGHT, Label.CENTER, Label.LEFT
-
- public void addMessageBoxLabel(String message, int alignment) {
- Label messageLabel = new Label(message, alignment);
- messageLabels.addElement(messageLabel);
- if (metrics.stringWidth(message) > messageWidth) {
- messageWidth = metrics.stringWidth(message);
- }
- messageHeight += metrics.getHeight() + 10;
- }
-
- // Display the single line message box with the message
-
- public void showMessageBox(String message) {
- if (simpleBox == null) {
- return;
- }
- simpleLabel.setText(message);
- simpleBox.setSize(metrics.stringWidth(message) + 40,
- metrics.getHeight() + 100);
- simpleBox.setVisible(true);
- }
-
- // Display the multiple line message box with its saved lines of text
-
- public void showMessageBox() {
- if (messageBox == null) {
- return;
- }
- GridBagLayout gridbag = new GridBagLayout();
- messageBox.setLayout(gridbag);
- GridBagConstraints constraints = new GridBagConstraints();
- constraints.weightx = 1.0;
- constraints.weighty = 1.0;
- constraints.fill = GridBagConstraints.NONE;
- constraints.gridwidth = GridBagConstraints.REMAINDER;
- for (int i = 0; i < messageLabels.size(); i++) {
- Label messageLabel = (Label)messageLabels.elementAt(i);
- gridbag.setConstraints(messageLabel, constraints);
- messageBox.add(messageLabel);
- }
- Button ok = new Button("OK");
- gridbag.setConstraints(ok, constraints);
- messageBox.add(ok);
- messageBox.setSize(messageWidth + 40, messageHeight);
- messageBox.setVisible(true);
- }
-
- // Hide both message boxes
-
- public void hideMessageBox() {
- if (simpleBox != null) {
- simpleBox.setVisible(false);
- }
- if (messageBox != null) {
- messageBox.setVisible(false);
- }
- }
-
- // Method for initializing the input box
- // Point (x,y) is the top left of the box relative to the parent
- // i.e. (0,0) is the top left of the parent
- // inputCols is the parameter to the java.awt.TextField constructor
-
- public void initializeInputBox(Frame parent, int inputCols,
- int x, int y) {
- inputBox = new DialogBox(parent, true, x, y);
- inputBox.setLayout(new GridLayout(2, 1, 30, 30));
- this.inputCols = inputCols;
- inputField = new TextField(inputCols);
- inputLabel = new Label("");
- inputBox.add(inputLabel);
- inputBox.add(inputField);
- inputBox.add(new Button("OK"));
- inputBox.add(new Button("Cancel"));
- inputBox.setSize(150, 115);
- inputBox.setLocation(x, y);
- }
-
- // Display the input box with a message
-
- public void showInputBox(String label) {
- if (inputBox == null) {
- return;
- }
- inputLabel.setText(label);
- int inputPixels = inputCols * metrics.stringWidth("M");
- inputBox.setSize(50 + metrics.stringWidth(label) + inputPixels, 115);
- inputBox.setVisible(true);
- }
-
- // Retrieve the current input of the input box
-
- public String getInput() {
- if (inputBox != null) {
- return inputField.getText();
- }
- return null;
- }
-
- // Set the current input of the input box
-
- public void setInput(String text) {
- if (inputBox != null) {
- inputField.setText(text);
- }
- }
-
- // Hide the input box
-
- public void hideInputBox() {
- if (inputBox != null) {
- inputBox.setVisible(false);
- }
- }
-
- // Method for initializing the list box
- // Point (x,y) is the top left of the box relative to the parent
- // i.e. (0,0) is at the top left of the parent
- // multipleSelections indicates if the user can select multiple items
-
- public void initializeListBox(Frame parent, int x, int y,
- boolean multipleSelections) {
- listBox = new DialogBox(parent, true, x, y);
- GridBagLayout gridbag = new GridBagLayout();
- GridBagConstraints constraints = new GridBagConstraints();
- constraints.fill = GridBagConstraints.HORIZONTAL;
- constraints.gridwidth = GridBagConstraints.REMAINDER;
- constraints.weightx = 1.0;
- constraints.weighty = 1.0;
-
- listBox.setLayout(gridbag);
-
- listLabel = new Label("", Label.CENTER);
- list = new List(10, multipleSelections);
- Button ok = new Button("OK");
- Button cancel = new Button("Cancel");
-
- gridbag.setConstraints(listLabel, constraints);
- listBox.add(listLabel);
- gridbag.setConstraints(list, constraints);
- listBox.add(list);
- constraints.fill = GridBagConstraints.NONE;
- constraints.gridwidth = 1;
- gridbag.setConstraints(ok, constraints);
- listBox.add(ok);
- gridbag.setConstraints(cancel, constraints);
- listBox.add(cancel);
-
- listBox.setLocation(x, y);
- }
-
- // Add string addend to the list box's selection strings
-
- public void addListString(String addend) {
- if (list != null) {
- if (metrics.stringWidth(addend) > listWidth) {
- listWidth = metrics.stringWidth(addend);
- }
- list.addItem(addend);
- }
- }
-
- // Clear the list of its items
-
- public void clearList() {
- if (list != null) {
- list.removeAll();
- listWidth = 100;
- }
- }
-
- // Display the list box with a message label
-
- public void showListBox(String label) {
- if (listBox == null) {
- return;
- }
- if (metrics.stringWidth(label) > listWidth) {
- listWidth = metrics.stringWidth(label);
- }
- list.select(0);
- listLabel.setText(label);
- listBox.setSize(100 + listWidth, 350);
- listBox.setVisible(true);
- }
-
- // Get the current selected index
-
- public int getSelectedIndex() {
- if (list == null) {
- return 0;
- }
- return list.getSelectedIndex();
- }
-
- // Get the string at the current selected index
-
- public String getItem(int index) {
- return list.getItem(index);
- }
-
- // Get an array of selected indices for a multiple line list box
-
- public int[] getSelectedIndexes() {
- if (list == null) {
- return null;
- }
- return list.getSelectedIndexes();
- }
-
- // Hide the list box
-
- public void hideListBox() {
- if (listBox != null) {
- listBox.setVisible(false);
- }
- }
-
- // Hide all dialog boxes
-
- public void hideAll() {
- if (simpleBox != null) {
- simpleBox.setVisible(false);
- }
- if (messageBox != null) {
- messageBox.setVisible(false);
- }
- if (inputBox != null) {
- inputBox.setVisible(false);
- }
- if (listBox != null) {
- listBox .setVisible(false);
- }
- }
- }
-
- /*
- * Class for handling change in Java, 1.0.2 which traps dialog events in
- * the dialog box. This subclass of java.awt.Dialog sends a button event
- * to its parent.
- */
-
- class DialogBox extends Dialog {
-
- // Save parent in order to call parent's action method later
- private Frame parent;
-
- private int x;
- private int y;
-
- // Create a dialog box
- // Point (x,y) is the top left of the box relative to the parent.
- // i.e. (0,0) is the top left of the parent window.
-
- public DialogBox(Frame parent, boolean isModal, int x, int y) {
- super(parent, isModal);
- this.parent = parent;
- this.x = x;
- this.y = y;
- }
-
- // Override the java.awt.Component.show method to move the box
-
- public void setVisible(boolean v) {
- super.setVisible(v);
- Point topLeft = parent.location();
- setLocation(topLeft.x + this.x, topLeft.y + this.y);
- }
-
- // action method sends all button events to the parent
-
- public boolean action(Event event, Object obj) {
- if (event.target instanceof Button) {
- return parent.action(event, obj);
- }
- return super.action(event, obj);
- }
- }
-