/*
* Gamer's 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;
import java.net.MalformedURLException;
import java.net.URL;
import net.sphene.goim.rcp.beans.GOIMGameItem;
import net.sphene.goim.rcp.beans.GOIMGameList;
import net.sphene.goim.rcp.extensionpoints.ContactListExtensionPoint;
import net.sphene.goim.rcp.extensionpoints.ContactListExtensionPoint.ContactListExtension;
import net.sphene.goim.rcp.preferences.GOIMAccountPreferences;
import net.sphene.goim.rcp.ui.GOIMIcons;
import net.sphene.goim.rcp.ui.dialogs.GOIMCreditDialog;
import net.sphene.goim.rcp.ui.dialogs.GOIMAboutDialog;
import net.sphene.libs.SpheneEvent;
import net.sphene.libs.SpheneListener;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ContributionItemFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.eclipse.update.ui.UpdateManagerUI;
/**
* An action bar advisor is responsible for creating, adding, and disposing of
* the actions added to a workbench window. Each window will be populated with
* new actions.
*/
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
// Actions - important to allocate these only in makeActions, and then use
// them
// in the fill methods. This ensures that the actions aren't recreated
// when fillActionBars is called with FILL_PROXY.
private IWorkbenchAction exitAction;
private IWorkbenchAction preferencesAction;
private MenuManager toolMenu;
private IWorkbenchAction aboutAction;
private IWorkbenchAction creditAction;
//private IWorkbenchAction showView;
private UpdateConfigurationManagerAction updateManagerAction;
private UpdateInstallAction updateInstallerAction;
private IWorkbenchAction helpContents;
private MenuManager launchMenu;
private IWorkbenchWindow workbenchWindow;
public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
super(configurer);
}
protected void makeActions(final IWorkbenchWindow window) {
// Creates the actions and registers them.
// Registering is needed to ensure that key bindings work.
// The corresponding commands keybindings are defined in the plugin.xml
// file.
// Registering also provides automatic disposal of the actions when
// the window is closed.
this.workbenchWindow = window;
exitAction = ActionFactory.QUIT.create(window);
//preferencesAction = ActionFactory.PREFERENCES.create(window);
preferencesAction = new OpenPreferencesAction(window);
creditAction = new OpenCreditDialog(window);
aboutAction = new OpenAboutDialog(window);
//showView = ActionFactory.SHOW_VIEW_MENU.create(window);
helpContents = new OpenHelpContents(window);
updateManagerAction = new UpdateConfigurationManagerAction(window);
updateInstallerAction = new UpdateInstallAction(window);
register(exitAction);
register(preferencesAction);
register(aboutAction);
register(creditAction);
//register(showView);
register(helpContents);
register(updateManagerAction);
register(updateInstallerAction);
}
public class OpenAboutDialog extends Action implements IWorkbenchAction {
private IWorkbenchWindow workbenchWindow;
public OpenAboutDialog (IWorkbenchWindow window) {
super("About GOIM");
this.workbenchWindow = window;
setId("aboutdialog");
setActionDefinitionId("org.eclipse.ui.about.AboutDialog"); //$NON-NLS-1$
}
public void run() {
if(workbenchWindow == null) return;
new GOIMAboutDialog(workbenchWindow.getShell()).open();
}
public void dispose() {
this.workbenchWindow = null;
}
}
public class OpenCreditDialog extends Action implements IWorkbenchAction {
private IWorkbenchWindow workbenchWindow;
public OpenCreditDialog (IWorkbenchWindow window) {
super("Credits");
this.workbenchWindow = window;
setId("creditdialog");
setActionDefinitionId("org.eclipse.ui.about.AboutDialog"); //$NON-NLS-1$
}
public void run() {
if(workbenchWindow == null) return;
new GOIMCreditDialog(workbenchWindow.getShell()).open();
}
public void dispose() {
this.workbenchWindow = null;
}
}
public class OpenHelpContents extends Action implements IWorkbenchAction {
private IWorkbenchWindow workbenchWindow;
public OpenHelpContents(IWorkbenchWindow window) {
super("Help Contents");
this.workbenchWindow = window;
setId("helpcontents");
setActionDefinitionId("org.eclipse.ui.help.helpContents"); //$NON-NLS-1$
}
public void run() {
if(workbenchWindow == null) return;
openBrowser(GOIMPlugin.goimSiteURLhelp);
// try {
// workbenchWindow.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(new URL(GOIMPlugin.goimSiteURLhelp));
// } catch (PartInitException e) {
// e.printStackTrace();
// ErrorDialog.openError(workbenchWindow.getShell(),"Could not open Browser","Could not open browser to display help.",new Status(IStatus.ERROR,GOIMPlugin.ID,IStatus.OK,"Error while opening browser.",e));
// } catch (MalformedURLException e) {
// e.printStackTrace();
// }
}
public void dispose() {
this.workbenchWindow = null;
}
}
public static class OpenPreferencesAction extends Action implements IWorkbenchAction {
private IWorkbenchWindow workbenchWindow;
public OpenPreferencesAction(IWorkbenchWindow window) {
super("Preferences...");
if (window == null) {
throw new IllegalArgumentException();
}
this.workbenchWindow = window;
setActionDefinitionId("org.eclipse.ui.window.preferences"); //$NON-NLS-1$
// @issue action id not set
//setToolTipText(WorkbenchMessages.OpenPreferences_toolTip);
//window.getWorkbench().getHelpSystem().setHelp(this,
//IWorkbenchHelpContextIds.OPEN_PREFERENCES_ACTION);
setId("preferences");
}
public void run() {
if (workbenchWindow == null) {
// action has been dispose
return;
}
//PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(workbenchWindow.getShell(), GOIMAccountPreferences.ID, new String[] { GOIMPreferencePage.ID,GOIMAccountPreferences.ID, GOIMGames.ID, Icons.ID }, null);
PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(workbenchWindow.getShell(), GOIMAccountPreferences.ID, null, null);
//dialog.setSelectedNode(GOIMPreferencePage.ID);
dialog.open();
}
public void dispose() {
workbenchWindow = null;
}
}
public static class UpdateConfigurationManagerAction extends Action implements IWorkbenchAction {
private IWorkbenchWindow workbenchWindow;
public UpdateConfigurationManagerAction(IWorkbenchWindow window) {
super("Manage Configuration",GOIMIcons.getImageDescriptor("eclipse_ui_ide/icons/full/elcl16/configs.gif"));
if (window == null) {
throw new IllegalArgumentException();
}
this.workbenchWindow = window;
//setActionDefinitionId("org.eclipse.ui.window.preferences"); //$NON-NLS-1$
// @issue action id not set
//setToolTipText(WorkbenchMessages.OpenPreferences_toolTip);
//window.getWorkbench().getHelpSystem().setHelp(this,
//IWorkbenchHelpContextIds.OPEN_PREFERENCES_ACTION);
setId("manageconfiguration");
}
public void run() {
if (workbenchWindow == null) return;
BusyIndicator.showWhile(workbenchWindow.getShell().getDisplay(), new Runnable() {
public void run() {
UpdateManagerUI.openConfigurationManager(workbenchWindow.getShell());
}
});
}
public void dispose() {
workbenchWindow = null;
}
}
public static class UpdateInstallAction extends Action implements IWorkbenchAction {
private IWorkbenchWindow workbenchWindow;
public UpdateInstallAction(IWorkbenchWindow window) {
super("Find and Install Updates ...",GOIMIcons.getImageDescriptor("eclipse_ui_ide/icons/full/elcl16/usearch_obj.gif"));
if (window == null) {
throw new IllegalArgumentException();
}
this.workbenchWindow = window;
//setActionDefinitionId("org.eclipse.ui.window.preferences"); //$NON-NLS-1$
// @issue action id not set
//setToolTipText(WorkbenchMessages.OpenPreferences_toolTip);
//window.getWorkbench().getHelpSystem().setHelp(this,
//IWorkbenchHelpContextIds.OPEN_PREFERENCES_ACTION);
setId("updateinstall");
}
public void run() {
if (workbenchWindow == null) return;
BusyIndicator.showWhile(workbenchWindow.getShell().getDisplay(), new Runnable() {
public void run() {
UpdateManagerUI.openInstaller(workbenchWindow.getShell());
}
});
}
public void dispose() {
workbenchWindow = null;
}
}
protected void fillMenuBar(IMenuManager menuBar) {
MenuManager fileMenu = new MenuManager("&File",
IWorkbenchActionConstants.M_FILE);
menuBar.add(fileMenu);
fileMenu.add(exitAction);
MenuManager windowMenu = new MenuManager("Window",IWorkbenchActionConstants.M_WINDOW);
menuBar.add(windowMenu);
windowMenu.add(preferencesAction);
//windowMenu.add(ActionFactory.EDIT_ACTION_SETS.create(window));
//windowMenu.add(showView);
MenuManager perspectiveMenu = new MenuManager("Open Perspective");
IContributionItem perspectiveList = ContributionItemFactory.PERSPECTIVES_SHORTLIST.create(workbenchWindow);
perspectiveMenu.add(perspectiveList);
windowMenu.add(perspectiveMenu);
MenuManager viewMenu = new MenuManager("Show View");
IContributionItem viewList = ContributionItemFactory.VIEWS_SHORTLIST.create(workbenchWindow);
viewMenu.add(viewList);
windowMenu.add(viewMenu);
toolMenu = new MenuManager("Tools","tools");
menuBar.add(toolMenu);
launchMenu = new MenuManager("Launch Game","tools/launch");
toolMenu.add(launchMenu);
createLaunchMenu();
GOIMGameList gameList = GOIMPlugin.getPreferenceObject(GOIMGameList.class);
gameList.changeListenerList.addListener(new SpheneListener<SpheneEvent>() {
public void handleEvent(SpheneEvent event) {
launchMenu.removeAll();
createLaunchMenu();
}});
MenuManager openContactList = new MenuManager("Open ContactList","tools/contactlist");
toolMenu.add(openContactList);
for(final ContactListExtension extension : ContactListExtensionPoint.getContactListExtensions()) {
Action action = new Action(extension.name) {
public void run() {
GOIMPlugin.getDefault().openContactList(extension.viewid);
}
};
openContactList.add(action);
}
MenuManager helpMenu = new MenuManager("&Help",IWorkbenchActionConstants.M_HELP);
menuBar.add(helpMenu);
helpMenu.add(helpContents);
MenuManager updateMenu = new MenuManager("Updates","updates");
helpMenu.add(updateMenu);
updateMenu.add(updateInstallerAction);
updateMenu.add(updateManagerAction);
helpMenu.add(new Separator());
helpMenu.add(new Action("Report a Bug") {
public void run() {
openBrowser(GOIMPlugin.goimSiteURLreportBug);
}
});
helpMenu.add(new Action("Request a Feature"){
public void run() {
openBrowser(GOIMPlugin.goimSiteURLrequestFeature);
}
});
helpMenu.add(new Separator());
helpMenu.add(aboutAction);
helpMenu.add(creditAction);
}
protected void openBrowser(String url) {
try {
workbenchWindow.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(new URL(url));
} catch (PartInitException e) {
e.printStackTrace();
ErrorDialog.openError(workbenchWindow.getShell(),"Could not open Browser","Could not open browser to display help.",new Status(IStatus.ERROR,GOIMPlugin.ID,IStatus.OK,"Error while opening browser.",e));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
protected void createLaunchMenu() {
GOIMGameList gameList = GOIMPlugin.getPreferenceObject(GOIMGameList.class);
for(final GOIMGameItem game : gameList) {
//game.retrieveExtensionProxy().
Action action = new Action(game.retrieveExtensionProxy().name) {
public void run() {
game.retrieveExtensionProxy().execute(game,null);
}
};
ImageDescriptor icon = game.retrieveExtensionProxy().getIcon();
if(icon != null) action.setImageDescriptor(icon);
launchMenu.add(action);
}
}
}