Skip to content
Snippets Groups Projects

Hai

Merged
Ho Dac Hairequested to merge
hai into master
15 files
+ 291
75
Compare changes
  • Side-by-side
  • Inline

Files

+ 84
15
@@ -9,6 +9,7 @@ import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Random;
public class ApplicationMain extends JPanel {
private Client client;
@@ -24,6 +25,33 @@ public class ApplicationMain extends JPanel {
return frame;
}
public int showManagerQuitMessage() {
int answer = JOptionPane.showConfirmDialog(null,
"Do you want to terminate the application for all the users?",
"Close the application", JOptionPane.YES_NO_CANCEL_OPTION);
return answer;
}
public int showNextManagerMessage() {
int answer = JOptionPane.showConfirmDialog(null,
"Before leaving, do you want to choose the next manager manually?",
"Close the application", JOptionPane.YES_NO_CANCEL_OPTION);
return answer;
}
public String showAssignManagerMessage() {
int numUsers = client.getChatScreen().getKickUserComboBox().getItemCount();
String[] userOptions = new String[numUsers];
for (int i = 0; i < numUsers; i++) {
userOptions[i] = client.getChatScreen().getKickUserComboBox().getItemAt(i).toString();
}
String input = (String) JOptionPane.showInputDialog(null, "Choose the next manager",
"Assign a new manager before leaving", JOptionPane.QUESTION_MESSAGE, null,
userOptions, // Array of choices
userOptions[0]); // Initial choice
return input;
}
public ApplicationMain(Client client) {
this.client = client;
this.chatScreen = new ChatScreen(client);
@@ -82,24 +110,52 @@ public class ApplicationMain extends JPanel {
@Override
public void windowClosing(WindowEvent arg0)
{
int reply = JOptionPane.showConfirmDialog(null,
"Are you sure you want to quit the session?",
"Shut down session", JOptionPane.YES_NO_OPTION);
if( reply == JOptionPane.YES_OPTION )
{
try
{
client.getClientController().quit(client.getUserName());
try {
if (client.getUserName().equals(client.getClientController().getAdmin())) {
int terminateAppAnswer = showManagerQuitMessage();
// If the manager terminates the application
if (terminateAppAnswer == 0) {
client.getClientController().kickAll(client.getUserName());
exitApplication();
}
else if (terminateAppAnswer == 1) {
int answer = showNextManagerMessage();
// If the manager wants to assign the next manager manually
if (answer == 0) {
String newManager = showAssignManagerMessage();
client.getClientController().assignAdmin(client.getUserName(), newManager);
client.getClientController().quit(client.getUserName());
exitApplication();
}
// If the manager wants to assign the next manager by random choice
if (answer == 1) {
int numUsers = client.getChatScreen().getKickUserComboBox().getItemCount();
Random random = new Random();
int randomUserIndex = random.nextInt(numUsers);
String newManager = client.getChatScreen().getKickUserComboBox().getItemAt(randomUserIndex).toString();
client.getClientController().assignAdmin(client.getUserName(), newManager);
client.getClientController().quit(client.getUserName());
exitApplication();
}
}
}
catch (RemoteException e)
{
StartScreen.showErrorMessage("Error in quitting the server");
//e.printStackTrace();
else {
int reply = JOptionPane.showConfirmDialog(null,
"Are you sure you want to quit the session?",
"Shut down session", JOptionPane.YES_NO_OPTION);
if( reply == 0 )
{
client.getClientController().quit(client.getUserName());
exitApplication();
}
}
System.exit(0);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
@@ -108,6 +164,19 @@ public class ApplicationMain extends JPanel {
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void exitApplication(){
System.out.println("I am in exit application");
frame.setVisible(false);
frame.dispose();
client.setVisibleStartScreen();
}
public void setVisible() {
frame.setVisible(true);
chatScreen.setUserName(client.getUserName());
}
}
Loading