/*
* Gamers Own Instant Messenger
* Copyright (C) 2005-2006 Herbert Poul (kahless@sphene.net)
* 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 java.util.ArrayList;
import java.util.List;
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.perspectives.ChatPerspectiveFactory;
import net.sphene.goim.rcp.preferences.PreferenceConstants;
import net.sphene.goim.rcp.ui.chat.GOIMChatObject;
import net.sphene.goim.rcp.views.ChatView;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.muc.MultiUserChat;
/**
* @deprecated
* @author kahless
*
*/
public class WindowManager {
//protected Map<String,ChatWrapper> chatWrappers = new HashMap<String,ChatWrapper>();
protected List<ChatWrapper> chatWrappers = new ArrayList<ChatWrapper>();
GOIMAccount account;
private DisposeListener disposeListener;
protected static int counter = 0;
public WindowManager(GOIMAccount account) {
this.account = account;
System.out.println("Constructing new WindowManager !!!");
disposeListener = new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
ChatWrapper wrapper = (ChatWrapper) e.widget.getData("wrapper");
chatWrappers.remove(wrapper);
}
};
}
public ChatWrapper getChatWrapper(String jid) {
for(ChatWrapper wrapper : chatWrappers) {
if(wrapper.getJID().equals(jid)) {
return wrapper;
}
}
return null;
}
public IChatGUI createChatWindow(String jid, Message msg) {
return createChatWindow(jid,msg,null,true,false);
}
public IChatGUI createChatWindow(String jid, Message msg, GOIMChatObject chat, boolean tryBareAddress, boolean alreadyHandled) {
ChatWrapper wrapper = getChatWrapper(jid);
if(wrapper == null && tryBareAddress) wrapper = getChatWrapper(StringUtils.parseBareAddress(jid));
if(wrapper != null) {
wrapper.openGUI(msg, alreadyHandled);
return wrapper.window;
}
if(msg != null && msg.getBody() == null) return null;
if(alreadyHandled) return null;
if(chat == null)
chat = new GOIMChatObject(account,jid,msg == null ? null : msg.getThread());
wrapper = new ChatWrapper(jid,null,chat);
chatWrappers.add(wrapper);
wrapper.openGUI(msg, alreadyHandled);
return wrapper.window;
}
public IChatGUI createChatWindow(String jid) {
return createChatWindow(jid,null);
}
public IChatGUI createMUCWindow(String jid, MultiUserChat muc) {
ChatWrapper wrapper = getChatWrapper(jid);
if(wrapper == null) {
wrapper = new ChatWrapper(jid,null,muc);
System.out.println("Putting MUC Wrapper: " + jid);
chatWrappers.add(wrapper);
wrapper.openGUI(null,false);
}
return wrapper.window;
}
public class ChatWrapper {
public IChatGUI window;
public GOIMChatObject chat;
public String jid;
public MultiUserChat muc;
public ChatWrapper(String jid, IChatGUI window, GOIMChatObject chat) {
this.jid = jid; this.window = window; this.chat = chat;
}
public ChatWrapper(String jid, IChatGUI window, MultiUserChat muc) {
this.jid = jid; this.window = window; this.muc = muc;
}
public String getJID() {
if(window == null) return jid;
return window.getJID();
}
private IChatGUI openChatView(IWorkbenchPage page) {
IViewPart view;
try {
view = page.showView(ChatView.ID,Long.toString(System.currentTimeMillis() + ++counter),IWorkbenchPage.VIEW_ACTIVATE);
if(chat != null)
((ChatView)view).init(account,chat);
else
((ChatView)view).init(account,muc);
IChatGUI gui = ((ChatView)view).getChatGUI();
gui.setData("wrapper",ChatWrapper.this);
gui.addDisposeListener(disposeListener);
return gui;
} catch (PartInitException e) {
e.printStackTrace();
return null;
}
}
public void openGUI(final Message msg, final boolean alreadyHandled) {
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
public void run() {
if(window != null) {
// if(!window.isFocusControl())
// window.forceFocus();
window.processMessage(msg, alreadyHandled);
return;
}
String openType = GOIMPlugin.getDefault().getMyPreferenceStore().getString(PreferenceConstants.P_CHAT_OPEN_TYPE);
if(openType.equals(PreferenceConstants.P_CHAT_OPEN_TYPE_NEWWINDOW)) {
ChatBrowserWindow chatWindow = new ChatBrowserWindow();
//final Shell chatWindow = new Shell(SWT.SHELL_TRIM);
// chatWindow.setImage(GOIMIcons.getImage(GOIMIcons.GOIM_WINDOW_ICON));
// chatWindow.setLayout(new FillLayout());
if(chat != null)
chatWindow.init(account,chat);
else
chatWindow.init(account,muc);
window = chatWindow.getChatGUI();
//chatWindow.setText(account.name);
if(msg != null) window.processMessage(msg, alreadyHandled);
chatWindow.open();
window.setData("wrapper",ChatWrapper.this);
window.addDisposeListener(disposeListener);
} else if(openType.equals(PreferenceConstants.P_CHAT_OPEN_TYPE_SAMEWINDOW)){
IWorkbenchPage page = account.getDefaultWorkbenchPage();
if(page == null) page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IChatGUI gui = openChatView(page);
if(msg != null) gui.processMessage(msg, alreadyHandled);
window = gui;
} else if(openType.equals(PreferenceConstants.P_CHAT_OPEN_TYPE_NEWTABBEDWINDOW)) {
IWorkbenchPage chatPage = null;
IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
for(IWorkbenchWindow window : windows) {
IWorkbenchPage[] pages = window.getPages();
for(IWorkbenchPage page : pages) {
for(IPerspectiveDescriptor perspective : page.getSortedPerspectives()) {
if(perspective.getId().equals(ChatPerspectiveFactory.PERSPECTIVE_ID)) {
chatPage = page;
break;
}
}
}
}
if(chatPage == null) {
try {
IWorkbenchWindow wwindow = PlatformUI.getWorkbench().openWorkbenchWindow(ChatPerspectiveFactory.PERSPECTIVE_ID,null);
IChatGUI gui = openChatView(wwindow.getActivePage());
if(msg != null) gui.processMessage(msg, alreadyHandled);
window = gui;
} catch (WorkbenchException e) {
e.printStackTrace();
}
} else {
IChatGUI gui = openChatView(chatPage);
if(msg != null) gui.processMessage(msg, alreadyHandled);
window = gui;
}
} else {
throw new RuntimeException("Unsupported open type ?: " + openType);
}
}
});
}
}
}