Skip to content
Snippets Groups Projects
Select Git revision
  • c4976e7db4b5b790a67c6b86b2f0ce026260dc16
  • master default protected
  • hai
  • isaac
  • CheHao
  • Eldar
  • mpriymak
  • master-before-merging-with-hai
  • master-before-merging-with-isaac
  • rmi-working-before-merging-with-isaac
  • all-code-merged-by-hai-v1
11 results

StartScreen.java

Blame
  • ApplicationMain.java 6.75 KiB
    package GUI;
    
    import client.Client;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.rmi.RemoteException;
    import java.util.ArrayList;
    import java.util.Random;
    
    public class ApplicationMain extends JPanel {
        private Client client;
        private ChatScreen chatScreen;
        private PaintGUI paintGUI;
        private JFrame frame;
    
        public ChatScreen getChatScreen() { return chatScreen; }
    
        public PaintGUI getPaintGUI() { return paintGUI; }
    
        public JFrame getFrame() {
            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);
            this.paintGUI = new PaintGUI(client);
        }
    
        public void createAndShowGUI()
        {
            frame = new JFrame("Application Main");
            JFrame.setDefaultLookAndFeelDecorated(true);
            Container content = frame.getContentPane();
            content.setLayout(new BorderLayout());
            content.add(paintGUI.getGlobal(), BorderLayout.WEST);
            content.add(chatScreen.panel2, BorderLayout.EAST);
            chatScreen.setUserName(client.getUserName());
    
    
            try {
                // Update canvas
                ArrayList<Shape> shapeList = client.getDrawingController().getShapeList();
                ArrayList<Color> colorList = client.getDrawingController().getColorList();
                ArrayList<Integer> strokeSizeList = client.getDrawingController().getStrokeSizeList();
    
                ArrayList<String> textList = client.getDrawingController().getTextList();
                ArrayList<Font> fontList = client.getDrawingController().getFontList();
                ArrayList<Point> textStartPointList = client.getDrawingController().getTextStartPointList();
    
                Graphics2D g2 = paintGUI.getDrawingArea().getG2();
    //            for (int i = 0; i < textList.size(); i++) {
    //                g2.setFont(fontList.get(i));
    //                g2.drawString(textList.get(i), textStartPointList.get(i).x, textStartPointList.get(i).y);
    //                client.getApplicationMain().getPaintGUI().getDrawingArea().repaint();
    //            }
    
                for (int i = 0; i < shapeList.size(); i++) {
                    g2.setStroke(new BasicStroke(strokeSizeList.get(i)));
                    g2.setColor(colorList.get(i));
                    g2.draw(shapeList.get(i));
                    paintGUI.getDrawingArea().repaint();
                }
            }
            catch (RemoteException e) {
                e.printStackTrace();
            }
    
    
            SwingUtilities.getRootPane(chatScreen.getSendButton()).setDefaultButton(chatScreen.getSendButton());
            frame.addWindowListener(new WindowAdapter()
            {
                @Override
                public void windowClosing(WindowEvent arg0)
                {
    
                    try {
                        if (client.getUserName().equals(client.getClientController().getAdmin())) {
    
                            int terminateAppAnswer = showManagerQuitMessage();
                            if (terminateAppAnswer == 0) {
    
                            }
                            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();
                                }
                            }
    
                        }
                        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();
                            }
                        }
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
    
                }
            });
    
            frame.setSize(1200, 700);
            frame.setLocationRelativeTo( null );
            frame.setResizable(false);
            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    
        public void exitApplication(){
            frame.setVisible(false);
            frame.dispose();
            client.setVisibleStartScreen();
        }
    
        public void setVisible() {
            frame.setVisible(true);
            chatScreen.setUserName(client.getUserName());
        }
    }