home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / ODesign / SetupPSE.exe / data.z / AppletIOSupport.java < prev    next >
Encoding:
Java Source  |  1997-05-08  |  10.4 KB  |  394 lines

  1. package COM.odi.demo.OSDraw;
  2.  
  3. /**
  4.  *      <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
  5.  */
  6.  
  7. import java.awt.*;
  8. import java.util.Vector;
  9.  
  10. /* 
  11.  * The class AppletIOSupport handles various dialog boxes. 
  12.  * Dialog boxes are: single text line message boxes,
  13.  * multiple text line boxes, input boxes, and list boxes.
  14.  * First call the method registerFontMetrics(FontMetrics metrics)
  15.  * Each box must be initialized by methods:
  16.  *   initializeMessageBox, initializeInputBox, initializeListBox
  17.  */
  18.  
  19. public class AppletIOSupport {
  20.  
  21.   // Record FontMetrics in order to determine physical dimensions of a box
  22.  
  23.   private FontMetrics metrics;
  24.  
  25.   private Dialog simpleBox;
  26.   private Label simpleLabel;
  27.  
  28.   private Dialog messageBox;
  29.   private Vector messageLabels;
  30.   private int messageWidth;
  31.   private int messageHeight;
  32.  
  33.   private DialogBox inputBox;
  34.   private TextField inputField;
  35.   private Label inputLabel;
  36.   private int inputCols;
  37.  
  38.   private DialogBox listBox;
  39.   private Label listLabel;
  40.   private List list;
  41.   private int listWidth;
  42.  
  43.   public AppletIOSupport() {
  44.     metrics = null;
  45.  
  46.     messageBox = null;
  47.     inputBox = null;
  48.     listBox = null;
  49.   }
  50.  
  51.   // MUST: Call registerFontMetrics method before using AppletIOSuport dialogs
  52.  
  53.   public void registerFontMetrics(FontMetrics metrics) {
  54.     this.metrics = metrics;
  55.   }
  56.  
  57.   // Return the FontMetrics instance
  58.  
  59.   public FontMetrics getFontMetrics() {
  60.     return metrics;
  61.   }
  62.  
  63.   // Method to initialize a simple message box with one line of text
  64.   // Point (x,y) is the top left point of the box relative to the parent 
  65.   // i.e. (0,0) is at the top left of the parent window
  66.  
  67.   public void initializeMessageBox(Frame parent, int x, int y) {
  68.     simpleBox = new DialogBox(parent, true, x, y);
  69.     GridBagLayout gridbag = new GridBagLayout();
  70.     GridBagConstraints constraints = new GridBagConstraints();
  71.     simpleBox.setLayout(gridbag);
  72.     constraints.weightx = 1.0;
  73.     constraints.weighty = 1.0;
  74.     constraints.fill = GridBagConstraints.NONE;
  75.     constraints.gridwidth = constraints.REMAINDER;
  76.     simpleLabel = new Label("", Label.CENTER);
  77.     gridbag.setConstraints(simpleLabel, constraints);
  78.     simpleBox.add(simpleLabel);
  79.     Button ok = new Button("OK");
  80.     gridbag.setConstraints(ok, constraints);
  81.     simpleBox.add(ok);
  82.     simpleBox.setSize(200, 115);
  83.     simpleBox.setLocation(x, y);
  84.   }
  85.  
  86.   // Method to initialize a message box with multiple lines of text
  87.   // multipleLines must be true
  88.   // Point (x,y) is the top left point of the box relative to the parent
  89.   // i.e. (0,0) is at the top left of the parent window
  90.  
  91.   public void initializeMessageBox(Frame parent, int x, int y,
  92.                    boolean multipleLines) {
  93.     if (!multipleLines) {
  94.       initializeMessageBox(parent, x, y);
  95.       return;
  96.     }
  97.     messageWidth = 100;
  98.     messageHeight = 100;
  99.     messageBox = new DialogBox(parent, true, x, y);
  100.     messageBox.setSize(200, 115);
  101.     messageBox.setLocation(x, y);
  102.     messageLabels = new Vector();
  103.   }
  104.  
  105.   // Reset lines of text in the message box
  106.  
  107.   public void resetMessageBox() {
  108.     messageWidth = 100;
  109.     messageHeight = 100;
  110.     messageLabels = new Vector();
  111.     messageBox.removeAll();    // Remove all components from this container
  112.   }
  113.  
  114.   // Add a line of text to the message box
  115.   // variable alignment is Label.RIGHT, Label.CENTER, Label.LEFT
  116.  
  117.   public void addMessageBoxLabel(String message, int alignment) {
  118.     Label messageLabel = new Label(message, alignment);
  119.     messageLabels.addElement(messageLabel);
  120.     if (metrics.stringWidth(message) > messageWidth) {
  121.       messageWidth = metrics.stringWidth(message);
  122.     }
  123.     messageHeight += metrics.getHeight() + 10;
  124.   }
  125.  
  126.   // Display the single line message box with the message
  127.  
  128.   public void showMessageBox(String message) {
  129.     if (simpleBox == null) {
  130.       return;
  131.     }
  132.     simpleLabel.setText(message);
  133.     simpleBox.setSize(metrics.stringWidth(message) + 40,
  134.              metrics.getHeight() + 100);
  135.     simpleBox.setVisible(true);
  136.   }
  137.  
  138.   // Display the multiple line message box with its saved lines of text
  139.  
  140.   public void showMessageBox() {
  141.     if (messageBox == null) {
  142.       return;
  143.     }
  144.     GridBagLayout gridbag = new GridBagLayout();
  145.     messageBox.setLayout(gridbag);
  146.     GridBagConstraints constraints = new GridBagConstraints();
  147.     constraints.weightx = 1.0;
  148.     constraints.weighty = 1.0;
  149.     constraints.fill = GridBagConstraints.NONE;
  150.     constraints.gridwidth = GridBagConstraints.REMAINDER;
  151.     for (int i = 0; i < messageLabels.size(); i++) {
  152.       Label messageLabel = (Label)messageLabels.elementAt(i);
  153.       gridbag.setConstraints(messageLabel, constraints);
  154.       messageBox.add(messageLabel);
  155.     }
  156.     Button ok = new Button("OK");
  157.     gridbag.setConstraints(ok, constraints);
  158.     messageBox.add(ok);
  159.     messageBox.setSize(messageWidth + 40, messageHeight);
  160.     messageBox.setVisible(true);
  161.   }
  162.  
  163.   // Hide both message boxes
  164.  
  165.   public void hideMessageBox() {
  166.     if (simpleBox != null) {
  167.       simpleBox.setVisible(false);
  168.     }
  169.     if (messageBox != null) {
  170.       messageBox.setVisible(false);
  171.     }
  172.   }
  173.  
  174.   // Method for initializing the input box
  175.   // Point (x,y) is the top left of the box relative to the parent
  176.   // i.e. (0,0) is the top left of the parent
  177.   // inputCols is the parameter to the java.awt.TextField constructor
  178.  
  179.   public void initializeInputBox(Frame parent, int inputCols,
  180.                  int x, int y) {
  181.     inputBox = new DialogBox(parent, true, x, y);
  182.     inputBox.setLayout(new GridLayout(2, 1, 30, 30));
  183.     this.inputCols = inputCols;
  184.     inputField = new TextField(inputCols);
  185.     inputLabel = new Label("");
  186.     inputBox.add(inputLabel);
  187.     inputBox.add(inputField);
  188.     inputBox.add(new Button("OK"));
  189.     inputBox.add(new Button("Cancel"));
  190.     inputBox.setSize(150, 115);
  191.     inputBox.setLocation(x, y);
  192.   }
  193.  
  194.   // Display the input box with a message
  195.  
  196.   public void showInputBox(String label) {
  197.     if (inputBox == null) {
  198.       return;
  199.     }
  200.     inputLabel.setText(label);
  201.     int inputPixels = inputCols * metrics.stringWidth("M");
  202.     inputBox.setSize(50 + metrics.stringWidth(label) + inputPixels, 115);
  203.     inputBox.setVisible(true);
  204.   }
  205.  
  206.   // Retrieve the current input of the input box
  207.  
  208.   public String getInput() {
  209.     if (inputBox != null) {
  210.       return inputField.getText();
  211.     }
  212.     return null;
  213.   }
  214.  
  215.   // Set the current input of the input box
  216.  
  217.   public void setInput(String text) {
  218.     if (inputBox != null) {
  219.       inputField.setText(text);
  220.     }
  221.   }
  222.  
  223.   // Hide the input box
  224.  
  225.   public void hideInputBox() {
  226.     if (inputBox != null) {
  227.       inputBox.setVisible(false);
  228.     }
  229.   }
  230.  
  231.   // Method for initializing the list box
  232.   // Point (x,y) is the top left of the box relative to the parent
  233.   // i.e. (0,0) is at the top left of the parent
  234.   // multipleSelections indicates if the user can select multiple items
  235.  
  236.   public void initializeListBox(Frame parent, int x, int y,
  237.                 boolean multipleSelections) {
  238.     listBox = new DialogBox(parent, true, x, y);
  239.     GridBagLayout gridbag = new GridBagLayout();
  240.     GridBagConstraints constraints = new GridBagConstraints();
  241.     constraints.fill = GridBagConstraints.HORIZONTAL;
  242.     constraints.gridwidth = GridBagConstraints.REMAINDER;
  243.     constraints.weightx = 1.0;
  244.     constraints.weighty = 1.0;
  245.  
  246.     listBox.setLayout(gridbag);
  247.  
  248.     listLabel = new Label("", Label.CENTER);
  249.     list = new List(10, multipleSelections);
  250.     Button ok = new Button("OK");
  251.     Button cancel = new Button("Cancel");
  252.  
  253.     gridbag.setConstraints(listLabel, constraints);
  254.     listBox.add(listLabel);
  255.     gridbag.setConstraints(list, constraints);
  256.     listBox.add(list);
  257.     constraints.fill = GridBagConstraints.NONE;
  258.     constraints.gridwidth = 1;
  259.     gridbag.setConstraints(ok, constraints);
  260.     listBox.add(ok);
  261.     gridbag.setConstraints(cancel, constraints);
  262.     listBox.add(cancel);
  263.  
  264.     listBox.setLocation(x, y);
  265.   }
  266.  
  267.   // Add string addend to the list box's selection strings
  268.  
  269.   public void addListString(String addend) {
  270.     if (list != null) {
  271.       if (metrics.stringWidth(addend) > listWidth) {
  272.     listWidth = metrics.stringWidth(addend);
  273.       }
  274.       list.addItem(addend);
  275.     }
  276.   }
  277.  
  278.   // Clear the list of its items
  279.  
  280.   public void clearList() {
  281.     if (list != null) {
  282.       list.removeAll();
  283.       listWidth = 100;
  284.     }
  285.   }
  286.  
  287.   // Display the list box with a message label
  288.  
  289.   public void showListBox(String label) {
  290.     if (listBox == null) {
  291.       return;
  292.     }
  293.     if (metrics.stringWidth(label) > listWidth) {
  294.       listWidth = metrics.stringWidth(label);
  295.     }
  296.     list.select(0);
  297.     listLabel.setText(label);
  298.     listBox.setSize(100 + listWidth, 350);
  299.     listBox.setVisible(true);
  300.   }
  301.  
  302.   // Get the current selected index
  303.  
  304.   public int getSelectedIndex() {
  305.     if (list == null) {
  306.       return 0;
  307.     }
  308.     return list.getSelectedIndex();
  309.   }
  310.  
  311.   // Get the string at the current selected index
  312.  
  313.   public String getItem(int index) {
  314.     return list.getItem(index);
  315.   }
  316.  
  317.   // Get an array of selected indices for a multiple line list box
  318.  
  319.   public int[] getSelectedIndexes() {
  320.     if (list == null) {
  321.       return null;
  322.     }
  323.     return list.getSelectedIndexes();
  324.   }
  325.  
  326.   // Hide the list box
  327.  
  328.   public void hideListBox() {
  329.     if (listBox != null) {
  330.       listBox.setVisible(false);
  331.     }
  332.   }
  333.  
  334.   // Hide all dialog boxes
  335.  
  336.   public void hideAll() {
  337.     if (simpleBox != null) {
  338.       simpleBox.setVisible(false);
  339.     }
  340.     if (messageBox != null) {
  341.       messageBox.setVisible(false);
  342.     }
  343.     if (inputBox != null) {
  344.       inputBox.setVisible(false);
  345.     }
  346.     if (listBox != null) {
  347.       listBox .setVisible(false);
  348.     }
  349.   }
  350. }
  351.  
  352. /* 
  353.  * Class for handling change in Java, 1.0.2 which traps dialog events in
  354.  * the dialog box. This subclass of java.awt.Dialog sends a button event
  355.  * to its parent.
  356.  */
  357.  
  358. class DialogBox extends Dialog {
  359.  
  360.   // Save parent in order to call parent's action method later
  361.   private Frame parent;
  362.  
  363.   private int x;
  364.   private int y;
  365.  
  366.   // Create a dialog box
  367.   // Point (x,y) is the top left of the box relative to the parent.
  368.   // i.e. (0,0) is the top left of the parent window.
  369.  
  370.   public DialogBox(Frame parent, boolean isModal, int x, int y) {
  371.     super(parent, isModal);
  372.     this.parent = parent;
  373.     this.x = x;
  374.     this.y = y;
  375.   }
  376.  
  377.   // Override the java.awt.Component.show method to move the box
  378.  
  379.   public void setVisible(boolean v) {
  380.     super.setVisible(v);
  381.     Point topLeft = parent.location();
  382.     setLocation(topLeft.x + this.x, topLeft.y + this.y);
  383.   }
  384.  
  385.   // action method sends all button events to the parent
  386.  
  387.   public boolean action(Event event, Object obj) {
  388.     if (event.target instanceof Button) {
  389.       return parent.action(event, obj);
  390.     }
  391.     return super.action(event, obj);
  392.   }
  393. }
  394.