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.viewers.ISelection;
18 import org.eclipse.jface.action.ActionContributionItem;
19 import org.eclipse.jface.action.IAction;
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.*;
27 * Action to open the Web broswer.
29 public class SwitchBrowserWorkbenchAction implements IWorkbenchWindowPulldownDelegate2 {
31 * The menu created by this action
35 protected boolean recreateMenu = false;
38 * SwitchBrowserWorkbenchAction constructor comment.
40 public SwitchBrowserWorkbenchAction() {
44 public void dispose() {
49 * Sets this action's drop-down menu, disposing the previous menu.
51 * @param menu the new menu
53 private void setMenu(Menu menu) {
60 public void init(IWorkbenchWindow window) { }
63 * Adds the given action to the specified menu with an accelerator specified
64 * by the given number.
66 * @param menu the menu to add the action to
67 * @param action the action to add
68 * @param accelerator the number that should appear as an accelerator
70 protected void addToMenu(Menu menu, IAction action, int accelerator) {
71 StringBuffer label= new StringBuffer();
72 if (accelerator >= 0 && accelerator < 10) {
73 //add the numerical accelerator
75 label.append(accelerator);
78 label.append(action.getText());
79 action.setText(label.toString());
80 ActionContributionItem item= new ActionContributionItem(action);
85 * Fills the drop-down menu with favorites and launch history,
86 * launch shortcuts, and an action to open the launch configuration dialog.
88 * @param menu the menu to fill
90 protected void fillMenu(Menu menu) {
91 IWebBrowser current = BrowserManager.getInstance().getCurrentWebBrowser();
92 Iterator iterator = BrowserManager.getInstance().getWebBrowsers().iterator();
94 while (iterator.hasNext()) {
95 IWebBrowser browser = (IWebBrowser) iterator.next();
96 addToMenu(menu, new SwitchDefaultBrowserAction(browser, browser.equals(current)), i++);
101 * Creates the menu for the action
103 private void initMenu() {
104 // Add listener to repopulate the menu each time
105 // it is shown because of dynamic history list
106 fMenu.addMenuListener(new MenuAdapter() {
107 public void menuShown(MenuEvent e) {
108 //if (recreateMenu) {
109 Menu m = (Menu) e.widget;
110 MenuItem[] items = m.getItems();
111 for (int i = 0; i < items.length; i++) {
115 recreateMenu = false;
121 public void selectionChanged(IAction action, ISelection selection) { }
123 public void run(IAction action) { }
125 public Menu getMenu(Menu parent) {
126 setMenu(new Menu(parent));
132 public Menu getMenu(Control parent) {
133 setMenu(new Menu(parent));