Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import javax.swing.event.ListSelectionEvent;
- import javax.swing.event.ListSelectionListener;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import java.util.*;
- /**
- * Created by Nivesh Varma on 28 Jan 17.
- */
- public class PictureChatClientGUI implements ActionListener, ListSelectionListener {
- // GUI objects
- private ObjectOutputStream oos;
- private String chatName;
- private JFrame chatWindow = new JFrame();
- private JButton sendButton = new JButton("Send to All");
- private JTextArea inTextArea = new JTextArea();
- private JTextArea outTextArea = new JTextArea();
- private JScrollPane inScrollPane = new JScrollPane(inTextArea); //new
- private JScrollPane outScrollPane = new JScrollPane(outTextArea);
- private JPanel centerPanel = new JPanel();
- private JPanel northPanel = new JPanel();
- private JLabel incomingMessagesLabel = new JLabel(" Chat Messages");
- private String newLine = System.lineSeparator();
- // picture GUI objects
- private JFrame picSendWindow = new JFrame();
- private JLabel selectPictureLabel = new JLabel("Select a picture to send");
- private JButton clearPictureSelectionButton = new JButton("Clear selection");
- private JList<ImageIcon> picturesToSendList = new JList<>();
- private JScrollPane picturesScrollPane = new JScrollPane(picturesToSendList);
- //new GUI objects
- private JPanel southPanel = new JPanel();
- private JPanel northWhosInPanel = new JPanel();
- private JPanel centerWhosInPanel = new JPanel();
- private JPanel southWhosInPanel = new JPanel();
- private JButton sendPrivateButton = new JButton("Send Private To");
- private JButton saveMessageButton = new JButton("Save Message For");
- private JButton clearPrivateButton= new JButton("Clear Selection");
- private JButton clearSaveButton = new JButton("Clear Selection");
- private JButton previewPicturesButton = new JButton("Preview Pictures To Send");
- private JLabel errorMessageLabel = new JLabel();
- private JList<String> whosInList = new JList<String>();
- private JList<String> whosNotList = new JList<String>();
- private JScrollPane whosInScrollPane = new JScrollPane(whosInList);
- private JScrollPane whosNotScrollPane = new JScrollPane(whosNotList);
- private File localDir = new File(System.getProperty("user.dir"));
- //=====================================================================================
- //Constructor
- public PictureChatClientGUI(String userChatName, ObjectOutputStream sendingOOS) {
- System.out.println("Local directory is:\n" + localDir);
- oos = sendingOOS; //copy pointer fields from the main thread STACK
- chatName = userChatName; //into program-object fields in ChatClientGUI
- // set the java "metal" look-&-feel
- try {
- UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
- } catch (Exception e) {
- System.out.println(e); //but don't terminate if this fails
- }
- // build the GUI
- picSendWindow.getContentPane().add(selectPictureLabel,"North");
- picSendWindow.getContentPane().add(picturesScrollPane,"Center");
- picSendWindow.getContentPane().add(clearPictureSelectionButton,"South");
- northWhosInPanel.setLayout(new GridLayout(1,2));
- northWhosInPanel.add(sendPrivateButton);
- northWhosInPanel.add(saveMessageButton);
- centerWhosInPanel.setLayout(new GridLayout(1,2));
- centerWhosInPanel.add(whosInScrollPane);
- centerWhosInPanel.add(whosNotScrollPane);
- southWhosInPanel.setLayout(new GridLayout(1,2));
- southWhosInPanel.add(clearPrivateButton);
- southWhosInPanel.add(clearSaveButton);
- northPanel.setLayout(new GridLayout(1, 3));
- northPanel.add(northWhosInPanel);
- northPanel.add(sendButton);
- northPanel.add(incomingMessagesLabel);
- chatWindow.getContentPane().add(northPanel, "North");
- centerPanel.setLayout(new GridLayout(1, 3));
- centerPanel.add(centerWhosInPanel);
- centerPanel.add(inScrollPane);
- centerPanel.add(outScrollPane);
- chatWindow.getContentPane().add(centerPanel, "Center");
- southPanel.setLayout(new GridLayout(1,3));
- southPanel.add(southWhosInPanel);
- southPanel.add(errorMessageLabel);
- southPanel.add(previewPicturesButton);
- chatWindow.getContentPane().add(southPanel, "South");
- //set the attributes
- picSendWindow.setTitle("Pictures in " + localDir);
- selectPictureLabel.setForeground(new Color(59, 89, 152));
- selectPictureLabel.setFont(new Font("Segoe UI", Font.PLAIN, 14));
- clearPictureSelectionButton.setBackground(new Color(59, 89, 152));
- clearPictureSelectionButton.setForeground(Color.white);
- clearPictureSelectionButton.setFont(new Font("Segoe UI Bold", Font.PLAIN, 14));
- picturesToSendList.setSelectionMode(0); // single-select
- chatWindow.setTitle(chatName + "'s Chat Window! (Close window to leave the Chat Room.)");
- chatWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- chatWindow.setSize(960, 540); //width, height (in "pixels")
- chatWindow.setVisible(true); //show it
- sendButton.setBackground(new Color(0, 128, 0));
- sendButton.setForeground(Color.white);
- sendButton.setFont(new Font("Segoe UI Bold", Font.PLAIN, 14));
- sendPrivateButton.setBackground(new Color(204,0,0));
- sendPrivateButton.setForeground(Color.white);
- sendPrivateButton.setFont(new Font("Segoe UI Bold", Font.PLAIN, 14));
- saveMessageButton.setBackground(new Color(255, 255,0));
- saveMessageButton.setForeground(Color.black);
- saveMessageButton.setFont(new Font("Segoe UI Bold", Font.PLAIN, 14));
- // previewPicturesButton.setBackground(new Color(240,240,240));
- // previewPicturesButton.setForeground(Color.gray);
- previewPicturesButton.setFont(new Font("Segoe UI Bold", Font.PLAIN, 14));
- clearPrivateButton.setFont(new Font("Segoe UI Bold", Font.PLAIN, 14));
- clearSaveButton.setFont(new Font("Segoe UI Bold", Font.PLAIN, 14));
- errorMessageLabel.setForeground(new Color(204,0,0));
- errorMessageLabel.setHorizontalAlignment(SwingConstants.CENTER);
- incomingMessagesLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
- incomingMessagesLabel.setHorizontalAlignment(SwingConstants.CENTER);
- incomingMessagesLabel.setOpaque(true);
- incomingMessagesLabel.setBackground(new Color(59, 89, 152));
- incomingMessagesLabel.setForeground(Color.white);
- inTextArea.setFont(new Font("Arial", Font.PLAIN, 20));
- // inTextArea.setBackground(new Color(240, 240, 240));
- inTextArea.setBackground(Color.white);
- inTextArea.setLineWrap(true);
- inTextArea.setWrapStyleWord(true);
- outTextArea.setFont(new Font("Tahoma", Font.PLAIN, 20));
- outTextArea.setEditable(false);
- outTextArea.setBackground(new Color(216, 223, 234));
- outTextArea.setLineWrap(true);
- outTextArea.setWrapStyleWord(true);
- sendButton.addActionListener(this); //give our program's address to the button
- sendPrivateButton.addActionListener(this);
- saveMessageButton.addActionListener(this);
- clearPrivateButton.addActionListener(this);
- clearSaveButton.addActionListener(this);
- previewPicturesButton.addActionListener(this);
- clearPictureSelectionButton.addActionListener(this);
- picturesToSendList.addListSelectionListener(this);
- }
- //=====================================================================================
- //sendButton waits for user to enter chat, then calls here
- public void actionPerformed(ActionEvent ae)
- {
- errorMessageLabel.setText(" "); // clear
- if (ae.getSource() == clearPrivateButton)
- {
- whosInList.clearSelection();
- return;
- }
- if (ae.getSource() == clearSaveButton)
- {
- whosNotList.clearSelection();
- return;
- }
- if (ae.getSource() == previewPicturesButton) {
- System.out.println("PreviewPicturesButton pushed");
- String[] listOfFiles = localDir.list();
- Vector<ImageIcon> pictures = new Vector<ImageIcon>();
- for (String fileName : listOfFiles) {
- if (fileName.endsWith(".gif")
- || fileName.endsWith(".jpg")
- || fileName.endsWith(".png")) {
- pictures.add(new ImageIcon(fileName, fileName
- + " from [" + chatName + "]"));
- }
- }
- if (pictures.isEmpty()) {
- errorMessageLabel.setText("No images in current directory!");
- System.out.println("No JPEG, GIF, or PNG images found in local directory ["
- + localDir + "]");
- return;
- }
- System.out.println(pictures.size() + " images found: " + pictures);
- picturesToSendList.setListData(pictures);
- //put pictures window next to chatWindow
- picSendWindow.setLocation(chatWindow.getX() + chatWindow.getWidth(), chatWindow.getY());
- //set pictures window size
- picSendWindow.setSize(1600 - chatWindow.getWidth(), chatWindow.getHeight());
- picSendWindow.setVisible(true);
- return;
- }
- if (ae.getSource() == clearPictureSelectionButton) {
- picturesToSendList.clearSelection();
- selectPictureLabel.setText("Select a Picture to Send");
- return;
- }
- //common processing for all send buttons
- ImageIcon pictureToSend = picturesToSendList.getSelectedValue(); //null means pic not selected
- String chat = inTextArea.getText().trim(); //length 0 means no msg
- if ((chat.length() == 0) && (pictureToSend == null)) {
- errorMessageLabel.setText("NO MESSAGE ENTERED, NO PICTURE SELECTED");
- return; //return early if nothing to send
- }
- boolean msgAndPic, picOnly, msgOnly;
- msgOnly = (chat.length() != 0) && (pictureToSend == null);
- picOnly = (chat.length() == 0) && (pictureToSend != null);
- msgAndPic = (chat.length() != 0) && (pictureToSend != null);
- Object thingToSend = null;
- if (ae.getSource() == sendButton)
- {
- if (msgOnly) {
- thingToSend = chat;
- System.out.println("Sending \"" + chat + "\" to all.");
- }
- else if (picOnly) {
- thingToSend = pictureToSend;
- System.out.println("Sending image to all with description \""
- + pictureToSend.getDescription() + "\"");
- }
- else if (msgAndPic) {
- ImageIcon copy = new ImageIcon(pictureToSend.getImage(),
- "from " + chatName + ": " + chat);
- thingToSend = copy;
- System.out.println("Sending image to all with description \""
- + copy.getDescription() + "\"");
- }
- // send to server
- try {
- oos.writeObject(thingToSend);
- } catch(IOException ioe) // main thread will also see error
- {
- errorMessageLabel.setText("CHAT SERVER CONNECTION FAILURE!");
- inTextArea.setEditable(false);// keep cursor out
- }
- inTextArea.setText(""); // write blank to clear input area
- picturesToSendList.clearSelection(); // clear picture send area
- selectPictureLabel.setText("Select a Picture to Send");
- }
- if (ae.getSource() == sendPrivateButton)
- {
- java.util.List<String> whosIn = whosInList.getSelectedValuesList();
- if (whosIn.isEmpty())
- {
- errorMessageLabel.setText("No Recipients Selected");
- return;
- }
- if (msgOnly) {
- String[] privateMsgArray = new String[whosIn.size() + 1];
- privateMsgArray[0] = chat;
- int i = 1;
- for (String chatName : whosIn) privateMsgArray[i++] = (String) chatName;
- thingToSend = privateMsgArray;
- try {
- oos.writeObject(thingToSend);
- } catch(IOException ioe) // main thread will also see error...
- {
- errorMessageLabel.setText("Chat Server Connection Failure");
- inTextArea.setEditable(false);// keep cursor out
- }
- System.out.println("Sending private message \"" + chat + "\" to " + whosIn);
- }
- else if (picOnly) {
- Object[] privatePicArray = new Object[whosIn.size() + 1];
- privatePicArray[0] = pictureToSend;
- int i = 1;
- for (String chatName : whosIn) privatePicArray[i++] = (String) chatName;
- thingToSend = privatePicArray;
- try {
- oos.writeObject(thingToSend);
- }
- catch(IOException ioe) // main thread will also see error...
- {
- errorMessageLabel.setText("Chat Server Connection Failure");
- inTextArea.setEditable(false);// keep cursor out
- }
- System.out.println("Sending private image with description \""
- + pictureToSend.getDescription() + "\" to " + whosIn);
- }
- else if (msgAndPic) {
- ImageIcon copy = new ImageIcon(pictureToSend.getImage(),
- "from " + chatName + ": " + chat);
- thingToSend = copy;
- Object[] privMsgPicArray = new Object[whosIn.size() + 1];
- privMsgPicArray[0] = thingToSend;
- int i = 1;
- for (String chatName : whosIn) privMsgPicArray[i++] = (String) chatName;
- try {
- oos.writeObject(privMsgPicArray);
- } catch (IOException ioe) // main thread will also see error...
- {
- errorMessageLabel.setText("Chat Server Connection Failure");
- inTextArea.setEditable(false);// keep cursor out
- }
- System.out.println("Sending private image with description \""
- + copy.getDescription() + "\" to " + whosIn);
- }
- // System.out.println("Sending private message '" + chat + "' to " + whosIn);
- inTextArea.setText(""); // write blank to clear input area
- picturesToSendList.clearSelection(); // clear picture send area
- selectPictureLabel.setText("Select a Picture to Send");
- }
- if (ae.getSource() == saveMessageButton)
- {
- java.util.List<String> whosNotIn = whosNotList.getSelectedValuesList();
- if (whosNotIn.isEmpty())
- {
- errorMessageLabel.setText("No Recipients Selected");
- return;
- }
- inTextArea.setText(""); // clear
- if (msgOnly) {
- String[] saveMsgArray = new String[whosNotIn.size() + 1];
- saveMsgArray[0] = chat;
- int i = 1;
- for (String chatName : whosNotIn) saveMsgArray[i++] = chatName;
- thingToSend = saveMsgArray;
- try {
- oos.writeObject(thingToSend);
- } catch (IOException ioe) {
- errorMessageLabel.setText("Chat Server Connection Failure");
- inTextArea.setEditable(false); //keep cursor out
- }
- System.out.println("Saving message '" + chat + "' for " + whosNotIn);
- }
- else if (picOnly) {
- Object[] savePicArray = new Object[whosNotIn.size() + 1];
- savePicArray[0] = pictureToSend;
- int i = 1;
- for (String chatName : whosNotIn) savePicArray[i++] = chatName;
- thingToSend = savePicArray;
- try {
- oos.writeObject(thingToSend);
- } catch (IOException ioe){ // main thread will also see error...
- errorMessageLabel.setText("Chat Server Connection Failure");
- inTextArea.setEditable(false);// no more sending!
- }
- System.out.println("Saving private image with description \""
- + pictureToSend.getDescription() + "\" for " + whosNotIn);
- }
- else if (msgAndPic) {
- ImageIcon copy = new ImageIcon(pictureToSend.getImage(),
- "from " + chatName + ": " + chat);
- thingToSend = copy;
- Object[] saveMsgPicArray = new Object[whosNotIn.size() + 1];
- saveMsgPicArray[0] = thingToSend;
- int i = 1;
- for (String chatName : whosNotIn) saveMsgPicArray[i++] = chatName;
- try {
- oos.writeObject(saveMsgPicArray);
- } catch (IOException ioe){ // main thread will also see error...
- errorMessageLabel.setText("Chat Server Connection Failure");
- inTextArea.setEditable(false);// no more sending!
- }
- System.out.println("Saving private image with description \""
- + copy.getDescription() + "\" to " + whosNotIn);
- }
- inTextArea.setText(""); // write blank to clear input area
- picturesToSendList.clearSelection(); // clear picture send area
- selectPictureLabel.setText("Select a Picture to Send");
- }
- } // GUI thread returns to calling GUI object
- //=====================================================================================
- //main thread branches in here
- //this is the "receive" method (writes to the outTextArea)
- public void showIncomingMessage(String messageFromServer) {
- outTextArea.append(newLine + messageFromServer);
- //scroll to the bottom
- outTextArea.setCaretPosition(outTextArea.getDocument().getLength());
- }
- public void showIncomingPicture(ImageIcon picture) {
- JFrame receivedPictureWindow = new JFrame();
- JList<ImageIcon> receivedPictureList = new JList<ImageIcon>();
- // code to compose and show the window
- receivedPictureWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- receivedPictureWindow.setTitle(picture.getDescription());
- receivedPictureWindow.getContentPane().add(receivedPictureList,"Center");
- ImageIcon[] arrayWithPicture = {picture}; //make an array for adding to the JList
- receivedPictureList.setListData(arrayWithPicture);
- // set dimensions of picture window
- receivedPictureWindow.setLocation(100,500); // left margin below chat
- int pictureWidth = picture.getImage().getWidth(receivedPictureWindow);
- int pictureHeight= picture.getImage().getHeight(receivedPictureWindow);
- receivedPictureWindow.setSize(pictureWidth, pictureHeight);
- receivedPictureWindow.setVisible(true); // show it!
- }
- public void showWhosIn(String[] whosIn)
- {
- whosInList.setListData(whosIn);
- }
- public void showWhosNotIn(String[] whosNotIn)
- {
- whosNotList.setListData(whosNotIn);
- }
- public void valueChanged(ListSelectionEvent lse) {
- if (lse.getValueIsAdjusting()) return; // user is still selecting!
- ImageIcon selectedPicture = picturesToSendList.getSelectedValue();
- if (selectedPicture == null) return; // selection has been cleared!
- String description = selectedPicture.getDescription();
- selectPictureLabel.setText(description);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement