// Yura Mamyrin, Group D

package risk.ui.SwingGUI;

import risk.engine.*;
import risk.engine.ai.AIPlayer;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.MouseInputListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Vector;
import java.util.ResourceBundle;
import java.net.URL;

/**
 * <p> Swing GUI Main Frame </p>
 * @author Yura Mamyrin
 */

public class SwingGUIPanel extends JPanel implements ActionListener{

	public final static String version = "1.0.6.4";
	public final static String product = "Swing GUI for RISK";

	// TRUE GUI GLOBAL THINGS:

	private ResourceBundle resbundle = risk.engine.translation.TranslationBundle.getBundle();

	private Risk myrisk;

	private int gameState;
	private boolean localGame;

	private JTabbedPane tabbedpane;
	private JToolBar currentToolbar;
	private JMenuBar gMenuBar;

	private GameTab gameTab;
	private ConsoleTab consoleTab;
	private StatisticsTab statisticsTab;
	private DebugTab debugTab;
	private MapEditor editorTab;







	// should not really be here


	private PicturePanel pp;
	private int c1Id;




	private JLabel mapPic;
	private JTextField cardsFile;


	private JLabel attacker;

	private JTextField country1;
	private JTextField country2;

	private JPanel inGameInput;
	private CardLayout inGameCards;

	// these are needed here as they need to be changed to display different amount of buttons
	private JPanel defend;
	private JPanel roll;

	private JSlider slider;
	private JSlider moveNumber;

	private JLabel armies;

	private JButton autoplace;




	private JLabel resultsLabel;


	private SetupPanel guiSetup;


	private JPanel gameOptions;

	private JButton roll1;
	private JButton roll2;
	private JButton roll3;

	private JComboBox mapViewComboBox;

	private JButton showMission;
	private JButton showCards;
	private JButton Undo;





	private JRadioButton domination;
	private JRadioButton capital;
	private JRadioButton mission;
	private JCheckBox AutoPlaceAll;
	private JCheckBox recycle;
	private JRadioButton fixed;
	private JRadioButton increasing;


	/**
	 * Creates a new SwingGUI
	 * @param r The Risk object for this GUI
	 */
	public SwingGUIPanel(Risk r) {

		if (Risk.applet==null) { Toolkit.getDefaultToolkit().getSystemEventQueue().push( new TCPopupEventQueue() ); }

		myrisk= r;

		c1Id = -1;
		gameState=-1; // (-1 means no game)








		setLayout(new java.awt.BorderLayout());

		// set the border of the window
		//setDefaultLookAndFeelDecorated(true);
		//setUndecorated(true);
		//getRootPane().setWindowDecorationStyle(JRootPane.FRAME);


		// add menu bar
		gMenuBar = new JMenuBar();

		tabbedpane = new JTabbedPane();

		gameTab = new GameTab();
		consoleTab = new ConsoleTab();
		statisticsTab = new StatisticsTab();
		debugTab = new DebugTab();
		editorTab = new MapEditor(myrisk);

		addTab(gameTab);
		addTab(consoleTab);
		addTab(statisticsTab);
		addTab(debugTab);
		addTab(editorTab);
		addTab( new risk.messagetool.MessageTool() );
		addTab(new BugsPanel());

		final JPanel oddpanelbug = new JPanel();
		oddpanelbug.setLayout(new java.awt.BorderLayout());
		oddpanelbug.add(tabbedpane, java.awt.BorderLayout.CENTER );
		add(oddpanelbug,java.awt.BorderLayout.CENTER);



		ChangeListener changeMenu = new ChangeListener() {
			public void stateChanged(ChangeEvent e) {


				SwingGUITab sgt = (SwingGUITab)tabbedpane.getSelectedComponent();

				if (currentToolbar!=null) { oddpanelbug.remove(currentToolbar); }
				currentToolbar = sgt.getToolBar();

				currentToolbar.setOrientation( javax.swing.JToolBar.HORIZONTAL );
				oddpanelbug.add( currentToolbar, java.awt.BorderLayout.NORTH );

				repaint();

			}
		};






		tabbedpane.addChangeListener( changeMenu );

		//oddpanelbug.add(toolbarGUI, java.awt.BorderLayout.NORTH );
		changeMenu.stateChanged(null);






		// create Help menu item
		JMenu gHelp = new JMenu(resbundle.getString("swing.menu.help"));
		gHelp.setMnemonic('H');

		JMenuItem gmManual = new JMenuItem(resbundle.getString("swing.menu.manual"));
		gmManual.setMnemonic('M');
		gmManual.setActionCommand("manual");
		gmManual.addActionListener( this );
		gHelp.add(gmManual);

		JMenuItem cmCommands = new JMenuItem(resbundle.getString("swing.menu.console.commands"));
		cmCommands.setMnemonic('C');
		cmCommands.setActionCommand("commands");
		cmCommands.addActionListener( this );
		gHelp.add(cmCommands);

		JMenuItem gmAbout = new JMenuItem(resbundle.getString("swing.menu.about"));
		gmAbout.setMnemonic('A');
		gmAbout.setActionCommand("about");
		gmAbout.addActionListener( this );
		gHelp.add(gmAbout);

		gMenuBar.add(gHelp);




		add(gMenuBar, java.awt.BorderLayout.NORTH );
		// sets menu bar
		//setJMenuBar(gMenuBar);
		//setBounds(new java.awt.Rectangle(0,0,905,629));


		// now gui is setup u can listen
		myrisk.addRiskListener( new SwingRiskAdapter() );
	}


	public void actionPerformed(ActionEvent a) {

		if (a.getActionCommand().equals("manual")) {

			try {
				Risk.openDocs( resbundle.getString("helpfiles.swing") );
			}
			catch(Exception e) {
				showError("Unable to open manual: "+e.getMessage() );
			}

		}
		else if (a.getActionCommand().equals("about")) {

			openAbout();

		}
		else if (a.getActionCommand().equals("quit")) {

			System.exit(0);

		}
		else if (a.getActionCommand().equals("commands")) {
			Commands();
		}
		else {

			System.out.print("command \""+a.getActionCommand()+"\" is not implemented yet\n");

		}


	}

	public void addTab(SwingGUITab a) {

		//tabbedpane.addTab(a.getName(),(Component)a);
		tabbedpane.add((Component)a);

		JMenu menu = a.getMenu();

		if (menu!=null) {

			gMenuBar.add(menu);

		}


	}


class ConsoleTab extends JPanel implements SwingGUITab, ActionListener {

	private String temptext;
	private Vector history;
	private int pointer;

	JMenu cConsole;
	JToolBar toolbarCon;

	private JTextArea Console;
	private JTextField Command;
	private JButton Submit;

	private JLabel statusBar;
	private JScrollPane Con;


	public ConsoleTab() {

		setName( resbundle.getString("swing.tab.console") );

		// ################### CONSOLE #######################

		history = new Vector();
		pointer=-1;

		statusBar = new JLabel(resbundle.getString("swing.status.loading"));
		Console = new JTextArea();
		Con = new JScrollPane(Console);

		Dimension conSize = new Dimension(PicturePanel.PP_X,PicturePanel.PP_Y+60);

		Con.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		Con.setPreferredSize(conSize);
		Con.setMinimumSize(conSize);

		// Console.setBackground(Color.white); // not needed with swing
		Console.setEditable(false);


		Command = new JTextField("");
		// Command.setColumns(75); // dont use because it goes odd in linux
		Dimension CommandSize = new Dimension(PicturePanel.PP_X-50 , 20);
		//c.ipadx = 600; // width
		//c.ipadx = 0; // width
		Command.setPreferredSize(CommandSize);
		Command.setMinimumSize(CommandSize);
		Command.setMaximumSize(CommandSize);

		Submit = new JButton(resbundle.getString("swing.button.submit"));


		Submit.setActionCommand("read command");
		Submit.addActionListener( this );
		Command.setActionCommand("read command");
		Command.addActionListener( this );




		// make tool bar

		toolbarCon = new JToolBar();
		toolbarCon.setRollover(true);
		toolbarCon.setFloatable(false);

		JButton cRunScript	= new JButton(resbundle.getString("swing.menu.console.runscript"));
		JButton cSaveConsole	= new JButton(resbundle.getString("swing.menu.console.save"));
		JButton cClearConsole	= new JButton(resbundle.getString("swing.menu.console.clear"));
		JButton cClearHistory	= new JButton(resbundle.getString("swing.menu.console.histclear"));

		JButton cCommands	= new JButton(resbundle.getString("swing.menu.console.commands"));
		JButton cManual		= new JButton(resbundle.getString("swing.menu.manual"));
		JButton cAbout		= new JButton(resbundle.getString("swing.menu.about"));
		JButton cQuit		= new JButton(resbundle.getString("swing.menu.quit"));

		cRunScript.setActionCommand("run script");
		cRunScript.addActionListener( this );
		cSaveConsole.setActionCommand("save console");
		cSaveConsole.addActionListener( this );
		cClearConsole.setActionCommand("clear console");
		cClearConsole.addActionListener( this );
		cClearHistory.setActionCommand("clear history");
		cClearHistory.addActionListener( this );

		cCommands.setActionCommand("commands");
		cCommands.addActionListener( this );
		cManual.setActionCommand("manual");
		cManual.addActionListener( this );
		cAbout.setActionCommand("about");
		cAbout.addActionListener( this );
		cQuit.setActionCommand("quit");
		cQuit.addActionListener( this );

		toolbarCon.add(cRunScript);
		toolbarCon.add(cSaveConsole);
		toolbarCon.add(cClearConsole);
		toolbarCon.add(cClearHistory);
		toolbarCon.addSeparator();
		toolbarCon.add(cCommands);
		toolbarCon.add(cManual);
		toolbarCon.add(cAbout);
		if (Risk.applet==null) { toolbarCon.add(cQuit); }









		// create Console menu item
		cConsole = new JMenu(resbundle.getString("swing.menu.console"));
		cConsole.setMnemonic('C');

		JMenuItem cmRunScript = new JMenuItem(resbundle.getString("swing.menu.console.runscript"));
		cmRunScript.setMnemonic('R');
		cmRunScript.setActionCommand("run script");
		cmRunScript.addActionListener( this );
		cConsole.add(cmRunScript);

		JMenuItem cmSaveConsole = new JMenuItem(resbundle.getString("swing.menu.console.save"));
		cmSaveConsole.setMnemonic('S');
		cmSaveConsole.setActionCommand("save console");
		cmSaveConsole.addActionListener( this );
		cConsole.add(cmSaveConsole);

		JMenuItem cmClearConsole = new JMenuItem(resbundle.getString("swing.menu.console.clear"));
		cmClearConsole.setMnemonic('C');
		cmClearConsole.setActionCommand("clear console");
		cmClearConsole.addActionListener( this );
		cConsole.add(cmClearConsole);

		JMenuItem cmClearHistory = new JMenuItem(resbundle.getString("swing.menu.console.histclear"));
		cmClearHistory.setMnemonic('H');
		cmClearHistory.setActionCommand("clear history");
		cmClearHistory.addActionListener( this );
		cConsole.add(cmClearHistory);





		GridBagConstraints c = new GridBagConstraints();
		c.insets = new java.awt.Insets(3, 3, 3, 3);
		c.fill = GridBagConstraints.BOTH;


		setLayout(new java.awt.GridBagLayout());

		c.gridx = 0; // col
		c.gridy = 0; // row
		c.gridwidth = 2; // width
		c.gridheight = 1; // height
		add(Con, c);

		c.gridx = 0; // col
		c.gridy = 1; // row
		c.gridwidth = 1; // width
		c.gridheight = 1; // height
		add(Command, c);

		c.gridx = 1; // col
		c.gridy = 1; // row
		c.gridwidth = 1; // width
		c.gridheight = 1; // height
		add(Submit, c);

		c.gridx = 0; // col
		c.gridy = 2; // row
		c.gridwidth = 2; // width
		c.gridheight = 1; // height
		add(statusBar, c);




		Command.addKeyListener( new KeyAdapter() {

			public void keyPressed(KeyEvent key) {


				if (key.getKeyCode() == 38) {
					// Testing.append("up key (history)\n");

					if (pointer < 0) {
						Toolkit.getDefaultToolkit().beep();
					}
					else {
						if (pointer == history.size()-1) { temptext=Command.getText(); }
						Command.setText( (String)history.elementAt(pointer) );
						pointer--;
					}
				}
				else if(key.getKeyCode() == 40) {
					// Testing.append("down key (history)\n");


					if (pointer > history.size()-2 ) {
						Toolkit.getDefaultToolkit().beep();
					}
					else if (pointer == history.size()-2 ) {
						Command.setText(temptext);
						pointer++;
					}
					else {
						pointer=pointer+2;
						Command.setText( (String)history.elementAt(pointer) );
						pointer--;
					}

				}
				else {
					pointer = history.size()-1;
				}

			}
		});

		setOpaque(false);


		statusBar.setText(resbundle.getString( "swing.status.ready"));

	}

