home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / BoardOptions.java < prev    next >
Encoding:
Java Source  |  1997-07-03  |  8.0 KB  |  232 lines

  1. package borland.samples.apps.chess.client;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.WindowEvent;
  6. import java.awt.event.ActionListener;
  7. import borland.jbcl.control.ColorChooserPanel;
  8. import borland.samples.apps.chess.client.board.*;
  9. import java.net.URL;
  10.  
  11. /**Dialog for modifying the appearance of the Chessboard
  12. */
  13. public class BoardOptions extends Dialog implements OkCancelDialog{
  14.  
  15.   ColorChooserPanel lightColorPanel ;
  16.   ColorChooserPanel darkColorPanel  ;
  17.   Panel dlgPanel = new Panel();
  18.   //ColorChooserPanel lightColorPanel = new ColorChooserPanel() ;
  19.   //ColorChooserPanel darkColorPanel  = new ColorChooserPanel() ;
  20.   Choice pieces = new Choice();
  21.   ChessViewer myParent;
  22.   Chessboard board;
  23.   Frame theframe;
  24.   URL[] imageNames;
  25.   Checkbox switchSides = new Checkbox();
  26.   Label eastLabel = new Label();
  27.   Label westLabel = new Label();
  28.   Label temp = new Label();
  29.   Label centerTitleText = new Label();
  30.   Label centerTitleText2 = new Label();
  31.   Panel NorthPanel    = new Panel();
  32.   Panel internalPanel = new Panel();
  33.   Panel centerPanel   = new Panel();
  34.   Panel centerTitle   = new Panel();
  35.   Panel bottomPanel   = new Panel();
  36.   Panel buttonBar     = new Panel();
  37.   Panel buttonBarGrid = new Panel();
  38.   GridLayout buttonBarGridLayout = new GridLayout();
  39.   FlowLayout buttonBarLayout = new FlowLayout()  ;
  40.   Button okButton     = new Button();
  41.   Button cancelButton = new Button() ;
  42.   BorderLayout internalPanelBorderLayout = new BorderLayout();
  43.   BorderLayout dialogBorderLayout        = new BorderLayout();
  44.   GridLayout northPanelGridLayout      = new GridLayout();
  45.   GridLayout centerTitleGridLayout     = new GridLayout();
  46.   GridLayout centerPanelGridLayout     = new GridLayout();
  47.   GridLayout bottomPanelGridLayout     = new GridLayout();
  48.   //Color lightColor = null;
  49.  // Color darkColor = null;
  50.   String text = CVS.BLACK_AT_TOP;
  51.   /** Constructor
  52.   *@param ChessViewer myParent gives access back to the Applet
  53.   *@param Frame  theframe  the frame that parents this dialog
  54.   *@URL[] imageNames an array of Chesspiece image files
  55.   *  Image files are 6 by 2 squares in size in Rook,Knight,Bishop,Queen,King,Pawn
  56.   *  order. The first row has the white pieces, the second has the black pieces.
  57.   *  all pieces have the same background color which is not otherwise used in
  58.   *  the image. The Chessboard Object makes this background transparent when
  59.   *  rendering the pieces.
  60.   */
  61.  
  62.   public BoardOptions(ChessViewer myParent,Frame theframe,URL[] imageNames,Chessboard board)  {
  63.     super(theframe,CVS.CHESSBOARD_OPTIONS,false);
  64.     try {
  65.       System.out.println("BoardOptions xtor...");
  66.       this.myParent = myParent;
  67.       this.board = board;
  68.       this.theframe = theframe;
  69.       this.imageNames = imageNames;
  70.       myParent.setModal(true);
  71.       enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  72.  
  73.  
  74.       if (board.isBlackOnTop())
  75.         text = CVS.WHITE_AT_TOP ;
  76.       jbInit();
  77.       add(dlgPanel);
  78.       lightColorPanel.setColorValue(board.getLightSquareColor());
  79.       darkColorPanel.setColorValue(board.getDarkSquareColor());
  80.  
  81.     }
  82.     catch (Exception e) {
  83.       System.out.println("BoardOptions xtor" + e);
  84.       e.printStackTrace();
  85.     }
  86.   }
  87.   public void jbInit() {
  88.     //Color lightColor = board.getLightSquareColor();
  89.     //Color darkColor = board.getDarkSquareColor();
  90.     lightColorPanel = new ColorChooserPanel() ;
  91.     darkColorPanel  = new ColorChooserPanel() ;
  92.     dlgPanel.setLayout(dialogBorderLayout);
  93.     northPanelGridLayout.setRows(3);
  94.     northPanelGridLayout.setColumns(1);
  95.     centerTitleGridLayout.setRows(1) ;
  96.     centerTitleGridLayout.setColumns(2) ;
  97.     centerPanelGridLayout.setRows(1) ;
  98.     centerPanelGridLayout.setColumns(2) ;
  99.     bottomPanelGridLayout.setRows(2) ;
  100.     bottomPanelGridLayout.setColumns(1) ;
  101.     internalPanel.setLayout(internalPanelBorderLayout);
  102.     centerPanel.setLayout(centerPanelGridLayout);
  103.     bottomPanel.setLayout(bottomPanelGridLayout);
  104.     buttonBar.setLayout(buttonBarLayout);
  105.     buttonBarGridLayout.setColumns(2);
  106.     buttonBarGridLayout.setHgap(6);
  107.     buttonBarGrid.setLayout(buttonBarGridLayout);
  108.  
  109.     NorthPanel.setLayout(northPanelGridLayout);
  110.     temp.setText(CVS.PIECES_);
  111.     NorthPanel.add(temp);
  112.     NorthPanel.add(pieces);
  113.     for (int i = 0; i< imageNames.length;i++)
  114.       pieces.addItem(imageNames[i].toString());
  115.     centerTitle.setLayout(centerTitleGridLayout );
  116.     centerTitleText.setText(CVS.LIGHT_SQUARES);
  117.     centerTitle.add(centerTitleText);
  118.     centerTitleText2.setText(CVS.DARK_SQUARES);
  119.     centerTitle.add(centerTitleText2);
  120.     NorthPanel.add(centerTitle);
  121.     lightColorPanel.setColorValue(Color.red);
  122.     darkColorPanel.setColorValue(Color.yellow );
  123.     centerPanel.add(lightColorPanel);
  124.     centerPanel.add(darkColorPanel);
  125.     System.out.println("BoardOptions xtor1");
  126.     buttonBarGrid.add(okButton);
  127.     okButton.addActionListener(new OkButtonListener(this));
  128.     buttonBarGrid.add(cancelButton);
  129.     cancelButton.setLabel(CVS.CANCEL);
  130.     okButton.setLabel(CVS.OK);
  131.     cancelButton.addActionListener(new CancelButtonListener(this));
  132.     buttonBar.add(buttonBarGrid);
  133.     System.out.println("BoardOptions xtor2");
  134.  
  135.     switchSides.setLabel(text);
  136.     bottomPanel.add(switchSides);
  137.     bottomPanel.add(buttonBar);
  138.     internalPanel.add(NorthPanel,BorderLayout.NORTH);
  139.     internalPanel.add(bottomPanel,BorderLayout.SOUTH);
  140.     internalPanel.add(centerPanel,BorderLayout.CENTER);
  141.     eastLabel.setText("  ");
  142.     westLabel.setText("  ");
  143.     dlgPanel.add(eastLabel,BorderLayout.EAST);
  144.     dlgPanel.add(westLabel,BorderLayout.WEST);
  145.     dlgPanel.add(internalPanel,BorderLayout.CENTER);
  146.   }
  147.    protected void processWindowEvent(WindowEvent e)  {
  148.  
  149.     if (e.getID() == WindowEvent.WINDOW_CLOSING)
  150.       cancel(null);
  151.     else
  152.       super.processWindowEvent(e);
  153.   }
  154.    public void show() {
  155.     super.show();
  156.     pieces.requestFocus();
  157.   }
  158.   /**Treat System Close the same as cancel
  159.  
  160.   protected void processWindowEvent(WindowEvent e)  {
  161.     if ((e.getID() == WindowEvent.WINDOW_CLOSED || e.getID() == WindowEvent.WINDOW_CLOSING)) {
  162.       dispose();
  163.       myParent.setModal(false);
  164.     }
  165.     else
  166.       super.processWindowEvent( e);
  167.   }  */
  168.   /**this OkCancelDialog interface method is called by the OkButtonListener
  169.   *   when it receives an ActionEvent
  170.   */
  171.  
  172.   public void ok(ActionEvent evt) {
  173.     try {
  174.       if (lightColorPanel.isChanged())
  175.         board.setLightSquareColor(lightColorPanel.getColorValue());
  176.       if (darkColorPanel.isChanged())
  177.         board.setDarkSquareColor(darkColorPanel.getColorValue());
  178.       int index = pieces.getSelectedIndex();
  179.       dispose();
  180.       if (index > 0){
  181.         board.setImageURL(imageNames[index]);
  182.         System.out.println("resize frame to" + theframe.getPreferredSize());
  183.         theframe.setSize(theframe.getPreferredSize());
  184.       }  
  185.       else
  186.         board.setPosition();
  187.       if (switchSides.getState())
  188.         myParent.switchSides();
  189.       theframe.validate();
  190.       myParent.setModal(false);
  191.     }
  192.     catch (Exception e) {
  193.       String message = e.toString() ;
  194.       e.printStackTrace();
  195.     }
  196.   }
  197.  
  198.   /**this OkCancelDialog interface method is called by the CancelButtonListener
  199.   *   when it receives an ActionEvent
  200.   */
  201.   public void cancel(ActionEvent evt) {
  202.     dispose();
  203.     myParent.setModal(false);
  204.   }
  205. }
  206.  
  207.  
  208.  
  209.   class CancelButtonListener implements ActionListener {
  210.     OkCancelDialog adaptee;
  211.     CancelButtonListener(OkCancelDialog adaptee) {
  212.       this.adaptee = adaptee;
  213.     }
  214.     public void actionPerformed(ActionEvent e) {
  215.       adaptee.cancel(e);
  216.     }
  217.     public void actionUIUpdate(ActionEvent e)
  218.     {}
  219.   }
  220.  
  221.   class OkButtonListener implements ActionListener {
  222.     OkCancelDialog adaptee;
  223.     OkButtonListener(OkCancelDialog adaptee) {
  224.       this.adaptee = adaptee;
  225.     }
  226.     public void actionPerformed(ActionEvent e) {
  227.       adaptee.ok(e);
  228.     }
  229.     public void actionUIUpdate(ActionEvent e)
  230.     {}
  231.   }
  232.