Files
cs8803p4/src/view/UserChooserFrame.java
2012-04-29 12:45:49 -04:00

168 lines
4.0 KiB
Java

package view;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import model.comPlayer.Player;
import model.playerModel.PlayerModel;
public class UserChooserFrame extends JFrame {
public static final String PLAYER_LIST_FILE = "players.dat";
public static final JLabel RULES_TEXT = new JLabel(
"Here should go some rules text. Lorem ipsum dolor sit amet, consectetur adipiscing elit...");
private static final long serialVersionUID = 1L;
private final Player comPlayer;
private final JButton playButton = new JButton("Play!");
private final JComboBox<String> userNameBox = new JComboBox<String>();
private ArrayList<String> users;
public UserChooserFrame(ParsedArgs parsedArgs) {
this.comPlayer = parsedArgs.getComPlayer();
initLayout();
initActions();
pack();
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
private String getUserListPath() {
return PlayerModel.DATA_FOLDER + PLAYER_LIST_FILE;
}
private ArrayList<String> getUsers() {
FileInputStream fin = null;
ObjectInputStream oin = null;
try {
fin = new FileInputStream(getUserListPath());
oin = new ObjectInputStream(fin);
@SuppressWarnings("unchecked")
ArrayList<String> list = (ArrayList<String>) oin.readObject();
oin.close();
return list;
} catch (Exception e) {
return new ArrayList<String>();
}
}
private void initActions() {
userNameBox.setEditable(true);
users = getUsers();
Collections.sort(users);
for (int i = 0; i < users.size(); i++) {
userNameBox.addItem(users.get(i));
}
ActionListener psActionListener = new PlayerSelectActionListener(this);
userNameBox.addActionListener(psActionListener);
playButton.addActionListener(psActionListener);
}
private void initLayout() {
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints con = new GridBagConstraints();
con.fill = GridBagConstraints.BOTH;
con.gridheight = 1;
con.gridwidth = 5;
con.gridx = 0;
con.gridy = 0;
con.insets = new Insets(20, 20, 5, 20);
con.weightx = 1;
con.weighty = 1;
gbl.setConstraints(RULES_TEXT, con);
con = new GridBagConstraints();
con.fill = GridBagConstraints.BOTH;
con.gridheight = 1;
con.gridwidth = 5;
con.gridx = 0;
con.gridy = 1;
con.insets = new Insets(5, 20, 5, 20);
con.weightx = 1;
con.weighty = 1;
gbl.setConstraints(userNameBox, con);
con = new GridBagConstraints();
con.fill = GridBagConstraints.BOTH;
con.gridheight = 1;
con.gridwidth = 1;
con.gridx = 2;
con.gridy = 2;
con.insets = new Insets(5, 20, 20, 20);
con.weightx = 1;
con.weighty = 1;
gbl.setConstraints(playButton, con);
setLayout(gbl);
add(RULES_TEXT);
add(userNameBox);
add(playButton);
}
private void saveUserList() {
FileOutputStream fout = null;
ObjectOutputStream oout = null;
String path = getUserListPath();
try {
(new File(PlayerModel.DATA_FOLDER)).mkdirs();
fout = new FileOutputStream(path);
oout = new ObjectOutputStream(fout);
oout.writeObject(users);
oout.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
void launchSelectedPlayer() {
String name = ((String) userNameBox.getSelectedItem());
name = name == null ? name : name.trim().toLowerCase();
if (name != null && name.compareTo("") != 0) {
UserChooserFrame.this.setVisible(false);
boolean found = false;
for (int i = 0; !found && i < users.size(); i++) {
if (name.compareTo(users.get(i)) == 0) {
found = true;
}
}
if (!found) {
users.add(name);
}
saveUserList();
new MainFrame(name, comPlayer);
}
}
}