Esempio n. 1
0
 public void itemStateChanged(ItemEvent e) {
   PlayerType ptype = (PlayerType) e.getItem();
   CardLayout cl = (CardLayout) mTabs.getLayout();
   assert (cl.getClass() == CardLayout.class);
   cl.show(mTabs, ptype.toString());
 }
Esempio n. 2
0
  public void setUpGameSetupPanel(GameSetupListener setupListener) {
    int boardSizeMaxDigits = 3;

    class PlayerSelectionListener implements ItemListener {
      /**
       * Constructor
       *
       * @param tabs panel whose state should be changed when event fired
       */
      public PlayerSelectionListener(JPanel tabs) {
        mTabs = tabs;
      }

      public void itemStateChanged(ItemEvent e) {
        PlayerType ptype = (PlayerType) e.getItem();
        CardLayout cl = (CardLayout) mTabs.getLayout();
        assert (cl.getClass() == CardLayout.class);
        cl.show(mTabs, ptype.toString());
      }

      private JPanel mTabs;
    }

    mGameSetupDialog = new JDialog(this, true);
    mGameSetupDialog.setTitle("Game setup");
    mGameSetupDialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    mGameSetupDialog.setSize(DIALOG_DIM);

    Container cpane = mGameSetupDialog.getContentPane();

    cpane.setLayout(new GridBagLayout());

    JLabel playerDes = new JLabel("Configure player");

    JComboBox<BotType> botType = new JComboBox<BotType>(BotType.values());
    JComboBox<PerfMeasureType> pmType = new JComboBox<PerfMeasureType>(PerfMeasureType.values());

    JButton addCustomPM = new JButton("+");
    addCustomPM.setVisible(false);
    CustomBotListener customAdder = new CustomBotListener(addCustomPM);
    addCustomPM.addActionListener(customAdder);
    pmType.addItemListener(customAdder);

    JTextField enterPlayerName = new JTextField("name");
    enterPlayerName.selectAll();
    setupListener.setupPlayerNameInput(enterPlayerName);

    JPanel botConfigPanel = new JPanel(new GridBagLayout());
    GridBagConstraints cBotType = new GridBagConstraints();
    cBotType.gridx = 0;
    cBotType.gridy = 0;
    cBotType.gridwidth = 1;
    cBotType.gridheight = 1;
    cBotType.fill = GridBagConstraints.BOTH;

    GridBagConstraints cPerfMeasureType = (GridBagConstraints) cBotType.clone();
    cPerfMeasureType.gridy = 1;
    GridBagConstraints cCustomAdder = (GridBagConstraints) cBotType.clone();
    cCustomAdder.gridx = 1;
    botConfigPanel.add(botType, cBotType);
    botConfigPanel.add(pmType, cPerfMeasureType);
    botConfigPanel.add(addCustomPM, cCustomAdder);
    setupListener.setupCustomBotListener(customAdder);

    JPanel playerConfigPanel = new JPanel(new CardLayout());
    playerConfigPanel.add(enterPlayerName, PlayerType.HUMAN.toString());
    playerConfigPanel.add(botConfigPanel, PlayerType.BOT.toString());

    JComboBox<PlayerType> playerType = new JComboBox<PlayerType>(PlayerType.values());
    PlayerSelectionListener listenPlayerSelection = new PlayerSelectionListener(playerConfigPanel);
    playerType.addItemListener(listenPlayerSelection);
    setupListener.setupTypeInput(playerType, botType, pmType);

    JLabel boardDes = new JLabel("Configure board");
    JTextField enterWidth = new JTextField("10");
    enterWidth.setColumns(boardSizeMaxDigits);
    JTextField enterHeight = new JTextField("15");
    enterHeight.setColumns(boardSizeMaxDigits);
    setupListener.setupBoardInputs(enterWidth, enterHeight);

    JButton startGameButton = new JButton("start");
    startGameButton.addActionListener(setupListener);

    int extSpace = 5;
    GridBagConstraints cUniversal = new GridBagConstraints();
    cUniversal.gridwidth = 1;
    cUniversal.gridheight = 1;
    cUniversal.fill = GridBagConstraints.BOTH;
    cUniversal.insets = new Insets(extSpace, extSpace, extSpace, extSpace);
    cUniversal.anchor = GridBagConstraints.CENTER;

    GridBagConstraints cDesPSelect = (GridBagConstraints) cUniversal.clone();
    cDesPSelect.gridx = 0;
    cDesPSelect.gridy = 0;
    cDesPSelect.gridwidth = 2;
    cDesPSelect.gridheight = 1;

    GridBagConstraints cPTypeSelect = (GridBagConstraints) cDesPSelect.clone();
    cPTypeSelect.gridy = 1;

    GridBagConstraints cPlayerSelection = (GridBagConstraints) cDesPSelect.clone();
    cPlayerSelection.gridy = 2;

    GridBagConstraints cDesBoardConfig = (GridBagConstraints) cUniversal.clone();
    cDesBoardConfig.gridx = 0;
    cDesBoardConfig.gridy = 3;
    cDesBoardConfig.gridwidth = 2;

    GridBagConstraints cBoardWInput;
    cBoardWInput = (GridBagConstraints) cDesBoardConfig.clone();
    cBoardWInput.gridy = 4;
    cBoardWInput.gridwidth = 1;
    cBoardWInput.weightx = 0.5;

    GridBagConstraints cBoardHInput;
    cBoardHInput = (GridBagConstraints) cBoardWInput.clone();
    cBoardHInput.gridx = 1;

    GridBagConstraints cStartButton;
    cStartButton = (GridBagConstraints) cDesPSelect.clone();
    cStartButton.gridy = 5;
    cStartButton.gridwidth = 2;

    mGameSetupDialog.add(playerDes, cDesPSelect);
    mGameSetupDialog.add(playerType, cPTypeSelect);
    mGameSetupDialog.add(playerConfigPanel, cPlayerSelection);
    mGameSetupDialog.add(boardDes, cDesBoardConfig);
    mGameSetupDialog.add(enterWidth, cBoardWInput);
    mGameSetupDialog.add(enterHeight, cBoardHInput);
    mGameSetupDialog.add(startGameButton, cStartButton);
  }