2 * Copyright (c) 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM - Initial API and implementation
11 package net.sourceforge.phpeclipse.webbrowser.internal;
13 import java.util.Iterator;
15 import net.sourceforge.phpeclipse.webbrowser.IWebBrowser;
17 import org.eclipse.jface.action.ActionContributionItem;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.swt.events.MenuAdapter;
21 import org.eclipse.swt.events.MenuEvent;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Menu;
24 import org.eclipse.swt.widgets.MenuItem;
25 import org.eclipse.ui.IWorkbenchWindow;
26 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
28 * Action to open the Web broswer.
30 public class SwitchBrowserWorkbenchAction implements IWorkbenchWindowPulldownDelegate2 {
32 * The menu created by this action
36 protected boolean recreateMenu = false;
39 * SwitchBrowserWorkbenchAction constructor comment.
41 public SwitchBrowserWorkbenchAction() {
45 public void dispose() {
50 * Sets this action's drop-down menu, disposing the previous menu.
52 * @param menu the new menu
54 private void setMenu(Menu menu) {
61 public void init(IWorkbenchWindow window) { }
64 * Adds the given action to the specified menu with an accelerator specified
65 * by the given number.
67 * @param menu the menu to add the action to
68 * @param action the action to add
69 * @param accelerator the number that should appear as an accelerator
71 protected void addToMenu(Menu menu, IAction action, int accelerator) {
72 StringBuffer label= new StringBuffer();
73 if (accelerator >= 0 && accelerator < 10) {
74 //add the numerical accelerator
76 label.append(accelerator);
79 label.append(action.getText());
80 action.setText(label.toString());
81 ActionContributionItem item= new ActionContributionItem(action);
86 * Fills the drop-down menu with favorites and launch history,
87 * launch shortcuts, and an action to open the launch configuration dialog.
89 * @param menu the menu to fill
91 protected void fillMenu(Menu menu) {
92 IWebBrowser current = BrowserManager.getInstance().getCurrentWebBrowser();
93 Iterator iterator = BrowserManager.getInstance().getWebBrowsers().iterator();
95 while (iterator.hasNext()) {
96 IWebBrowser browser = (IWebBrowser) iterator.next();
97 addToMenu(menu, new SwitchDefaultBrowserAction(browser, browser.equals(current)), i++);
102 * Creates the menu for the action
104 private void initMenu() {
105 // Add listener to repopulate the menu each time
106 // it is shown because of dynamic history list
107 fMenu.addMenuListener(new MenuAdapter() {
108 public void menuShown(MenuEvent e) {
109 //if (recreateMenu) {
110 Menu m = (Menu) e.widget;
111 MenuItem[] items = m.getItems();
112 for (int i = 0; i < items.length; i++) {
116 recreateMenu = false;
122 public void selectionChanged(IAction action, ISelection selection) { }
124 public void run(IAction action) { }
126 public Menu getMenu(Menu parent) {
127 setMenu(new Menu(parent));
133 public Menu getMenu(Control parent) {
134 setMenu(new Menu(parent));