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 org.eclipse.webbrowser.internal;
13 import java.util.Iterator;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.action.ActionContributionItem;
17 import org.eclipse.jface.action.IAction;
18 import org.eclipse.swt.events.MenuAdapter;
19 import org.eclipse.swt.events.MenuEvent;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Menu;
22 import org.eclipse.swt.widgets.MenuItem;
23 import org.eclipse.ui.*;
24 import org.eclipse.webbrowser.IWebBrowser;
26 * Action to open the Web broswer.
28 public class SwitchBrowserWorkbenchAction implements IWorkbenchWindowPulldownDelegate2 {
30 * The menu created by this action
34 protected boolean recreateMenu = false;
37 * SwitchBrowserWorkbenchAction constructor comment.
39 public SwitchBrowserWorkbenchAction() {
43 public void dispose() {
48 * Sets this action's drop-down menu, disposing the previous menu.
50 * @param menu the new menu
52 private void setMenu(Menu menu) {
59 public void init(IWorkbenchWindow window) { }
62 * Adds the given action to the specified menu with an accelerator specified
63 * by the given number.
65 * @param menu the menu to add the action to
66 * @param action the action to add
67 * @param accelerator the number that should appear as an accelerator
69 protected void addToMenu(Menu menu, IAction action, int accelerator) {
70 StringBuffer label= new StringBuffer();
71 if (accelerator >= 0 && accelerator < 10) {
72 //add the numerical accelerator
74 label.append(accelerator);
77 label.append(action.getText());
78 action.setText(label.toString());
79 ActionContributionItem item= new ActionContributionItem(action);
84 * Fills the drop-down menu with favorites and launch history,
85 * launch shortcuts, and an action to open the launch configuration dialog.
87 * @param menu the menu to fill
89 protected void fillMenu(Menu menu) {
90 IWebBrowser current = BrowserManager.getInstance().getCurrentWebBrowser();
91 Iterator iterator = BrowserManager.getInstance().getWebBrowsers().iterator();
93 while (iterator.hasNext()) {
94 IWebBrowser browser = (IWebBrowser) iterator.next();
95 addToMenu(menu, new SwitchDefaultBrowserAction(browser, browser.equals(current)), i++);
100 * Creates the menu for the action
102 private void initMenu() {
103 // Add listener to repopulate the menu each time
104 // it is shown because of dynamic history list
105 fMenu.addMenuListener(new MenuAdapter() {
106 public void menuShown(MenuEvent e) {
107 //if (recreateMenu) {
108 Menu m = (Menu) e.widget;
109 MenuItem[] items = m.getItems();
110 for (int i = 0; i < items.length; i++) {
114 recreateMenu = false;
120 public void selectionChanged(IAction action, ISelection selection) { }
122 public void run(IAction action) { }
124 public Menu getMenu(Menu parent) {
125 setMenu(new Menu(parent));
131 public Menu getMenu(Control parent) {
132 setMenu(new Menu(parent));