intial source from http://www.sf.net/projects/wdte
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / internal / SwitchBrowserWorkbenchAction.java
1 /**
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
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  */
11 package net.sourceforge.phpeclipse.webbrowser.internal;
12
13 import java.util.Iterator;
14
15 import net.sourceforge.phpeclipse.webbrowser.IWebBrowser;
16
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.*;
26 /**
27  * Action to open the Web broswer.
28  */
29 public class SwitchBrowserWorkbenchAction implements IWorkbenchWindowPulldownDelegate2 {
30         /**
31          * The menu created by this action
32          */
33         private Menu fMenu;
34                 
35         protected boolean recreateMenu = false;
36
37         /**
38          * SwitchBrowserWorkbenchAction constructor comment.
39          */
40         public SwitchBrowserWorkbenchAction() {
41                 super();
42         }
43
44         public void dispose() {
45                 setMenu(null);
46         }
47         
48         /**
49          * Sets this action's drop-down menu, disposing the previous menu.
50          * 
51          * @param menu the new menu
52          */
53         private void setMenu(Menu menu) {
54                 if (fMenu != null) {
55                         fMenu.dispose();
56                 }
57                 fMenu = menu;
58         }
59
60         public void init(IWorkbenchWindow window) { }
61         
62         /**
63          * Adds the given action to the specified menu with an accelerator specified
64          * by the given number.
65          * 
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
69          */
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
74                         label.append('&');
75                         label.append(accelerator);
76                         label.append(' ');
77                 }
78                 label.append(action.getText());
79                 action.setText(label.toString());
80                 ActionContributionItem item= new ActionContributionItem(action);
81                 item.fill(menu, -1);
82         }
83         
84         /**
85          * Fills the drop-down menu with favorites and launch history,
86          * launch shortcuts, and an action to open the launch configuration dialog.
87          *
88          * @param menu the menu to fill
89          */
90         protected void fillMenu(Menu menu) {
91                 IWebBrowser current = BrowserManager.getInstance().getCurrentWebBrowser();
92                 Iterator iterator = BrowserManager.getInstance().getWebBrowsers().iterator();
93                 int i = 0;
94                 while (iterator.hasNext()) {
95                         IWebBrowser browser = (IWebBrowser) iterator.next();
96                         addToMenu(menu, new SwitchDefaultBrowserAction(browser, browser.equals(current)), i++);
97                 }
98         }
99
100         /**
101          * Creates the menu for the action
102          */
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++) {
112                                                 items[i].dispose();
113                                         }
114                                         fillMenu(m);
115                                         recreateMenu = false;
116                                 //}
117                         }
118                 });
119         }
120
121         public void selectionChanged(IAction action, ISelection selection) { }
122         
123         public void run(IAction action) { }
124
125         public Menu getMenu(Menu parent) {
126                 setMenu(new Menu(parent));
127                 //fillMenu(fMenu);
128                 initMenu();
129                 return fMenu;
130         }
131
132         public Menu getMenu(Control parent) {
133                 setMenu(new Menu(parent));
134                 //fillMenu(fMenu);
135                 initMenu();
136                 return fMenu;
137         }
138 }