home *** CD-ROM | disk | FTP | other *** search
/ PC World 1998 October / PCWorld_1998-10_cd.bin / software / prehled / inprise / JSAMPLES.Z / InfoDlg.java < prev    next >
Text File  |  1998-05-08  |  12KB  |  321 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. package borland.samples.apps.chess.client  ;
  21.  
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. import java.util.*;
  25. import borland.jbcl.layout.*;
  26. import borland.jbcl.control.*;
  27.  
  28. /**Displays a Dialog where the user can supply personal information as desired
  29. */
  30. public class InfoDlg extends Dialog  implements OkCancelDialog ,PlayerListListener
  31. {
  32.   ResourceBundle res = ResourceBundle.getBundle("borland.samples.apps.chess.client.Res");
  33.   Panel dlgPanel = new Panel();
  34.   Button cancelButton = new Button();
  35.   Button okButton = new Button();
  36.   String[] data;
  37.   String userValue;
  38.   String ratingValue;
  39.   String nameValue;
  40.   String emailValue;
  41.   String locationValue;
  42.   String otherValue;
  43.   TextField nameText = new TextField();
  44.   TextField otherText = new TextField();
  45.   TextField emailText = new TextField();
  46.   TextField locationText = new TextField();
  47.   TextField noteText = new TextField();
  48.   Checkbox cb = new Checkbox();
  49.   List list = new List();
  50.   Button sendButton = new Button();
  51.   Label userText = new Label();
  52.   Label ratingText  = new Label();
  53.   Vector listVector = new Vector();
  54.   Panel buttonPanel = new Panel();
  55.   Panel client = new Panel();
  56.   Label eastSpace = new Label();
  57.   Label westSpace = new Label();
  58.   Label nameLabel = new Label();
  59.   Label emailLabel = new Label();
  60.   Label locationLabel = new Label();
  61.   Label otherLabel = new Label();
  62.   BorderLayout dialogLayout = new BorderLayout();
  63.   GridBagLayout clientGridBagLayout = new GridBagLayout();
  64.   String lastRecipient = "";
  65.   Panel panel1 = new Panel();
  66.   GridLayout gridLayout1 = new GridLayout();
  67.   FlowLayout flowLayout1 = new FlowLayout();
  68.   ChessViewer dlgParent;
  69.  
  70.   /**Constructor
  71.   *@param ChessViewer theParent gives access to the frame
  72.   *@param String[] data array of the fields sent from the server in the Information msg
  73.   */
  74.   public InfoDlg(ChessViewer theParent,String[] data)  {
  75.     super(theParent.f,"",false);
  76.     setTitle(res.getString("YOUR_PUBLIC"));
  77.     this.data = data;
  78.     dlgParent = theParent;
  79.     if (data.length < 9) {
  80.       theParent.statusLine.setText(res.getString("EXPECTED_EIGHT_PIECES") + data.length);
  81.     }
  82.     else {
  83.       try {
  84.         enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  85.         dlgParent.statusLine.setText(res.getString("CHANGE_AS_DESIRED"));
  86.         dlgParent.sendMsg("LogonList","");
  87.         dlgParent.setListListener(this);
  88.         userValue = data[1];
  89.         ratingValue = res.getString("RATING") + data[7] + "    " + data[8] + res.getString("GAMES_PLAYED");
  90.         nameValue = data[3];
  91.         emailValue = data[4];
  92.         locationValue = data[5];
  93.         otherValue = data[6];
  94.         jbInit();
  95.         add(dlgPanel);
  96.         dlgParent.setModal(true);
  97.       }
  98.       catch (Exception e) {
  99.         e.printStackTrace();
  100.       }
  101.     }
  102.   }
  103.  
  104.   private void jbInit() throws Exception {
  105.     Font boldFont = new Font(getFont().getName(),Font.BOLD,getFont().getSize());
  106.     userText.setFont(boldFont);
  107.     dlgPanel.setLayout(dialogLayout);
  108.     dlgPanel.add(eastSpace,BorderLayout.EAST);
  109.     dlgPanel.add(westSpace,BorderLayout.WEST);
  110.     dlgPanel.add(client,BorderLayout.NORTH);
  111.     dlgPanel.add(list,BorderLayout.CENTER);
  112.     dlgPanel.add(buttonPanel,BorderLayout.SOUTH);
  113.     buttonPanel.add(cb);
  114.     buttonPanel.add(panel1, null);
  115.     panel1.add(okButton);
  116.     panel1.add(cancelButton);
  117.     list.addItemListener(new ListListener(this));
  118.     client.setLayout(clientGridBagLayout);
  119.     userText.setText(userValue);
  120.     ratingText.setText(ratingValue);
  121.     buttonPanel.setLayout(flowLayout1);
  122.     nameLabel.setText(res.getString("NAME"));
  123.     nameText.setText(nameValue);
  124.     emailLabel.setText(res.getString("EMAIL_ADDRESS"));
  125.     emailText.setText(emailValue);
  126.     locationLabel.setText(res.getString("LOCATION"));
  127.     locationText.setText(locationValue);
  128.     otherLabel.setText(res.getString("OTHER_STUFF"));
  129.     gridLayout1.setHgap(6);
  130.     flowLayout1.setHgap(25);
  131.     panel1.setLayout(gridLayout1);
  132.     otherText.setText(otherValue);
  133.     sendButton.setLabel(res.getString("SEND_NOTE"));
  134.     sendButton.setEnabled(false);
  135.     noteText.setText(res.getString("NOTE_INSTRUCTIONS"));
  136.     noteText.setEditable(false);
  137.     client.add(userText,new GridBagConstraints2(0, 0, 1, 1, 0.0, 0.0
  138.             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(4, 20, 4, 10), 20, 0));
  139.     client.add(ratingText,new GridBagConstraints2(1,0,1,1,1.0,0.0,GridBagConstraints.WEST
  140.                              ,GridBagConstraints.HORIZONTAL,new Insets(4,0,4,20),100,0));
  141.     client.add(nameLabel,new GridBagConstraints2(0,1,1,1,0.0,0.0,GridBagConstraints.WEST
  142.                              ,GridBagConstraints.NONE,new Insets(4,20,4,0),20,0));
  143.     client.add(nameText,new GridBagConstraints2(1,1,1,1,1.0,0.0,GridBagConstraints.WEST
  144.                              ,GridBagConstraints.HORIZONTAL,new Insets(4,0,4,20),100,0));
  145.     client.add(emailLabel,new GridBagConstraints2(0,2,1,1,0.0,0.0,GridBagConstraints.WEST
  146.                               ,GridBagConstraints.NONE,new Insets(4,20,4,0),20,0));
  147.     client.add(emailText,new GridBagConstraints2(1,2,1,1,1.0,0.0,GridBagConstraints.WEST
  148.                             ,GridBagConstraints.HORIZONTAL,new Insets(4,0,4,20),100,0));
  149.     client.add(locationLabel,new GridBagConstraints2(0,3,1,1,0.0,0.0,GridBagConstraints.WEST
  150.                              ,GridBagConstraints.NONE,new Insets(4,20,4,0),20,0));
  151.     client.add(locationText,new GridBagConstraints2(1,3,1,1,1.0,0.0,GridBagConstraints.WEST
  152.                                ,GridBagConstraints.HORIZONTAL,new Insets(4,0,4,20),100,0));
  153.     client.add(otherLabel,new GridBagConstraints2(0,4,1,1,0.0,0.0,GridBagConstraints.WEST
  154.                              ,GridBagConstraints.NONE,new Insets(4,20,4,0),20,0));
  155.     client.add(otherText,new GridBagConstraints2(1,4,1,1,1.0,0.0,GridBagConstraints.WEST
  156.                             ,GridBagConstraints.HORIZONTAL,new Insets(4,0,4,20),100,0));
  157.     client.add(sendButton,new GridBagConstraints2(0,5,1,1,0.0,0.0,GridBagConstraints.WEST
  158.                              ,GridBagConstraints.NONE,new Insets(4,20,4,0),20,0));
  159.     client.add(noteText,new GridBagConstraints2(1,5,1,1,1.0,0.0,GridBagConstraints.WEST
  160.                            ,GridBagConstraints.HORIZONTAL,new Insets(4,0,4,20),100,0));
  161.     sendButton.addActionListener(new SendButtonListener(this));
  162.     okButton.setLabel(res.getString("OK"));
  163.     cancelButton.setLabel(res.getString("CANCEL"));
  164.     okButton.addActionListener(new OkButtonListener(this));
  165.     cancelButton.addActionListener(new CancelButtonListener(this));
  166.     cb.setLabel(res.getString("ONLY_SHOW"));
  167.     cb.setState(true);
  168.     cb.addItemListener(new CBListener(this));
  169.     eastSpace.setText("  ");
  170.     westSpace.setText("  ");
  171.   }
  172.  
  173.   public void setVisible(boolean isVisible) {
  174.     super.setVisible(isVisible);
  175.     if (isVisible)
  176.       nameText.requestFocus();
  177.   }
  178.  
  179.   public void displayData(String[] data) {
  180.     if (data.length > 8){
  181.       boolean editable = data[1].equals(dlgParent.myName);
  182.       if (editable)   {
  183.         noteText.setText(res.getString("NOTE_INSTRUCTIONS"));
  184.         setTitle(res.getString("YOUR_PUBLIC"));
  185.       }
  186.       else {
  187.         setTitle(res.getString("PUBLIC_INFO") + data[1]);
  188.         noteText.setText("");
  189.       }
  190.       noteText.setEditable(!editable);
  191.       sendButton.setEnabled(!editable);
  192.       nameText.setEditable(editable);
  193.       emailText.setEditable(editable);
  194.       locationText.setEditable(editable);
  195.       otherText.setEditable(editable);
  196.  
  197.       userText.setText(data[1]);
  198.       ratingText.setText(res.getString("RATING") + data[7] + "    " + data[8] + res.getString("GAMES_PLAYED"));
  199.       nameText.setText(data[3]);
  200.       emailText.setText(data[4]);
  201.       locationText.setText(data[5]);
  202.       otherText.setText(data[6]);
  203.     }
  204.   }
  205.  
  206.   public void listMessage(String msg) {
  207.     String[] data = PersistString.parse(msg) ;
  208.     System.out.println("parmcount=" + data.length + " " + msg);
  209.     int i = 0;
  210.     while (i+2 < data.length) {
  211.       listVector.addElement(data[i+1]);
  212.       list.addItem(data[i ] + " " + data[i+1] + "  " + data[i+2]);
  213.       i = i + 3;
  214.     }
  215.   }
  216.  
  217.   public void listItemSelected() {
  218.     int index = list.getSelectedIndex();
  219.     String id = (String)listVector.elementAt(index);
  220.     dlgParent.sendMsg("Information",id);
  221.   }
  222.  
  223.   public void cbAction() {
  224.     list.removeAll();
  225.     listVector.removeAllElements();
  226.     if (cb.getState())
  227.       dlgParent.sendMsg("LogonList","");
  228.     else
  229.       dlgParent.sendMsg("PlayerList","");
  230.   }
  231.  
  232.   public void sendNote(ActionEvent e) {
  233.     String[] data = new String[2];
  234.     data[0]       = userText.getText();
  235.     String note   = noteText.getText();
  236.  
  237.     if (note.length() > 0 && !note.startsWith(res.getString("MESSAGE_SENT")))  {
  238.       data[1] = dlgParent.myName + ": " + note;
  239.       dlgParent.sendMsg("Note",data);
  240.       noteText.setText(res.getString("MESSAGE_SENT") + data[0]);
  241.     }
  242.   }
  243.   /** method that the OkButtonListener calls when button is pressed
  244.   *
  245.   */
  246.   public void ok(ActionEvent evt) {
  247.     try {
  248.  
  249.       if (data[1].equals(dlgParent.myName))  {
  250.         data[3] = nameText.getText().trim();
  251.         data[4] = emailText.getText().trim();
  252.         data[5] = locationText.getText().trim();
  253.         data[6] = otherText.getText().trim();
  254.         dlgParent.sendMsg("UpdateInfo",data);
  255.         dlgParent.statusLine.setText(res.getString("USER_DATA_UPDATED"));
  256.       }
  257.       dispose();
  258.       dlgParent.setModal(false);
  259.     }
  260.     catch (Exception e) {
  261.       e.printStackTrace();
  262.     }
  263.   }
  264.  
  265.   /**ovveride so window can close  if system close is pressed
  266.   *
  267.   */
  268.   protected void processWindowEvent(WindowEvent e)  {
  269.     if (e.getID() == WindowEvent.WINDOW_CLOSING)
  270.       cancel(null);
  271.     else
  272.       super.processWindowEvent(e);
  273.   }
  274.  
  275.   /**Method called when the CancelButtonListener is invoked by a button press
  276.   */
  277.   public void cancel(ActionEvent evt) {
  278.     try {
  279.       dlgParent.removeListListener();
  280.       dlgParent.statusLine.setText("");
  281.       dispose();
  282.       dlgParent.setModal(false);
  283.     }
  284.     catch (Exception e) {
  285.       e.printStackTrace();
  286.     }
  287.   }
  288. }
  289.  
  290. class CBListener implements  ItemListener{
  291.   InfoDlg adaptee;
  292.   CBListener(InfoDlg adaptee) {
  293.     this.adaptee = adaptee;
  294.   }
  295.   public void itemStateChanged(ItemEvent parm1) {
  296.     adaptee.cbAction();
  297.   }
  298. }
  299.  
  300. class ListListener implements  ItemListener{
  301.   InfoDlg adaptee;
  302.   ListListener(InfoDlg adaptee) {
  303.     this.adaptee = adaptee;
  304.   }
  305.   public void itemStateChanged(ItemEvent parm1) {
  306.     adaptee.listItemSelected();
  307.   }
  308. }
  309.  
  310. class SendButtonListener implements ActionListener {
  311.   InfoDlg adaptee;
  312.   SendButtonListener(InfoDlg adaptee) {
  313.     this.adaptee = adaptee;
  314.   }
  315.   public void actionPerformed(ActionEvent e) {
  316.     adaptee.sendNote(e);
  317.   }
  318. }
  319.  
  320.  
  321.