A massive organize imports and formatting of the sources using default Eclipse code...
[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 /**
29  * Action to open the Web broswer.
30  */
31 public class SwitchBrowserWorkbenchAction implements
32                 IWorkbenchWindowPulldownDelegate2 {
33         /**
34          * The menu created by this action
35          */
36         private Menu fMenu;
37
38         protected boolean recreateMenu = false;
39
40         /**
41          * SwitchBrowserWorkbenchAction constructor comment.
42          */
43         public SwitchBrowserWorkbenchAction() {
44                 super();
45         }
46
47         public void dispose() {
48                 setMenu(null);
49         }
50
51         /**
52          * Sets this action's drop-down menu, disposing the previous menu.
53          * 
54          * @param menu
55          *            the new menu
56          */
57         private void setMenu(Menu menu) {
58                 if (fMenu != null) {
59                         fMenu.dispose();
60                 }
61                 fMenu = menu;
62         }
63
64         public void init(IWorkbenchWindow window) {
65         }
66
67         /**
68          * Adds the given action to the specified menu with an accelerator specified
69          * by the given number.
70          * 
71          * @param menu
72          *            the menu to add the action to
73          * @param action
74          *            the action to add
75          * @param accelerator
76          *            the number that should appear as an accelerator
77          */
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
82                         label.append('&');
83                         label.append(accelerator);
84                         label.append(' ');
85                 }
86                 label.append(action.getText());
87                 action.setText(label.toString());
88                 ActionContributionItem item = new ActionContributionItem(action);
89                 item.fill(menu, -1);
90         }
91
92         /**
93          * Fills the drop-down menu with favorites and launch history, launch
94          * shortcuts, and an action to open the launch configuration dialog.
95          * 
96          * @param menu
97          *            the menu to fill
98          */
99         protected void fillMenu(Menu menu) {
100                 IWebBrowser current = BrowserManager.getInstance()
101                                 .getCurrentWebBrowser();
102                 Iterator iterator = BrowserManager.getInstance().getWebBrowsers()
103                                 .iterator();
104                 int i = 0;
105                 while (iterator.hasNext()) {
106                         IWebBrowser browser = (IWebBrowser) iterator.next();
107                         addToMenu(menu, new SwitchDefaultBrowserAction(browser, browser
108                                         .equals(current)), i++);
109                 }
110         }
111
112         /**
113          * Creates the menu for the action
114          */
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++) {
124                                         items[i].dispose();
125                                 }
126                                 fillMenu(m);
127                                 recreateMenu = false;
128                                 // }
129                         }
130                 });
131         }
132
133         public void selectionChanged(IAction action, ISelection selection) {
134         }
135
136         public void run(IAction action) {
137         }
138
139         public Menu getMenu(Menu parent) {
140                 setMenu(new Menu(parent));
141                 // fillMenu(fMenu);
142                 initMenu();
143                 return fMenu;
144         }
145
146         public Menu getMenu(Control parent) {
147                 setMenu(new Menu(parent));
148                 // fillMenu(fMenu);
149                 initMenu();
150                 return fMenu;
151         }
152 }