Package gui

Source Code of gui.ContactsMgrPanel

/*
* 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.JList;
import javax.swing.ListSelectionModel;
import javax.swing.JPanel;
import javax.swing.DefaultListModel;
import javax.swing.JScrollPane;
import javax.swing.JButton;

import java.awt.Insets;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

import java.util.Vector;

import gui.tools.PropertiesManager;
import gui.tools.Traces;
import gui.action.ConnectionInfos;
import gui.action.AddBuddy;
import gui.action.DelBuddy;
import gui.action.LaunchChat;

import hamsam.api.Buddy;
import hamsam.exception.IllegalArgumentException;
import hamsam.exception.IllegalStateException;

/**
* <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 ContactsMgrPanel extends JPanel {
  private PropertiesManager propertiesMgr = null;
  private DefaultListModel model = new DefaultListModel();
  private JList buddyList = new JList(model);
  private JScrollPane buddyPanel = new JScrollPane(buddyList);
  private JButton add = null;
  private JButton del = null;
  private JButton chat = null;
  private Vector myBuddies = new Vector();

  /**
   * Constructor.
   */
  public ContactsMgrPanel(PropertiesManager p) {
    this.propertiesMgr = p;
    this.init();
  }

  /**
   *
   */
  public void updateBuddyList(Buddy[] b) {   
    for (int i=0; i<b.length; i++) {
      model.addElement(b[i].getUsername());
      myBuddies.addElement(b[i]);
    }
  }

  public void delSelectedBuddies() {
    Buddy[] tempBud = this.getSelectedBuddies();
   
    for (int i=0; i<tempBud.length; i++) {
      Traces.printTraces("Selected buddy : " + tempBud[i].getUsername());
      try {
        //remove from buddy list
        ConnectionInfos.currentProtocol.deleteFromBuddyList(tempBud[i]);
        //remove from myBuddies Vector
        this.myBuddies.removeElementAt(buddyList.getSelectedIndex());
        //remove from displayed list
        model.removeElementAt(buddyList.getSelectedIndex());                             
      }
      catch(IllegalArgumentException e) {
        Traces.printErrorMessage(e, MainFrame.myRef);
     
      catch(IllegalStateException e) {
        Traces.printErrorMessage(e, MainFrame.myRef);
     
    }
   
    //Buddy left
    Buddy b = null;     
    Traces.printTraces("Buddy left :");
    for (int i=0; i<myBuddies.size(); i++) {
      b = (Buddy)myBuddies.elementAt(i);
      Traces.printTraces("buddy " + i + " : " + b.getUsername());
    }     
  }
 
  public void addBuddy(Buddy bud) { 
  try {
    ConnectionInfos.currentProtocol.addToBuddyList(bud);
    model.addElement(bud.getUsername());
    myBuddies.addElement(bud);
  }
  catch(IllegalArgumentException e) {
    Traces.printErrorMessage(e, MainFrame.myRef);
  } catch (IllegalStateException e) {
    Traces.printErrorMessage(e, MainFrame.myRef);
  }
  }

  public Buddy[] getSelectedBuddies() {   
    int index[] = buddyList.getSelectedIndices();
  Buddy ret[] = new Buddy[index.length];
    for (int i=0; i<index.length; i++) {
      ret[i] = (Buddy)myBuddies.elementAt(index[i]);
    }
    return ret;
  }

  private void init() {
    add = new JButton(this.propertiesMgr.getValue("addBuddyToList"));
    add.addActionListener(new AddBuddy(this.propertiesMgr, this));
    del = new JButton(this.propertiesMgr.getValue("delBuddyToList"));
    del.addActionListener(new DelBuddy(this));
    chat = new JButton(this.propertiesMgr.getValue("talkWith"));
    chat.addActionListener(new LaunchChat(this.propertiesMgr, MainFrame.myRef, this));

    buddyList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
   
    this.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1;
    c.insets = new Insets(5, 5, 5, 5);
    c.fill = GridBagConstraints.BOTH;

    //List Position
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 3;
    c.gridheight = 4;
    this.add(buddyPanel, c);

    //add button position
    c.gridx = 3;
    c.gridy = 1;
    c.gridwidth = 1;
    c.gridheight = 1;
    this.add(add, c);


    //del button position
    c.gridx = 3;
    c.gridy = 2;
    c.gridwidth = 1;
    c.gridheight = 1;
    this.add(del, c);

    //chat button position
    c.gridx = 3;
    c.gridy = 3;
    c.gridwidth = 1;
    c.gridheight = 1;
    this.add(chat, c);
  }
}
TOP

Related Classes of gui.ContactsMgrPanel

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.