package GUI;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JTextPane;
import Communication.ChatProvider;
import Communication.Status;
import Control.Contact;
import Control.Message;
import Control.Interfaces.EventHandler;
import GUI.Interfaces.ChatWindowInterface;
import GUI.Interfaces.FragmentationBarListener;
import GUI.Services.Hypen;
import GUI.Services.WindowFragmentationBar;
/**
* Displays most of all two {@link JTextPane}s for outgoing and incoming messages. By using the
* {@link WindowFragmentationBar} more than one {@link Contact}s can be managed in one {@link ChatWindow}
*
* @author Sebastian
*
*/
public class ChatWindow extends SatedaWindow implements ChatWindowInterface, FragmentationBarListener{
private static final long serialVersionUID = -6245854641517728727L;
/**
* The {@link EventHandler} to inform on user interactions --> Interface
*/
private EventHandler eventHandler;
/**
* Displays the history of the recent chat, meaning incoming and outgoing {@link Message}s
*/
private SatedaTextPane incomingPane = new SatedaTextPane(false, new Date());
/**
* Enables the user to write an output message
*/
private SatedaTextPane outgoingPane = new SatedaTextPane(true, new Date());
/**
* A special line to be displayed
*/
private Hypen hypen = new Hypen();
/**
* A bar to manage several {@link Contact}s in one {@link ChatWindow}. Depending on
* the chosen focus the {@link JTextPane}s for the incoming and outgoing messages gets adapted
*/
private WindowFragmentationBar fragmentationBar = new WindowFragmentationBar();
/**
* The {@link Contact}s to manage in that {@link ChatWindow}
*/
private ArrayList<Contact> contacts = new ArrayList<Contact>();
public ChatWindow(Rectangle rectangle, EventHandler eventHandler) {
super(rectangle, false, -1);
setVisible(true);
super.setMinimumSize(new Dimension(550, 400));
this.eventHandler = eventHandler;
add(hypen);
add(fragmentationBar);
incomingPane.setDoubleBuffered(true);
outgoingPane.setDoubleBuffered(true);
add(incomingPane);
add(outgoingPane);
fragmentationBar.addFragmentationBarListener(this);
fragmentationBar.setVisible(true);
}
public void componentHidden(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
/**
* Informs the {@link EventHandler} that the {@link ChatWindow} was moved
*/
public void componentMoved(ComponentEvent arg0) {
eventHandler.chatWindowBoundsChanged(this.getBounds());
}
public void componentShown(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
/**
* Informs the {@link EventHandler} that the boundaries changed
*/
public void componentResized(ComponentEvent arg0) {
super.componentResized(arg0);
int spaceToUse = (getHeight() - 50) - (fragmentationBar.getHeight() + 25) - 15;
incomingPane.setBounds(12, fragmentationBar.getHeight() + 25, getWidth() - 24,
(int) (spaceToUse * (0.7)));
outgoingPane.setBounds(12, fragmentationBar.getHeight() + 25 + incomingPane.getHeight() + 15,
getWidth() - 24, (int) (spaceToUse - (spaceToUse * 0.7)));
eventHandler.chatWindowBoundsChanged(getBounds());
fragmentationBar.setBounds(new Rectangle(10, 13, getWidth() - 20, 30));
}
/**
* Adds a {@link Contact} to the list of {@link Contact}s to manage in that {@link ChatWindow}
*/
public void addContact(Contact contact) {
if(!contacts.contains(contact)){
/*
* Testdaten f�r das incoming pane und das outgoing
*/
Message message1 = new Message("Hi, das ist ein Test, um das incoming pane " +
"mal darzustellen", true, new Contact(
"333", ChatProvider.ICQ, Status.Invisible, "Felix", "felix"),
new Date(new Date().getTime() - 1300));
Message message2 = new Message("Das ist aber ein toller Test", false, new Contact(
"333", ChatProvider.ICQ, Status.Invisible, "Julia", "felix"),
new Date(new Date().getTime() - 800));
Message message3 = new Message("Vor allem weil bei einem abstand von kleiner einer minute " +
"mein text noch hinter dem anderen angezeigt wird, ohne den Header", false, new Contact(
"333", ChatProvider.ICQ, Status.Invisible, "Julia", "felix"),
new Date());
ArrayList<Message> messages = new ArrayList<Message>();
messages.add(message1);
messages.add(message2);
incomingPane.setMessages(messages);
incomingPane.addMessage(message3);
outgoingPane.setText("Hier wird der Text geschrieben.... Was noch fehlt sind Scrollbars, " +
"ein gutes Design und noch ein paar Komfortfunktionen im ChatWindow. ");
outgoingPane.requestFocus();
/*
* Ende der Testdaten
*/
contacts.add(contact);
fragmentationBar.addElement(contact.getDisplayedName(), contact.getStatus());
fragmentationBar.focusElement(contact.getDisplayedName());
refreshStatus();
}
}
public void receiveMessage(Message message) {
// TODO Auto-generated method stub
}
/**
* Removes a {@link Contact} if existing
*/
public void removeContact(Contact contact) {
if(contacts.contains(contact)){
contacts.remove(contact);
contact.setIsInChatWindow(false);
refreshStatus();
}
}
/**
* Adapts the title of the {@link ChatWindow}, showing the name of the {@link Contact} if
* only one is managed. The amount of contacts managed otherwise
*/
private void refreshStatus(){
if(contacts.size() == 1){
setTitle(((Contact) contacts.get(0)).getDisplayedName());
}else{
setTitle(contacts.size() + " Kontakte");
}
}
/**
* Informs on a closed element of the {@link WindowFragmentationBar}, removing the concerning
* {@link Contact} from the list of {@link Contact}s
*/
public void elementClosed(String closedElement) {
Contact removedContact = null;
for(Contact contact : contacts){
if(contact.getDisplayedName().equals(closedElement)){
removedContact = contact;
break;
}
}
contacts.remove(removedContact);
removedContact.setIsInChatWindow(false);
if(contacts.size() == 0){
eventHandler.chatWindowDisposed(this);
super.dispose();
return;
}
refreshStatus();
}
/**
* Informs that the focus was layed on a new {@link Contact}
*/
public void itemSelectionChanged(String selectedItem) {
}
public void barIsFullyDisplayable() {
}
public void barIsNotFullyDisplayable() {
}
}