⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msgrsessiongui.java

📁 基于jxta的P2P框架的系统
💻 JAVA
字号:
/**
 * -- Copyright (C) 2006 Hisham Khalil. All rights reserved.
 *
 * 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,
 *
 * Author: Hisham Khalil <hishberlin@hotmail.com>
 */


package connex.plugins.Messenger;

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.text.html.HTMLEditorKit;
import java.io.File;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.util.GregorianCalendar;
import connex.app.utils.ToolBar;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import org.jdesktop.swingx.border.DropShadowBorder;
import org.jdesktop.swingx.JXPanel;
import connex.app.utils.fileChooserUtils.ImagePreview;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class MsgrSessionGUI
    extends JFrame implements PropertyChangeListener {
  /**
   * @directed
   */
  private SendListener session;

  JPanel jPanel1 = new JPanel();

  JPanel jPanel4 = new JPanel();
  JPanel jPanel6 = new JPanel();
  JPanel jPanel8 = new JPanel();
  JPanel jPanel12 = new JPanel();
  JPanel jPanel13 = new JPanel();
  JPanel jPanel3 = new JPanel();

  JXPanel jPanel2 = new JXPanel();
  JXPanel jPanel14 = new JXPanel();
  JXPanel jPanel9 = new JXPanel();

  MyImagePanel myImage = new MyImagePanel();
  MyImagePanel hisImage = new MyImagePanel();

  JFileChooser fc;
  JButton sendBtn = new JButton();

  /**
   * @link aggregation
   * @directed
   */
  ChatEditor te;

  JScrollPane jScrollPane2 = new JScrollPane();
  JEditorPane board = new JEditorPane();

  ImageIcon dimg = new ImageIcon("images" + File.separator + "user.jpg");
  ImageIcon save = new ImageIcon(MsgrSessionGUI.class.getResource(
      "save_small.png"));
  File myPic;

  boolean freezed = false;

  DropShadowBorder border5 = new DropShadowBorder(Color.black, 10, 7, .7f, 12, false, true, true, true);
  BorderLayout borderLayout1 = new BorderLayout();
  BorderLayout borderLayout2 = new BorderLayout();
  BorderLayout borderLayout3 = new BorderLayout();
  ToolBar jbar5 = new ToolBar();

  JPanel jPanel7 = new JPanel();
  ToolBar jPanel11 = new ToolBar();

  JButton jButton2 = new JButton(save);

  BorderLayout borderLayout4 = new BorderLayout();
  BorderLayout borderLayout5 = new BorderLayout();
  BorderLayout borderLayout6 = new BorderLayout();
  BorderLayout borderLayout7 = new BorderLayout();

  public MsgrSessionGUI(String name) {
    super(name);
    try {
      jbInit();
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }
  }

  public void setSender(SendListener session) {
    this.session = session;
  }

  public JEditorPane getBoard() {

    return board;
  }

  public void setHisImage(ImageIcon img) {
    //this.hisImage.setIcon(img);

    this.hisImage.setImage(img.getImage());
    this.hisImage.repaint();

  }

  public void setMyImage(ImageIcon img) {

  }

  private void jbInit() throws Exception {

    this.setSize(526, 432);
    this.getContentPane().setLayout(borderLayout5);

    jPanel1.setBorder(null);
    jPanel1.setBounds(new Rectangle(8, 12, 345, 293));
    jPanel1.setLayout(borderLayout1);
    jPanel3.setBackground(new Color(235, 235, 235));
    jPanel3.setBorder(null);
    jPanel3.setPreferredSize(new Dimension(150, 1));
    jPanel3.setBounds(new Rectangle(356, 7, 157, 303));
    jPanel3.setLayout(null);

    board.setEditorKit(new HTMLEditorKit());
    board.setEditable(false);
    sendBtn.addActionListener(new MsgrSessionGUI_sendBtn_actionAdapter(this));
    myImage.setOpaque(true);
    myImage.setBorder(border5);
    myImage.setOpaque(true);
    myImage.addPropertyChangeListener(this);
    myImage.setBounds(new Rectangle(5, 125, 140, 120));
    myImage.setEditable(true);

    this.setMyImage(dimg);
    this.addWindowListener(new MsgrSessionGUI_this_windowAdapter(this));
    hisImage.setBorder(border5);
    hisImage.setEditable(false);
    hisImage.setBounds(new Rectangle(5, 4, 140, 120));

    this.setHisImage(dimg);

    jPanel2.setLayout(borderLayout2);
    //borderLayout2.setVgap(10);
    sendBtn.setPreferredSize(new Dimension(102, 78));

    jPanel4.setLayout(borderLayout3);
    jPanel4.setPreferredSize(new Dimension(102, 68));
    borderLayout3.setHgap(10);
    borderLayout3.setVgap(10);
    jbar5.setBackground(UIManager.getColor("ColorChooser.background"));
    jbar5.setPreferredSize(new Dimension(30, 39));
    jbar5.setFloatable(false);

    jPanel2.setBackground(Color.white);
    jPanel2.setPreferredSize(new Dimension(102, 120));
    //jPanel7.setBackground(new Color(236, 233, 216));
    jPanel6.setBackground(Color.white);
    jPanel8.setBackground(Color.white);
    jPanel8.setLayout(borderLayout7);
    jPanel9.setLayout(borderLayout6);
    jScrollPane2.setPreferredSize(new Dimension(200, 200));
    jPanel12.setBackground(Color.white);
    jPanel13.setBackground(Color.white);

    jPanel11.setFloatable(false);
    jButton2.setOpaque(false);
    jButton2.setFocusable(false);
    jButton2.addActionListener(new MsgrSessionGUI_jButton2_actionAdapter(this));
    jPanel14.setBackground(new Color(235, 235, 235));
    jScrollPane2.getViewport().add(board);
    jPanel3.add(myImage);
    jPanel3.add(hisImage);

    sendBtn.setText("Send");

    jPanel9.add(jPanel3, java.awt.BorderLayout.EAST);
    jPanel9.add(jPanel1, java.awt.BorderLayout.CENTER);
    jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);
    jPanel9.add(jPanel11, java.awt.BorderLayout.NORTH);
    jPanel11.add(jButton2);

    //jPanel8.add(jPanel12, java.awt.BorderLayout.NORTH);
    jPanel8.add(sendBtn, java.awt.BorderLayout.CENTER);
    // jPanel8.add(jPanel13, java.awt.BorderLayout.EAST);

    jPanel8.add(jPanel6, java.awt.BorderLayout.EAST);
    jPanel4.setBackground(Color.white);
    jPanel4.add(jbar5, java.awt.BorderLayout.NORTH);
    jPanel4.add(jPanel8, java.awt.BorderLayout.CENTER);

    jPanel2.add(jPanel4, java.awt.BorderLayout.EAST);

    jPanel2.add(jPanel7, java.awt.BorderLayout.SOUTH);

    this.getContentPane().add(jPanel9, java.awt.BorderLayout.CENTER);
    this.getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
    this.getContentPane().add(jPanel14, java.awt.BorderLayout.WEST);

    te = new ChatEditor();

    jPanel2.add(te, java.awt.BorderLayout.CENTER);
    te.getEditor().addKeyListener(new MsgrSessionGUI_editor_keyAdapter(this));
    centerFrame();

    this.validate();
  }

  public void sendBtn_actionPerformed(ActionEvent e) {
    if (session != null) {
      this.session.sendChatMessage(te.getMessage());
    }
    showMsg("Me", te.getMessage());
    te.clear();

  }

  private void centerFrame() {

    // Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = this.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    this.setLocation( (screenSize.width - frameSize.width) / 2,
                     (screenSize.height - frameSize.height) / 2);
    this.setVisible(true);

  }

  public void this_windowClosing(WindowEvent e) {
    if (!freezed) {
      if (session != null) {
        this.session.stop();
      }
    }

    this.dispose();

  }

  public void freez() {

    this.sendBtn.setEnabled(false);
    te.getEditor().setEnabled(false);

    this.setTitle(" ----NO CONNECTION-------");
    jPanel9.setAlpha( (float) 0.8);
    jPanel2.setAlpha( (float) 0.8);
    jPanel14.setAlpha( (float) 0.5);

    freezed = true;
  }

  public void showMsg(String user, String data) {

    if ( -1 != data.indexOf("@sdi@")) {
      data = data.replaceAll("@sdi@", System.getProperty("smiliesDir"));
    }
    String data2 =
        "<font size=" + 4 + " color='#ff0033' face='Comic Sans MS'>" +
        user + " </FONT>" +
        "<font size=" + 2 + " color='#999999' face='Comic Sans MS'>" + "@" +
        GregorianCalendar.getInstance().getTime().toString() + " : </font> "
        + data;

    try {

      ( (HTMLEditorKit) getBoard().getEditorKit()).read(new
          java.io.StringReader(
              data2),
          getBoard().getDocument(),
          getBoard().getDocument().getLength());

      getBoard().setCaretPosition(getBoard().
                                  getDocument().getLength());

    }
    catch (Exception e) {
      System.out.println(e.getMessage());
    }

  }

  public static void main(String[] art) {
    new MsgrSessionGUI("test");
  }

  public void editor_keyTyped(KeyEvent e) {

    if (e.getKeyChar() == '\n') {
      sendBtn_actionPerformed(null);
    }
  }

  public void jButton2_actionPerformed(ActionEvent e) {

    //Set up the file chooser.
    if (fc == null) {
      fc = new JFileChooser();
    }

    //Show it.
    int returnVal = fc.showSaveDialog(this);

    //Process the results.

    File file = fc.getSelectedFile();

    if (file != null) {
      System.out.println(file.getAbsolutePath());
      try {
        FileOutputStream out = new FileOutputStream(file + "_" + this.getTitle() +
            ".html");
        out.write(getBoard().getText().getBytes());
        out.close();

      }
      catch (Exception ex) {
        System.out.print("Session not saved: " + ex.getMessage());
      }
    }

    //Reset the file chooser for the next time it's shown.
    //fc.setSelectedFile(null);


  }

  public void propertyChange(PropertyChangeEvent evt) {
    if (evt.getPropertyName().equals("image")) {
      Image img = (Image) evt.getNewValue();

      BufferedImage dst = new BufferedImage(
          img.getWidth(null),
          img.getHeight(null),
          BufferedImage.TYPE_INT_ARGB);
      Graphics2D g = (Graphics2D) dst.getGraphics();

      g.drawImage(img, 0, 0, null);
      g.dispose();

      File tmp = new File("tmp");
      if (!tmp.exists()) {
        tmp.mkdir();
      }
      myPic = new File("tmp" + File.separator + "MyImg.png");
      try {

        ImageIO.write(dst, "jpg", myPic);
        if (session != null) {
          this.session.sendPicture(myPic);
        }
      }
      catch (IOException ex1) {
        System.err.println(ex1.getMessage());
      }
    }
  }

}

class MsgrSessionGUI_jButton2_actionAdapter
    implements ActionListener {
  private MsgrSessionGUI adaptee;
  MsgrSessionGUI_jButton2_actionAdapter(MsgrSessionGUI adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}

class MsgrSessionGUI_this_windowAdapter
    extends WindowAdapter {
  private MsgrSessionGUI adaptee;
  MsgrSessionGUI_this_windowAdapter(MsgrSessionGUI adaptee) {
    this.adaptee = adaptee;
  }

  public void windowClosing(WindowEvent e) {
    adaptee.this_windowClosing(e);
  }
}

class MsgrSessionGUI_sendBtn_actionAdapter
    implements ActionListener {
  private MsgrSessionGUI adaptee;
  MsgrSessionGUI_sendBtn_actionAdapter(MsgrSessionGUI adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.sendBtn_actionPerformed(e);
  }
}

class MsgrSessionGUI_editor_keyAdapter
    extends KeyAdapter {
  private MsgrSessionGUI adaptee;
  MsgrSessionGUI_editor_keyAdapter(MsgrSessionGUI adaptee) {
    this.adaptee = adaptee;
  }

  public void keyTyped(KeyEvent e) {
    adaptee.editor_keyTyped(e);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -