FileDocCategorySizeDatePackage
WannaPlay.javaAPI DocExample6361Sat Sep 12 03:01:00 BST 1998borland.samples.apps.chess.client

WannaPlay.java

/*
 * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
 * 
 * This SOURCE CODE FILE, which has been provided by Borland as part
 * of a Borland product for use ONLY by licensed users of the product,
 * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
 *
 * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
 * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
 * THE PRODUCT.
 *
 * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
 * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
 * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
 * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
 * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
 * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
 * CODE FILE.
 */
package borland.samples.apps.chess.client ;

import java.awt.*;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
/**Displays the Dialog when a player has been challenged for a game
* initiates a message to the server with the players response
*/
public class WannaPlay extends Dialog implements OkCancelDialog
{
  ResourceBundle res = ResourceBundle.getBundle("borland.samples.apps.chess.client.Res");
  Panel dlgPanel = new Panel();
  Button yes = new Button();
  Button no = new Button();
  String pieceColor;
  String opponent;
  String opponentRating;
  String myRating;
  int gameTime;
  int moveTime;
  String[] parm;
  Label timeControlLabel = new Label();
  GridLayout dialogLayout = new GridLayout();
  Panel buttonBar = new Panel();
  Label text1 = new Label();
  Label text2 = new Label();
  String timeControl;
  Panel panel1 = new Panel();
  GridLayout gridLayout1 = new GridLayout();
  ChessViewer dlgParent;
  /**Constructor
  *param Frame parent The Frame of this dialogs parent
  *param ChessViewer theapp
  *param String[] parm the parsed message data from the Challenge message
  */
  public WannaPlay(Frame parent,ChessViewer theapp,String [] parm) {
    super(parent,"",false);
    setTitle(res.getString("FRESH_MEAT_"));
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    if (parm.length < 6)
      System.out.println("WannaPlay expected six parameters, found " + parm.length);
    dlgParent =  theapp;
    opponent = parm[2];
    opponentRating = parm[3];
    myRating = parm[5];
    this.parm = parm;
    try {
      gameTime = Integer.parseInt(parm[0]);
      moveTime = Integer.parseInt(parm[1]);
    }
    catch (NumberFormatException e) {}
    timeControl = CVS.Insert(res.getString("TIME_CONTROL"),parm[0],parm[1]);
    jbInit();
    add(dlgPanel);
    timeControlLabel.setText(timeControl);
    Random color = new Random();
    if (opponent.equals(dlgParent.opponentName))
      if (opponent.equals(dlgParent.game.getPlayerW()))
        pieceColor = "white";
      else
        pieceColor = "black";
    else
      if (color.nextInt() > 0)
        pieceColor = "white";
      else
        pieceColor = "black";

    if (pieceColor.equals("white"))  {
      dlgParent.yourColor = ChessRules.White;
      text2.setText(res.getString("YOU_GET_WHITE"));
    }
    else {
      dlgParent.yourColor = ChessRules.Black;
      text2.setText(res.getString("YOU_GET_BLACK"));
    }
    text1.setText(CVS.Insert(res.getString("DO_YOU_WANT_TO_PLAY"),opponent));


  }

  private void jbInit() {
    dialogLayout.setRows(4);
    dialogLayout.setColumns(1);
    gridLayout1.setHgap(6);
    buttonBar.setLayout(gridLayout1);
    dlgPanel.setLayout(dialogLayout);
    dlgPanel.add(text1);
    dlgPanel.add(text2);
    timeControlLabel.setText(res.getString("TIME_CONTROL"));
    text2.setText(res.getString("YOU_GET_BLACK"));
    text1.setText(res.getString("DO_YOU_WANT_TO_PLAY") );
    dlgPanel.add(timeControlLabel);
    dlgPanel.add(panel1, null);
    panel1.add(buttonBar);
    buttonBar.add(yes);
    buttonBar.add(no);
    yes.setLabel(res.getString("YES"));
    yes.addActionListener(new OkButtonListener(this));
    no.setLabel(res.getString("NO"));
    no.addActionListener(new CancelButtonListener(this));
  }
  /**So System Close acts like pressing No
  */
  protected void processWindowEvent(WindowEvent e)  {
    if (e.getID() == WindowEvent.WINDOW_CLOSING)
      cancel(null);
    else
      super.processWindowEvent(e);
  }

  /**the OkCancelDialog method that the OkButtonListener calls when its receives an action
  */
  public void ok(ActionEvent evt) {
    dlgParent.gameTimeValue = gameTime;
    dlgParent.playerTime[0] = dlgParent.gameTimeValue * 60000;
    dlgParent.playerTime[1] = dlgParent.playerTime[0];
    dlgParent.lMoveTime = moveTime * 1000;
    parm[0] = pieceColor;
    dlgParent.sendMsg("GameAccept",parm);
    dlgParent.opponentName = opponent;
    if (dlgParent.yourColor == ChessRules.White) {
      dlgParent.newGame(dlgParent.myName,opponent);
      dlgParent.statusLine.setText(res.getString("YOUR_MOVE"));
      dlgParent.game.setWhiteElo(myRating);
      dlgParent.game.setBlackElo(opponentRating);
    }
    else {
      dlgParent.newGame(opponent,dlgParent.myName);
      dlgParent.statusLine.setText(CVS.Insert(res.getString("WAITING_FOR"),dlgParent.game.getPlayerW()) );
      dlgParent.game.setWhiteElo(opponentRating);
      dlgParent.game.setBlackElo(myRating);
    }
    dlgParent.game.setSite(res.getString("BORLAND_CHESS_CLUB"));
    dlgParent.game.setRound("-");
    dlgParent.game.setEvent(res.getString("RATED_GAME"));
    GregorianCalendar today = new GregorianCalendar();
    dlgParent.game.setDate(String.valueOf(today.get(Calendar.YEAR))+ "." +
                           String.valueOf(today.get(Calendar.MONTH)+1) + "." +
                           String.valueOf(today.get(Calendar.DAY_OF_MONTH)));
    dispose();

  }

  /**The OkCancelInterface method that the CancelButtonListener calls when its receives an action
  */
  public void cancel(ActionEvent evt) {
    dlgParent.sendMsg("RefuseChallenge",pieceColor) ;
    dlgParent.opponentName = null;
    //  synchronized (dlgParent){
    //    dlgParent.notify();

    // }
    dispose();
  }
}