/*
(C) 2007 yura.net
This file is part of Lobby.
Lobby is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License
Lobby is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.yura.lobby.client;
import net.yura.lobby.model.PlayerInfo;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.text.JTextComponent;
import java.awt.Component;
public class PlayerInfoWindow extends JDialog implements ActionListener {
private JPanel panel;
private JPanel bottom;
private JButton editButton;
private LobbyClientGUI gui;
private String name;
private List playerInfoObjects;
public PlayerInfoWindow(LobbyClientGUI owner, String name,boolean model) {
super((Frame)javax.swing.SwingUtilities.getAncestorOfClass(Frame.class, owner),"Info: "+name,model);
gui = owner;
this.name = name;
panel = new JPanel();
panel.setLayout( new GridLayout(0,2) );
panel.add( new JLabel("Loading...") );
getContentPane().add( new JScrollPane(panel) );
bottom = new JPanel();
JButton okButton = new JButton("OK");
okButton.setActionCommand("ok");
okButton.addActionListener(this);
bottom.add(okButton);
getContentPane().add( bottom, BorderLayout.SOUTH );
}
public void updateInfo(List list) {
playerInfoObjects = list;
panel.removeAll();
boolean edit=false;
for (int c=0;c<list.size();c++) {
PlayerInfo playerInfo = (PlayerInfo)list.get(c);
panel.add(new JLabel(playerInfo.getLabel()));
if (playerInfo.getCanEdit()) {
panel.add(new JTextField(playerInfo.getInfo()));
edit = true;
}
else {
panel.add(new JLabel(playerInfo.getInfo()));
}
}
if (editButton==null && edit) {
editButton = new JButton("Save");
editButton.setActionCommand("save");
editButton.addActionListener(this);
bottom.add(editButton);
}
else if (editButton!=null && !edit) {
bottom.remove(editButton);
editButton = null;
}
panel.revalidate();
bottom.revalidate();
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("ok")) {
setVisible(false);
}
else if (ae.getActionCommand().equals("save")) {
List list = new Vector();
for (int c=0;c<playerInfoObjects.size();c++) {
PlayerInfo playerInfo = (PlayerInfo)playerInfoObjects.get(c);
if (playerInfo.getCanEdit()) {
Component component = panel.getComponent(((c+1)*2)-1);
if (component instanceof JTextComponent) {
playerInfo.setInfo( ((JTextComponent)component).getText() );
}
// SO FAR THIS IS THE ONLY TYPE WE KNOW ABOUT
list.add(playerInfo);
}
}
gui.savePlayerInfo(name,list);
setVisible(false);
}
else {
System.out.println(ae.getActionCommand());
}
}
}