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

BoardOptions.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.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import borland.jbcl.control.ColorChooserPanel;
import borland.samples.apps.chess.client.board.*;
import java.net.URL;
import java.util.ResourceBundle;

/**Dialog for modifying the appearance of the Chessboard
*/
public class BoardOptions extends Dialog implements OkCancelDialog{
  ResourceBundle res = ResourceBundle.getBundle("borland.samples.apps.chess.client.Res");
  ColorChooserPanel lightColorPanel ;
  ColorChooserPanel darkColorPanel  ;
  Panel dlgPanel = new Panel();
  //ColorChooserPanel lightColorPanel = new ColorChooserPanel() ;
  //ColorChooserPanel darkColorPanel  = new ColorChooserPanel() ;
  Choice pieces = new Choice();
  ChessViewer myParent;
  Chessboard board;
  Frame theframe;
  URL[] imageNames;
  Checkbox switchSides = new Checkbox();
  Label eastLabel = new Label();
  Label westLabel = new Label();
  Label temp = new Label();
  Label centerTitleText = new Label();
  Label centerTitleText2 = new Label();
  Panel northPanel    = new Panel();
  Panel internalPanel = new Panel();
  Panel centerPanel   = new Panel();
  Panel centerTitle   = new Panel();
  Panel bottomPanel   = new Panel();
  Panel buttonBar     = new Panel();
  Panel buttonBarGrid = new Panel();
  GridLayout buttonBarGridLayout = new GridLayout();
  FlowLayout buttonBarLayout = new FlowLayout()  ;
  Button okButton     = new Button();
  Button cancelButton = new Button() ;
  BorderLayout internalPanelBorderLayout = new BorderLayout();
  BorderLayout dialogBorderLayout        = new BorderLayout();
  GridLayout northPanelGridLayout      = new GridLayout();
  GridLayout centerTitleGridLayout     = new GridLayout();
  GridLayout centerPanelGridLayout     = new GridLayout();
  GridLayout bottomPanelGridLayout     = new GridLayout();
  //Color lightColor = null;
 // Color darkColor = null;
  String text = res.getString("BLACK_AT_TOP");
  /** Constructor
  *@param ChessViewer myParent gives access back to the Applet
  *@param Frame  theframe  the frame that parents this dialog
  *@URL[] imageNames an array of Chesspiece image files
  *  Image files are 6 by 2 squares in size in Rook,Knight,Bishop,Queen,King,Pawn
  *  order. The first row has the white pieces, the second has the black pieces.
  *  all pieces have the same background color which is not otherwise used in
  *  the image. The Chessboard Object makes this background transparent when
  *  rendering the pieces.
  */

