/*
* AdminPanel.java
*
* Created on May 21, 2009, 11:44:23 PM
*
* This file is a part of Shoddy Battle.
* Copyright (C) 2009 Catherine Fitzpatrick and Benjamin Gwin
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, visit the Free Software Foundation, Inc.
* online at http://gnu.org.
*/
/*
* AdminPanel.java
* This is another panel that is used for moderators to view user information
*/
package shoddybattleclient.forms;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.table.AbstractTableModel;
import shoddybattleclient.LobbyWindow;
import shoddybattleclient.network.ServerLink;
import shoddybattleclient.network.ServerLink.BanElement;
import shoddybattleclient.utils.CloseableTabbedPane.CloseableTab;
/**
*
* @author ben
*/
public class AdminPanel extends javax.swing.JPanel implements CloseableTab {
public static interface ChannelLookup {
public String getChannelName(int id);
}
private static class BanTableModel extends AbstractTableModel {
private final static String[] COLUMN_NAMES =
new String[] {"Channel", "Username", "Expiry"};
private ChannelLookup m_lookup;
private List<BanElement> m_data = new ArrayList<BanElement>();
public BanTableModel(ChannelLookup lookup) {
m_lookup = lookup;
}
public void setData(List<BanElement> data) {
m_data = data;
}
@Override
public int getRowCount() {
return m_data.size();
}
@Override
public int getColumnCount() {
return COLUMN_NAMES.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
BanElement be = m_data.get(rowIndex);
switch (columnIndex) {
case 0:
return m_lookup.getChannelName(be.channel);
case 1:
return be.name;
case 2:
long l = be.expiry;
return LobbyWindow.Channel.DATE_FORMATTER.format(l * 1000);
default:
throw new InternalError();
}
}
@Override
public String getColumnName(int colId) {
return COLUMN_NAMES[colId];
}
@Override
public boolean isCellEditable(int rId, int cId) { return false; }
}
private ServerLink m_link;
private String m_name = "";
private String m_ip = "";
private List<String> m_aliases = new ArrayList<String>();
private List<BanElement> m_bans = new ArrayList<BanElement>();
/** Creates new form AdminPanel */
public AdminPanel(ServerLink link) {
initComponents();
m_link = link;
tblBans.setModel(new BanTableModel(m_link.getLobby()));
for (int i = 0; i < tblBans.getColumnCount(); i++) {
tblBans.getColumnModel().getColumn(i).setPreferredWidth(getColumnWidth(i));
}
}
private int getColumnWidth(int col) {
switch (col) {
case 0:
return 100;
case 1:
return 100;
case 2:
return 300;
default:
return 0;
}
}
private void sendRequest() {
String name = txtLookup.getText();
m_link.requestUserLookup(name);
}
public void updateDetails(String name, String ip, List<String> aliases, List<BanElement> bans) {
m_name = name;
m_ip = ip;
m_aliases = aliases;
m_bans = bans;
setErrorText(null);
refreshView();
}
private void refreshView() {
txtName.setText(m_name);
txtIp.setText(m_ip);
DefaultListModel model = new DefaultListModel();
for (String s : m_aliases) {
model.addElement(s);
}
lstAliases.setModel(model);
((BanTableModel)tblBans.getModel()).setData(m_bans);
}
public void setErrorText(String s) {
txtError.setText(s);
}
@Override
public boolean informClosed() {
return true;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
txtLookup = new javax.swing.JTextField();
btnLookup = new javax.swing.JButton();
txtError = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
lstAliases = new javax.swing.JList();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jScrollPane3 = new javax.swing.JScrollPane();
tblBans = new javax.swing.JTable();
jPanel3 = new javax.swing.JPanel();
txtName = new javax.swing.JLabel();
txtIp = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
setOpaque(false);
jPanel1.setOpaque(false);
jLabel1.setText("User Lookup:");
txtLookup.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
txtLookupKeyReleased(evt);
}
});
btnLookup.setText("Lookup");
btnLookup.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLookupActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(jLabel1)
.add(btnLookup)
.add(txtError, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(txtLookup, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 128, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(txtLookup, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(btnLookup)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(txtError)
.addContainerGap(246, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jPanel2.setOpaque(false);
jScrollPane1.setViewportView(lstAliases);
jLabel5.setText("Aliases:");
jLabel6.setText("Bans:");
tblBans.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{},
{},
{},
{}
},
new String [] {
}
));
jScrollPane3.setViewportView(tblBans);
jPanel3.setOpaque(false);
txtName.setFont(new java.awt.Font("Lucida Grande", 1, 18));
txtName.setText("bearzly");
txtIp.setFont(new java.awt.Font("Lucida Grande", 0, 10));
txtIp.setText("255.255.255.255");
jLabel3.setFont(new java.awt.Font("Lucida Grande", 0, 10));
jLabel3.setText("IP Address:");
org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel3Layout.createSequentialGroup()
.add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(org.jdesktop.layout.GroupLayout.LEADING, txtName)
.add(org.jdesktop.layout.GroupLayout.LEADING, jPanel3Layout.createSequentialGroup()
.add(jLabel3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(txtIp)))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel3Layout.createSequentialGroup()
.add(txtName)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel3)
.add(txtIp)))
);
org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
.add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel5)
.add(jLabel6))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel2Layout.createSequentialGroup()
.add(20, 20, 20)
.add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 58, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(17, 17, 17)
.add(jLabel5)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel6)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
.addContainerGap())
);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
private void btnLookupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLookupActionPerformed
sendRequest();
}//GEN-LAST:event_btnLookupActionPerformed
private void txtLookupKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtLookupKeyReleased
if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
sendRequest();
}
}//GEN-LAST:event_txtLookupKeyReleased
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnLookup;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JList lstAliases;
private javax.swing.JTable tblBans;
private javax.swing.JLabel txtError;
private javax.swing.JLabel txtIp;
private javax.swing.JTextField txtLookup;
private javax.swing.JLabel txtName;
// End of variables declaration//GEN-END:variables
}