Package Control

Source Code of Control.SatedaHandler

package Control;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Queue;

import Communication.Interfaces.Connection;
import Communication.ChatProvider;
import Communication.Status;
import Control.Interfaces.EventHandler;
import Control.Interfaces.ListElement;
import GUI.ChatWindow;
import GUI.ContactList;
import GUI.ContactListMenue;
import GUI.MainWindow;
import GUI.NotificationWindow;
import GUI.RecordWindow;
import GUI.SettingsWindow;
import GUI.Interfaces.ChatWindowInterface;
import GUI.Interfaces.ContactListListener;
import GUI.Interfaces.NotificationListener;
import GUI.Interfaces.RecordWindowListener;

/**
* The main logic of the client being the control between the {@link Connection}s and the user.
*
* @author Sebastian
*
*/
public class SatedaHandler implements EventHandler, NotificationListener, ContactListListener,
RecordWindowListener{

  /**
   * Lists all {@link Connection}s to whom the client shall be connected.
   */
  private Collection<Connection> connectionList = new ArrayList<Connection>();

  /**
   * The {@link RecordWindow} showing the clients {@link Record}
   */
  private RecordWindow recordWindow;

  /**
   * The {@link MainWindow} displaying e.g. the buddy list.
   */
  private MainWindow mainWindow;

  /**
   * The {@link Settings} that define the clients actions
   */
  private Settings settings;

  /**
   * A {@link SettingsWindow} to change the {@link Settings}
   */
  private SettingsWindow settingsWindow;

  /**
   * A list of all {@link Contact}s used in the client
   */
  private ArrayList<Contact> contacts = new ArrayList<Contact>();

  /**
   * The {@link ContactList} that is displayed in the {@link MainWindow}
   */
  private ContactList contactList;

  /**
   * The list of all {@link ChatWindow}s currently used
   */
  private ArrayList<ChatWindowInterface> chatWindows = new ArrayList<ChatWindowInterface>();

  /**
   * The {@link Record} of the client
   */
  private Record record;

  /**
   * A Queue containing all displayed {@link NotificationWindow}s
   */
  private Queue<NotificationWindow> notificationWindows = new LinkedList<NotificationWindow>();

  /**
   * The {@link ContactListMenue}
   */
  private ContactListMenue listMenue = null;

  /**
   * Indicates how many {@link Connection}s have already already established
   */
  private int connectionsEstablished;


  public SatedaHandler(Settings settings, MainWindow mainWindow, Record record) {
    this.settings = settings;
    this.mainWindow = mainWindow;
    this.record = record;
  }

  public void addConnection(Connection connection) {

    if(!connectionList.contains(connection)){
      connectionList.add(connection);
    }
  }

  public void statusChanged(Status status) {

  }

  public void mainWindowBoundsChanged(Rectangle rectangle) {
    settings.mainWindowBounds = rectangle;
    settings.save();
  }


  public void settingsRequested() {
    int x = 200;

    Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    if(mainWindow.getX() + mainWindow.getWidth() + 470 < screenSize.getWidth()){
      x = mainWindow.getX() + mainWindow.getWidth() + 10;
    }else if(mainWindow.getX() - 470 > 0){
      x = mainWindow.getX() - 460;
    }
    settingsWindow =  new SettingsWindow(new Rectangle(x,150,450,298), settings, this);
  }

  public void addNewContact(Contact newContact) {
    if( ! contacts.contains(newContact)){
      contacts.add(newContact);
    }
  }


  public void updateContactStatus(String uin, ChatProvider chatProvider, Status newStatus) {

  }


  public void connectionToProviderEstablished(ChatProvider chatProvider) {
    connectionsEstablished++;

    if(connectionsEstablished == connectionList.size()){

      Contact[] contacts = new Contact[this.contacts.size()];
      int i = 0;
      for(Contact contact : this.contacts){
        contacts[i] = contact;
        i++;
      }

      contactList = new ContactList(settings, contacts);
      contactList.addContactListListener(this);
      mainWindow.setContactList(contactList);
    }
  }

  public void startClient() {

    if(settings.loginAtClientStart && connectionList.size() > 0){

      for(Connection connection : connectionList){
        connection.connect();
      }
    }
  }


  public void exitRequested() {

    for(Connection connection : connectionList){
      connection.quit();
    }
    System.exit(0);
  }


  public void stopLogin() {

    for(Connection connection : connectionList){
      connection.stopLogin();
    }

    contacts.clear();
  }


  public void connectionSettingsRequested() {
    settingsRequested();
    settingsWindow.requestChatConnectionSettings();
  }

  public void chatConnectionsUpdated(){
    mainWindow.setNewContactsCount(connectionList.size());
  }

  public void chatWindowBoundsChanged(Rectangle rectangle) {
    settings.chatWindowBounds = rectangle;
    settings.save();
  }

  public void chatWindowDisposed(ChatWindow chatWindow) {
    chatWindows.remove(chatWindow)
  }

  public void sendMessage(Message message) {

    record.addMessage(message);
  }

  public void receiveMessage(Message message) {

    record.addMessage(message);
  }

  public void notificationDisposed(NotificationWindow notification) {

    notificationWindows.poll();

    for(NotificationWindow notificationWindow : notificationWindows){

      int x = notificationWindow.getLocation().x;
      int y = notificationWindow.getLocation().y;
      notificationWindow.setLocation(new Point(x, y + 90));
    }
  }

  public void userOffgoing(Contact contact) {

    if(settings.showWhenOffGoing == 1 || (settings.showWhenOffGoing == 2
        && settings.showPersonWhenOffGoing.contains(contact.getUin()))){
      NotificationWindow notificationWindow =  new NotificationWindow(new Rectangle(
          java.awt.Toolkit.getDefaultToolkit().getScreenSize().width - 200,
          java.awt.Toolkit.getDefaultToolkit().getScreenSize().height - 140
          - 90 * notificationWindows.size(),
          200, 90), contact.getDisplayedName() + " ist offline", contact.getStatus(), this);
      notificationWindows.add(notificationWindow);
      notificationWindow.startDisplaying();
    }
  }

  public void userOngoing(Contact contact) {

    if(settings.showWhenOnGoing == 1 || (settings.showWhenOnGoing == 2
        && settings.showPersonWhenOnGoing.contains(contact.getUin()))){

      NotificationWindow notificationWindow =  new NotificationWindow(new Rectangle(
          java.awt.Toolkit.getDefaultToolkit().getScreenSize().width - 200,
          java.awt.Toolkit.getDefaultToolkit().getScreenSize().height - 140
          - 90 * notificationWindows.size(),
          200, 90), contact.getDisplayedName() + " ist online", contact.getStatus(), this);
      notificationWindows.add(notificationWindow);
      notificationWindow.startDisplaying();
    }
  }

  public void recordsRequested() {

    int x = settings.recordLocation.x;
    int y = settings.recordLocation.y;
   
    if(settings.recordLocation == new Point(-1, -1)){
      x = mainWindow.getX() - 800;
      if(x < 20){
        x = mainWindow.getX() + mainWindow.getWidth() + 40;
        if(x + 750 > Toolkit.getDefaultToolkit().getScreenSize().width){
          x = 120;
        }
      }
      y = 130;
    }

    recordWindow = new RecordWindow(new Rectangle(x,y,750,560), true, record);
    recordWindow.addRecordWindowListener(this);
  }

  @Override
  public void chatRequested(Contact contact) {

    if( ! contact.isInChatWindow() || settings.oneChatWindowPerChatPartner){
      contact.setIsInChatWindow(true);

      if(settings.oneChatWindowPerChatPartner){

        ChatWindowInterface chatWindow = new ChatWindow(new Rectangle(settings.chatWindowBounds.x,
            settings.chatWindowBounds.y,
            settings.chatWindowBounds.width,
            settings.chatWindowBounds.height), this);
        chatWindows.add(chatWindow);
        chatWindow.addContact(contact);
      }else{

        if(chatWindows.size() > 0){
          chatWindows.get(chatWindows.size() - 1).addContact(contact);
        }else{
          ChatWindowInterface chatWindow = new ChatWindow(new Rectangle(settings.chatWindowBounds.x,
              settings.chatWindowBounds.y,
              settings.chatWindowBounds.width,
              settings.chatWindowBounds.height), this);
          chatWindows.add(chatWindow);
          chatWindow.addContact(contact);
        }
      }
    }
  }

  @Override
  public void contactListSettingRequested(ListElement element, Point point) {

    if(listMenue != null){
      listMenue.dispose();
    }

    listMenue = new ContactListMenue(settings, element);
    listMenue.setLocation(settings.mainWindowBounds.x + 9 + point.x,
        settings.mainWindowBounds.y + 20 + point.y);
    listMenue.setVisible(true);
  }

  @Override
  public void elementMarked(ListElement element) {

  }

  @Override
  public void mouseClickedOnList() {
    if(listMenue != null){
      listMenue.dispose();
    }
  }

  @Override
  public void disposeScrollBar() {
    // TODO Auto-generated method stub

  }

  @Override
  public void scrollBarNeeded(int totalHeight) {
    // TODO Auto-generated method stub

  }

  @Override
  public void recordWindowLocationChanged(Point newPoint) {

    settings.recordLocation = newPoint;
    settings.save();
  }
}
TOP

Related Classes of Control.SatedaHandler

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.