Package gui

Source Code of gui.ChatPanel

/*
* Hamsam - Instant Messaging API
* Copyright (C) 2003 Raghu K
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package gui;

import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.DefaultListModel;

import java.util.Vector;

import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

import gui.tools.PropertiesManager;
import gui.action.SendMessage;
import gui.action.SendMessageToGroup;
import gui.action.IgnoreSomeone;
import gui.action.UnIgnoreSomeone;
import gui.action.RemoveChatPanel;

import hamsam.api.Buddy;

/**
* <p>Titre : Hamsam Project</p>
* <p>Description : Instant Messenger</p>
* <p>Copyright : Copyright (c) 2003</p>
* <p>Soci�t� : </p>
* @author Fr�d�ric DIB
* @version 0.1
*/

public class ChatPanel extends JPanel
{
  private PropertiesManager propertiesMgr = null;
  private JTextArea textArea = new JTextArea(20, 40);
  private JPanel rightPanel = new JPanel();
  private JTextField messageField = new JTextField(40);
  private JButton sendButton = null;
  private BorderLayout layout = new BorderLayout();
  private JPanel messagePanel = new JPanel();
  private Vector buddyVector = null;
  private DefaultListModel model = new DefaultListModel();
  private JList buddyList = new JList(model);
  private JScrollPane buddyPane = null;
  private JButton ignore = null;
  private JButton unIgnore = null;
  private JButton close = null;

  /**
   * Constructor.
   */
  public ChatPanel(PropertiesManager p, Vector v)
  {
    this.propertiesMgr = p;
    this.buddyVector = v;
    this.init();
  }

  private void initRighPanel()
  {
    rightPanel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1;
    c.fill = GridBagConstraints.HORIZONTAL;

    //buddy list position
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 4;
    c.gridwidth = 2;
    rightPanel.add(buddyPane, c);

    //ignore button position
    c.gridx = 0;
    c.gridy = 5;
    c.gridheight = 1;
    c.gridwidth = 1;
    rightPanel.add(ignore, c);

    //unignore button position
    c.gridx = 0;
    c.gridy = 6;
    c.gridheight = 1;
    c.gridwidth = 1;
    rightPanel.add(unIgnore, c);

    //close button position
    c.gridx = 0;
    c.gridy = 7;
    c.gridheight = 1;
    c.gridwidth = 1;
    rightPanel.add(close, c);

  }

  private void initMessagePanel()
  {
    //Init the message panel
    this.messagePanel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1;
    c.fill = GridBagConstraints.HORIZONTAL;

    this.messagePanel.add(this.messageField, c);
    this.messagePanel.add(this.sendButton, c);
  }

  /**
   * Init the chat panel.
   */
  private void init()
  {
    sendButton = new JButton(this.propertiesMgr.getValue("sendButton"));
    ignore = new JButton(this.propertiesMgr.getValue("ignoreButton"));
    unIgnore = new JButton(this.propertiesMgr.getValue("unIgnoreButton"));
    close = new JButton(this.propertiesMgr.getValue("closeButton"));
    Buddy b = null;
    for (int i = 0; i < buddyVector.size(); i++)
    {
      b = (Buddy) buddyVector.elementAt(i);
      model.addElement(b.getUsername());
    }
    this.buddyList.setSelectionMode(
      ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    buddyPane = new JScrollPane(buddyList);

    //Set actions
    if (buddyVector.size() == 1)
    {
      sendButton.addActionListener(
        new SendMessage(this, (Buddy) buddyVector.elementAt(0)));
      messageField.addKeyListener(
        new SendMessage(this, (Buddy) buddyVector.elementAt(0)));
    }
    else if (1 <= buddyVector.size())
    {
      sendButton.addActionListener(new SendMessageToGroup());
      messageField.addKeyListener(new SendMessageToGroup());
    }
    ignore.addActionListener(new IgnoreSomeone());
    unIgnore.addActionListener(new UnIgnoreSomeone());
    close.addActionListener(new RemoveChatPanel(MainFrame.myRef));

    this.setLayout(layout);
    this.initMessagePanel();
    this.initRighPanel();

    this.add(textArea, BorderLayout.CENTER);
    this.add(messagePanel, BorderLayout.SOUTH);
    this.add(rightPanel, BorderLayout.EAST);
  }

  public String getMessageFieldText()
  {
    return this.messageField.getText();
  }

  public void setMessageFieldText(String text)
  {
    this.messageField.setText(text);
  }

  public void messageSent(String newText)
  {
    String text = this.textArea.getText();
    this.textArea.setText(text + newText + '\n');
  }
}
TOP

Related Classes of gui.ChatPanel

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.