	private void cgo(String input) {

		if (input.equals("exit") ) {
			// Testing.append("Exit.\n");
			System.exit(0);
		}
		else if (input.equals("help") ) {
			Commands();
		}
		else if (input.equals("about") ) {
			openAbout();
		}
		else if (input.equals("clear") ) {
			// Testing.append("Console cleared\n");
			Console.setText("");
		}
		else if (input.equals("manual") ) {

			try {
				Risk.openDocs( resbundle.getString("helpfiles.swing") );
			}
			catch(Exception e) {
				addOutput("Unable to open manual: "+e.getMessage() );
			}

		}
		else {

			go(input);

		}

	}

	public void addOutput(String output) {

		Console.append(output + System.getProperty("line.separator") );

		Console.setCaretPosition(Console.getDocument().getLength());

	}

	public void blockInput() {

		statusBar.setText(resbundle.getString("swing.status.working"));
		Submit.setEnabled(false);
		Command.setEnabled(false);

	}

	public void getInput() {

		Submit.setEnabled(true);
		Command.setEnabled(true);
		Command.requestFocus();
		statusBar.setText(resbundle.getString("swing.status.doneready"));

	}

	public void setVisible(boolean v) {

		super.setVisible(v);

		Command.requestFocus(); // does not work too well

	}


	public JToolBar getToolBar() {

		return toolbarCon;

	}
	public JMenu getMenu() {

		return cConsole;

	}

	public void actionPerformed(ActionEvent a) {

		if (a.getActionCommand().equals("read command")) {

			String input = Command.getText();
			Command.setText("");

			history.add(input);
			pointer = history.size()-1;
			cgo(input);

		}
		else if (a.getActionCommand().equals("run script")) {

			JFileChooser fc = new JFileChooser();
			RiskFileFilter filter = new RiskFileFilter(RiskFileFilter.RISK_SCRIPT_FILES);
			fc.setFileFilter(filter);

			int returnVal = fc.showOpenDialog( Risk.findParentFrame(this) );
			if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
				java.io.File file = fc.getSelectedFile();
				// Write your code here what to do with selected file

				try {
					// Testing.append("Opening file: "+ file.getPath() +"\n");
					// Testing.append("Running Script...\n");

					FileReader filein = new FileReader(file);
					BufferedReader bufferin = new BufferedReader(filein);

					String input = bufferin.readLine();
					while(input != null) {

						go(input);
						input = bufferin.readLine();

					}
					bufferin.close();
					// Testing.append("Script end\n");

				}
				catch(Exception error) {
					// Testing.append("Error: "+error.getMessage() + "\n");
				}

			} else {
				// Write your code here what to do if user has canceled Open dialog
			}

		}
		else if (a.getActionCommand().equals("save console")) {

			saveLog(Console);

		}
		else if (a.getActionCommand().equals("clear console")) {

			Console.setText("");

		}
		else if (a.getActionCommand().equals("clear history")) {

			history.clear();
			pointer = -1;

		}
		else {

			SwingGUIPanel.this.actionPerformed(a);

		}
	}
}


class GameTab extends JPanel implements SwingGUITab, ActionListener {

	private JPanel guiGame;

	private JLabel gameStatus;
	private JLabel Pix;

	private JMenu gGame;
	private JToolBar toolbarGUI;

	private boolean serverOn;

	private JButton gNewGame;
	private JButton gLoadGame;
	private JButton gSaveGame;
	private JButton gCloseGame;

	private JButton gJoinGame;
	private JButton gStartServer;

	private JButton gOptions;
	private JMenuItem gmOptions;
	private JMenuItem gmReplay;

	private JMenuItem gmStartServer;
	private JMenuItem gmJoinGame;

	private JMenuItem gmNewGame;
	private JMenuItem gmLoadGame;
	private JMenuItem gmSaveGame;
	private JMenuItem gmCloseGame;

	public JMenu getMenu() {

		return gGame;
	}

	public JToolBar getToolBar() {

		return toolbarGUI;

	}

	public GameTab() {


		setName( resbundle.getString("swing.tab.game") );


		serverOn=false;


		// ################### GUI #######################



		gameStatus = new JLabel("");
		Pix = new JLabel(new javax.swing.ImageIcon(  this.getClass().getResource("about.jpg") ));

		setLayout(new java.awt.BorderLayout());

		add(Pix, java.awt.BorderLayout.CENTER );

		add(gameStatus, java.awt.BorderLayout.SOUTH);



		javax.swing.border.Border goodBorder = BorderFactory.createLoweredBevelBorder(); // Con.getBorder();
		gameStatus.setBorder( goodBorder );

		mapPic = new JLabel();
		mapPic.setBorder( goodBorder );


		Dimension size = new Dimension(203,127);

		mapPic.setPreferredSize(size);
		mapPic.setMinimumSize(size);
		mapPic.setMaximumSize(size);


		showMission = new JButton(resbundle.getString("swing.button.mission"));
		showMission.setToolTipText(resbundle.getString("game.button.mission"));

		showCards = new JButton(resbundle.getString("swing.button.cards"));
		showCards.setToolTipText(resbundle.getString("game.button.cards"));

		Undo = new JButton(resbundle.getString("swing.button.undo"));
		Undo.setToolTipText(resbundle.getString("game.button.undo"));

		moveNumber = new JSlider();
		slider = new JSlider();

		armies = new JLabel();

		toolbarGUI = new JToolBar();
		toolbarGUI.setRollover(true);



		autoplace			= new JButton(resbundle.getString("game.button.go.autoplace"));

		guiSetup = new SetupPanel();
		guiGame = new GamePanel();

		guiSetup.setOpaque(false);
		guiGame.setOpaque(false);





		// make toolbar

		gNewGame		= new JButton(resbundle.getString("swing.menu.new"));
		gLoadGame		= new JButton(resbundle.getString("swing.menu.load"));
		gSaveGame		= new JButton(resbundle.getString("swing.menu.save"));
		gCloseGame		= new JButton(resbundle.getString("swing.menu.close"));

		gOptions		= new JButton(resbundle.getString("swing.menu.options"));

		gJoinGame		= new JButton(resbundle.getString("swing.menu.joingame"));
		gStartServer		= new JButton(resbundle.getString("swing.menu.startserver"));

		JButton gManual		= new JButton(resbundle.getString("swing.menu.manual"));
		JButton gAbout		= new JButton(resbundle.getString("swing.menu.about"));
		JButton gQuit		= new JButton(resbundle.getString("swing.menu.quit"));

		gNewGame.setActionCommand("new game");
		gNewGame.addActionListener( this );
		gLoadGame.setActionCommand("load game");
		gLoadGame.addActionListener( this );
		gSaveGame.setActionCommand("save game");
		gSaveGame.addActionListener( this );
		gCloseGame.setActionCommand("close game");
		gCloseGame.addActionListener( this );
		gOptions.setActionCommand("options");
		gOptions.addActionListener( this );
		gManual.setActionCommand("manual");
		gManual.addActionListener( this );
		gAbout.setActionCommand("about");
		gAbout.addActionListener( this );
		gQuit.setActionCommand("quit");
		gQuit.addActionListener( this );

		gStartServer.setActionCommand("start server");
		gStartServer.addActionListener( this );
		gJoinGame.setActionCommand("join game");
		gJoinGame.addActionListener( this );



		toolbarGUI.add(gNewGame);
		toolbarGUI.add(gLoadGame);
		toolbarGUI.add(gSaveGame);
		toolbarGUI.add(gCloseGame);
		toolbarGUI.addSeparator();
		toolbarGUI.add(gJoinGame);
		toolbarGUI.add(gStartServer);
		toolbarGUI.addSeparator();
		toolbarGUI.add(gOptions);
		toolbarGUI.add(gManual);
		toolbarGUI.add(gAbout);
		if (Risk.applet==null) { toolbarGUI.add(gQuit); }

		toolbarGUI.setFloatable(false);






		// create Game menu item
		gGame = new JMenu(resbundle.getString("swing.menu.game"));
		gGame.setMnemonic('G');

		gmNewGame = new JMenuItem(resbundle.getString("swing.menu.new"));
		gmNewGame.setMnemonic('N');
		gmNewGame.setActionCommand("new game");
		gmNewGame.addActionListener( this );
		gGame.add(gmNewGame);

		gmLoadGame = new JMenuItem(resbundle.getString("swing.menu.load"));
		gmLoadGame.setMnemonic('L');
		gmLoadGame.setActionCommand("load game");
		gmLoadGame.addActionListener( this );
		gGame.add(gmLoadGame);

		gmSaveGame = new JMenuItem(resbundle.getString("swing.menu.save"));
		gmSaveGame.setMnemonic('S');
		gmSaveGame.setActionCommand("save game");
		gmSaveGame.addActionListener( this );
		gGame.add(gmSaveGame);

		gmCloseGame = new JMenuItem(resbundle.getString("swing.menu.close"));
		gmCloseGame.setMnemonic('C');
		gmCloseGame.setActionCommand("close game");
		gmCloseGame.addActionListener( this );
		gGame.add(gmCloseGame);

		gGame.addSeparator();

		gmJoinGame = new JMenuItem(resbundle.getString("swing.menu.joingame"));
		gmJoinGame.setMnemonic('J');
		gmJoinGame.setActionCommand("join game");
		gmJoinGame.addActionListener( this );
		gGame.add(gmJoinGame);

		gmStartServer = new JMenuItem(resbundle.getString("swing.menu.startserver"));
		gmStartServer.setMnemonic('V');
		gmStartServer.setActionCommand("start server");
		gmStartServer.addActionListener( this );
		gGame.add(gmStartServer);

		gGame.addSeparator();

		gmOptions = new JMenuItem(resbundle.getString("swing.menu.options"));
		gmOptions.setMnemonic('O');
		gmOptions.setActionCommand("options");
		gmOptions.addActionListener( this );
		gGame.add(gmOptions);

		gmReplay = new JMenuItem("Replay");
		gmReplay.setMnemonic('R');
		gmReplay.setActionCommand("replay");
		gmReplay.addActionListener( this );
		gGame.add(gmReplay);

		if (Risk.applet==null) {

		gGame.addSeparator();

			JMenuItem gmQuit = new JMenuItem(resbundle.getString("swing.menu.quit"));
			gmQuit.setMnemonic('Q');
			gmQuit.setActionCommand("quit");
			gmQuit.addActionListener( this );
			gGame.add(gmQuit);

		}





		roll1 = new JButton(resbundle.getString("swing.dice.roll1"));
		roll2 = new JButton(resbundle.getString("swing.dice.roll2"));
		roll3 = new JButton(resbundle.getString("swing.dice.roll3"));

		roll1.setActionCommand("roll 1");
		roll2.setActionCommand("roll 2");
		roll3.setActionCommand("roll 3");

		roll1.addActionListener(this);
		roll2.addActionListener(this);
		roll3.addActionListener(this);


		gOptions.setEnabled(false);
		gmOptions.setEnabled(false);
		gmReplay.setEnabled(false);

		gSaveGame.setEnabled(false);
		gCloseGame.setEnabled(false);

		gmSaveGame.setEnabled(false);
		gmCloseGame.setEnabled(false);

		setOpaque(false);


	}

	public void actionPerformed(ActionEvent a) {

		if (a.getActionCommand().equals("roll 1")) {

			go("roll 1");

		}
		else if (a.getActionCommand().equals("roll 2")) {

			go("roll 2");

		}
		else if (a.getActionCommand().equals("roll 3")) {

			go("roll 3");

		}
		else if (a.getActionCommand().equals("join game")) {

			String result = JOptionPane.showInputDialog(Risk.findParentFrame(this), "type the server name", myrisk.getRiskConfig("default.host") );
			if (result!=null) { go("join "+result); }

		}
		else if (a.getActionCommand().equals("new game")) {

			SwingGUIPanel.this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

			go("newgame");

		}
		else if (a.getActionCommand().equals("load game")) {

			JFileChooser fc = new JFileChooser("saves/");
			RiskFileFilter filter = new RiskFileFilter(RiskFileFilter.RISK_SAVE_FILES);
			fc.setFileFilter(filter);

			int returnVal = fc.showOpenDialog( Risk.findParentFrame(this) );
			if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
				java.io.File file = fc.getSelectedFile();
				// Write your code here what to do with selected file

				go("loadgame " + file.getAbsolutePath() );

			} else {
				// Write your code here what to do if user has canceled Open dialog
			}
		}
		else if (a.getActionCommand().equals("save game")) {

			JFileChooser fc = new JFileChooser("saves/");
			RiskFileFilter filter = new RiskFileFilter(RiskFileFilter.RISK_SAVE_FILES);
			fc.setFileFilter(filter);

			int returnVal = fc.showSaveDialog( Risk.findParentFrame(this) );
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				java.io.File file = fc.getSelectedFile();
				// Write your code here what to do with selected file

				String fileName = file.getAbsolutePath();

				if (!(fileName.endsWith( "." + RiskFileFilter.RISK_SAVE_FILES ))) {
					fileName = fileName + "." + RiskFileFilter.RISK_SAVE_FILES;
				}

				go("savegame " + fileName );

			} else {
				// Write your code here what to do if user has canceled Save dialog
			}

		}
		else if (a.getActionCommand().equals("close game")) {


			//if (localGame) {
				go("closegame");
			//}
			//else {
			//	go("leave");
			//}
		}
		else if (a.getActionCommand().equals("options")) {

			Object[] message = new Object[2];
			message[0] = new JCheckBox("Auto End Go");
			message[1] = new JCheckBox("Auto Defend");


			((JCheckBox)message[0]).setSelected( myrisk.getAutoEndGo() );
			((JCheckBox)message[1]).setSelected( myrisk.getAutoDefend() );

			String[] options = {
			    "OK", 
			    "cancel"
			}; 

			int result = JOptionPane.showOptionDialog( 
			    this,                             // the parent that the dialog blocks 
			    message,                                    // the dialog message array 
			    "Options", // the title of the dialog window 
			    JOptionPane.OK_CANCEL_OPTION,                 // option type 
			    JOptionPane.PLAIN_MESSAGE,            // message type 
			    null,                                       // optional icon, use null to use the default icon 
			    options,                                    // options string array, will be made into buttons 
			    options[0]                                  // option that should be made into a default button 
			);

			if (result == JOptionPane.OK_OPTION ) {

				myrisk.parser( "autoendgo " + ((((JCheckBox)message[0]).isSelected()) ? "on" : "off") );

				myrisk.parser( "autodefend " + ((((JCheckBox)message[1]).isSelected()) ? "on" : "off") );

			}
/*

			Frame frame = Risk.findParentFrame(this);

			OptionsDialog optionsDialog = new OptionsDialog( frame , true, myrisk);
			Dimension frameSize = frame.getSize();
			Dimension optionsSize = optionsDialog.getPreferredSize();
			int x = frame.getLocation().x + (frameSize.width - optionsSize.width) / 2;
			int y = frame.getLocation().y + (frameSize.height - optionsSize.height) / 2;
			if (x < 0) x = 0;
			if (y < 0) y = 0;
			optionsDialog.setLocation(x, y);
			optionsDialog.setVisible(true);
*/
		}
		else if (a.getActionCommand().equals("replay")) {

			go("replay");

		}
		else if (a.getActionCommand().equals("start server")) {

			if (serverOn) {
				go("killserver");
			}
			else {
				go("startserver");
			}


		}
		else {

			SwingGUIPanel.this.actionPerformed(a);

		}

	}

	public void blockInput() {

		gSaveGame.setEnabled(false);
		gmSaveGame.setEnabled(false);

		showMission.setEnabled(false);
		showCards.setEnabled(false);
		Undo.setEnabled(false);

		gOptions.setEnabled(false);
		gmOptions.setEnabled(false);
		gmReplay.setEnabled(false);

	}

	public void getInput() {

		if (localGame) {
			gSaveGame.setEnabled(true);
			gmSaveGame.setEnabled(true);
			Undo.setEnabled(true);
			gmReplay.setEnabled(true);
		}

		showMission.setEnabled(true);
		showCards.setEnabled(true);

		gOptions.setEnabled(true);
		gmOptions.setEnabled(true);
	}

	public void newGame() {

			gNewGame.setEnabled(false);
			gLoadGame.setEnabled(false);
			//gSaveGame.setEnabled(true);
			gCloseGame.setEnabled(true);

			gmNewGame.setEnabled(false);
			gmLoadGame.setEnabled(false);
			gmStartServer.setEnabled(false);
			gmJoinGame.setEnabled(false);

			gStartServer.setEnabled(false);
			gJoinGame.setEnabled(false);

			//gmSaveGame.setEnabled(true);
			gmCloseGame.setEnabled(true);

			remove(Pix);
			remove(guiGame);

			guiSetup.setupGame();

			add(guiSetup, java.awt.BorderLayout.CENTER );

			SwingGUIPanel.this.setCursor(null); // Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)
	}

	public void closeGame() {


			gNewGame.setEnabled(true);
			gLoadGame.setEnabled(true);
			gSaveGame.setEnabled(false);
			gCloseGame.setEnabled(false);

			gmNewGame.setEnabled(true);
			gmLoadGame.setEnabled(true);
			gmStartServer.setEnabled(true);
			gmJoinGame.setEnabled(true);

			gStartServer.setEnabled(true);
			gJoinGame.setEnabled(true);

			gmSaveGame.setEnabled(false);
			gmCloseGame.setEnabled(false);

			gOptions.setEnabled(false);
			gmOptions.setEnabled(false);
			gmReplay.setEnabled(false);

			remove(guiGame);
			remove(guiSetup);

			add(Pix, java.awt.BorderLayout.CENTER );

	}

	public void startGame() {

			gNewGame.setEnabled(false);
			gLoadGame.setEnabled(false);
			//gSaveGame.setEnabled(true);
			gCloseGame.setEnabled(true);

			gmNewGame.setEnabled(false);
			gmLoadGame.setEnabled(false);
			gmStartServer.setEnabled(false);
			gmJoinGame.setEnabled(false);

			gStartServer.setEnabled(false);
			gJoinGame.setEnabled(false);

			//gmSaveGame.setEnabled(true);
			gmCloseGame.setEnabled(true);

			remove(Pix);
			remove(guiSetup);

			add(guiGame, java.awt.BorderLayout.CENTER );

	}

	public void serverState(boolean s) {


			serverOn=s;

			if (serverOn) {

				gmStartServer.setText(resbundle.getString("swing.menu.stopserver"));
				gStartServer.setText(resbundle.getString("swing.menu.stopserver"));

			}
			else {

				gmStartServer.setText(resbundle.getString("swing.menu.startserver"));
				gStartServer.setText(resbundle.getString("swing.menu.startserver"));

			}

	}

	public void setGameStatus(String state) {

		gameStatus.setText(" "+state);

	}

}

class DebugTab extends JSplitPane implements SwingGUITab,ActionListener {

	private JTextArea debugText;
	private JTextArea errText;

	private JToolBar toolbarDebug;
	private JMenu mDebug;

	public JToolBar getToolBar() {

		return toolbarDebug;

	}

	public JMenu getMenu() {

		return mDebug;

	}

	public void sendDebug(String a) {

			debugText.append(a + System.getProperty("line.separator") );

			debugText.setCaretPosition(debugText.getDocument().getLength());

	}

	public DebugTab() {
		super(JSplitPane.HORIZONTAL_SPLIT);

		setName("Debug");

		//##################### Debug ####################


		toolbarDebug = new JToolBar();
		toolbarDebug.setRollover(true);

		JButton tdSaveDebug  = new JButton("Save Debug Log");
		JButton tdPlayDebug  = new JButton("Play Debug Log");
		JButton tdClearDebug = new JButton("Clear Debug Log");
		JButton tdSaveError = new JButton("Save Error Log");
		JButton sendError = new JButton("Send Error Log");
		JButton changeaiwait = new JButton("Change AI wait");

		tdSaveDebug.setActionCommand("save debug");
		tdPlayDebug.setActionCommand("play debug");
		tdClearDebug.setActionCommand("clear debug");
		tdSaveError.setActionCommand("save error");
		sendError.setActionCommand("send error");
		changeaiwait.setActionCommand("aiwait");

		tdSaveDebug.addActionListener(this);
		tdPlayDebug.addActionListener(this);
		tdClearDebug.addActionListener(this);
		tdSaveError.addActionListener(this);
		sendError.addActionListener(this);
		changeaiwait.addActionListener(this);

		toolbarDebug.add(tdSaveDebug);
		toolbarDebug.add(tdPlayDebug);
		toolbarDebug.add(tdClearDebug);
		toolbarDebug.addSeparator();
		toolbarDebug.add(tdSaveError);
		toolbarDebug.add(sendError);
		toolbarDebug.addSeparator();
		toolbarDebug.add(changeaiwait);

		toolbarDebug.setFloatable(false);





		mDebug = new JMenu("Debug");
		mDebug.setMnemonic('D');

		JMenuItem dSave = new JMenuItem("Save Debug Log");
		dSave.setMnemonic('S');
		dSave.setActionCommand("save debug");
		dSave.addActionListener( this );
		mDebug.add(dSave);

		JMenuItem dPlay = new JMenuItem("Play Debug Log");
		dPlay.setMnemonic('P');
		dPlay.setActionCommand("play debug");
		dPlay.addActionListener( this );
		mDebug.add(dPlay);

		JMenuItem dClear = new JMenuItem("Clear Debug Log");
		dClear.setMnemonic('C');
		dClear.setActionCommand("clear debug");
		dClear.addActionListener( this );
		mDebug.add(dClear);

		mDebug.addSeparator();

		JMenuItem dSaveErr = new JMenuItem("Save Error Log");
		dSaveErr.setMnemonic('E');
		dSaveErr.setActionCommand("save error");
		dSaveErr.addActionListener( this );
		mDebug.add(dSaveErr);

		JMenuItem send = new JMenuItem("Send Error Log");
		send.setMnemonic('S');
		send.setActionCommand("send error");
		send.addActionListener( this );
		mDebug.add(send);

		mDebug.addSeparator();

		JMenuItem aiwait = new JMenuItem("Change AI wait");
		aiwait.setMnemonic('A');
		aiwait.setActionCommand("aiwait");
		aiwait.addActionListener( this );
		mDebug.add(aiwait);






		debugText = new JTextArea();
		debugText.setEditable(false);

		JScrollPane debugScroll = new JScrollPane(debugText);
		debugScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		//debugScroll.setBorder( new javax.swing.border.TitledBorder( goodBorder, "Debug Log", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP ) );

		JPanel debugPanel = new JPanel( new BorderLayout() );
		debugPanel.add( new JLabel("  Debug Log"), BorderLayout.NORTH );
		debugPanel.add(debugScroll);

		// ####### err

		errText = new JTextArea();
		errText.setEditable(false);

		PrintStream ps = SimplePrintStream.getSimplePrintStream(

		    new StringWriter() {
			public void write(String x) {
			    errText.append(x);

			}
		    }

		);

		if (Risk.applet == null) {

			// catch everything in my PrintStream
			System.setOut(ps);
			System.setErr(ps);

		}

		JScrollPane errScroll = new JScrollPane( errText );
		//errScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		//errScroll.setBorder( BorderFactory.createTitledBorder( BorderFactory.createLoweredBevelBorder() , "Error Log", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP ) );

		JPanel errPanel = new JPanel( new BorderLayout() );
		errPanel.add( new JLabel("  Error Log"), BorderLayout.NORTH );
		errPanel.add(errScroll);

		// ######## split

		debugPanel.setOpaque(false);
		errPanel.setOpaque(false);

		setLeftComponent(debugPanel);
		setRightComponent(errPanel);

		setContinuousLayout(true); 
		setOneTouchExpandable(true); 
		setDividerLocation(400);
		setBorder( BorderFactory.createEmptyBorder() );

		setOpaque(false);

	}

	public void actionPerformed(ActionEvent a) {

		if (a.getActionCommand().equals("play debug")) {

			JFileChooser fc = new JFileChooser();
			RiskFileFilter filter = new RiskFileFilter(RiskFileFilter.RISK_LOG_FILES);
			fc.setFileFilter(filter);

			int returnVal = fc.showOpenDialog( Risk.findParentFrame(this) );
			if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
				java.io.File file = fc.getSelectedFile();
				// Write your code here what to do with selected file

				String fileName = file.getAbsolutePath();

				go("newgame");
				go("play " + fileName );

			} else {
				// Write your code here what to do if user has canceled Open dialog
			}

		}
		else if (a.getActionCommand().equals("save debug")) {

			saveLog(debugText);

		}
		else if (a.getActionCommand().equals("clear debug")) {

			debugText.setText("");

		}
		else if (a.getActionCommand().equals("save error")) {

			saveLog(errText);

		}

		else if (a.getActionCommand().equals("send error")) {

						String email = JOptionPane.showInputDialog(this,"tell me your e-mail please");

						if (email == null) { email ="none"; }

						try {

							Risk.sendText(email , debugText.getText() +"\n\nRisk version: "+Risk.RISK_VERSION+"\n\n"+ errText.getText() );

							JOptionPane.showMessageDialog(this, "SENT!");


						}
						catch(Exception ex) {

							showError("unable to send: "+ex.getMessage() );

						}


		}
		else if (a.getActionCommand().equals("aiwait")) {

			Object[] message = new Object[2];
			message[0] = new JLabel("AI wait time (in milliseconds):");
			message[1] = new JSpinner( new SpinnerNumberModel( AIPlayer.getWait(),0,10000,100 ) );

			String[] options = {
			    "OK", 
			    "cancel"
			}; 

			int result = JOptionPane.showOptionDialog( 
			    this,                             // the parent that the dialog blocks 
			    message,                                    // the dialog message array 
			    "AI Options", // the title of the dialog window 
			    JOptionPane.OK_CANCEL_OPTION,                 // option type 
			    JOptionPane.PLAIN_MESSAGE,            // message type 
			    null,                                       // optional icon, use null to use the default icon 
			    options,                                    // options string array, will be made into buttons 
			    options[0]                                  // option that should be made into a default button 
			);

			if (result == JOptionPane.OK_OPTION ) {

				AIPlayer.setWait( ((Integer)((JSpinner)message[1]).getValue()).intValue() );
			}

		}

		else {

			System.out.print("command \""+a.getActionCommand()+"\" is not implemented yet\n");

		}
	}
}


class StatisticsTab extends JPanel implements SwingGUITab,ActionListener {

	private JToolBar toolbarStat;
	private JMenu sStatistics;
	private StatsPanel graph;

	private AbstractButton[] statbuttons;

	public JToolBar getToolBar() {

		return toolbarStat;
	}

	public JMenu getMenu() {

		return sStatistics;
	}

	public void actionPerformed(ActionEvent a) {

		graph.repaintStats( Integer.parseInt( a.getActionCommand() ) );
		graph.repaint();
	}

	public StatisticsTab() {

		setName( resbundle.getString("swing.tab.statistics") );

		//##################### graph ####################



		toolbarStat = new JToolBar();
		toolbarStat.setRollover(true);

		toolbarStat.setFloatable(false);



		graph = new StatsPanel(myrisk);
		graph.setBorder( BorderFactory.createLoweredBevelBorder() );



		statbuttons = new AbstractButton[24];



		statbuttons[0]=new JButton(resbundle.getString("swing.toolbar.countries"));
		statbuttons[1]=new JButton(resbundle.getString("swing.toolbar.armies"));
		statbuttons[2]=new JButton(resbundle.getString("swing.toolbar.kills"));
		statbuttons[3]=new JButton(resbundle.getString("swing.toolbar.casualties"));
		statbuttons[4]=new JButton(resbundle.getString("swing.toolbar.reinforcements"));
		statbuttons[5]=new JButton(resbundle.getString("swing.toolbar.continents"));
		statbuttons[6]=new JButton(resbundle.getString("swing.toolbar.empire"));
		statbuttons[7]=new JButton(resbundle.getString("swing.toolbar.attacks"));
		statbuttons[8]=new JButton(resbundle.getString("swing.toolbar.retreats"));
		statbuttons[9]=new JButton(resbundle.getString("swing.toolbar.victories"));
		statbuttons[10]=new JButton(resbundle.getString("swing.toolbar.defeats"));
		statbuttons[11]=new JButton(resbundle.getString("swing.toolbar.attacked"));

		for (int a=0; a<12; a++) {

			statbuttons[a].setActionCommand( (a+1)+"" );
			statbuttons[a].addActionListener(this);
			toolbarStat.add(statbuttons[a]);
			statbuttons[a].setEnabled(false);

		}




		// create Statistics menu item
		sStatistics = new JMenu( resbundle.getString("swing.tab.statistics") );
		sStatistics.setMnemonic('S');

		statbuttons[12]=new JMenuItem(resbundle.getString("swing.toolbar.countries"));
		statbuttons[13]=new JMenuItem(resbundle.getString("swing.toolbar.armies"));
		statbuttons[14]=new JMenuItem(resbundle.getString("swing.toolbar.kills"));
		statbuttons[15]=new JMenuItem(resbundle.getString("swing.toolbar.casualties"));
		statbuttons[16]=new JMenuItem(resbundle.getString("swing.toolbar.reinforcements"));
		statbuttons[17]=new JMenuItem(resbundle.getString("swing.toolbar.continents"));
		statbuttons[18]=new JMenuItem(resbundle.getString("swing.toolbar.empire"));
		statbuttons[19]=new JMenuItem(resbundle.getString("swing.toolbar.attacks"));
		statbuttons[20]=new JMenuItem(resbundle.getString("swing.toolbar.retreats"));
		statbuttons[21]=new JMenuItem(resbundle.getString("swing.toolbar.victories"));
		statbuttons[22]=new JMenuItem(resbundle.getString("swing.toolbar.defeats"));
		statbuttons[23]=new JMenuItem(resbundle.getString("swing.toolbar.attacked"));


		for (int a=12; a<statbuttons.length; a++) {

			statbuttons[a].setActionCommand( (a-11)+"" );
			statbuttons[a].addActionListener(this);
			sStatistics.add(statbuttons[a]);
			statbuttons[a].setEnabled(false);

		}

		setLayout( new BorderLayout() );
		add(graph);

	}

	public void startGame() {

			for (int a=0; a<statbuttons.length; a++) {

				statbuttons[a].setEnabled(true);

			}

	}

	public void closeGame() {



			for (int a=0; a<statbuttons.length; a++) {

				statbuttons[a].setEnabled(false);

			}

	}

}



	public void saveLog(JTextArea textArea) {



			JFileChooser fc = new JFileChooser();
			RiskFileFilter filter = new RiskFileFilter(RiskFileFilter.RISK_LOG_FILES);
			fc.setFileFilter(filter);

			int returnVal = fc.showSaveDialog( Risk.findParentFrame(this) );
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				java.io.File file = fc.getSelectedFile();
				// Write your code here what to do with selected file

				String fileName = file.getAbsolutePath();

				if (!(fileName.endsWith( "." + RiskFileFilter.RISK_LOG_FILES ))) {
					fileName = fileName + "." + RiskFileFilter.RISK_LOG_FILES;
				}

				try {

					FileWriter fileout = new FileWriter(fileName);
					BufferedWriter buffer = new BufferedWriter(fileout);
					PrintWriter printer = new PrintWriter(buffer);

					printer.write(textArea.getText());

					printer.close();

				}

				catch(Exception error) {
					showError( error.getMessage() );
				}

			} else {
				// Write your code here what to do if user has canceled Save dialog
			}


	}

	/**
	 * Submits input to parser if neccessary
	 * @param input The string that is checked
	 */
	public void go(String input) {

		pp.setHighLight(255);
		// Testing.append("Submitted: \""+input+"\"\n");

		if (gameState!=2 || !myrisk.getGame().getSetup() ) { blockInput(); }

		myrisk.parser(input);

		// Console.setCaretPosition(Console.getDocument().getLength());

	}

	/**
	 * Blocks the game panel
	 */
	public void blockInput() {

		gameState= -1;

		inGameCards.show(inGameInput, "nothing");

		gameTab.blockInput();
		consoleTab.blockInput();

	}

	/**
	 * This reads in a file for the commands
	 */
	public void Commands() {

		String commands="";

		try {

			URL url;

			if (Risk.applet==null) {

				url = (new File("commands.txt")).toURI().toURL();

			}
			else {

				url = new URL( risk.engine.Risk.applet.getCodeBase() , "commands.txt" );

			}

			BufferedReader bufferin=new BufferedReader(new InputStreamReader(url.openStream()));

			String input = bufferin.readLine();
			while(input != null) {
				if (commands.equals("")) { commands = input; }
				else { commands = commands + "\n" + input; }
				input = bufferin.readLine();
			}
			bufferin.close();
			// Testing.append("Commands Box opened\n");
			JOptionPane.showMessageDialog( Risk.findParentFrame(this) , commands, resbundle.getString("swing.message.commands"), JOptionPane.PLAIN_MESSAGE);
		}
		catch (Exception e) {
			showError("error with commands.txt file: "+e.getMessage() );
		}


	}

	/**
	 * This opens the about dialog box
	 */
	public void openAbout() {

		Frame frame = Risk.findParentFrame(this);

		Risk.openAbout(frame,product, version);
	}

	/**
	 * This opens the about dialog box
	 */
	public void openCards() {

		Frame frame = Risk.findParentFrame(this);

		CardsDialog cardsDialog = new CardsDialog( Risk.findParentFrame(this) ,pp, true, myrisk , (gameState==1) );
		Dimension frameSize = frame.getSize();
		Dimension aboutSize = cardsDialog.getPreferredSize();
		int x = frame.getLocation().x + (frameSize.width - aboutSize.width) / 2;
		int y = frame.getLocation().y + (frameSize.height - aboutSize.height) / 2;
		if (x < 0) x = 0;
		if (y < 0) y = 0;
		cardsDialog.setLocation(x, y);
		cardsDialog.setVisible(true);
	}

	public void showQuestion(int n) {


		moveNumber.setMaximum(n);
		moveNumber.setMinimum(1);
		moveNumber.setValue(1);



		String[] options = {
			resbundle.getString("swing.move.move"),
			resbundle.getString("swing.move.moveall"),
			resbundle.getString("swing.move.cancel")
		};

		int a = JOptionPane.showOptionDialog(
				Risk.findParentFrame(this),                            // the parent that the dialog blocks
				moveNumber,                      // the dialog message array
				resbundle.getString("swing.move.title"), // the title of the dialog window
				JOptionPane.DEFAULT_OPTION,      // option type
				JOptionPane.QUESTION_MESSAGE,    // message type
				null,                            // optional icon, use null to use the default icon
				options,                         // options string array, will be made into buttons
				options[0]                       // option that should be made into a default button
		);

		if (a==0) {
			go("movearmies " + pp.getC1() + " " + pp.getC2() + " "+ moveNumber.getValue() );
		}
		if (a==1) {
			go("movearmies " + pp.getC1() + " " + pp.getC2() + " "+ n );
		}

	}

	public void showError(String error) {
		JOptionPane.showMessageDialog(Risk.findParentFrame(this), resbundle.getString("swing.message.error") + " " + error, resbundle.getString("swing.title.error"), JOptionPane.ERROR_MESSAGE);
	}

	public void showMission(String mission) {
		JOptionPane.showMessageDialog(Risk.findParentFrame(this), resbundle.getString("swing.message.mission") + " " + mission, resbundle.getString("swing.title.mission"), JOptionPane.INFORMATION_MESSAGE);
	}

	public void pprepaintCountries() {

		String tmp = (String)mapViewComboBox.getSelectedItem();
		int newview = -1;

		if (tmp.equals(resbundle.getString("game.tabs.continents")))           { newview=PicturePanel.VIEW_CONTINENTS; }
		else if (tmp.equals(resbundle.getString("game.tabs.ownership")))       { newview=PicturePanel.VIEW_OWNERSHIP; }
		else if (tmp.equals(resbundle.getString("game.tabs.borderthreat")))    { newview=PicturePanel.VIEW_BORDER_THREAT; }
		else if (tmp.equals(resbundle.getString("game.tabs.cardownership")))   { newview=PicturePanel.VIEW_CARD_OWNERSHIP; }
		else if (tmp.equals(resbundle.getString("game.tabs.troopstrength")))   { newview=PicturePanel.VIEW_TROOP_STRENGTH; }
		else if (tmp.equals(resbundle.getString("game.tabs.connectedempire"))) { newview=PicturePanel.VIEW_CONNECTED_EMPIRE; }

		pp.repaintCountries( newview );

	}



	//############################################################################################################

