📄 clientgui.java
字号:
//started from ServerControlGUI to deal with clients
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class ClientGUI extends WindowAdapter implements Runnable,ActionListener,ListSelectionListener/**/{
private JFrame frame;
private JFormattedTextField idField;
private JLabel statusLabel;
private static JLabel totalNumLabel,onlineNumLabel;
private JScrollPane scrollPane;
private JTable table;
private boolean isMultiRowSelected=false;
private int[] rows;
private final static String SEARCH="search";
private final static String DEL="del";
private final static String VIEWALL="all";
private final static String ONLINE="online";
private final static String FRIEND="friend";
private final static String EXIT="exit";
public void run(){
frame.setDefaultLookAndFeelDecorated(true);
frame=new JFrame("Client Management");
frame.addWindowListener(this);
frame.setContentPane(makeMainPane());
//frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae){
String type=ae.getActionCommand();
if(type.equals(SEARCH)){
int clientID=Integer.parseInt(idField.getText());
if(!checkIDField(clientID))
return;
statusLabel.setText("Information of ID:"+idField.getText());
statusLabel.setForeground(Color.BLACK);
SearchClientTableModel.initiate(clientID);
table.setModel(new SearchClientTableModel());
SearchClientTableModel.setTableWidth(table);
scrollPane.setPreferredSize(new Dimension(table.getPreferredSize().width,200));
}
else if(type.equals(DEL)){
if(isMultiRowSelected){ //maybe adm wants to del more than one clients
int choice=JOptionPane.showConfirmDialog(frame,
"Are you sure to delete ALL these clients?",
"Delete Confirm",
JOptionPane.YES_NO_OPTION);
if(choice==1) //do not want to del
return;
for(int k=0;k<rows.length;k++){
long id=((Long)table.getValueAt(rows[k],0)).longValue();
Server.getServerDatabase().delClient(id); //del it from database
}
}
else{
int clientID=Integer.parseInt(idField.getText());
if(!checkIDField(clientID))
return;
//0:yes 1:no
int choice=JOptionPane.showConfirmDialog(frame,
"Are you sure to delete this client, ID:"+idField.getText()+"?",
"Delete Confirm",
JOptionPane.YES_NO_OPTION);
//System.out.println("m type"+choice);
if(choice==1) //do not want to del
return;
//table.setModel(new DefaultTableModel()); //clear content of the table
Server.getServerDatabase().delClient(clientID); //del it from database
}
//to update info(rebuild the table)
ServerControlGUI.resetNumber();
resetNumber();
int registeredNumber=(int)Server.getServerDatabase().getRegisteredClient();
if(registeredNumber==0){
table.setModel(new DefaultTableModel());
statusLabel.setText("No registered client!");
statusLabel.setForeground(Color.RED);
}
else{
AllClientTableModel.initiate(registeredNumber);
table.setModel(new AllClientTableModel());
AllClientTableModel.setTableWidth(table);
statusLabel.setText("All Registered Clients' Information:");
statusLabel.setForeground(Color.BLACK);
//scrollPane.setPreferredSize(new Dimension(table.getPreferredSize().width,200));
}
}
else if(type.equals(VIEWALL)){
int registeredNumber=(int)Server.getServerDatabase().getRegisteredClient();
if(registeredNumber==0){
statusLabel.setText("No registered client!");
statusLabel.setForeground(Color.RED);
table.setModel(new DefaultTableModel());
return;
}
statusLabel.setText("All Registered Clients' Information:");
statusLabel.setForeground(Color.BLACK);
AllClientTableModel.initiate(registeredNumber);
table.setModel(new AllClientTableModel());
AllClientTableModel.setTableWidth(table);
scrollPane.setPreferredSize(new Dimension(table.getPreferredSize().width,200));
}
else if(type.equals(ONLINE)){
int onlineNum=(int)Server.getServerDatabase().getOnlineClient();
if(onlineNum==0){ //if no client online
statusLabel.setText("No client online!");
statusLabel.setForeground(Color.RED);
table.setModel(new DefaultTableModel());
return;
}
statusLabel.setText("Online Clients' Information");
statusLabel.setForeground(Color.BLACK);
OnlineClientTableModel.initiate(onlineNum,(LinkedList<Long>)
Server.getServerDatabase().getOnlineClientsList());
//System.out.println("online:"+Server.getServerDatabase().getOnlineClientsList());
table.setModel(new OnlineClientTableModel());
OnlineClientTableModel.setTableWidth(table);
scrollPane.setPreferredSize(new Dimension(table.getPreferredSize().width,200));
}
else if(type.equals(FRIEND)){
int clientID=Integer.parseInt(idField.getText());
int friendNum;
if(!checkIDField(clientID))
return;
friendNum=Server.getServerDatabase().findClient(clientID).getFriends().size();
if(friendNum==0){ //if have no friend
statusLabel.setText("ID: "+idField.getText()+" has no friend!");
statusLabel.setForeground(Color.RED);
table.setModel(new DefaultTableModel());
return;
}
statusLabel.setText("ID:"+idField.getText()+" friends' information");
statusLabel.setForeground(Color.BLACK);
if(ClientFriendTableModel.initiate(friendNum,clientID,
(LinkedList<Long>)Server.getServerDatabase().findClient(clientID).getFriends())){
table.setModel(new ClientFriendTableModel());
ClientFriendTableModel.setTableWidth(table);
scrollPane.setPreferredSize(new Dimension(table.getPreferredSize().width,200));
}
else{
statusLabel.setText("ID: "+idField.getText()+" has no friend!");
statusLabel.setForeground(Color.RED);
table.setModel(new DefaultTableModel());
}
}
else{
frame.dispose();
ServerControlGUI.clientGUIExit();
}
}
public void valueChanged(ListSelectionEvent lse){
//System.out.println("valueChanged is called");
int index=0; //index of the id column
rows=table.getSelectedRows();
if(rows.length==1){ //only one row is selected,row-count starts at 0
for(int i=0;i<table.getColumnCount();i++){ //because the columns are repositionable
if("ID".endsWith(table.getColumnName(i))){
index=i;
break;
}
}
Long id=(Long)table.getValueAt(rows[0],index);
idField.setText(id.toString());
isMultiRowSelected=false;
}
else if(rows.length>1){
isMultiRowSelected=true;
}
}
public void windowClosing(WindowEvent we){
ServerControlGUI.clientGUIExit();
}
private Container makeMainPane(){
Container original=new JPanel();
original.setLayout(new BoxLayout(original,BoxLayout.Y_AXIS));
JPanel clientNumPane=new JPanel(new GridLayout(2,1)); //display registered and online client num
totalNumLabel=new JLabel("Registered Client Number:"+Server.getServerDatabase().getRegisteredClient());
totalNumLabel.setHorizontalAlignment(SwingConstants.CENTER);
totalNumLabel.setToolTipText("This number will not update dynamically," +
"restart this modul for the latest infomation");
clientNumPane.add(totalNumLabel);
onlineNumLabel=new JLabel("Online Client Number:"+Server.getServerDatabase().getOnlineClient());
onlineNumLabel.setHorizontalAlignment(SwingConstants.CENTER);
onlineNumLabel.setToolTipText("This number will not update dynamically," +
"restart this modul for the latest infomation");
clientNumPane.add(onlineNumLabel);
//to input client id to search
JPanel inputPane=new JPanel(new GridLayout(1,2));
JLabel header=new JLabel("Client's ID:");
idField=new JFormattedTextField();
idField.setValue(new Long(0));
inputPane.add(header);
inputPane.add(idField);
JPanel statusPane=new JPanel();
statusLabel=new JLabel("All Registered Clients' Information:");
statusLabel.setHorizontalAlignment(SwingConstants.CENTER);
statusPane.add(statusLabel);
//setup buttons
JPanel buttonPane=new JPanel();
buttonPane.setLayout(new GridLayout(1,6));
JButton button;
button=new JButton("Search"); //search button
button.setMnemonic(KeyEvent.VK_S);
button.setToolTipText("To search a client information.");
button.setActionCommand(SEARCH);
button.addActionListener(this);
buttonPane.add(button);
//button.add(Box.createHorizontalStrut(5));
//button.add(Box.createHorizontalGlue());
button=new JButton("Delete"); //del button
button.setMnemonic(KeyEvent.VK_D);
button.setToolTipText("To delete client(s) from database");
button.setActionCommand(DEL);
button.addActionListener(this);
buttonPane.add(button);
//button.add(Box.createHorizontalStrut(5));
//button.add(Box.createHorizontalGlue());
button=new JButton("View All"); //view-all button
button.setMnemonic(KeyEvent.VK_A);
button.setToolTipText("To view all registered clients.");
button.setActionCommand(VIEWALL);
button.addActionListener(this);
buttonPane.add(button);
//button.add(Box.createHorizontalStrut(5));
//button.add(Box.createHorizontalGlue());
button=new JButton("View Online"); //view-online button
button.setMnemonic(KeyEvent.VK_O);
button.setToolTipText("To view all online clients");
button.setActionCommand(ONLINE);
button.addActionListener(this);
buttonPane.add(button);
button=new JButton("View Friend"); //view-friend button
button.setMnemonic(KeyEvent.VK_F);
button.setToolTipText("To view a clients' friends.");
button.setActionCommand(FRIEND);
button.addActionListener(this);
buttonPane.add(button);
button=new JButton("Exit"); //exit buton
button.setMnemonic(KeyEvent.VK_E);
button.setActionCommand(EXIT);
button.addActionListener(this);
buttonPane.add(button);
int registeredNumber=(int)Server.getServerDatabase().getRegisteredClient();
if(registeredNumber==0){
statusLabel.setText("No registered client!");
statusLabel.setForeground(Color.RED);
table=new JTable(new DefaultTableModel());
//table.setDragEnabled(false);
}
else{
AllClientTableModel.initiate(registeredNumber); //set first
table=new JTable(new AllClientTableModel());
//table.setDragEnabled(false);
//setupFriendColumn(table,table.getColumnModel().getColumn(9)); //to make Friend-ID column combobox
//table=new JTable();
//scrollPane=new JScrollPane();
//scrollPane=new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
//JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
AllClientTableModel.setTableWidth(table);
//scrollPane.setPreferredSize(new Dimension(table.getPreferredSize().width,200));
}
//table.setSelectionMode(JTable)
table.getSelectionModel().addListSelectionListener(this);
//scrollPane=new JScrollPane();
scrollPane=new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(table.getPreferredSize().width,200));
original.add(clientNumPane);
original.add(inputPane);
original.add(buttonPane);
original.add(statusPane);
original.add(Box.createVerticalGlue());
original.add(scrollPane);
return original;
}
private boolean checkIDField(int clientID){
if(clientID<=0){ //if id is negative
statusLabel.setText("ID: "+idField.getText()+" is not valid");
statusLabel.setForeground(Color.RED);
table.setModel(new DefaultTableModel());
return false;
}
if(!Server.getServerDatabase().contains(clientID)) { //if id dosen't exist
statusLabel.setText("ID: "+idField.getText()+" doesn't exsit!");
statusLabel.setForeground(Color.RED);
table.setModel(new DefaultTableModel());
return false;
}
return true;
}
synchronized static void resetNumber(){
totalNumLabel.setText("Registered Client Number:"+Server.getServerDatabase().getRegisteredClient());
onlineNumLabel.setText("Online Client Number:"+Server.getServerDatabase().getOnlineClient());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -