Package client

Source Code of client.ClientApplet$ShowConfirmationBox

/*- ClientApplet.java ---------------------------------------------+
|                                                                 |
|  Copyright (C) 2008-2009 Prateek Jain, pchat                    |
|                     prateekjainaa@gmail.com                     |
|              http://prateekjainaa.blogspot.com                  |
|                                                                 |
| This program is free software; you can redistribute it and/or   |
| modify it under the terms of the GNU General Public License     |
| as published by the Free Software Foundation; either version 2  |
| of the License, or (at your option) any later version           |
|                                                                 |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of  |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   |
| GNU General Public License for more details.                    |
|                                                                 |
| A copy of the GNU General Public License may be found in the    |
| installation directory named "GNUGPL.txt"                       |
|                                                                 |
+-----------------------------------------------------------------+
*/

package client;

import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import commoms.CommandIF;
import commoms.CommandImpl;
import commoms.ProtocolConstants;

public class ClientApplet extends JApplet implements ActionListener,
  ListSelectionListener, KeyListener {

    private static final long serialVersionUID = -3081553677085569841L;
    public String locationURL;
    String site;
    long maxUploadFileSize;
    JList jlist;
    RichEditor red;
    JTextArea jta;
    JButton jb1;
    Container c;
    static boolean mute = Boolean.FALSE;
    static String message;
    String sendername, server;
    DefaultListModel dlm;
    String privateMsgTo;
    final String label = "<html><font size=\"4\">send</font></html>";
    AudioClip loginSound;
    AudioClip recieveSound, alertSound;
    JButton smileyButton, muteButton;

    String smileyText = "Emotions";
    String muteText = "Mute";
    String ohText = "<input type=\"hidden\" name=\"ohButton\">";
    String smText = "<input type=\"hidden\" name=\"smButton\">";
    String sadText = "<input type=\"hidden\" name=\"sadButton\">";
    String tongueText = "<input type=\"hidden\" name=\"tongueButton\">";
    String winkingText = "<input type=\"hidden\" name=\"winkingButton\">";
    String questionText = "<input type=\"hidden\" name=\"questionButton\">";
    String sendFileText = "Send File";
   
    SmileyDialog dial;
    ShowMessage showMessage;
    JButton sm, oh, sad, tongue, winking, question, sendFile;
    ImageIcon icon8;
    Image image8, image9;
    JLabel info;

    JLabel confirmation, saveMsg;
    JButton conf_yes, conf_no;
    ShowConfirmationBox showConfirmationBox;
    boolean acceptFile = false;
    String acceptText = "Accept";
    String ignoreText = "Ignore";
    boolean userPressed = false;
   
    ShowSuccessMessage ssm;
   
    public String getSendername() {
  return sendername;
    }

    int port;
    AppletConnection ac;
    String imgPath;

    public String getImgPath() {
  return imgPath;
    }

    public void init() {

  server = getParameter("serverip");
  port = Integer.parseInt(getParameter("serverport"));
  sendername = getParameter("sender");
 
  maxUploadFileSize = (getParameter("uploadFileSize") == null)? 10000000 : Long.parseLong(getParameter("uploadFileSize"));
 
  imgPath = "http://" + server + "/resources/";

  initGUI();
  loginSound = getAudioClip(getDocumentBase(), "resources/login.wav");
  recieveSound = getAudioClip(getDocumentBase(), "resources/receive.wav");
  alertSound = getAudioClip(getDocumentBase(), "resources/alert.wav");
  ac = new AppletConnection(server, port, this);
  ac.start();
  CommandIF com = makeCommandForSendToAllMessage(sendername,
    "welcome here\n");
  ac.sendMessage(com);
    }

    private void initGUI() {
  try {
      message = "@@ Divya Jyoti Jagrati Sansthan @@";
      c = getContentPane();
      setSize(new Dimension(615, 392));
      c.setLayout(null);
      c.setBackground(Color.WHITE);

      dlm = new DefaultListModel();

      jlist = new JList(dlm);
      jlist.setBorder(new TitledBorder("users online"));
      jlist
        .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
      jlist.addListSelectionListener(this);
      jlist.setSize(new Dimension(100, 392));
      c.add(jlist);

      red = new RichEditor(this);
      JScrollPane textScroller = new JScrollPane(red,
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      textScroller.setBounds(110, 0, 500, 270);
      c.add(textScroller);

      // add those smiley buttons here
      smileyButton = new JButton();
      smileyButton.setBackground(Color.WHITE);
      ImageIcon icon = new ImageIcon();
      Image image = getImage(getDocumentBase(), "resources/smiley.png");
      icon.setImage(image);
      smileyButton.setIcon(icon);
      smileyButton.setBounds(112, 275, 40, 30);
      smileyButton.setToolTipText("emotions");
      smileyButton.setText(smileyText);
      smileyButton.addActionListener(this);
      c.add(smileyButton);

      muteButton = new JButton();
      muteButton.setBackground(Color.WHITE);
      image8 = getImage(getDocumentBase(), "resources/musical-note.png");
      image9 = getImage(getDocumentBase(), "resources/peace.png");
      icon8 = new ImageIcon();
      icon8.setImage(image8);
      muteButton.setIcon(icon8);
      muteButton.setBounds(152, 275, 40, 30);
      muteButton.setToolTipText("Mute on");
      muteButton.setText(muteText);
      muteButton.addActionListener(this);
      c.add(muteButton);

      sendFile = new JButton(sendFileText);
      sendFile.setBackground(Color.WHITE);
      sendFile.setBounds(192, 275, 100, 30);
      sendFile.addActionListener(this);
      c.add(sendFile);

      jta = new JTextArea();
      jta.setText("Type your message here.");
      jta.addKeyListener(this);
      JScrollPane jsp = new JScrollPane(jta,
        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      TitledBorder border = new TitledBorder("Type here");
      jsp.setBackground(Color.WHITE);
      jsp.setBorder(border);
      jsp.setBounds(110, 305, 400, 80);
      c.add(jsp);

      jb1 = new JButton();
      jb1.setText(label);
      jb1.setBackground(Color.WHITE);
      jb1.setBounds(515, 315, 90, 60);
      jb1.addActionListener(this);
      c.add(jb1);

      // smiley dialog code here!
      dial = new SmileyDialog();
      dial.setVisible(false);

      sm = new JButton();
      ImageIcon icon1 = new ImageIcon();
      Image image1 = getImage(getDocumentBase(), "resources/smiley.png");
      icon1.setImage(image1);
      sm.setIcon(icon1);
      sm.setBackground(Color.WHITE);
      sm.setToolTipText(":)");
      sm.setText(smText);
      sm.addActionListener(this);
      dial.add(sm);

      oh = new JButton();
      ImageIcon icon2 = new ImageIcon();
      Image image2 = getImage(getDocumentBase(), "resources/oh.png");
      icon2.setImage(image2);
      oh.setIcon(icon2);
      oh.setBackground(Color.WHITE);
      oh.setToolTipText(":o");
      oh.setText(ohText);
      oh.addActionListener(this);
      dial.add(oh);

      sad = new JButton();
      ImageIcon icon3 = new ImageIcon();
      Image image3 = getImage(getDocumentBase(), "resources/sad.png");
      icon3.setImage(image3);
      sad.setIcon(icon3);
      sad.setBackground(Color.WHITE);
      sad.setToolTipText(":(");
      sad.setText(sadText);
      sad.addActionListener(this);
      dial.add(sad);

      tongue = new JButton();
      ImageIcon icon4 = new ImageIcon();
      tongue.setBackground(Color.WHITE);
      Image image4 = getImage(getDocumentBase(), "resources/tongue.png");
      icon4.setImage(image4);
      tongue.setIcon(icon2);
      tongue.setToolTipText(":p");
      tongue.setText(tongueText);
      tongue.addActionListener(this);
      dial.add(tongue);

      winking = new JButton();
      ImageIcon icon5 = new ImageIcon();
      winking.setBackground(Color.WHITE);
      Image image5 = getImage(getDocumentBase(), "resources/winking.png");
      icon5.setImage(image5);
      winking.setIcon(icon5);
      winking.setToolTipText(":$");
      winking.setText(winkingText);
      winking.addActionListener(this);
      dial.add(winking);

      question = new JButton();
      ImageIcon icon6 = new ImageIcon();
      question.setBackground(Color.WHITE);
      Image image6 = getImage(getDocumentBase(), "resources/question.png");
      icon6.setImage(image6);
      question.setIcon(icon6);
      question.setToolTipText(":$");
      question.setText(questionText);
      question.addActionListener(this);
      dial.add(question);

      showMessage = new ShowMessage();
      showMessage.setVisible(false);
      info = new JLabel();
      info.setText("Use this to present user some information");
      info.setBackground(Color.WHITE);
      showMessage.add(info);

      // for confirmation dialog.
      showConfirmationBox = new ShowConfirmationBox();
     
     
      conf_no = new JButton(ignoreText);
      conf_no.setBackground(Color.WHITE);
      conf_no.addActionListener(this);

      conf_yes = new JButton(acceptText);
      conf_yes.setBackground(Color.WHITE);
      conf_yes.addActionListener(this);

      confirmation = new JLabel("label here");
      confirmation.setBackground(Color.WHITE);
     
      
      showConfirmationBox.add(confirmation);
      showConfirmationBox.add(conf_yes);
      showConfirmationBox.add(conf_no);
      showConfirmationBox.setVisible(false);
     
     
      ssm = new ShowSuccessMessage();
      saveMsg = new JLabel("This will be overridden!");
      saveMsg.setBackground(Color.WHITE);
         
      ssm.add(saveMsg);
      ssm.setVisible(false);
     
  } catch (Exception e) {
      message = "Server down contact network administrator";
      e.printStackTrace();
  }

  showStatus(message);

    }

    public void actionPerformed(ActionEvent ae) {

  String s = ae.getActionCommand();

  if (s.equals(label)) {
      sendMessage();
  } else if (s.equals(smileyText)) {
      dial.setVisible(true);
  } else if (s.equals(smText)) {
      jta.setText(jta.getText() + ":)");
  } else if (s.equals(sadText)) {
      jta.setText(jta.getText() + ":(");
  } else if (s.equals(ohText)) {
      jta.setText(jta.getText() + ":o");
  } else if (s.equals(tongueText)) {
      jta.setText(jta.getText() + ":p");
  } else if (s.equals(winkingText)) {
      jta.setText(jta.getText() + ":$");
  } else if (s.equals(questionText)) {
      jta.setText(jta.getText() + ":?");
  } else if (s.equals(muteText)) {
      mute = !mute;
      if (mute) {
    icon8.setImage(image9);
    muteButton.setToolTipText("Mute off");
      } else {
    icon8.setImage(image8);
    muteButton.setToolTipText("Mute on");
      }
  } else if (s.equals(sendFileText)) {
      System.out.println("User is trying to send file");
      CommandIF comm = makeSendFileCommand();
      if (null != comm) {
    ac.sendMessage(comm);
      }// if
  } else if (s.equals(acceptText)) {
      acceptFile = true;
      showConfirmationBox.setVisible(false);
      userPressed = true;
      ac.accept_reject = true;
  } else if (s.equals(ignoreText)) {
      acceptFile = false;
      showConfirmationBox.setVisible(false);
      userPressed = true;
      ac.accept_reject = true;
  }

    }

    public void valueChanged(ListSelectionEvent e) {

  Object[] objs = jlist.getSelectedValues();
  String obj = "";
  for (int i = 0; i < objs.length; i++) {
      obj += objs[i].toString() + ",";

  }
  obj = obj.substring(0, obj.length() - 1);

  privateMsgTo = obj;
    }

    protected RichEditor getRichEditor() {
  return red;
    }

    boolean updateUsersList(CommandIF comm) {
  boolean flag = true;
  try {
      List list = comm.getNewPeerList();
      dlm.removeAllElements();
      Iterator itr = list.iterator();

      int size = 0;
      dlm.add(size, ProtocolConstants.SEND_TO_ALL);
      while (itr.hasNext()) {

    dlm.add(size, itr.next());
    ++size;
      }
      String mess = comm.getMessage();
      red.error(mess);
      loginSound.play();
  } catch (Exception exp) {
      flag = false;
  }

  return flag;
    }

    /*
     * never ever uncomment this :) public void paint(Graphics g) {
     *
     * showStatus(message);
     *
     * }
     */

    public void keyPressed(KeyEvent e) {

    }

    public void keyReleased(KeyEvent e) {

    }

    public void keyTyped(KeyEvent e) {

  if (e.getKeyChar() == KeyEvent.VK_ENTER) {
      sendMessage();
  }

    }

    private void sendMessage() {
  String mess = null;
  try {
      mess = jta.getText();
      if ("".equals(mess.trim())) {
    return;
      }
      CommandIF comm = null;
      if ((null == privateMsgTo) || ("".equals(privateMsgTo))) {
    privateMsgTo = ProtocolConstants.SEND_TO_ALL;
      }
      if (privateMsgTo.equals(ProtocolConstants.SEND_TO_ALL)) {
    comm = makeCommandForSendToAllMessage(sendername, mess);
      } else {
    comm = makeCommandForPvtMessage(sendername, mess);
      }
      ac.sendMessage(comm);
      jta.setText("");
      jta.setEditable(true);

  } catch (Exception ie) {
      showStatus("Message sending failed");
      ie.printStackTrace();
  }
  showStatus("message sent");

    }

    CommandIF makeCommandForSendToAllMessage(String senderName, String msg) {
  CommandImpl impl = new CommandImpl();

  msg = formatMessage(msg);
  impl.setMessage(msg);
  impl.setCommand(ProtocolConstants.COMMAND_SEND);
  List list = new ArrayList();
  list.add(ProtocolConstants.SEND_TO_ALL);
  impl.setPrivateMessage(false);
  impl.setRecipientNames(list);
  impl.setMsgFrom(senderName);
  return impl;
    }

    CommandIF makeCommandForPvtMessage(String senderName, String msg) {
  CommandImpl impl = new CommandImpl();

  msg = formatMessage(msg);
  impl.setMessage(msg);
  impl.setCommand(ProtocolConstants.COMMAND_SEND);
  List list = new ArrayList();
  String[] strArr = privateMsgTo.split(",");
  for (String str : strArr) {
      list.add(str);
  }
  impl.setPrivateMessage(true);
  impl.setRecipientNames(list);
  impl.setMsgFrom(senderName);
  return impl;
    }

    String formatMessage(String typedMsg) {
  String msg = typedMsg;
  msg = msg.replaceAll("<", "&lt;");
  msg = msg.replaceAll(">", "&gt;");
  msg = msg.replaceAll("\n", "<br>");

  return msg;
    }

    public void start() {

    }

    @Override
    public void stop() {

  super.stop();
    }

    @Override
    public void destroy() {

  super.destroy();
    }

    String getSmileyPath() {
  return getImgPath() + "smiley.png";
    }

    String getSaddyPath() {
  return getImgPath() + "sad.png";
    }

    String getTonguePath() {
  return getImgPath() + "tongue.png";
    }

    String getOhPath() {
  return getImgPath() + "oh.png";
    }

    String getWinkingPath() {
  return getImgPath() + "winking.png";
    }

    String getQuestionPath() {
  return getImgPath() + "question.png";
    }

    void playRecieveSound() {
  if (!mute) {
      recieveSound.play();
  }
    }

    void playAlertSound() {
  if (!mute) {
      alertSound.play();
  }
    }

    private CommandIF makeSendFileCommand() {
  File f = null;
  JFileChooser fileChooser = new JFileChooser(System
    .getProperty("user.home"));
  int status = fileChooser.showOpenDialog(null);
  if (status == JFileChooser.APPROVE_OPTION) {
      f = fileChooser.getSelectedFile();

  } else {
      return null;
  }

  CommandImpl impl = new CommandImpl();
  impl.setCommand(ProtocolConstants.COMMAND_SEND);
  impl.setSendingFile(true);
  impl.setPrivateMessage(true);
  impl.setMsgFrom(sendername);
  impl.setFileName(f.getName());
  List list = new ArrayList();
  if ((null == privateMsgTo) || (privateMsgTo.equals(""))
    || (privateMsgTo.contains(ProtocolConstants.SEND_TO_ALL))) {
      showMessage.setTitle(" Error ");
      info.setText("\n    Please select valid users from left\n");
      showMessage.add(info);
      showMessage.setBounds(100, 300, 280, 100);
      showMessage.setVisible(true);
      playAlertSound();
      return null;
  }

  String[] strArr = privateMsgTo.split(",");
  for (String str : strArr) {
      list.add(str);
  }
  impl.setRecipientNames(list);

  try {
      FileInputStream fis = new FileInputStream(f);
      int size = fis.available();
      // size should be less than 10 MB
      // TODO make it configurable
      if (size > maxUploadFileSize) {
    fis.close();
    showMessage.setTitle(" Error ");
    info.setText("\n    File size exceeds max. limit permitted\n");
    showMessage.add(info);
    showMessage.setBounds(100, 300, 280, 100);
    showMessage.setVisible(true);
    playAlertSound();
    return null;
      }

      byte[] bytes = new byte[size];
      fis.read(bytes);

      impl.setFile(bytes);
      fis.close();

  } catch (IOException ioe) {
      ioe.printStackTrace();
  }

  return impl;
    }

   
   
    class SmileyDialog extends JDialog {

  public SmileyDialog() {
      setTitle("Emotions");
      setLayout(new GridLayout(2, 3));
      setBackground(Color.WHITE);
      setBounds(100, 300, 120, 130);

  }

    }

    class ShowMessage extends JDialog {

  public ShowMessage() {
      setLayout(new GridLayout(1, 1));
      setBackground(Color.WHITE);
  }
    }

    class ShowConfirmationBox extends JDialog {
  public ShowConfirmationBox() {
      setTitle("Confirm");
      //BoxLayout lay = new BoxLayout(this,BoxLayout.Y_AXIS);
      setLayout(new FlowLayout());
      setBounds(100, 300, 320, 130);
      setBackground(Color.WHITE);
  }
    }
   
    class ShowSuccessMessage extends JDialog {
  public ShowSuccessMessage() {
      setTitle("Success");
      //BoxLayout lay = new BoxLayout(this,BoxLayout.Y_AXIS);
      setLayout(new FlowLayout());
      setBounds(100, 300, 420, 80);
      setBackground(Color.WHITE);
  }
    }
}
TOP

Related Classes of client.ClientApplet$ShowConfirmationBox

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.