// this get all the commands from the game and does what needs to be done
	class SwingRiskAdapter extends RiskAdapter {

		/**
		 * Checks if redrawing or repainting is needed
		 * @param output
		 * @param redrawNeeded If frame needs to be redrawn
		 * @param repaintNeeded If frame needs to be repainted
		 */
		public void sendMessage(String output, boolean redrawNeeded, boolean repaintNeeded) {

			// Testing.append("Returned: \""+output+"\"\n");

			consoleTab.addOutput(output);

			if (redrawNeeded) {
				pprepaintCountries();
			}
			if (repaintNeeded) {
				repaint();
			}
		}

		public void sendDebug(String a) {

			debugTab.sendDebug(a);

		}

		public void showMessageDialog(String a) {

			showError(a);

		}

		/**
		 * Blocks the game panel
		 */
		public void noInput() {

			blockInput();

		}

		/**
		 * checks if the the frame needs input
		 * @param s determines what needs input
		 */
		public void needInput(int s) {

			gameState=s;

			if ((gameState != -1) && (gameState != 0) && (gameState != 10) && (gameState != 4)) {

				gameTab.getInput();

			}

			if (gameState == 0) {
				inGameCards.show(inGameInput, "nothing");
			}
			else if (gameState == 1) {

				// after wiping out someone if you go into trade mode
				pp.setC1(255);
				pp.setC2(255);

				inGameCards.show(inGameInput, "tradeCards");
			}
			else if (gameState == 2) {
				inGameCards.show(inGameInput, "placeArmies");
			}
			else if (gameState == 3) {

				pp.setC1(255);
				pp.setC2(255);

				inGameCards.show(inGameInput, "attack");
			}
			else if (gameState == 4) {
				inGameCards.show(inGameInput, "roll");
			}
			else if (gameState == 5) {
				inGameCards.show(inGameInput, "move");
			}
			else if (gameState == 6) {
				inGameCards.show(inGameInput, "tacMove");
			}
			else if (gameState == 7) {
				inGameCards.show(inGameInput, "endgo");
			}
			else if (gameState == 8) {
				inGameCards.show(inGameInput, "winner");
			}
			else if (gameState == 9) {
				inGameCards.show(inGameInput, "capital");
			}
			else if (gameState == 10) {
				inGameCards.show(inGameInput, "defend");
			}

			consoleTab.getInput();

			repaint();

		}

		/**
		 * Displays a message
		 * @param state The message that is needed to be displayed
		 */
		public void setGameStatus(String state) {

			gameTab.setGameStatus(state);
		}

		public void showMapPic(Image p) {

			if (p!=null) {

				mapPic.setIcon( new ImageIcon(p) ); // SCALE_DEFAULT

			}
			else {

				mapPic.setIcon(null);

			}

			SwingGUIPanel.this.setCursor(null); // Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)

		}

		public void showCardsFile(String c, boolean m) {

			cardsFile.setText(c);

			if ( m==false && mission.isSelected() ) { domination.setSelected(true); AutoPlaceAll.setEnabled(true); }

			mission.setEnabled(m);

		}

		public void newGame(boolean t) { // t==true: this is a local game

			localGame = t;

			gameTab.newGame();

		}

		private boolean memoryload;

		/**
		 * Starts the game
		 * @param s If the game is a local game
		 */
		public void startGame(boolean s) {

			setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

			localGame = s;

			// check maybe we should load from memory
			if (myrisk.getGame().getMapFile() == null && myrisk.getGame().getCardsFile() == null) {

				pp.memoryLoad(editorTab.getImageMap(),editorTab.getImagePic());

			}
			else {

				try {
					pp.load();
				}
				catch(IOException e) {
					e.printStackTrace();
				}

			}



			// YURA: not sure why this needs to be here, used to work without it
			pprepaintCountries();

			gameTab.startGame();

			statisticsTab.startGame();




			SwingGUIPanel.this.setCursor(null); // Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)

		}

		/**
		 * Closes the game
		 */
		public void closeGame() {

			gameTab.closeGame();

			statisticsTab.closeGame();

			System.gc();

		}

		public void setSlider(int min, int c1num, int c2num) {

			int max = myrisk.hasArmiesInt( c1num ) -1;

			slider.setMaximum(max);
			slider.setMinimum(min);
			slider.setValue(min);

		}

		public void armiesLeft(int l, boolean s) {
			armies.setText( resbundle.getString("core.input.armiesleft").replaceAll("\\{0\\}", "" + l));

			if (s) {
				autoplace.setVisible(false);
			}
			else {
				autoplace.setVisible(true);
			}
		}

		public void showDice(int n, boolean w) {

			JPanel p;

			if (w) { p=roll; }
			else { p=defend; }

			p.remove(roll1);
			p.remove(roll2);
			p.remove(roll3);

			GridBagConstraints c = new GridBagConstraints();
			c.insets = new java.awt.Insets(3, 3, 3, 3);
			c.fill = GridBagConstraints.BOTH;

			if (n > 0) {

				c.gridx = 0; // col
				c.gridy = 0; // row
				c.gridwidth = 1; // width
				c.gridheight = 1; // height
				p.add(roll1, c);


				if (n > 1) {

					c.gridx = 1; // col
					c.gridy = 0; // row
					c.gridwidth = 1; // width
					c.gridheight = 1; // height
					p.add(roll2, c);


					if (n > 2) {

						c.gridx = 2; // col
						c.gridy = 0; // row
						c.gridwidth = 1; // width
						c.gridheight = 1; // height
						p.add(roll3, c);

					}

				}

			}

		}

		public void showDiceResults(int[] att, int[] def) {

			String output=resbundle.getString("core.dice.results");

			output = output + " " + resbundle.getString("core.dice.attacker");
			for (int c=0; c< att.length ; c++) {
				output = output + " " + (att[c]+1);
			}

			output = output + " " + resbundle.getString("core.dice.defender");
			for (int c=0; c< def.length ; c++) {
				output = output + " " + (def[c]+1);
			}

			resultsLabel.setText(output);

			inGameCards.show(inGameInput, "results");

		}

		public void closeBattle() {

			blockInput();

		}

		public void serverState(boolean s) {

			gameTab.serverState(s);

		}

		public void addPlayer(int t, String name, Color color, String ip) {

			guiSetup.addPlayer(t, name, color, ip);

		}

		public void delPlayer(String name) {

			guiSetup.delPlayer(name);

		}

/*  // other things that need to be done

public void openBattle(int c1num, int c2num) {}
public void setNODAttacker(int n) {}
public void setNODDefender(int n) {}

*/

	}

	//############################################################################################################

	class playersPanel extends JPanel {

		public void paintComponent(Graphics g) {

			Color[] colors = myrisk.getPlayerColors();

			for (int c=0; c < colors.length ; c++) {
				g.setColor( colors[c] );
				g.fillRect( ((120/colors.length) * c) , 0 , (120/colors.length) , 20);
			}

			g.setColor( Risk.getTextColorFor( colors[0] ) );

			g.drawRect( 2 , 2 , (120/colors.length)-5 , 15);

			g.setColor( Color.black );
			g.drawLine( (120/colors.length)-1 , 0, (120/colors.length)-1, 19);

		}

	}


	class GameOptionsPanel extends JPanel {

		public GameOptionsPanel() {

			this.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 10, 0));

			setOpaque(false);

			JLabel mapLookLabel = new JLabel(resbundle.getString("game.tabs.mapview") + ":");

			mapViewComboBox = new JComboBox();

			Dimension mapViewSize = new Dimension(120 , 20);

			mapViewComboBox.setPreferredSize(mapViewSize);
			mapViewComboBox.setMinimumSize(mapViewSize);
			mapViewComboBox.setMaximumSize(mapViewSize);

			mapViewComboBox.addItem(resbundle.getString("game.tabs.continents"));
			mapViewComboBox.addItem(resbundle.getString("game.tabs.ownership"));
			mapViewComboBox.addItem(resbundle.getString("game.tabs.borderthreat"));
			mapViewComboBox.addItem(resbundle.getString("game.tabs.cardownership"));
			mapViewComboBox.addItem(resbundle.getString("game.tabs.troopstrength"));
			mapViewComboBox.addItem(resbundle.getString("game.tabs.connectedempire"));

			mapViewComboBox.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {

							pprepaintCountries();

							pp.repaint();
						}
					}
			);

			JLabel playersLabel = new JLabel(resbundle.getString("newgame.label.players"));

			Dimension playerPanelSize = new Dimension(120 , 20);

			JPanel players = new playersPanel();

			players.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0,0,0),1));

			players.setPreferredSize(playerPanelSize);
			players.setMinimumSize(playerPanelSize);
			players.setMaximumSize(playerPanelSize);

			showMission.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {
							showMission( myrisk.getCurrentMission() );
						}
					}
			);

			showCards.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {
							openCards();
						}
					}
			);

			Undo.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {
							go("undo");
						}
					}
			);

			this.add(mapLookLabel);
			this.add(mapViewComboBox);
			this.add(playersLabel);
			this.add(players);
			this.add(showMission);
			this.add(showCards);
			this.add(Undo);

		}
	}

	class GamePanel extends JPanel implements MouseInputListener {

		public GamePanel() {

			resultsLabel = new JLabel("RESULTS");

			Dimension mapSize = new Dimension(PicturePanel.PP_X,PicturePanel.PP_Y);

			pp = new PicturePanel(myrisk);
			pp.setPreferredSize(mapSize);
			pp.setMinimumSize(mapSize);
			pp.setMaximumSize(mapSize);
			pp.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0,0,0),1));
			pp.addMouseListener(this);
			pp.addMouseMotionListener(this);

			Dimension gameOptionsSize = new Dimension(PicturePanel.PP_X,25);

			gameOptions = new GameOptionsPanel();
			gameOptions.setPreferredSize(gameOptionsSize);
			gameOptions.setMinimumSize(gameOptionsSize);
			gameOptions.setMaximumSize(gameOptionsSize);

			Dimension d = new Dimension(PicturePanel.PP_X , 50);

			inGameCards = new CardLayout();

			inGameInput = new JPanel();
			inGameInput.setLayout( inGameCards );
			inGameInput.setPreferredSize(d);
			inGameInput.setMinimumSize(d);
			inGameInput.setMaximumSize(d);

			JPanel nothing = new JPanel();
			nothing.setPreferredSize(d);
			nothing.setMinimumSize(d);
			nothing.setMaximumSize(d);
			nothing.add(new JLabel(resbundle.getString("game.pleasewaitnetwork")) );

			JPanel results = new JPanel();
			results.setPreferredSize(d);
			results.setMinimumSize(d);
			results.setMaximumSize(d);
			results.add(resultsLabel);

			JPanel placeArmies = new placeArmiesPanel();
			placeArmies.setPreferredSize(d);
			placeArmies.setMinimumSize(d);
			placeArmies.setMaximumSize(d);

			roll = new rollPanel();
			roll.setPreferredSize(d);
			roll.setMinimumSize(d);
			roll.setMaximumSize(d);

			JPanel move = new movePanel();
			move.setPreferredSize(d);
			move.setMinimumSize(d);
			move.setMaximumSize(d);

			JPanel attack = new attackPanel();
			attack.setPreferredSize(d);
			attack.setMinimumSize(d);
			attack.setMaximumSize(d);

			defend = new defendPanel();
			defend.setPreferredSize(d);
			defend.setMinimumSize(d);
			defend.setMaximumSize(d);

			JPanel tacMove = new tacMovePanel();
			tacMove.setPreferredSize(d);
			tacMove.setMinimumSize(d);
			tacMove.setMaximumSize(d);

			JPanel capital = new capitalPanel();
			capital.setPreferredSize(d);
			capital.setMinimumSize(d);
			capital.setMaximumSize(d);

			JPanel tradeCards = new tradeCardsPanel();
			tradeCards.setPreferredSize(d);
			tradeCards.setMinimumSize(d);
			tradeCards.setMaximumSize(d);

			JPanel winner = new winnerPanel();
			winner.setPreferredSize(d);
			winner.setMinimumSize(d);
			winner.setMaximumSize(d);

			JPanel endgo = new endgoPanel();
			endgo.setPreferredSize(d);
			endgo.setMinimumSize(d);
			endgo.setMaximumSize(d);

			inGameInput.setOpaque(false);

			nothing.setOpaque(false);
			placeArmies.setOpaque(false);
			roll.setOpaque(false);
			move.setOpaque(false);
			attack.setOpaque(false);
			defend.setOpaque(false);
			tacMove.setOpaque(false);
			capital.setOpaque(false);
			tradeCards.setOpaque(false);
			winner.setOpaque(false);
			endgo.setOpaque(false);
			results.setOpaque(false);

			inGameInput.add(nothing, "nothing");
			inGameInput.add(placeArmies, "placeArmies");
			inGameInput.add(roll, "roll");
			inGameInput.add(move, "move");
			inGameInput.add(attack, "attack");
			inGameInput.add(defend, "defend");
			inGameInput.add(tacMove, "tacMove");
			inGameInput.add(capital, "capital");
			inGameInput.add(tradeCards, "tradeCards");
			inGameInput.add(winner, "winner");
			inGameInput.add(endgo, "endgo");
			inGameInput.add(results, "results");

			// ################### IN GAME #######################
			this.setLayout(new java.awt.GridBagLayout());

			GridBagConstraints c = new GridBagConstraints();
			c.insets = new java.awt.Insets(3, 3, 3, 3);
			c.fill = GridBagConstraints.BOTH;

			c.gridx = 0; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(gameOptions, c);

			c.gridx = 0; // col
			c.gridy = 1; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(pp, c);

			c.gridx = 0; // col
			c.gridy = 2; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(inGameInput, c);



		}

		//**********************************************************************
		//                     MouseListener Interface
		//**********************************************************************

		public void mouseClicked(MouseEvent e) {
		}

		public void mouseEntered(MouseEvent e) {
		}

		public void mouseExited(MouseEvent e) {

			if (pp.getHighLight() != 255) {
				pp.setHighLight(255);
				pp.repaint();
			}

			e.consume();

		}

		public void mousePressed(MouseEvent e) {
		}

		public void mouseReleased(MouseEvent e) {

			if ((e.getX() < PicturePanel.PP_X) && (e.getY() < PicturePanel.PP_Y) && (e.getX() >= 0) && (e.getY() >= 0) ) {

				int pixColor = pp.getCountryNumber(e.getX(),e.getY());
				String name = myrisk.getCountryName( pixColor );

				if ( e.getModifiers() == java.awt.event.InputEvent.BUTTON1_MASK ) {

					// System.out.print(e.getX()+" "+e.getY()+" "+PicturePanel.PP_X+" "+PicturePanel.PP_Y+"\n"); // testing
					// Testing.append("Click (color: " + pixColor + ")\n");

					if (pixColor == 255 ) {

					}
					else if (gameState == 2) {
						if (myrisk.isOwnedCurrentPlayerInt(pixColor) && (autoplace.isVisible()==false || myrisk.hasArmiesInt(pixColor) == 0)) {
							go( "placearmies " + pixColor + " 1" );
						}
					}
					else if (gameState == 3) {
						if ( pixColor == c1Id ) {
							attacker.setText(resbundle.getString("game.note.selectattacker"));
							c1Id = -1;
							pp.setC1(255);
							pp.setC2(255);
						}
						else if ( myrisk.isOwnedCurrentPlayerInt(pixColor) && ( myrisk.hasArmiesInt(pixColor) > 1) ) {
							attacker.setText(resbundle.getString("game.note.attackerisseldefender").replaceAll( "\\{0\\}", myrisk.getCountryName( pixColor)));
							c1Id = pixColor;

							pp.setC1(pixColor);
							pp.setC2(255);

							pp.repaint();
						}
						else if ( !(myrisk.isOwnedCurrentPlayerInt(pixColor)) && c1Id != -1 && myrisk.canAttack( c1Id , pixColor) ) {
							pp.setC2(pixColor);

							go("attack " + c1Id + " " + pixColor);
							attacker.setText(resbundle.getString("game.note.selectattacker"));

							pp.repaint();
						}
					}
					else if (gameState == 6) {

						if ( !(country1.getText().equals("")) && (country2.getText().equals("")) && country1.getText().equals( name ) ) {
							country1.setText("");
							pp.setC1(255);
						}
						else if ( myrisk.isOwnedCurrentPlayerInt(pixColor) && (myrisk.hasArmiesInt(pixColor) > 1) && (country1.getText().equals("")) ) {
							country1.setText(name);
							pp.setC1( pixColor );
						}
						else if ( myrisk.isOwnedCurrentPlayerInt(pixColor) && !(country1.getText().equals("")) && (country2.getText().equals("")) && myrisk.canAttack( pp.getC1() , pixColor) ) { // country1.getText(),name
							country2.setText(name);
							pp.setC2( pixColor );
						}
						else if ( myrisk.isOwnedCurrentPlayerInt(pixColor) && !(country1.getText().equals("")) && !(country2.getText().equals(""))  && ( myrisk.hasArmiesInt(pixColor) > 1) ) {
							country1.setText(name);
							country2.setText("");
							pp.setC1( pixColor );
							pp.setC2(255);
						}

						pp.repaint();

					}
					else if (gameState == 9) {
						//go( "capital " + pixColor );

						c1Id = pixColor;
						pp.setC1(pixColor);

						pp.repaint();
					}
				}
				else if ( e.getModifiers() == java.awt.event.InputEvent.BUTTON3_MASK ) {

					if (pixColor == 255 ) {

					}
					else if (gameState == 2) {
						if (myrisk.isOwnedCurrentPlayerInt(pixColor) && (autoplace.isVisible()==false || myrisk.hasArmiesInt(pixColor) == 0)) {
							go( "placearmies " + pixColor + " 10" );
						}
					}

				}

			}
			e.consume();
		}

		public void mouseDragged(MouseEvent e) {
		}
		public void mouseMoved(MouseEvent e) {

			int pixColor = pp.getCountryNumber(e.getX(),e.getY());
			String name="";
			int cc;

			if (pixColor != 255) {
				name = myrisk.getCountryName( pixColor );
			}

			if (pixColor == 255 ) {
				cc = 255;
			}
			else if (gameState == 2) {
				if (myrisk.isOwnedCurrentPlayerInt(pixColor) && (autoplace.isVisible()==false || myrisk.hasArmiesInt(pixColor) == 0)) {
					cc = pixColor;
				}
				else {
					cc = 255;
				}
			}
			else if ( gameState == 3) {
				if ( myrisk.isOwnedCurrentPlayerInt(pixColor) && (myrisk.hasArmiesInt(pixColor) > 1) ) {
					cc = pixColor;
				}
				else if ( !(myrisk.isOwnedCurrentPlayerInt(pixColor)) && c1Id != -1 && myrisk.canAttack( c1Id , pixColor) ) {
					cc = pixColor;
				}
				else {
					cc = 255;
				}

			}
			else if ( gameState == 6) {
				if ( myrisk.isOwnedCurrentPlayerInt(pixColor) && (myrisk.hasArmiesInt(pixColor) > 1) ) {
					cc = pixColor;
				}
				else if ( myrisk.isOwnedCurrentPlayerInt(pixColor) && !(country1.getText().equals("")) && (country2.getText().equals("")) && myrisk.canAttack( pp.getC1(), pixColor) ) { // country1.getText(),name
					cc = pixColor;
				}
				else if ( myrisk.isOwnedCurrentPlayerInt(pixColor) && !(country1.getText().equals(""))&& !(country2.getText().equals(""))  && (myrisk.hasArmiesInt(pixColor) > 1) ) {
					cc = pixColor;
				}
				else {
					cc = 255;
				}
			}
			else if (gameState == 9) {
				if ( myrisk.isOwnedCurrentPlayerInt(pixColor) ) {
					cc = pixColor;
				}
				else {
					cc = 255;
				}
			}
			else {
				cc = 255;

			}

			if (pp.getHighLight() != cc) {
				pp.setHighLight(cc);
				pp.repaint();
			}

			e.consume();
		}

	}

	class SetupPanel extends JPanel implements ActionListener {

			class NamedColor extends Color {

				private String name;
				private String realname;

				public NamedColor(Color color, String rn, String n) {
					super(color.getRGB());
					realname = rn;
					name = n;
				}

				public String toString() {
					return name;
				}

				public String getRealName() {
					return realname;
				}

			}
		private ButtonGroup CardTypeButtonGroup;
		private ButtonGroup GameTypeButtonGroup;
		private JTable players;
		private TableModel dataModel;
		private JButton defaultPlayers;
		private Object[][] data;
		private NamedColor[] namedColors;

		public void actionPerformed(ActionEvent e) {

			if (e.getSource()==mission) {

				AutoPlaceAll.setEnabled(false);

			}
			else if (e.getSource()==domination) {

				AutoPlaceAll.setEnabled(true);

			}
			else if (e.getSource()==capital) {

				AutoPlaceAll.setEnabled(true);

			}

		}

		public SetupPanel() {

			GridBagConstraints c = new GridBagConstraints();
			c.insets = new java.awt.Insets(3, 3, 3, 3);
			c.fill = GridBagConstraints.BOTH;

		// ##########################################################################################################

			JPanel playerOptions = new JPanel();
			playerOptions.setLayout(new java.awt.GridBagLayout());
			playerOptions.setBorder(javax.swing.BorderFactory.createTitledBorder( resbundle.getString("newgame.label.players") ));

			namedColors = new NamedColor[] {

				new NamedColor(Color.black,	"black"     , resbundle.getString("color.black")),
				new NamedColor(Color.blue,	"blue"      , resbundle.getString("color.blue")),
				new NamedColor(Color.cyan,	"cyan"      , resbundle.getString("color.cyan")),
				new NamedColor(Color.darkGray,  "darkgray"  , resbundle.getString("color.darkgray")),
				new NamedColor(Color.green,	"green"     , resbundle.getString("color.green")),
				new NamedColor(Color.lightGray, "lightgray" , resbundle.getString("color.lightgray")),
				new NamedColor(Color.magenta,	"magenta"   , resbundle.getString("color.magenta")),
				new NamedColor(Color.orange,	"orange"    , resbundle.getString("color.orange")),
				new NamedColor(Color.pink,	"pink"      , resbundle.getString("color.pink")),
				new NamedColor(Color.red, 	"red"       , resbundle.getString("color.red")),
				new NamedColor(Color.white,	"white"     , resbundle.getString("color.white")),
				new NamedColor(Color.yellow,	"yellow"    , resbundle.getString("color.yellow"))

			};

			final String[] names = {
				resbundle.getString("newgame.label.name"),
				resbundle.getString("newgame.label.color"),
				resbundle.getString("newgame.label.type"),
			};

			//String name="appletuser";
			//if (Risk.applet == null) {
			//	name = System.getProperty("user.name");
			//}

			// Create the dummy data (a few rows of names)
/*
			data = new Object[][] {
				{ name , namedColors[4], resbundle.getString("newgame.player.type.human")},
				{"bob", namedColors[1], resbundle.getString("newgame.player.type.easyai")},
				{"fred", namedColors[9], resbundle.getString("newgame.player.type.easyai")},
				{"ted", namedColors[11], resbundle.getString("newgame.player.type.easyai")},
				{"yura", namedColors[6], resbundle.getString("newgame.player.type.hardai")},
				{"lala", namedColors[2], resbundle.getString("newgame.player.type.hardai")}
			};
*/
			data = new Object[6][3];

			for (int cc=1;cc<=risk.engine.core.RiskGame.MAX_PLAYERS;cc++) {

				data[cc-1][0] = myrisk.getRiskConfig("default.player"+cc+".name");
				data[cc-1][1] = findColor( risk.engine.core.RiskGame.getColor( myrisk.getRiskConfig("default.player"+cc+".color") ) );
				data[cc-1][2] = findType( myrisk.getType( myrisk.getRiskConfig("default.player"+cc+".type") ) );

			}

			dataModel = new DefaultTableModel(data, names) { // AbstractTableModel
				//public int getColumnCount() { return names.length; }
				//public int getRowCount() { return data.length;}
				//public Object getValueAt(int row, int col) {return data[row][col];}
				public String getColumnName(int column) {return names[column];}
				public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
				public boolean isCellEditable(int row, int col) { return localGame; }
				public void setValueAt(Object aValue, int row, int column) { if(column==0) { aValue = ((String)aValue).replace('$','S'); if( ((String)aValue).length() > 15 ) { aValue = ((String)aValue).substring(0,15); } } super.setValueAt(aValue,row,column); } // if(column==0 && ((String)aValue).indexOf('$')!=-1 ) { return; } else { }
			};

			players = new JTable( dataModel );
			players.setSelectionMode(0);
			players.getTableHeader().setReorderingAllowed(false);

			DefaultTableCellRenderer colorRenderer = new DefaultTableCellRenderer() {
				public void setValue(Object value) {
					if (value instanceof NamedColor) {
						NamedColor c = (NamedColor) value;
						setBackground(c);
						setForeground( Risk.getTextColorFor( c ) );
						setText(c.toString());
					} else {
						super.setValue(value);
					}
				}
			};

			final JComboBox colorComboBox = new JComboBox( namedColors );
			//for (int a=0; a < namedColors.length; a++) {

			//	colorComboBox.addItem(namedColors[a]);

			//}

			final JComboBox typeComboBox = new JComboBox();
			typeComboBox.addItem(resbundle.getString("newgame.player.type.human"));
			typeComboBox.addItem(resbundle.getString("newgame.player.type.crapai"));
			typeComboBox.addItem(resbundle.getString("newgame.player.type.easyai"));
			typeComboBox.addItem(resbundle.getString("newgame.player.type.hardai"));



			TableColumn colorColumn = players.getColumn(resbundle.getString("newgame.label.color"));
			colorColumn.setCellEditor(new DefaultCellEditor(colorComboBox));
			colorRenderer.setHorizontalAlignment(JLabel.CENTER);
			colorColumn.setCellRenderer(colorRenderer);

			TableColumn typeColumn = players.getColumn(resbundle.getString("newgame.label.type"));
			typeColumn.setCellEditor(new DefaultCellEditor(typeComboBox));


			JScrollPane scrollpane = new JScrollPane(players);
			Dimension psize = new Dimension(200,140);

			scrollpane.setPreferredSize(psize);
			scrollpane.setMinimumSize(psize);
			scrollpane.setMaximumSize(psize);

			// re-useing the combo box created for the table
			colorComboBox.addActionListener(
			    new ActionListener() {
				public void actionPerformed(ActionEvent a) {

					Color c = (Color)colorComboBox.getSelectedItem();
					colorComboBox.setBackground( c );
					colorComboBox.setForeground( Risk.getTextColorFor(c) );

				}
			    }
			);

			JButton newPlayer = new JButton(resbundle.getString("newgame.newplayer"));

			newPlayer.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent a) {

						if (localGame) {

							((DefaultTableModel)dataModel).addRow( new Object[] { resbundle.getString("newgame.newplayername") ,namedColors[0],resbundle.getString("newgame.player.type.human")} );

						}
						else {

                					colorComboBox.setSelectedIndex(0);
							typeComboBox.setSelectedIndex(0);

 							// Messages 
							Object[] message = new Object[6];

							message[0] = resbundle.getString("newgame.label.name");
							message[1] = new JTextField( resbundle.getString("newgame.newplayername") );
							message[2] = resbundle.getString("newgame.label.color");
							message[3] = colorComboBox;
							message[4] = resbundle.getString("newgame.label.type");
							message[5] = typeComboBox;

							// Options 
							String[] options = { 
								"OK",
								"Cancel"
							}; 
							int result = JOptionPane.showOptionDialog( 
								Risk.findParentFrame(SwingGUIPanel.this),			// the parent that the dialog blocks 
								message,				// the dialog message array 
								"create new player",			// the title of the dialog window 
								JOptionPane.OK_CANCEL_OPTION,		// option type 
								JOptionPane.QUESTION_MESSAGE,		// message type 
								null,					// optional icon, use null to use the default icon 
								options,				// options string array, will be made into buttons 
								options[0]				// option that should be made into a default button 
							); 

							if (result==0) {


								String type=null;
								int types=typeComboBox.getSelectedIndex();

								switch (types) {
									case 0: type = "human"; break;
									case 1: type = "ai crap"; break;
									case 2: type = "ai easy"; break;
									case 3: type = "ai hard"; break;
								}

								go("newplayer "+type+" "+((NamedColor)colorComboBox.getSelectedItem()).getRealName()+" "+((JTextField)message[1]).getText());

							}
						}
					}
				}
			);


			JButton delPlayer = new JButton(resbundle.getString("newgame.removeplayer"));

			delPlayer.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent a) {

						if (players.getSelectedRow() == -1 ) { return; }

						if (localGame) {

							players.removeEditor();

							((DefaultTableModel)dataModel).removeRow( players.getSelectedRow() );

						}
						else {
							go("delplayer "+ players.getValueAt( players.getSelectedRow() , 0));
						}

					}
				}
			);

			defaultPlayers = new JButton(resbundle.getString("newgame.resetplayers"));

			defaultPlayers.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent a) {

						players.removeEditor();
						resetPlayers();

					}
				}
			);

			c.gridx = 0; // col
			c.gridy = 0; // row
			c.gridwidth = 3; // width
			c.gridheight = 1; // height
			playerOptions.add(scrollpane, c);

			c.gridx = 0; // col
			c.gridy = 1; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			playerOptions.add(newPlayer, c);

			c.gridx = 1; // col
			c.gridy = 1; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			playerOptions.add(delPlayer, c);

			c.gridx = 2; // col
			c.gridy = 1; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			playerOptions.add(defaultPlayers, c);

		// ##########################################################################################################

			JPanel mapOptions = new JPanel();
			mapOptions.setLayout(new java.awt.GridBagLayout());
			mapOptions.setBorder(javax.swing.BorderFactory.createTitledBorder( resbundle.getString("newgame.label.map") ));

			JButton chooseMap = new JButton(resbundle.getString("newgame.choosemap"));

			chooseMap.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {

							String name = myrisk.getNewFile( Risk.findParentFrame(SwingGUIPanel.this), RiskFileFilter.RISK_MAP_FILES);

							if (name != null) {

								SwingGUIPanel.this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

								go("choosemap " + name );

							}
						}
					}
			);


			JButton defaultMap = new JButton(resbundle.getString("newgame.defaultmap"));

			defaultMap.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {

							go("choosemap default");
						}
					}
			);

			c.gridx = 0; // col
			c.gridy = 0; // row
			c.gridwidth = 2; // width
			c.gridheight = 1; // height
			mapOptions.add(mapPic, c);

			c.gridx = 0; // col
			c.gridy = 1; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			mapOptions.add(chooseMap, c);

			c.gridx = 1; // col
			c.gridy = 1; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			mapOptions.add(defaultMap, c);



			JPanel cardOptions = new JPanel();
			cardOptions.setLayout(new java.awt.GridBagLayout());
			cardOptions.setBorder(javax.swing.BorderFactory.createTitledBorder( resbundle.getString("newgame.label.cards") ));


			cardsFile = new JTextField("");
			cardsFile.setEditable(false);
			cardsFile.setBackground( mapOptions.getBackground() ); // SystemColor.control

			Dimension size = new Dimension(200,20);

			cardsFile.setPreferredSize(size);
			cardsFile.setMinimumSize(size);
			cardsFile.setMaximumSize(size);

			JButton chooseCards = new JButton(resbundle.getString("newgame.choosecards"));

			chooseCards.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {

							String name = myrisk.getNewFile( Risk.findParentFrame(SwingGUIPanel.this), RiskFileFilter.RISK_CARDS_FILES);

							if (name != null) {

								go("choosecards " + name );
							}

						}
					}
			);


			JButton defaultCards = new JButton(resbundle.getString("newgame.defaultcards"));

			defaultCards.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {

							go("choosecards default");
						}
					}
			);


			c.gridx = 0; // col
			c.gridy = 0; // row
			c.gridwidth = 2; // width
			c.gridheight = 1; // height
			cardOptions.add(cardsFile, c);

			c.gridx = 0; // col
			c.gridy = 1; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			cardOptions.add(chooseCards, c);

			c.gridx = 1; // col
			c.gridy = 1; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			cardOptions.add(defaultCards, c);


			JPanel GameTypeButtons = new JPanel();
			GameTypeButtons.setLayout(new javax.swing.BoxLayout(GameTypeButtons, javax.swing.BoxLayout.Y_AXIS));
			GameTypeButtons.setBorder(javax.swing.BorderFactory.createTitledBorder(resbundle.getString("newgame.label.gametype")));

			JPanel cardsOptions = new JPanel();
			cardsOptions.setLayout(new javax.swing.BoxLayout(cardsOptions, javax.swing.BoxLayout.Y_AXIS));
			cardsOptions.setBorder(javax.swing.BorderFactory.createTitledBorder(resbundle.getString("newgame.label.cardsoptions")));


			GameTypeButtonGroup = new ButtonGroup();
			CardTypeButtonGroup = new ButtonGroup();

			domination = new JRadioButton(resbundle.getString("newgame.mode.domination"), true);
			capital = new JRadioButton(resbundle.getString("newgame.mode.capital"));
			mission = new JRadioButton(resbundle.getString("newgame.mode.mission"));

			domination.setOpaque(false);
			capital.setOpaque(false);
			mission.setOpaque(false);

			domination.addActionListener( this );
			capital.addActionListener( this );
			mission.addActionListener( this );

			increasing = new JRadioButton(resbundle.getString("newgame.cardmode.increasing"), true);
			fixed = new JRadioButton(resbundle.getString("newgame.cardmode.fixed"));

			increasing.setOpaque(false);
			fixed.setOpaque(false);

			GameTypeButtonGroup.add ( domination );
			GameTypeButtonGroup.add ( capital );
			GameTypeButtonGroup.add ( mission );

			CardTypeButtonGroup.add ( increasing );
			CardTypeButtonGroup.add ( fixed );

			GameTypeButtons.add( domination );
			GameTypeButtons.add( capital );
			GameTypeButtons.add( mission );

			cardsOptions.add( increasing );
			cardsOptions.add( fixed );

			JPanel GameOptionsButtons = new JPanel();
			GameOptionsButtons.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
			GameOptionsButtons.setBorder(javax.swing.BorderFactory.createTitledBorder(resbundle.getString("newgame.label.startgameoptions")));

			AutoPlaceAll = new JCheckBox(resbundle.getString("newgame.autoplace"));
			GameOptionsButtons.add( AutoPlaceAll );
			AutoPlaceAll.setOpaque(false);

			recycle = new JCheckBox(resbundle.getString("newgame.recycle"));
			GameOptionsButtons.add( recycle );
			recycle.setOpaque(false);

			JButton startGame = new JButton(resbundle.getString("newgame.startgame"));

			startGame.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {

							boolean setupOK=true;
							String error="";

							if (players.getRowCount() >= 2 && players.getRowCount() <= risk.engine.core.RiskGame.MAX_PLAYERS) {

								for (int c=0; c < players.getRowCount(); c++) {
									for (int b=(c+1); b < players.getRowCount(); b++) {
										if ( ((String)players.getValueAt(c, 0)).equals( players.getValueAt(b, 0) ) ) {
											setupOK=false;
											error=resbundle.getString("newgame.error.samename");
										}
										if ( players.getValueAt(c, 1) == ( players.getValueAt(b, 1) ) ) {
											setupOK=false;
											error=resbundle.getString("newgame.error.samecolor");
										}
									}
								}

							}
							else {
								setupOK=false;
								error=resbundle.getString("newgame.error.2to6players");
							}

							//if ((players.getRowCount() == 2) && ( !(((String)players.getValueAt(0, 2)).equals("Human")) || !(((String)players.getValueAt(1, 2)).equals("Human")) || !(domination.isSelected()) ) ) {
							//	setupOK=false;
							//	error=resbundle.getString("newgame.error.2playerdominationonly");
							//}

							if (setupOK == true) {

								if (localGame) {

								    for (int c=0; c < players.getRowCount(); c++) {

									String Ptype="";

									if ( players.getValueAt(c, 2).equals(      resbundle.getString("newgame.player.type.human")  ) ) { Ptype="human"; }
									else if ( players.getValueAt(c, 2).equals( resbundle.getString("newgame.player.type.crapai") ) ) { Ptype="ai crap"; }
									else if ( players.getValueAt(c, 2).equals( resbundle.getString("newgame.player.type.easyai") ) ) { Ptype="ai easy"; }
									else if ( players.getValueAt(c, 2).equals( resbundle.getString("newgame.player.type.hardai") ) ) { Ptype="ai hard"; }

									go("newplayer " + Ptype + " " + ((NamedColor)players.getValueAt(c, 1)).getRealName() + " " + players.getValueAt(c, 0) );


								    }

								}

								String type="";

								if (domination.isSelected()) type = "domination";
								else if (capital.isSelected()) type = "capital";
								else if (mission.isSelected()) type = "mission";

								if (increasing.isSelected()) type += " increasing";
								else if (fixed.isSelected()) type += " fixed";

								go("startgame " + type + (( AutoPlaceAll.isSelected() )?(" autoplaceall"):("")) + (( recycle.isSelected() )?(" recycle"):("")) );

							}
							else {
								showError(error);
							}

						}
					}
			);


			// ################### GAME SETUP #######################
			this.setLayout(new java.awt.GridBagLayout());

			playerOptions.setOpaque(false);
			GameTypeButtons.setOpaque(false);
			mapOptions.setOpaque(false);
			cardOptions.setOpaque(false);
			GameOptionsButtons.setOpaque(false);
			cardsOptions.setOpaque(false);

			c.gridx = 0; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(playerOptions, c);

			c.gridx = 0; // col
			c.gridy = 2; // row
			c.gridwidth = 1; // width
			c.gridheight = 2; // height
			this.add(GameTypeButtons, c);


			c.gridx = 1; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(mapOptions, c);

			c.gridx = 0; // col
			c.gridy = 1; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(cardsOptions, c);

			c.gridx = 1; // col
			c.gridy = 1; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(cardOptions, c);

			c.gridx = 1; // col
			c.gridy = 2; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(GameOptionsButtons, c);

			c.gridx = 1; // col
			c.gridy = 3; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(startGame, c);

		}

		public void resetPlayers() {

			while (players.getRowCount() != 0) ((DefaultTableModel)dataModel).removeRow( 0 );

			for (int c=0; c < data.length; c++) {
				((DefaultTableModel)dataModel).addRow( data[c] );
			}

		}

		public void setupGame() {

			players.removeEditor();

			if (localGame) {

				resetPlayers();
				defaultPlayers.setEnabled(true);
			}
			else {

				// remove all players
				while (players.getRowCount() != 0) ((DefaultTableModel)dataModel).removeRow( 0 );
				defaultPlayers.setEnabled(false);
			}

		}


		public void addPlayer(int t, String name, Color color, String ip) {

			if (!localGame) {

				NamedColor c=findColor(color);

				String type=findType(t);

				((DefaultTableModel)dataModel).addRow( new Object[] {name, c ,type} );

			}

		}

		public NamedColor findColor(Color color) {

				// go though array of colors and find correct NamedColor

				for (int a=0; a < namedColors.length; a++) {

					if (namedColors[a].getRGB()==color.getRGB()) { return namedColors[a]; }

				}

				return null;
		}
		public String findType(int t) {

				if (t == risk.engine.core.Player.PLAYER_HUMAN) {
					return resbundle.getString("newgame.player.type.human");
				}

				if (t == risk.engine.core.Player.PLAYER_AI_CRAP) {
					return resbundle.getString("newgame.player.type.crapai");
				}

				if (t == risk.engine.core.Player.PLAYER_AI_EASY) {
					return resbundle.getString("newgame.player.type.easyai");
				}

				if (t == risk.engine.core.Player.PLAYER_AI_HARD) {
					return resbundle.getString("newgame.player.type.hardai");
				}

				return null;

		}

		public void delPlayer(String name) {

			if (!localGame) {

				for (int c=0; c < players.getRowCount(); c++) {

					if ( players.getValueAt(c,0).equals(name) ) {
						((DefaultTableModel)dataModel).removeRow( c );
						break;
					}
				}
			}
		}


	}

	class capitalPanel extends JPanel {

		public capitalPanel() {

			this.setLayout(new java.awt.GridBagLayout());

			GridBagConstraints c = new GridBagConstraints();
			c.insets = new java.awt.Insets(3, 3, 3, 3);
			c.fill = GridBagConstraints.BOTH;

			JButton endtrade = new JButton(resbundle.getString("about.okbutton"));

			endtrade.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {

							pp.setC1(255);
							go("capital "+c1Id );
							c1Id = -1;
						}
					}
			);

			JLabel label = new JLabel(resbundle.getString("core.help.selectcapital"));

			c.gridx = 0; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(label, c);

			c.gridx = 1; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(endtrade, c);

		}
	}

	class tradeCardsPanel extends JPanel {

		public tradeCardsPanel() {

			this.setLayout(new java.awt.GridBagLayout());

			GridBagConstraints c = new GridBagConstraints();
			c.insets = new java.awt.Insets(3, 3, 3, 3);
			c.fill = GridBagConstraints.BOTH;

			JButton endtrade = new JButton(resbundle.getString("game.button.go.endtrade"));

			endtrade.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {
							go("endtrade");
						}
					}
			);

			JLabel label = new JLabel(resbundle.getString("cards.totradeclick"));

			c.gridx = 0; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(label, c);

			c.gridx = 1; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(endtrade, c);

		}
	}

	class endgoPanel extends JPanel {

		public endgoPanel() {

			this.setLayout(new java.awt.GridBagLayout());

			GridBagConstraints c = new GridBagConstraints();
			c.insets = new java.awt.Insets(3, 3, 3, 3);
			c.fill = GridBagConstraints.BOTH;

			JButton endturn = new JButton("End Go");

			endturn.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {
							go("endgo");
						}
					}
			);

			c.gridx = 0; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(endturn, c);

		}
	}

	class placeArmiesPanel extends JPanel {

		public placeArmiesPanel() {

			this.setLayout(new java.awt.GridBagLayout());

			GridBagConstraints c = new GridBagConstraints();
			c.insets = new java.awt.Insets(3, 3, 3, 3);
			c.fill = GridBagConstraints.BOTH;

			autoplace.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {
							go("autoplace");
						}
					}
			);

			c.gridx = 0; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(armies, c);

			c.gridx = 1; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(autoplace, c);

		}
	}

	class attackPanel extends JPanel {

		public attackPanel() {

			this.setLayout(new java.awt.GridBagLayout());

			Dimension size = new Dimension(300,20);

			attacker = new JLabel(resbundle.getString("game.note.selectattacker"));

			attacker.setPreferredSize(size);
			attacker.setMinimumSize(size);
			attacker.setMaximumSize(size);

			GridBagConstraints c = new GridBagConstraints();
			c.insets = new java.awt.Insets(3, 3, 3, 3);
			c.fill = GridBagConstraints.BOTH;


			JButton endAttackButton = new JButton(resbundle.getString("game.button.go.endattack"));

			endAttackButton.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {
							go("endattack");
							attacker.setText(resbundle.getString("game.note.selectattacker"));

							c1Id = -1;
							pp.setC1(255);
							pp.setC2(255);
							pp.repaint();
						}
					}
			);


			c.gridx = 0; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(attacker, c);

			c.gridx = 1; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(endAttackButton, c);

		}
	}

	class tacMovePanel extends JPanel {

		public tacMovePanel() {

			this.setLayout(new java.awt.GridBagLayout());

			Dimension size = new Dimension(200,40);

			country1 = new JTextField("");
			country1.setEditable(false);
			//country1.setBackground(SystemColor.control);
			country1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED),"Source",javax.swing.border.TitledBorder.LEADING,javax.swing.border.TitledBorder.TOP,new java.awt.Font("SansSerif",0,11),new java.awt.Color(60,60,60)));

			country1.setPreferredSize(size);
			country1.setMinimumSize(size);
			country1.setMaximumSize(size);

			country2 = new JTextField("");
			country2.setEditable(false);
			//country2.setBackground(SystemColor.control);
			country2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED),"Target",javax.swing.border.TitledBorder.LEADING,javax.swing.border.TitledBorder.TOP,new java.awt.Font("SansSerif",0,11),new java.awt.Color(60,60,60)));

			country2.setPreferredSize(size);
			country2.setMinimumSize(size);
			country2.setMaximumSize(size);

			country1.setOpaque(false);
			country2.setOpaque(false);

			// s.setBorder(new TitledBorder("Number of Armies to move") );

			moveNumber.setMinimumSize(new Dimension(300, 50));

			moveNumber.putClientProperty("JSlider.isFilled", Boolean.TRUE );

			moveNumber.setPaintTicks(true);
			moveNumber.setMajorTickSpacing(1);
			// moveNumber.setMinorTickSpacing(1);

			moveNumber.setPaintLabels( true );
			moveNumber.setSnapToTicks( true );

			moveNumber.getLabelTable().put(new Integer(11), new JLabel(new Integer(11).toString(), JLabel.CENTER));
			moveNumber.setLabelTable( slider.getLabelTable() );

			moveNumber.getAccessibleContext().setAccessibleName("slider");
			moveNumber.getAccessibleContext().setAccessibleDescription("move armies slider");

			// moveNumber.addChangeListener(listener);


			GridBagConstraints c = new GridBagConstraints();
			c.insets = new java.awt.Insets(3, 3, 3, 3);
			c.anchor = GridBagConstraints.SOUTH;

			JButton moveButton = new JButton(resbundle.getString("move.move"));

			moveButton.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {

							if ( !(country1.getText().equals("")) && !(country2.getText().equals("")) ) {

								//FIXME: get the id of the country, don't use the string name
								int nCountryId = myrisk.getGame().getCountryInt(pp.getC1()).getColor();
								//int nCountryId = myrisk.getGame().getCountry(country1.getText()).getColor();

								showQuestion( myrisk.hasArmiesInt(nCountryId) - 1);
//			    showQuestion( myrisk.hasArmies(country1.getText() )-1 );

								country1.setText("");
								country2.setText("");
								pp.setC1(255);
								pp.setC2(255);

							}
						}
					}
			);

			JButton noMoveButton = new JButton(resbundle.getString("game.button.go.nomove"));

			noMoveButton.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {
							go("nomove");
							country1.setText("");
							country2.setText("");
							pp.setC1(255);
							pp.setC2(255);
						}
					}
			);

			c.gridx = 0; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(country1, c);

			c.gridx = 1; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(country2, c);

			c.gridx = 2; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(moveButton, c);

			c.gridx = 3; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(noMoveButton, c);

		}
	}

	class rollPanel extends JPanel {

		public rollPanel() {

			this.setLayout(new java.awt.GridBagLayout());

			GridBagConstraints c = new GridBagConstraints();
			c.insets = new java.awt.Insets(3, 3, 3, 3);
			c.fill = GridBagConstraints.BOTH;

			JButton retreat = new JButton(resbundle.getString("battle.retreat"));

			retreat.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {
							go("retreat");
						}
					}
			);

			c.gridx = 3; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(retreat, c);

		}
	}

	class defendPanel extends JPanel {

		public defendPanel() {

			this.setLayout(new java.awt.GridBagLayout());

			GridBagConstraints c = new GridBagConstraints();
			c.insets = new java.awt.Insets(3, 3, 3, 3);
			c.fill = GridBagConstraints.BOTH;

		}
	}

	class movePanel extends JPanel {

		public movePanel() {

			this.setLayout(new java.awt.GridBagLayout());

			GridBagConstraints c = new GridBagConstraints();
			c.insets = new java.awt.Insets(3, 3, 3, 3);
			//c.fill = GridBagConstraints.BOTH;


			// s.setBorder(new TitledBorder("Number of Armies to move") );

			slider.setMinimumSize(new Dimension(300, 50));

			slider.putClientProperty("JSlider.isFilled", Boolean.TRUE );

			slider.setPaintTicks(true);
			slider.setMajorTickSpacing(1);
			// slider.setMinorTickSpacing(1);

			slider.setPaintLabels( true );
			slider.setSnapToTicks( true );

			slider.getLabelTable().put(new Integer(11), new JLabel(new Integer(11).toString(), JLabel.CENTER));
			slider.setLabelTable( slider.getLabelTable() );

			slider.getAccessibleContext().setAccessibleName("slider");
			slider.getAccessibleContext().setAccessibleDescription("move armies slider");

			// slider.addChangeListener(listener);

			slider.setOpaque(false);

			JLabel label = new JLabel(resbundle.getString("move.numberofarmies"));

			JButton move1 = new JButton(resbundle.getString("move.move"));

			move1.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {
							go("move " + slider.getValue() );
						}
					}
			);

			JButton moveall = new JButton(resbundle.getString("move.moveall"));

			moveall.addActionListener(
					new ActionListener() {
						public void actionPerformed(ActionEvent a) {
							go("move all");
						}
					}
			);

			c.gridx = 0; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(label, c);

			c.gridx = 1; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(slider, c);

			c.gridx = 2; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(move1, c);

			c.gridx = 3; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(moveall, c);


		}
	}

	class winnerPanel extends JPanel {

		public winnerPanel() {

			this.setLayout(new java.awt.GridBagLayout());

			GridBagConstraints c = new GridBagConstraints();
			c.insets = new java.awt.Insets(3, 3, 3, 3);
			c.fill = GridBagConstraints.BOTH;

			JLabel label = new JLabel(resbundle.getString("game.over"));

			c.gridx = 0; // col
			c.gridy = 0; // row
			c.gridwidth = 1; // width
			c.gridheight = 1; // height
			this.add(label, c);

		}
	}

}
