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;
29 * Action to open the Web broswer.
31 public class SwitchBrowserWorkbenchAction implements
32 IWorkbenchWindowPulldownDelegate2 {
34 * The menu created by this action
38 protected boolean recreateMenu = false;
41 * SwitchBrowserWorkbenchAction constructor comment.
43 public SwitchBrowserWorkbenchAction() {
47 public void dispose() {
52 * Sets this action's drop-down menu, disposing the previous menu.
57 private void setMenu(Menu menu) {
64 public void init(IWorkbenchWindow window) {
68 * Adds the given action to the specified menu with an accelerator specified
69 * by the given number.
72 * the menu to add the action to
76 * the number that should appear as an accelerator
78 protected void addToMenu(Menu menu, IAction action, int accelerator) {
79 StringBuffer label = new StringBuffer();
80 if (accelerator >= 0 && accelerator < 10) {
81 // add the numerical accelerator
83 label.append(accelerator);
86 label.append(action.getText());
87 action.setText(label.toString());
88 ActionContributionItem item = new ActionContributionItem(action);
93 * Fills the drop-down menu with favorites and launch history, launch
94 * shortcuts, and an action to open the launch configuration dialog.
99 protected void fillMenu(Menu menu) {
100 IWebBrowser current = BrowserManager.getInstance()
101 .getCurrentWebBrowser();
102 Iterator iterator = BrowserManager.getInstance().getWebBrowsers()
105 while (iterator.hasNext()) {
106 IWebBrowser browser = (IWebBrowser) iterator.next();
107 addToMenu(menu, new SwitchDefaultBrowserAction(browser, browser
108 .equals(current)), i++);
113 * Creates the menu for the action
115 private void initMenu() {
116 // Add listener to repopulate the menu each time
117 // it is shown because of dynamic history list
118 fMenu.addMenuListener(new MenuAdapter() {
119 public void menuShown(MenuEvent e) {
120 // if (recreateMenu) {
121 Menu m = (Menu) e.widget;
122 MenuItem[] items = m.getItems();
123 for (int i = 0; i < items.length; i++) {
127 recreateMenu = false;
133 public void selectionChanged(IAction action, ISelection selection) {
136 public void run(IAction action) {
139 public Menu getMenu(Menu parent) {
140 setMenu(new Menu(parent));
146 public Menu getMenu(Control parent) {
147 setMenu(new Menu(parent));