package org.javwer;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.util.StringUtils;
public class NotificationManager {
private JavwerManager parent;
private Notification[] notifications = {};
NotificationManager( JavwerManager f_parent ) {
parent = f_parent;
}
private class Notification {
private Shell shell;
Notification( final String from, final String text, final Message.Type type ) {
//GUI
shell = new Shell( SWT.ON_TOP );
shell.setSize( 150, 80 );
shell.setLayout( new FillLayout() );
Label textLabel = new Label( shell, SWT.WRAP );
if ( type == null ) {
textLabel.setText( StringUtils.parseName( from ) + ":\n" + text );
} else {
if ( type.equals( Message.Type.GROUP_CHAT ) ) {
textLabel.setText( StringUtils.parseResource( from ) + ":\n" + text );
}
else {
if ( parent.getJRoster().getRoster().getEntry( StringUtils.parseBareAddress( from ) ) != null ) {
textLabel.setText( parent.getJRoster().getRoster().getEntry( StringUtils.parseBareAddress( from ) ).getName() + ":\n" + text );
}
else {
textLabel.setText( StringUtils.parseName( from ) + ":\n" + text );
}
}
}
textLabel.addMouseListener( new MouseListener() {
public void mouseDoubleClick( MouseEvent e ) {}
public void mouseUp( MouseEvent e ) {}
public void mouseDown( MouseEvent e ) {
if ( type != null )
parent.getJChat().focusTab( from, type );
shell.dispose();
}
});
Rectangle notificationRect = shell.getBounds();
Rectangle displayRect = parent.getDisplay().getBounds();
int x = (displayRect.width - notificationRect.width);
int y = (displayRect.height - ( notificationRect.height * notifications.length ) );
shell.setLocation(x, y);
shell.open();
}
public void cleanUp() {
if( !shell.isDisposed() )
shell.dispose();
}
public void goDown() {
if( !shell.isDisposed() )
shell.setLocation( shell.getBounds().x, shell.getBounds().y + shell.getBounds().height );
}
}
public void addNotification( Message message ) {
addNotification( message.getFrom(), message.getBody(), message.getType() );
}
public void addNotification( final String from, final String text, final Message.Type type ) {
if ( text != null ) {
Notification[] temp = new Notification[ notifications.length ];
System.arraycopy( notifications, 0, temp, 0, notifications.length );
notifications = new Notification[ notifications.length + 1 ];
System.arraycopy( temp, 0, notifications, 0, temp.length );
final Notification notification = new Notification( from, text, type );
notifications[ temp.length ] = notification;
Thread timer = new Thread() {
public void run() {
try {
sleep( 7000 );
} catch ( InterruptedException e ) {}
parent.getDisplay().asyncExec( new Runnable() {
public void run() {
removeNotification( notification );
}
});
}
};
timer.start();
}
}
private void removeNotification( Notification notification ) {
for ( Notification not : notifications ) {
not.goDown();
}
for ( int i = 0; i < notifications.length; i++ ) {
if ( notification.equals( notifications[ i ] ) ) {
Notification[] temp = new Notification[ notifications.length - 1 ];
if ( i != notifications.length - 1 )
System.arraycopy( notifications, i + 1, notifications, i, notifications.length - i - 1 );
System.arraycopy( notifications, 0, temp, 0, temp.length );
notifications = temp;
}
}
notification.cleanUp();
}
}