home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / InfoDlg.java < prev    next >
Text File  |  1997-07-03  |  11KB  |  299 lines

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