/*
* File : ChatBrowserWindow.java
* Created : 28.12.2005
* By : kahless
*
* GOIM - Gamers Own Instant Messenger
* Copyright (C) 2005 Herbert Poul (kahless@sphene.net / herbert.poul@gmail.com)
* http://goim.sphene.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package net.sphene.goim.rcp.ui;
import net.sphene.goim.rcp.GOIMPlugin;
import net.sphene.goim.rcp.beans.GOIMAccount;
import net.sphene.goim.rcp.extensionpoints.IChatGUI;
import net.sphene.goim.rcp.preferences.PreferenceConstants;
import net.sphene.goim.rcp.ui.chat.GOIMChatObject;
import net.sphene.libs.SpheneEvent;
import net.sphene.libs.SpheneListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smackx.muc.MultiUserChat;
public class ChatBrowserWindow {
private Shell shell;
private GOIMAccount account;
private GOIMChatObject chat;
private MultiUserChat muc;
private ChatBrowserGUI window;
boolean isDirty = false;
protected boolean isActive;
protected String originalTitle = null;
public ChatBrowserWindow() {
shell = new Shell(SWT.SHELL_TRIM);
shell.setImage(GOIMIcons.getImage(GOIMIcons.GOIM_WINDOW_ICON));
shell.setLayout(new FillLayout());
}
public void setOriginalTitle(String originalTitle) {
this.originalTitle = originalTitle;
if(shell != null)
setShellTitle();
}
public void setIsDirty(boolean isDirty, boolean alreadyHandled) {
if(this.isDirty != isDirty) {
if(isDirty && isActive) return; // Don't set dirty if window is active
this.isDirty = isDirty;
setShellTitle();
if(isDirty && !alreadyHandled)
doForceActive();
}
}
/**
* Creates a ChatBrowserWindow which is usable for a already existing
* shell.
* @param window
*/
public ChatBrowserWindow(Shell shell, GOIMAccount account) {
this.shell = shell;
this.account = account;
}
protected void setShellTitle() {
System.out.println(" ---- setShellTitle " + isActive + " .. " + isDirty + " ... " + shell.isFocusControl() + " " + originalTitle);
String prefix = isDirty ? "* " : "";
String title = originalTitle;
shell.setText(prefix + title);
}
protected void myInit() {
if(chat != null) {
window = new ChatBrowserGUI(shell,SWT.NULL);
window.init(account,chat);
shell.setText(chat.getJID());
} else {
window = new MUCBrowserGUI(shell,SWT.NULL);
((MUCBrowserGUI)window).init(account,muc);
shell.setText(muc.getRoom());
}
setShellTitle();
initListeners();
shell.setMinimumSize(500,300);
shell.pack(true);
}
public void doForceActive() {
if(GOIMPlugin.getDefault().getMyPreferenceStore().getBoolean(PreferenceConstants.P_CHAT_ACTIVATE_CHAT_WINDOW)) {
if(GOIMPlugin.getDefault().getMyPreferenceStore().getBoolean(PreferenceConstants.P_CHAT_ACTIVATE_CHAT_WINDOW_ONLY_IFAVAILABLE)) {
if(account.xmpp.getOwnPresence() != null &&
account.xmpp.getOwnPresence().getType() == Presence.Type.AVAILABLE) {
//isActive = true;
//setIsDirty(false,false);
shell.forceActive();
}
} else {
//isActive = true;
//setIsDirty(false,false);
shell.forceActive();
}
}
}
public void initListeners() {
if(window != null) {
window.addChangeListener(new SpheneListener<SpheneEvent>(){
public void handleEvent(final SpheneEvent event) {
shell.getDisplay().asyncExec(new Runnable() {public void run() {
if(!originalTitle.equals(window.getJID()))
setOriginalTitle(window.getJID());
if(!isActive) {
isDirty = true;
setShellTitle();
}
Object obj = event.getParam(ChatBrowserGUI.EVENT_PARAM_ALREADYHANDLED);
if(obj == null || !((Boolean)obj).booleanValue())
doForceActive();
//setActive();
//System.out.println("want to be active *asking*");
}});
}});
}
//shell.addShellListener(new ShellListener() { });
shell.addShellListener(new ShellAdapter() {
@Override
public void shellActivated(ShellEvent e) {
System.out.println(" ---- shellActivated");
if(isDirty) {
isDirty = false;
setShellTitle();
}
isActive = true;
}
@Override
public void shellDeactivated(ShellEvent e) {
System.out.println(" ---- shellDeactivated");
isActive = false;
}
});
shell.addListener(SWT.FocusIn,new Listener() {
public void handleEvent(Event event) {
window.setCustomFocus();
}
});
}
public void init(GOIMAccount account, GOIMChatObject chat) {
this.account = account;
this.chat = chat;
originalTitle = chat.getJID();
myInit();
}
public void init(GOIMAccount account, MultiUserChat muc) {
this.account = account;
this.muc = muc;
originalTitle = muc.getRoom();
myInit();
}
public IChatGUI getChatGUI() {
return window;
}
public void open() {
shell.open();
}
}