core/com.nokia.carbide.cpp/src/com/nokia/carbide/cpp/ProductStartup.java
author cawthron
Tue, 24 Mar 2009 22:20:21 -0500
changeset 2 d760517a8095
child 9 6ef327765a4e
permissions -rw-r--r--
new
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
cawthron
parents:
diff changeset
     1
package com.nokia.carbide.cpp;
cawthron
parents:
diff changeset
     2
cawthron
parents:
diff changeset
     3
import java.lang.reflect.InvocationHandler;
cawthron
parents:
diff changeset
     4
import java.lang.reflect.Method;
cawthron
parents:
diff changeset
     5
import java.lang.reflect.Proxy;
cawthron
parents:
diff changeset
     6
cawthron
parents:
diff changeset
     7
import org.eclipse.jface.action.ActionContributionItem;
cawthron
parents:
diff changeset
     8
import org.eclipse.jface.action.IAction;
cawthron
parents:
diff changeset
     9
import org.eclipse.jface.action.IContributionItem;
cawthron
parents:
diff changeset
    10
import org.eclipse.jface.action.MenuManager;
cawthron
parents:
diff changeset
    11
import org.eclipse.jface.preference.IPreferenceStore;
cawthron
parents:
diff changeset
    12
import org.eclipse.swt.widgets.Display;
cawthron
parents:
diff changeset
    13
import org.eclipse.ui.IStartup;
cawthron
parents:
diff changeset
    14
import org.eclipse.ui.IWindowListener;
cawthron
parents:
diff changeset
    15
import org.eclipse.ui.IWorkbenchWindow;
cawthron
parents:
diff changeset
    16
import org.eclipse.ui.PlatformUI;
cawthron
parents:
diff changeset
    17
import org.eclipse.ui.internal.WorkbenchWindow;
cawthron
parents:
diff changeset
    18
cawthron
parents:
diff changeset
    19
public class ProductStartup implements IStartup {
cawthron
parents:
diff changeset
    20
cawthron
parents:
diff changeset
    21
cawthron
parents:
diff changeset
    22
	public void replaceAboutAction(final IWorkbenchWindow window)
cawthron
parents:
diff changeset
    23
	{
cawthron
parents:
diff changeset
    24
		
cawthron
parents:
diff changeset
    25
		if (window == null || !(window instanceof WorkbenchWindow))
cawthron
parents:
diff changeset
    26
			return;
cawthron
parents:
diff changeset
    27
		MenuManager manager = ((WorkbenchWindow)window).getMenuManager();
cawthron
parents:
diff changeset
    28
		final IPreferenceStore store = ProductPlugin.getDefault().getPreferenceStore();
cawthron
parents:
diff changeset
    29
cawthron
parents:
diff changeset
    30
		for (IContributionItem item : manager.getItems()) {
cawthron
parents:
diff changeset
    31
			if (item instanceof MenuManager) {
cawthron
parents:
diff changeset
    32
				MenuManager mgr = (MenuManager) item;
cawthron
parents:
diff changeset
    33
				if (item.getId().equals("help"))
cawthron
parents:
diff changeset
    34
				{
cawthron
parents:
diff changeset
    35
					final IContributionItem contrib = mgr.remove("about");
cawthron
parents:
diff changeset
    36
cawthron
parents:
diff changeset
    37
					if (contrib instanceof ActionContributionItem) {
cawthron
parents:
diff changeset
    38
						final IAction about = ((ActionContributionItem) contrib)
cawthron
parents:
diff changeset
    39
								.getAction();
cawthron
parents:
diff changeset
    40
						mgr.add(new ActionContributionItem((IAction) Proxy
cawthron
parents:
diff changeset
    41
								.newProxyInstance(about.getClass()
cawthron
parents:
diff changeset
    42
										.getClassLoader(),
cawthron
parents:
diff changeset
    43
										new Class[] { IAction.class },
cawthron
parents:
diff changeset
    44
										new InvocationHandler() {
cawthron
parents:
diff changeset
    45
cawthron
parents:
diff changeset
    46
											public Object invoke(Object arg0, Method method, Object[] arg2) throws Throwable {
cawthron
parents:
diff changeset
    47
cawthron
parents:
diff changeset
    48
												if ("runWithEvent"
cawthron
parents:
diff changeset
    49
														.equals(method
cawthron
parents:
diff changeset
    50
																.getName())
cawthron
parents:
diff changeset
    51
														|| "run".equals(method
cawthron
parents:
diff changeset
    52
																.getName())) {
cawthron
parents:
diff changeset
    53
													new CustomAboutDialog(
cawthron
parents:
diff changeset
    54
															window.getShell())
cawthron
parents:
diff changeset
    55
															.open();
cawthron
parents:
diff changeset
    56
													return null;
cawthron
parents:
diff changeset
    57
												}
cawthron
parents:
diff changeset
    58
												return method.invoke(about,
cawthron
parents:
diff changeset
    59
														arg2);
cawthron
parents:
diff changeset
    60
																						}
cawthron
parents:
diff changeset
    61
										})));
cawthron
parents:
diff changeset
    62
					}
cawthron
parents:
diff changeset
    63
					
cawthron
parents:
diff changeset
    64
				}
cawthron
parents:
diff changeset
    65
			}
cawthron
parents:
diff changeset
    66
		}
cawthron
parents:
diff changeset
    67
cawthron
parents:
diff changeset
    68
	}
cawthron
parents:
diff changeset
    69
	
cawthron
parents:
diff changeset
    70
	public void initializeMenus() {
cawthron
parents:
diff changeset
    71
cawthron
parents:
diff changeset
    72
		PlatformUI.getWorkbench().addWindowListener(new IWindowListener() {
cawthron
parents:
diff changeset
    73
cawthron
parents:
diff changeset
    74
			public void windowActivated(IWorkbenchWindow window) {
cawthron
parents:
diff changeset
    75
			}
cawthron
parents:
diff changeset
    76
cawthron
parents:
diff changeset
    77
			public void windowClosed(IWorkbenchWindow window) {
cawthron
parents:
diff changeset
    78
			}
cawthron
parents:
diff changeset
    79
cawthron
parents:
diff changeset
    80
			public void windowDeactivated(IWorkbenchWindow window) {
cawthron
parents:
diff changeset
    81
			}
cawthron
parents:
diff changeset
    82
cawthron
parents:
diff changeset
    83
			public void windowOpened(IWorkbenchWindow window) {
cawthron
parents:
diff changeset
    84
				replaceAboutAction(window);
cawthron
parents:
diff changeset
    85
			}
cawthron
parents:
diff changeset
    86
		});
cawthron
parents:
diff changeset
    87
cawthron
parents:
diff changeset
    88
		Display.getDefault().asyncExec(new Runnable() {
cawthron
parents:
diff changeset
    89
			public void run() {
cawthron
parents:
diff changeset
    90
				replaceAboutAction(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
cawthron
parents:
diff changeset
    91
			}
cawthron
parents:
diff changeset
    92
		});
cawthron
parents:
diff changeset
    93
		
cawthron
parents:
diff changeset
    94
	}
cawthron
parents:
diff changeset
    95
cawthron
parents:
diff changeset
    96
	public void earlyStartup() {
cawthron
parents:
diff changeset
    97
		
cawthron
parents:
diff changeset
    98
		initializeMenus();
cawthron
parents:
diff changeset
    99
		
cawthron
parents:
diff changeset
   100
	}
cawthron
parents:
diff changeset
   101
cawthron
parents:
diff changeset
   102
}