home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / dxma.exe / DXMA05.cab / samples / da / java / apps / chess / GameFrame.java < prev    next >
Encoding:
Java Source  |  1997-11-13  |  5.1 KB  |  215 lines

  1. import java.awt.*;
  2. import java.util.*;
  3. import PGNViewFrame;
  4.  
  5. public class GameFrame extends Frame
  6. {
  7.     protected Button m_nextButton = new Button("Next Game");
  8.     protected Button m_prevButton = new Button("Prev Game");
  9.     protected Label m_gameLabel = new Label("Game 0 of 0");
  10.     protected TextArea m_tagsText = new TextArea();
  11.     protected TextArea m_srcText = new TextArea();
  12.     protected PGNViewFrame m_mainFrame = null;
  13.   protected Vector m_games = null;
  14.   protected int m_curGameIdx;
  15.  
  16.     public GameFrame(PGNViewFrame parent)
  17.     {
  18.         super("Games in this PGN file");
  19.  
  20.         m_mainFrame = parent;
  21.  
  22.     setBackground(Color.lightGray);
  23.         setForeground(Color.black);
  24.  
  25.     m_tagsText.setEditable(false);
  26.     m_srcText.setEditable(false);
  27.  
  28.         //setup the other components
  29.         GridBagLayout layout = new GridBagLayout();
  30.         setLayout(layout);
  31.         GridBagConstraints gbc = new GridBagConstraints();
  32.  
  33.     // add the enxt and previous game buttons
  34.         gbc.weightx = 0;
  35.         gbc.fill = GridBagConstraints.HORIZONTAL;
  36.         gbc.ipadx = 20;
  37.         layout.setConstraints(m_prevButton, gbc);
  38.         add(m_prevButton);
  39.  
  40.  
  41.         layout.setConstraints(m_nextButton, gbc);
  42.         add(m_nextButton);
  43.     
  44.     // add the label that displays the current game
  45.     gbc.ipadx = 0;
  46.         gbc.gridwidth = GridBagConstraints.REMAINDER;
  47.         gbc.weightx = 1;
  48.         gbc.insets = new Insets(0,0,0,20);
  49.         gbc.anchor = GridBagConstraints.EAST;
  50.  
  51.         m_gameLabel.setAlignment(Label.RIGHT);
  52.         layout.setConstraints(m_gameLabel, gbc);
  53.         add(m_gameLabel);
  54.      
  55.     // setup the tags area
  56.         gbc.insets = new Insets(0,0,0,0);    
  57.         gbc.fill = GridBagConstraints.BOTH;
  58.         gbc.anchor = GridBagConstraints.NORTHWEST;
  59.         gbc.weighty = 0;
  60.         gbc.weightx = 0;
  61.  
  62.         Label label = new Label("Game Information");
  63.         layout.setConstraints(label, gbc);
  64.         add(label);
  65.  
  66.         gbc.weighty = 1;
  67.         gbc.weightx = 1;
  68.         layout.setConstraints(m_tagsText, gbc);
  69.         add(m_tagsText);
  70.  
  71.     // setup the game source text area
  72.         gbc.weighty = 0;
  73.         gbc.weightx = 0;
  74.         label = new Label("Game MoveText");
  75.         layout.setConstraints(label, gbc);
  76.         add(label);
  77.  
  78.         gbc.weighty = 1;
  79.         gbc.weightx = 1;
  80.         layout.setConstraints(m_srcText, gbc);        
  81.         add(m_srcText);
  82.  
  83.         pack();
  84.     
  85.     }
  86.  
  87.     public boolean handleEvent(Event evt)
  88.     {
  89.         switch (evt.id)
  90.         {
  91.             case Event.WINDOW_DESTROY:
  92.                 hide();
  93.         // uncheck the viewGame in the main frame
  94.                 m_mainFrame.m_fileGame.setState(false);
  95.                 return true;
  96.  
  97.             default:
  98.                 return super.handleEvent(evt);
  99.         }             
  100.     }
  101.  
  102.     public boolean action(Event  evt, Object  what) {
  103.         if (evt.target == m_nextButton) {
  104.             Next();
  105.             return true;
  106.         }    
  107.         else if (evt.target == m_prevButton) {
  108.             Prev();
  109.             return true;
  110.         }
  111.         
  112.         return false;
  113.     }
  114.  
  115.   void Next() {
  116.     // if we're at the last game do nothing
  117.     if (m_curGameIdx >= m_games.size())
  118.       return;
  119.  
  120.     // display the next game
  121.     m_curGameIdx++;
  122.     ResetButtons();
  123.     m_mainFrame.SetGame((Game)m_games.elementAt(m_curGameIdx));
  124.     m_mainFrame.ResetGame();
  125.   }
  126.  
  127.   void Prev() {
  128.     // if there is no previous game do nothing
  129.     if (m_curGameIdx <= 0)
  130.       return;
  131.  
  132.     // display the previous game
  133.     m_curGameIdx--;
  134.     ResetButtons();
  135.     m_mainFrame.SetGame((Game)m_games.elementAt(m_curGameIdx));
  136.     m_mainFrame.ResetGame();
  137.   }
  138.  
  139.   
  140.   void ResetButtons() {
  141.     // set the game label text
  142.     m_gameLabel.setText("Game " + (m_curGameIdx + 1) + " of " + m_games.size());
  143.     SetText((Game)m_games.elementAt(m_curGameIdx));
  144.  
  145.     // enable/disable the next and prev buttons
  146.     if (m_curGameIdx > 0)
  147.       m_prevButton.enable();
  148.     else
  149.       m_prevButton.disable();
  150.  
  151.     if (m_curGameIdx < m_games.size() - 1)
  152.       m_nextButton.enable();
  153.     else
  154.       m_nextButton.disable();
  155.   }
  156.  
  157.   // set the text inside the tags and source text areas
  158.   protected void SetText(Game game) {
  159.  
  160.     // set the tags text
  161.     m_tagsText.setText("");
  162.     Enumeration tags = game.m_tags.elements();
  163.     while(tags.hasMoreElements()){
  164.       m_tagsText.appendText((String)tags.nextElement());
  165.       m_tagsText.appendText("\n");
  166.     }
  167.  
  168.     // set the source text
  169.     m_srcText.setText(WrapString(game.m_src,40));
  170.   }
  171.  
  172.   // utility function to wrap a string to fit into a certain number of columns
  173.   public static String WrapString(String str, int cols) {
  174.     StringBuffer buffer = new StringBuffer();
  175.  
  176.     char[] chars = str.toCharArray();
  177.  
  178.     int left = chars.length;
  179.     int start = 0;
  180.     int end = 0;
  181.  
  182.     while (left > 0 ) {
  183.       if (cols > left) {
  184.         buffer.append(chars,start,left);
  185.         return buffer.toString();
  186.       }
  187.       end = start + cols;
  188.       while (chars[end] != ' ') {
  189.         end--;
  190.         if (end <= start) {
  191.           end = start + cols;
  192.           break;
  193.         }
  194.       }
  195.       buffer.append(chars,start,end-start);
  196.       buffer.append("\r\n");
  197.       left -= (end - start);
  198.       start += (end - start);
  199.     }
  200.     return buffer.toString();
  201.   }
  202.  
  203.   public void SetGames(Vector games) {
  204.     m_games = games;
  205.     m_curGameIdx = 0;
  206.     if (games == null ) {
  207.       // !!! do something smart 
  208.     }
  209.     m_mainFrame.SetGame((Game)m_games.elementAt(m_curGameIdx));
  210.     ResetButtons();
  211.   }
  212.  
  213. }
  214.  
  215.