package org.javwer;
import java.util.concurrent.locks.ReentrantLock;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
import org.jivesoftware.smack.packet.*;
public class JChat {
private CTabFolder chatTabFolder;
private JavwerManager parent;
private Display display;
private Shell shell;
private JChatTab[] chatTabs = {};
private ReentrantLock tabsLock;
JChat( JavwerManager f_parent ) {
parent = f_parent;
display = parent.getDisplay();
shell = new Shell( display, SWT.BORDER | SWT.RESIZE | SWT.TITLE | SWT.TOOL );
tabsLock = new ReentrantLock();
createGUI();
shell.addShellListener( new ShellAdapter() {
public void shellClosed( ShellEvent e ) {
e.doit = false;
for ( JChatTab chatTab : chatTabs ) {
chatTab.close();
}
}
});/*
shell.addDisposeListener( new DisposeListener() {
public void widgetDisposed( DisposeEvent e ) {
for ( JChatTab tab : chatTabs )
tab.close();
}
});*/
chatTabFolder.addSelectionListener( new SelectionListener() {
public void widgetDefaultSelected( SelectionEvent e ) {}
public void widgetSelected( SelectionEvent e ) {
if ( chatTabFolder.getSelection() != null )
for ( JChatTab tab : chatTabs ) {
if ( chatTabFolder.getSelection().equals( tab.getTabItem() ) ) {
tab.widgetSelected();
}
}
}
});
}
public void open() {
if ( shell.isDisposed() ) {
shell = new Shell( display, SWT.BORDER | SWT.RESIZE | SWT.TITLE | SWT.TOOL );
createGUI();
}
display.asyncExec( new Runnable() {
public void run() {
shell.setVisible( true );
shell.forceActive();
}
});
}
public CTabFolder getTabFolder() {
if ( null == chatTabFolder )
createGUI();
return chatTabFolder;
}
public JavwerManager getJavwerManager() {
return parent;
}
public void closeChatTab( JChatTab tab ) {
tabsLock.lock();
for ( int i = 0; i < chatTabs.length; i++ ) {
if ( tab.equals( chatTabs[ i ] ) ) {
JChatTab[] temp = new JChatTab[ chatTabs.length - 1 ];
if ( i != chatTabs.length - 1 )
System.arraycopy( chatTabs, i + 1, chatTabs, i, chatTabs.length - i - 1 );
System.arraycopy( chatTabs, 0, temp, 0, temp.length );
chatTabs = temp;
//temp = null;
}
}
tabsLock.unlock();
if ( chatTabs.length == 0 ) {
shell.setVisible( false );
}
}
public void getMessage( String name, Message message ) {
if ( message.getBody() != null )
getChatTab( name, message.getType() ).addMessage( message );
}
public JChatTab getChatTab( String name, Message.Type type ) {
tabsLock.lock();
for ( JChatTab chatTab : chatTabs ) {
// System.out.println( "chatTab: " + chatTab.getName() + " | message: " + name );
if ( name.startsWith( chatTab.getName() ) && ( type != Message.Type.CHAT || type.equals( chatTab.getType() ) ) ) {
tabsLock.unlock();
return chatTab;
}
}
// System.out.println("no tab found");
tabsLock.unlock();
return addChatTab( name, "", "", JVW.TYPE.SUC );
}
public JChatTab getStatusTab() {
tabsLock.lock();
for ( JChatTab chatTab : chatTabs ) {
// System.out.println( "chatTab: " + chatTab.getName() + " | message: " + name );
if ( chatTab.getClass().equals( StatusLogManager.StatusTab.class ) ) {
tabsLock.unlock();
return chatTab;
}
}
tabsLock.unlock();
return addChatTab( "", "", "", JVW.TYPE.STATUS );
}
public void focusTab( String name, Message.Type type ) {
shell.forceActive();
chatTabFolder.setSelection( getChatTab( name, type ).getTabItem() );
}
public Shell getShell() {
return shell;
}
public JChatTab addChatTab( String name, String nick, String password, int type ) {
tabsLock.lock();
JChatTab[] temp = new JChatTab[ chatTabs.length ];
System.arraycopy( chatTabs, 0, temp, 0, chatTabs.length );
chatTabs = new JChatTab[ chatTabs.length + 1 ];
System.arraycopy( temp, 0, chatTabs, 0, temp.length );
if( type == JVW.TYPE.SUC )
chatTabs[ temp.length ] = (JChatTab) (new JSUCTab( this, name ));
else if ( type == JVW.TYPE.MUC ) {
JMUCTab tmp = new JMUCTab( this, name, nick, password );
chatTabs[ temp.length ] = (JChatTab) tmp;
}
else if( type == JVW.TYPE.STATUS ) {
chatTabs[ temp.length ] = (JChatTab) parent.getStatusLogManager().show();
}
open();
tabsLock.unlock();
return chatTabs[ temp.length ];
}
private void createGUI() {
shell.setText( "Javwer - Chat" );
shell.setSize( 500, 300 );
shell.setLayout( new FillLayout() );
chatTabFolder = new CTabFolder( shell, SWT.BOTTOM | SWT.CLOSE | SWT.BORDER | SWT.FLAT);
shell.open();
}
}