Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.89 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.event.ListSelectionEvent;
  3. import javax.swing.event.ListSelectionListener;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.io.ObjectOutputStream;
  10. import java.util.*;
  11.  
  12. /**
  13. * Created by Nivesh Varma on 28 Jan 17.
  14. */
  15. public class PictureChatClientGUI implements ActionListener, ListSelectionListener {
  16. // GUI objects
  17.  
  18. private ObjectOutputStream oos;
  19. private String chatName;
  20.  
  21. private JFrame chatWindow = new JFrame();
  22. private JButton sendButton = new JButton("Send to All");
  23. private JTextArea inTextArea = new JTextArea();
  24. private JTextArea outTextArea = new JTextArea();
  25. private JScrollPane inScrollPane = new JScrollPane(inTextArea); //new
  26. private JScrollPane outScrollPane = new JScrollPane(outTextArea);
  27. private JPanel centerPanel = new JPanel();
  28. private JPanel northPanel = new JPanel();
  29. private JLabel incomingMessagesLabel = new JLabel(" Chat Messages");
  30. private String newLine = System.lineSeparator();
  31.  
  32. // picture GUI objects
  33. private JFrame picSendWindow = new JFrame();
  34. private JLabel selectPictureLabel = new JLabel("Select a picture to send");
  35. private JButton clearPictureSelectionButton = new JButton("Clear selection");
  36. private JList<ImageIcon> picturesToSendList = new JList<>();
  37. private JScrollPane picturesScrollPane = new JScrollPane(picturesToSendList);
  38.  
  39. //new GUI objects
  40. private JPanel southPanel = new JPanel();
  41. private JPanel northWhosInPanel = new JPanel();
  42. private JPanel centerWhosInPanel = new JPanel();
  43. private JPanel southWhosInPanel = new JPanel();
  44. private JButton sendPrivateButton = new JButton("Send Private To");
  45. private JButton saveMessageButton = new JButton("Save Message For");
  46. private JButton clearPrivateButton= new JButton("Clear Selection");
  47. private JButton clearSaveButton = new JButton("Clear Selection");
  48. private JButton previewPicturesButton = new JButton("Preview Pictures To Send");
  49. private JLabel errorMessageLabel = new JLabel();
  50.  
  51. private JList<String> whosInList = new JList<String>();
  52. private JList<String> whosNotList = new JList<String>();
  53. private JScrollPane whosInScrollPane = new JScrollPane(whosInList);
  54. private JScrollPane whosNotScrollPane = new JScrollPane(whosNotList);
  55.  
  56. private File localDir = new File(System.getProperty("user.dir"));
  57.  
  58. //=====================================================================================
  59.  
  60. //Constructor
  61.  
  62. public PictureChatClientGUI(String userChatName, ObjectOutputStream sendingOOS) {
  63. System.out.println("Local directory is:\n" + localDir);
  64.  
  65. oos = sendingOOS; //copy pointer fields from the main thread STACK
  66. chatName = userChatName; //into program-object fields in ChatClientGUI
  67.  
  68. // set the java "metal" look-&-feel
  69. try {
  70. UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  71. } catch (Exception e) {
  72. System.out.println(e); //but don't terminate if this fails
  73. }
  74.  
  75. // build the GUI
  76.  
  77. picSendWindow.getContentPane().add(selectPictureLabel,"North");
  78. picSendWindow.getContentPane().add(picturesScrollPane,"Center");
  79. picSendWindow.getContentPane().add(clearPictureSelectionButton,"South");
  80.  
  81. northWhosInPanel.setLayout(new GridLayout(1,2));
  82. northWhosInPanel.add(sendPrivateButton);
  83. northWhosInPanel.add(saveMessageButton);
  84.  
  85. centerWhosInPanel.setLayout(new GridLayout(1,2));
  86. centerWhosInPanel.add(whosInScrollPane);
  87. centerWhosInPanel.add(whosNotScrollPane);
  88.  
  89. southWhosInPanel.setLayout(new GridLayout(1,2));
  90. southWhosInPanel.add(clearPrivateButton);
  91. southWhosInPanel.add(clearSaveButton);
  92.  
  93. northPanel.setLayout(new GridLayout(1, 3));
  94. northPanel.add(northWhosInPanel);
  95. northPanel.add(sendButton);
  96. northPanel.add(incomingMessagesLabel);
  97. chatWindow.getContentPane().add(northPanel, "North");
  98.  
  99. centerPanel.setLayout(new GridLayout(1, 3));
  100. centerPanel.add(centerWhosInPanel);
  101. centerPanel.add(inScrollPane);
  102. centerPanel.add(outScrollPane);
  103. chatWindow.getContentPane().add(centerPanel, "Center");
  104.  
  105. southPanel.setLayout(new GridLayout(1,3));
  106. southPanel.add(southWhosInPanel);
  107. southPanel.add(errorMessageLabel);
  108. southPanel.add(previewPicturesButton);
  109. chatWindow.getContentPane().add(southPanel, "South");
  110.  
  111. //set the attributes
  112.  
  113. picSendWindow.setTitle("Pictures in " + localDir);
  114.  
  115. selectPictureLabel.setForeground(new Color(59, 89, 152));
  116. selectPictureLabel.setFont(new Font("Segoe UI", Font.PLAIN, 14));
  117.  
  118. clearPictureSelectionButton.setBackground(new Color(59, 89, 152));
  119. clearPictureSelectionButton.setForeground(Color.white);
  120. clearPictureSelectionButton.setFont(new Font("Segoe UI Bold", Font.PLAIN, 14));
  121.  
  122. picturesToSendList.setSelectionMode(0); // single-select
  123.  
  124. chatWindow.setTitle(chatName + "'s Chat Window! (Close window to leave the Chat Room.)");
  125. chatWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  126. chatWindow.setSize(960, 540); //width, height (in "pixels")
  127. chatWindow.setVisible(true); //show it
  128.  
  129. sendButton.setBackground(new Color(0, 128, 0));
  130. sendButton.setForeground(Color.white);
  131. sendButton.setFont(new Font("Segoe UI Bold", Font.PLAIN, 14));
  132.  
  133. sendPrivateButton.setBackground(new Color(204,0,0));
  134. sendPrivateButton.setForeground(Color.white);
  135. sendPrivateButton.setFont(new Font("Segoe UI Bold", Font.PLAIN, 14));
  136.  
  137. saveMessageButton.setBackground(new Color(255, 255,0));
  138. saveMessageButton.setForeground(Color.black);
  139. saveMessageButton.setFont(new Font("Segoe UI Bold", Font.PLAIN, 14));
  140.  
  141. // previewPicturesButton.setBackground(new Color(240,240,240));
  142. // previewPicturesButton.setForeground(Color.gray);
  143. previewPicturesButton.setFont(new Font("Segoe UI Bold", Font.PLAIN, 14));
  144.  
  145. clearPrivateButton.setFont(new Font("Segoe UI Bold", Font.PLAIN, 14));
  146. clearSaveButton.setFont(new Font("Segoe UI Bold", Font.PLAIN, 14));
  147.  
  148. errorMessageLabel.setForeground(new Color(204,0,0));
  149. errorMessageLabel.setHorizontalAlignment(SwingConstants.CENTER);
  150.  
  151. incomingMessagesLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
  152. incomingMessagesLabel.setHorizontalAlignment(SwingConstants.CENTER);
  153. incomingMessagesLabel.setOpaque(true);
  154. incomingMessagesLabel.setBackground(new Color(59, 89, 152));
  155. incomingMessagesLabel.setForeground(Color.white);
  156.  
  157. inTextArea.setFont(new Font("Arial", Font.PLAIN, 20));
  158. // inTextArea.setBackground(new Color(240, 240, 240));
  159. inTextArea.setBackground(Color.white);
  160. inTextArea.setLineWrap(true);
  161. inTextArea.setWrapStyleWord(true);
  162.  
  163. outTextArea.setFont(new Font("Tahoma", Font.PLAIN, 20));
  164. outTextArea.setEditable(false);
  165. outTextArea.setBackground(new Color(216, 223, 234));
  166. outTextArea.setLineWrap(true);
  167. outTextArea.setWrapStyleWord(true);
  168.  
  169. sendButton.addActionListener(this); //give our program's address to the button
  170. sendPrivateButton.addActionListener(this);
  171. saveMessageButton.addActionListener(this);
  172. clearPrivateButton.addActionListener(this);
  173. clearSaveButton.addActionListener(this);
  174. previewPicturesButton.addActionListener(this);
  175. clearPictureSelectionButton.addActionListener(this);
  176.  
  177. picturesToSendList.addListSelectionListener(this);
  178. }
  179.  
  180. //=====================================================================================
  181.  
  182. //sendButton waits for user to enter chat, then calls here
  183.  
  184. public void actionPerformed(ActionEvent ae)
  185. {
  186. errorMessageLabel.setText(" "); // clear
  187.  
  188. if (ae.getSource() == clearPrivateButton)
  189. {
  190. whosInList.clearSelection();
  191. return;
  192. }
  193.  
  194. if (ae.getSource() == clearSaveButton)
  195. {
  196. whosNotList.clearSelection();
  197. return;
  198. }
  199.  
  200. if (ae.getSource() == previewPicturesButton) {
  201. System.out.println("PreviewPicturesButton pushed");
  202. String[] listOfFiles = localDir.list();
  203. Vector<ImageIcon> pictures = new Vector<ImageIcon>();
  204.  
  205. for (String fileName : listOfFiles) {
  206. if (fileName.endsWith(".gif")
  207. || fileName.endsWith(".jpg")
  208. || fileName.endsWith(".png")) {
  209. pictures.add(new ImageIcon(fileName, fileName
  210. + " from [" + chatName + "]"));
  211. }
  212. }
  213.  
  214. if (pictures.isEmpty()) {
  215. errorMessageLabel.setText("No images in current directory!");
  216. System.out.println("No JPEG, GIF, or PNG images found in local directory ["
  217. + localDir + "]");
  218. return;
  219. }
  220. System.out.println(pictures.size() + " images found: " + pictures);
  221.  
  222. picturesToSendList.setListData(pictures);
  223.  
  224. //put pictures window next to chatWindow
  225. picSendWindow.setLocation(chatWindow.getX() + chatWindow.getWidth(), chatWindow.getY());
  226.  
  227. //set pictures window size
  228. picSendWindow.setSize(1600 - chatWindow.getWidth(), chatWindow.getHeight());
  229. picSendWindow.setVisible(true);
  230.  
  231. return;
  232. }
  233.  
  234. if (ae.getSource() == clearPictureSelectionButton) {
  235. picturesToSendList.clearSelection();
  236. selectPictureLabel.setText("Select a Picture to Send");
  237. return;
  238. }
  239.  
  240. //common processing for all send buttons
  241. ImageIcon pictureToSend = picturesToSendList.getSelectedValue(); //null means pic not selected
  242. String chat = inTextArea.getText().trim(); //length 0 means no msg
  243. if ((chat.length() == 0) && (pictureToSend == null)) {
  244. errorMessageLabel.setText("NO MESSAGE ENTERED, NO PICTURE SELECTED");
  245. return; //return early if nothing to send
  246. }
  247. boolean msgAndPic, picOnly, msgOnly;
  248. msgOnly = (chat.length() != 0) && (pictureToSend == null);
  249. picOnly = (chat.length() == 0) && (pictureToSend != null);
  250. msgAndPic = (chat.length() != 0) && (pictureToSend != null);
  251.  
  252. Object thingToSend = null;
  253.  
  254.  
  255. if (ae.getSource() == sendButton)
  256. {
  257. if (msgOnly) {
  258. thingToSend = chat;
  259. System.out.println("Sending \"" + chat + "\" to all.");
  260. }
  261. else if (picOnly) {
  262. thingToSend = pictureToSend;
  263. System.out.println("Sending image to all with description \""
  264. + pictureToSend.getDescription() + "\"");
  265. }
  266. else if (msgAndPic) {
  267. ImageIcon copy = new ImageIcon(pictureToSend.getImage(),
  268. "from " + chatName + ": " + chat);
  269. thingToSend = copy;
  270. System.out.println("Sending image to all with description \""
  271. + copy.getDescription() + "\"");
  272. }
  273.  
  274. // send to server
  275. try {
  276. oos.writeObject(thingToSend);
  277. } catch(IOException ioe) // main thread will also see error
  278. {
  279. errorMessageLabel.setText("CHAT SERVER CONNECTION FAILURE!");
  280. inTextArea.setEditable(false);// keep cursor out
  281. }
  282. inTextArea.setText(""); // write blank to clear input area
  283. picturesToSendList.clearSelection(); // clear picture send area
  284. selectPictureLabel.setText("Select a Picture to Send");
  285. }
  286.  
  287. if (ae.getSource() == sendPrivateButton)
  288. {
  289. java.util.List<String> whosIn = whosInList.getSelectedValuesList();
  290. if (whosIn.isEmpty())
  291. {
  292. errorMessageLabel.setText("No Recipients Selected");
  293. return;
  294. }
  295.  
  296. if (msgOnly) {
  297. String[] privateMsgArray = new String[whosIn.size() + 1];
  298. privateMsgArray[0] = chat;
  299.  
  300. int i = 1;
  301. for (String chatName : whosIn) privateMsgArray[i++] = (String) chatName;
  302. thingToSend = privateMsgArray;
  303.  
  304. try {
  305. oos.writeObject(thingToSend);
  306. } catch(IOException ioe) // main thread will also see error...
  307. {
  308. errorMessageLabel.setText("Chat Server Connection Failure");
  309. inTextArea.setEditable(false);// keep cursor out
  310. }
  311. System.out.println("Sending private message \"" + chat + "\" to " + whosIn);
  312. }
  313. else if (picOnly) {
  314. Object[] privatePicArray = new Object[whosIn.size() + 1];
  315. privatePicArray[0] = pictureToSend;
  316.  
  317. int i = 1;
  318. for (String chatName : whosIn) privatePicArray[i++] = (String) chatName;
  319.  
  320. thingToSend = privatePicArray;
  321.  
  322. try {
  323. oos.writeObject(thingToSend);
  324. }
  325. catch(IOException ioe) // main thread will also see error...
  326. {
  327. errorMessageLabel.setText("Chat Server Connection Failure");
  328. inTextArea.setEditable(false);// keep cursor out
  329. }
  330.  
  331. System.out.println("Sending private image with description \""
  332. + pictureToSend.getDescription() + "\" to " + whosIn);
  333. }
  334. else if (msgAndPic) {
  335. ImageIcon copy = new ImageIcon(pictureToSend.getImage(),
  336. "from " + chatName + ": " + chat);
  337. thingToSend = copy;
  338. Object[] privMsgPicArray = new Object[whosIn.size() + 1];
  339. privMsgPicArray[0] = thingToSend;
  340. int i = 1;
  341. for (String chatName : whosIn) privMsgPicArray[i++] = (String) chatName;
  342.  
  343. try {
  344. oos.writeObject(privMsgPicArray);
  345. } catch (IOException ioe) // main thread will also see error...
  346. {
  347. errorMessageLabel.setText("Chat Server Connection Failure");
  348. inTextArea.setEditable(false);// keep cursor out
  349. }
  350. System.out.println("Sending private image with description \""
  351. + copy.getDescription() + "\" to " + whosIn);
  352. }
  353.  
  354. // System.out.println("Sending private message '" + chat + "' to " + whosIn);
  355.  
  356. inTextArea.setText(""); // write blank to clear input area
  357. picturesToSendList.clearSelection(); // clear picture send area
  358. selectPictureLabel.setText("Select a Picture to Send");
  359. }
  360.  
  361. if (ae.getSource() == saveMessageButton)
  362. {
  363. java.util.List<String> whosNotIn = whosNotList.getSelectedValuesList();
  364. if (whosNotIn.isEmpty())
  365. {
  366. errorMessageLabel.setText("No Recipients Selected");
  367. return;
  368. }
  369. inTextArea.setText(""); // clear
  370.  
  371. if (msgOnly) {
  372. String[] saveMsgArray = new String[whosNotIn.size() + 1];
  373. saveMsgArray[0] = chat;
  374. int i = 1;
  375. for (String chatName : whosNotIn) saveMsgArray[i++] = chatName;
  376. thingToSend = saveMsgArray;
  377.  
  378. try {
  379. oos.writeObject(thingToSend);
  380. } catch (IOException ioe) {
  381. errorMessageLabel.setText("Chat Server Connection Failure");
  382. inTextArea.setEditable(false); //keep cursor out
  383. }
  384.  
  385. System.out.println("Saving message '" + chat + "' for " + whosNotIn);
  386. }
  387.  
  388. else if (picOnly) {
  389. Object[] savePicArray = new Object[whosNotIn.size() + 1];
  390. savePicArray[0] = pictureToSend;
  391. int i = 1;
  392. for (String chatName : whosNotIn) savePicArray[i++] = chatName;
  393. thingToSend = savePicArray;
  394.  
  395. try {
  396. oos.writeObject(thingToSend);
  397. } catch (IOException ioe){ // main thread will also see error...
  398. errorMessageLabel.setText("Chat Server Connection Failure");
  399. inTextArea.setEditable(false);// no more sending!
  400. }
  401.  
  402. System.out.println("Saving private image with description \""
  403. + pictureToSend.getDescription() + "\" for " + whosNotIn);
  404. }
  405.  
  406. else if (msgAndPic) {
  407. ImageIcon copy = new ImageIcon(pictureToSend.getImage(),
  408. "from " + chatName + ": " + chat);
  409. thingToSend = copy;
  410. Object[] saveMsgPicArray = new Object[whosNotIn.size() + 1];
  411. saveMsgPicArray[0] = thingToSend;
  412. int i = 1;
  413. for (String chatName : whosNotIn) saveMsgPicArray[i++] = chatName;
  414.  
  415. try {
  416. oos.writeObject(saveMsgPicArray);
  417. } catch (IOException ioe){ // main thread will also see error...
  418. errorMessageLabel.setText("Chat Server Connection Failure");
  419. inTextArea.setEditable(false);// no more sending!
  420. }
  421.  
  422. System.out.println("Saving private image with description \""
  423. + copy.getDescription() + "\" to " + whosNotIn);
  424. }
  425.  
  426. inTextArea.setText(""); // write blank to clear input area
  427. picturesToSendList.clearSelection(); // clear picture send area
  428. selectPictureLabel.setText("Select a Picture to Send");
  429. }
  430.  
  431. } // GUI thread returns to calling GUI object
  432.  
  433. //=====================================================================================
  434.  
  435. //main thread branches in here
  436. //this is the "receive" method (writes to the outTextArea)
  437.  
  438. public void showIncomingMessage(String messageFromServer) {
  439. outTextArea.append(newLine + messageFromServer);
  440. //scroll to the bottom
  441. outTextArea.setCaretPosition(outTextArea.getDocument().getLength());
  442. }
  443.  
  444. public void showIncomingPicture(ImageIcon picture) {
  445. JFrame receivedPictureWindow = new JFrame();
  446. JList<ImageIcon> receivedPictureList = new JList<ImageIcon>();
  447.  
  448. // code to compose and show the window
  449. receivedPictureWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  450. receivedPictureWindow.setTitle(picture.getDescription());
  451. receivedPictureWindow.getContentPane().add(receivedPictureList,"Center");
  452. ImageIcon[] arrayWithPicture = {picture}; //make an array for adding to the JList
  453. receivedPictureList.setListData(arrayWithPicture);
  454.  
  455. // set dimensions of picture window
  456. receivedPictureWindow.setLocation(100,500); // left margin below chat
  457. int pictureWidth = picture.getImage().getWidth(receivedPictureWindow);
  458. int pictureHeight= picture.getImage().getHeight(receivedPictureWindow);
  459. receivedPictureWindow.setSize(pictureWidth, pictureHeight);
  460. receivedPictureWindow.setVisible(true); // show it!
  461. }
  462.  
  463. public void showWhosIn(String[] whosIn)
  464. {
  465. whosInList.setListData(whosIn);
  466. }
  467.  
  468. public void showWhosNotIn(String[] whosNotIn)
  469. {
  470. whosNotList.setListData(whosNotIn);
  471. }
  472.  
  473. public void valueChanged(ListSelectionEvent lse) {
  474. if (lse.getValueIsAdjusting()) return; // user is still selecting!
  475. ImageIcon selectedPicture = picturesToSendList.getSelectedValue();
  476. if (selectedPicture == null) return; // selection has been cleared!
  477. String description = selectedPicture.getDescription();
  478. selectPictureLabel.setText(description);
  479. }
  480. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement