home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-03 | 8.0 KB | 232 lines |
- 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;
-
- /**Dialog for modifying the appearance of the Chessboard
- */
- public class BoardOptions extends Dialog implements OkCancelDialog{
-
- 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 = CVS.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,CVS.CHESSBOARD_OPTIONS,false);
- try {
- 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 = CVS.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();
- }
- }
- public 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(CVS.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(CVS.LIGHT_SQUARES);
- centerTitle.add(centerTitleText);
- centerTitleText2.setText(CVS.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(CVS.CANCEL);
- okButton.setLabel(CVS.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 show() {
- super.show();
- 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)
- {}
- }
-