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