  public BoardOptions(ChessViewer myParent,Frame theframe,URL[] imageNames,Chessboard board)  {
    super(theframe,"",false);
    try {
      setTitle(res.getString("CHESSBOARD_OPTIONS"));
      System.out.println("BoardOptions xtor...");
      this.myParent = myParent;
      this.board = board;
      this.theframe = theframe;
      this.imageNames = imageNames;
      myParent.setModal(true);
      enableEvents(AWTEvent.WINDOW_EVENT_MASK);


      if (board.isBlackOnTop())
        text = res.getString("WHITE_AT_TOP") ;
      jbInit();
      add(dlgPanel);
      lightColorPanel.setColorValue(board.getLightSquareColor());
      darkColorPanel.setColorValue(board.getDarkSquareColor());

    }
    catch (Exception e) {
      System.out.println("BoardOptions xtor" + e);
      e.printStackTrace();
    }
  }
  private void jbInit() {
    //Color lightColor = board.getLightSquareColor();
    //Color darkColor = board.getDarkSquareColor();
    lightColorPanel = new ColorChooserPanel() ;
    darkColorPanel  = new ColorChooserPanel() ;
    dlgPanel.setLayout(dialogBorderLayout);
    northPanelGridLayout.setRows(3);
    northPanelGridLayout.setColumns(1);
    centerTitleGridLayout.setRows(1) ;
    centerTitleGridLayout.setColumns(2) ;
    centerPanelGridLayout.setRows(1) ;
    centerPanelGridLayout.setColumns(2) ;
    bottomPanelGridLayout.setRows(2) ;
    bottomPanelGridLayout.setColumns(1) ;
    internalPanel.setLayout(internalPanelBorderLayout);
    centerPanel.setLayout(centerPanelGridLayout);
    bottomPanel.setLayout(bottomPanelGridLayout);
    buttonBar.setLayout(buttonBarLayout);
    buttonBarGridLayout.setColumns(2);
    buttonBarGridLayout.setHgap(6);
    buttonBarGrid.setLayout(buttonBarGridLayout);

    northPanel.setLayout(northPanelGridLayout);
    temp.setText(res.getString("PIECES_"));
    northPanel.add(temp);
    northPanel.add(pieces);
    for (int i = 0; i< imageNames.length;i++)
      pieces.addItem(imageNames[i].toString());
    centerTitle.setLayout(centerTitleGridLayout );
    centerTitleText.setText(res.getString("LIGHT_SQUARES"));
    centerTitle.add(centerTitleText);
    centerTitleText2.setText(res.getString("DARK_SQUARES"));
    centerTitle.add(centerTitleText2);
    northPanel.add(centerTitle);
    lightColorPanel.setColorValue(Color.red);
    darkColorPanel.setColorValue(Color.yellow );
    centerPanel.add(lightColorPanel);
    centerPanel.add(darkColorPanel);
    System.out.println("BoardOptions xtor1");
    buttonBarGrid.add(okButton);
    okButton.addActionListener(new OkButtonListener(this));
    buttonBarGrid.add(cancelButton);
    cancelButton.setLabel(res.getString("CANCEL"));
    okButton.setLabel(res.getString("OK"));
    cancelButton.addActionListener(new CancelButtonListener(this));
    buttonBar.add(buttonBarGrid);
    System.out.println("BoardOptions xtor2");

    switchSides.setLabel(text);
    bottomPanel.add(switchSides);
    bottomPanel.add(buttonBar);
    internalPanel.add(northPanel,BorderLayout.NORTH);
    internalPanel.add(bottomPanel,BorderLayout.SOUTH);
    internalPanel.add(centerPanel,BorderLayout.CENTER);
    eastLabel.setText("  ");
    westLabel.setText("  ");
    dlgPanel.add(eastLabel,BorderLayout.EAST);
    dlgPanel.add(westLabel,BorderLayout.WEST);
    dlgPanel.add(internalPanel,BorderLayout.CENTER);
  }
   protected void processWindowEvent(WindowEvent e)  {

    if (e.getID() == WindowEvent.WINDOW_CLOSING)
      cancel(null);
    else
      super.processWindowEvent(e);
  }
   public void setVisible(boolean isVisible) {
    super.setVisible(isVisible);
    if (isVisible)
      pieces.requestFocus();
  }
  /**Treat System Close the same as cancel

  protected void processWindowEvent(WindowEvent e)  {
    if ((e.getID() == WindowEvent.WINDOW_CLOSED || e.getID() == WindowEvent.WINDOW_CLOSING)) {
      dispose();
      myParent.setModal(false);
    }
    else
      super.processWindowEvent( e);
  }  */
  /**this OkCancelDialog interface method is called by the OkButtonListener
  *   when it receives an ActionEvent
  */

  public void ok(ActionEvent evt) {
    try {
      if (lightColorPanel.isChanged())
        board.setLightSquareColor(lightColorPanel.getColorValue());
      if (darkColorPanel.isChanged())
        board.setDarkSquareColor(darkColorPanel.getColorValue());
      int index = pieces.getSelectedIndex();
      dispose();
      if (index > 0){
        board.setImageURL(imageNames[index]);
        System.out.println("resize frame to" + theframe.getPreferredSize());
        theframe.setSize(theframe.getPreferredSize());
      }  
      else
        board.setPosition();
      if (switchSides.getState())
        myParent.switchSides();
      theframe.validate();
      myParent.setModal(false);
    }
    catch (Exception e) {
      String message = e.toString() ;
      e.printStackTrace();
    }
  }

  /**this OkCancelDialog interface method is called by the CancelButtonListener
  *   when it receives an ActionEvent
  */
  public void cancel(ActionEvent evt) {
    dispose();
    myParent.setModal(false);
  }
}



  class CancelButtonListener implements ActionListener {
    OkCancelDialog adaptee;
    CancelButtonListener(OkCancelDialog adaptee) {
      this.adaptee = adaptee;
    }
    public void actionPerformed(ActionEvent e) {
      adaptee.cancel(e);
    }
    public void actionUIUpdate(ActionEvent e)
    {}
  }

  class OkButtonListener implements ActionListener {
    OkCancelDialog adaptee;
    OkButtonListener(OkCancelDialog adaptee) {
      this.adaptee = adaptee;
    }
    public void actionPerformed(ActionEvent e) {
      adaptee.ok(e);
    }
    public void actionUIUpdate(ActionEvent e)
    {}
  }