/* NamesDialog - HipBone games copyright (C) Charles Cameron Copyright (C) 2004 John Comeau This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ package com.jcomeau.hipbone; import java.awt.*; import java.awt.event.*; import uk.ac.gla.dcs.johnson.NewGridBagLayout; import com.jcomeau.*; public class NamesDialog extends Dialog implements ActionListener { Hipbone parent = null; Panel dialogPanel = new Panel(); NewGridBagLayout layout = new NewGridBagLayout(); Panel buttons = new Panel(); GridLayout buttonLayout = null; Button submit = new Button("OK"); Button cancel = new Button("Cancel"); TextField name = null; TextField conception = null; TextArea comment = null; int joint = 0; public NamesDialog(Frame frame, Hipbone parent, int joint) { super(frame, true); // this dialog is modal! lock em in till they decide! setTitle("Position " + joint); this.parent = parent; this.joint = joint; add(dialogPanel); dialogPanel.setLayout(layout); layout.setRareConstraints(GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0, 1, 1); layout.setPositionSizeAdd(new Label("Name"), dialogPanel, 0, 0, 1, 1); layout.setPositionSizeAdd((name = new TextField(20)), dialogPanel, 1, 0, 1, 1); layout.setPositionSizeAdd(new Label("Title"), dialogPanel, 0, 1, 1, 1); layout.setPositionSizeAdd((conception = new TextField(20)), dialogPanel, 1, 1, 1, 1); layout.setPositionSizeAdd(new Label("Comment"), dialogPanel, 0, 2, 1, 3); layout.setPositionSizeAdd((comment = new TextArea("", 3, 20, TextArea.SCROLLBARS_VERTICAL_ONLY)), dialogPanel, 1, 2, 1, 3); buttons.setLayout(buttonLayout = new GridLayout(0, 2)); buttons.add(submit); buttons.add(cancel); layout.setPositionSizeAdd(buttons, dialogPanel, 0, 6, 2, 1); try { name.setText(parent.gamelog.getPlayer(getPlayer())); conception.setText(parent.gamelog.getConception(joint - 1)); comment.setText(parent.gamelog.getComment(joint)); } catch (NullPointerException noParent) { name.setText("unknown"); } pack(); submit.addActionListener(this); cancel.addActionListener(this); show(); } public int getPlayer() { int player = 0; Common.debugprintln("trying to determine which player this is"); if (parent != null) { for (int i = 0; i < parent.gamelog.playOrder.length; i++) { Common.debugprintln("i = " + i + ", playOrder[i] = " + parent.gamelog.playOrder[i]); if (parent.gamelog.playOrder[i] == 0) return player; if (parent.gamelog.playOrder[i] == joint) return player; player = (player + 1) % 2; } } return 0; } public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); String text = null; String logEntry = null; Common.debugprintln("dialog action: " + event); if (command.equals("OK")) { logEntry = Integer.toString(getPlayer()) + " " + Integer.toString(joint) + " " + '"' + name.getText().trim() + '"'; if ((text = conception.getText().trim()) != null) { logEntry += " " + '"' + text + '"'; } if ((text = comment.getText().trim()) != null) { logEntry += "\n" + text; } logEntry += "\n\n"; parent.gamelog.sendLog(logEntry); dispose(); } else if (command.equals("Cancel")) { dispose(); } if (parent == null) System.exit(0); } public static void main(String[] args) { Common.debugging = true; CloseableFrame frame = new CloseableFrame(); frame.setSize(300, 500); NamesDialog dialog = new NamesDialog(frame, null, 0); frame.add(dialog); frame.pack(); frame.show(); } }