/*
* 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.actions;
import net.sphene.goim.rcp.beans.GOIMAccount;
import net.sphene.goim.rcp.beans.GOIMAccountList;
import net.sphene.goim.rcp.extensionpoints.IContactListView;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
import org.eclipse.ui.PlatformUI;
public class AccountAction implements IWorkbenchWindowPulldownDelegate2 {
public Menu getMenu(Control parent) {
System.out.println("AccountAction getMenu");
return null;
}
public void dispose() {
System.out.println("AccountAction dispose");
}
public void init(IWorkbenchWindow window) {
System.out.println("AccountAction init");
}
public void run(IAction action) {
System.out.println("AccountAction run");
}
public void selectionChanged(IAction action, ISelection selection) {
System.out.println("AccountAction selectionChanged");
}
public Menu getMenu(Menu parent) {
System.out.println("AccountAction getMenu(Menu)");
final Menu menu = new Menu(parent);
menu.addListener(SWT.Show,new Listener() {
public void handleEvent(Event event) {
System.out.println("Show Menu...");
try {
Object obj = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
if(obj instanceof IContactListView) {
final IContactListView listView = (IContactListView)obj;
GOIMAccountList accountList = listView.getAccountList();
if(accountList == null) {
Menu accMenu = listView.getActiveAccountContextMenu(null);//.setVisible(true);
// menu.getParent().setMenu(accMenu);
// menu.setVisible(false);
//accMenu.getItems();
// accMenu.setVisible(true);
copyMenu(accMenu,menu);
} else {
for(GOIMAccount account : accountList) {
final GOIMAccount acc = account;
MenuItem accountItem = new MenuItem(menu,SWT.CASCADE);
accountItem.setText(account.name + " (" + account.jid + ")");
final Menu accountMenu = new Menu(accountItem);
accountItem.setMenu(accountMenu);
accountMenu.addListener(SWT.Show,new Listener() {
public void handleEvent(Event event) {
Menu accMenu = listView.getActiveAccountContextMenu(acc);
copyMenu(accMenu,accountMenu);
} });
accountMenu.addListener(SWT.Hide,new Listener() {
public void handleEvent(Event event) {
for(MenuItem item : accountMenu.getItems()) {
if((item.getStyle() & SWT.CASCADE) != 0)
item.setMenu(null);
item.dispose();
}
}});
}
}
}
} catch( NullPointerException e ) {
// Ignoring NullPointerExceptions ....
}
}});
menu.addListener(SWT.Hide,new Listener() {
public void handleEvent(Event event) {
for(MenuItem item : menu.getItems()) {
if((item.getStyle() & SWT.CASCADE) != 0) {
item.setMenu(null);
}
item.dispose();
}
}});
return menu;
}
protected void copyMenu(Menu source, Menu target) {
MenuItem[] items = source.getItems();
for(MenuItem item : items) {
MenuItem newItem = new MenuItem(target,item.getStyle());
newItem.setEnabled(item.getEnabled());
newItem.setText(item.getText());
// for(int i = 0 ; i < 40 ; i ++) // currently there are 37 event codes
// if(i != SWT.Dispose)
// copyMenuAddListener(newItem,i,item);
if((item.getStyle() & SWT.CASCADE) != 0)
newItem.setMenu(item.getMenu());
copyMenuAddListener(newItem,SWT.Selection,item);
copyMenuAddListener(newItem,SWT.MouseDown,item);
copyMenuAddListener(newItem,SWT.MouseUp,item);
copyMenuAddListener(newItem,SWT.MouseEnter,item);
copyMenuAddListener(newItem,SWT.MouseExit,item);
copyMenuAddListener(newItem,SWT.MouseMove,item);
}
}
protected void copyMenuAddListener(MenuItem newItem, final int selection, final MenuItem originalItem) {
newItem.addListener(selection,new Listener(){
public void handleEvent(Event event) {
originalItem.notifyListeners(selection,event);
}});
}
}