package clueless.view;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;
import java.util.Vector;
import clueless.controller.GameController;
import clueless.events.CharacterSelectedEvent;
import clueless.events.CharacterSelectedEventListener;
import clueless.events.CharactersAvailableEvent;
import clueless.events.CharactersAvailableEventListener;
import clueless.events.CharactersRequestEvent;
import clueless.events.DisconnectEvent;
import clueless.events.MessageEvent;
import clueless.model.Character.CharacterName;
import clueless.messaging.Message;
public class PlayerSetup extends AbstractView implements CharactersAvailableEventListener{
private String name = null;
private CharacterName characterChoice = null;
private Vector<CharacterName> availableNames = null;
private final JComboBox characterMenu = new JComboBox();
/** handle to the controller. */
private GameController controller;
public PlayerSetup(GameController controller) {
this.controller = controller;
availableNames = new Vector<CharacterName>();
controller.addCharactersAvailableEventListener(this);
setCharacter();
/** Request list of CharacterNames available*/
controller.fireCharactersRequestEvent(new CharactersRequestEvent(this));
return;
}
/**
* setCharacter() : Setter for the user's character selection
*/
private void setCharacter() {
jfrm = new JFrame("Select Your Character");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(220, 110);
jfrm.setResizable(false);
jfrm.setUndecorated(false);
jfrm.setAlwaysOnTop(true);
jfrm.setLocation((screen.width - jfrm.getWidth())/2,
(screen.height - jfrm.getHeight())/2);
JLabel jlbl = new JLabel("Select Your Character");
JButton submit = new JButton("Submit");
jfrm.add(jlbl);
jfrm.add(characterMenu);
jfrm.add(submit);
jfrm.setVisible(true);
/** Intercept Window Closure -- Confirm and gracefully exit */
jfrm.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
jfrm.addWindowListener(new WindowListener(){
public void windowActivated(WindowEvent arg0) {}
public void windowClosed(WindowEvent arg0) {
System.exit(0);
}
public void windowClosing(WindowEvent arg0) {
int choice = JOptionPane.showConfirmDialog(jfrm, "Are you sure you wish to quit?",
"Quit Game", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (choice == JOptionPane.YES_OPTION) {
controller.fireDisconnectEvent(new DisconnectEvent(this));
jfrm.dispose();
}
}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
});
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int selection = characterMenu.getSelectedIndex();
characterChoice = (CharacterName)characterMenu.getItemAt(selection);
controller.fireCharacterSelectedEvent(new CharacterSelectedEvent(this, characterChoice));
jfrm.setVisible(false);
/* TODO: There is still a chance that a user's choice may not be
* available even with the updating menu. If the server cannot
* honor user choice, then it should send a message display a
* message to user and respawn this dialog box
*/
}
});
}
@Override
public void handleCharactersAvailableEvent(CharactersAvailableEvent event) {
/** Updates list of available Character Names */
availableNames = event.getNames();
characterMenu.removeAllItems();
for (CharacterName character : availableNames)
{
characterMenu.addItem(character);
}
}
}