Package ants.p2p.gui

Source Code of ants.p2p.gui.PrivateChatFrame_jTextField1_keyAdapter

//******************************************************************
//******************************************************************
//**********          ANts Peer To Peer Sources        *************
//
// ANts P2P realizes a third generation P2P net. It protects your
// privacy while you are connected and makes you not trackable, hiding
// your identity (ip) and crypting everything you are sending/receiving
// from others.

// Copyright (C) 2004  Roberto Rossi

// 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.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

package ants.p2p.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.*;

import ants.p2p.*;
import ants.p2p.filesharing.*;
import ants.p2p.messages.PrivateChatMessage;

import org.apache.log4j.*;

public class PrivateChatFrame extends JFrame implements PropertyChangeListener{
  BorderLayout borderLayout1 = new BorderLayout();
  JScrollPane jScrollPane1 = new JScrollPane();
  JEditorPane jEditorPane1 = new JEditorPane();
  JTextField jTextField1 = new JTextField();

  String peerId = null;
  WarriorAnt wa = null;
  boolean initiator;

  public static Hashtable activeChats = new Hashtable();

  static Logger _logger = Logger.getLogger(PrivateChatFrame.class.getName());

  public PrivateChatFrame(String peerId, WarriorAnt wa, boolean initiator) {
    super("Chatting with: "+peerId.substring(0,10));
    this.setBounds(100,100,300,300);
    this.peerId = peerId;
    this.wa = wa;
    this.initiator = initiator;
    this.wa.getPropertyChangeSupport().addPropertyChangeListener(this);
    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  public static PrivateChatFrame getChatWith(String peerId, WarriorAnt wa, boolean initiator){
    PrivateChatFrame pcf = (PrivateChatFrame)PrivateChatFrame.activeChats.get(peerId);
    if(pcf != null) return pcf;
    else{
      pcf = new PrivateChatFrame(peerId, wa, initiator);
      activeChats.put(peerId, pcf);
      return pcf;
    }
  }

  public static void removeChatWith(String peerId, WarriorAnt wa){
    PrivateChatFrame pcf = (PrivateChatFrame)PrivateChatFrame.activeChats.get(peerId);
    if(pcf != null){
      activeChats.remove(peerId);
      wa.getPropertyChangeSupport().removePropertyChangeListener(pcf);
      pcf.hide();
    }
  }

  public static void removeAllChats(WarriorAnt wa){
    Enumeration keys = activeChats.keys();
    while(keys.hasMoreElements()){
      removeChatWith((String)keys.nextElement(),wa);
    }
  }

  public void propertyChange(PropertyChangeEvent e){
    if(e.getPropertyName().equals("privateChatMessageSending")){
      PrivateChatMessage pcm = (PrivateChatMessage)e.getNewValue();
      if(pcm.getDest().equals(this.peerId))
        this.setTitle("Chatting with: "+peerId.substring(0,10)+"   [Sending message...]");
    }else if(e.getPropertyName().equals("privateChatMessageFailed")){
      PrivateChatMessage pcm = (PrivateChatMessage)e.getNewValue();
      if(pcm.getDest().equals(this.peerId)){
        this.setTitle("Chatting with: " + peerId.substring(0, 10));
        JOptionPane.showMessageDialog(this, ji.JI.i("Error in sending message!"),
                                      ji.JI.i("Chat error"),
                                      JOptionPane.ERROR_MESSAGE);
      }
    }else if(e.getPropertyName().equals("privateChatMessageDelivered")){
      PrivateChatMessage pcm = (PrivateChatMessage)e.getNewValue();
      if(pcm.getDest().equals(this.peerId)){
        this.setTitle("Chatting with: " + peerId.substring(0, 10));
        this.jEditorPane1.setText(this.jEditorPane1.getText() +
                                  "You:\t" + pcm.getContent()+"\n");
      }
    }
  }

  public void insertExternalMessage(String message){
    this.jEditorPane1.setText(this.jEditorPane1.getText() +
                              this.peerId.substring(0,10)+":\t"+message+"\n");
  }

  void jbInit() throws Exception {
    this.getContentPane().setLayout(borderLayout1);
    jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    jScrollPane1.setAutoscrolls(true);
    jEditorPane1.setText("");
    jTextField1.setText("");
    jTextField1.addKeyListener(new PrivateChatFrame_jTextField1_keyAdapter(this));
    this.getContentPane().add(jScrollPane1,  BorderLayout.CENTER);
    this.getContentPane().add(jTextField1,  BorderLayout.SOUTH);
    jScrollPane1.getViewport().add(jEditorPane1, null);
  }

  void jTextField1_keyReleased(KeyEvent e) {
    if(e.getKeyChar()=='\n'){
      try{
        String message = this.jTextField1.getText();
        this.jTextField1.setText("");
        this.wa.sendPrivateChatMessage(this.peerId, message, this.initiator);
      }catch(Exception ex){
        _logger.error("",ex);
        JOptionPane.showMessageDialog(this, ji.JI.i("Error in sending message: ")+ex.getMessage(),ji.JI.i("Chat error"),JOptionPane.ERROR_MESSAGE);
      }
    }
  }

}

class PrivateChatFrame_jTextField1_keyAdapter extends java.awt.event.KeyAdapter {
  PrivateChatFrame adaptee;

  PrivateChatFrame_jTextField1_keyAdapter(PrivateChatFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void keyReleased(final KeyEvent e) {
    Thread runner = new Thread(){
      public void run(){
        adaptee.jTextField1_keyReleased(e);
      }
    };
    runner.start();
  }
}
TOP

Related Classes of ants.p2p.gui.PrivateChatFrame_jTextField1_keyAdapter

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.