001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.rcp.browser;
011:
012: import org.eclipse.jface.action.Action;
013: import org.eclipse.jface.action.ActionContributionItem;
014: import org.eclipse.jface.action.IAction;
015: import org.eclipse.jface.action.ICoolBarManager;
016: import org.eclipse.jface.action.IMenuManager;
017: import org.eclipse.jface.action.IToolBarManager;
018: import org.eclipse.jface.action.MenuManager;
019: import org.eclipse.jface.action.Separator;
020: import org.eclipse.jface.action.ToolBarContributionItem;
021: import org.eclipse.jface.action.ToolBarManager;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.ui.ISharedImages;
024: import org.eclipse.ui.IViewPart;
025: import org.eclipse.ui.IWorkbenchPage;
026: import org.eclipse.ui.IWorkbenchWindow;
027: import org.eclipse.ui.PartInitException;
028: import org.eclipse.ui.actions.ActionFactory;
029: import org.eclipse.ui.actions.RetargetAction;
030: import org.eclipse.ui.application.ActionBarAdvisor;
031: import org.eclipse.ui.application.IActionBarConfigurer;
032:
033: /**
034: * Builds the actions and populates the menubar and toolbar when a new window
035: * is opened.
036: * This work is factored into a separate class to avoid cluttering
037: * <code>BrowserAdvisor</code>
038: * <p>
039: * This adds several actions to the menus and toolbar that are typical for
040: * web browsers (e.g. Back, Forward, Stop, Refresh). These are defined as
041: * retargetable actions, for which the <code>BrowserView</code> registers
042: * handling actions.
043: *
044: * @since 3.1
045: */
046: public class BrowserActionBarAdvisor extends ActionBarAdvisor {
047:
048: private IAction newWindowAction, newTabAction, quitAction,
049: historyAction, aboutAction;
050: private RetargetAction backAction, forwardAction, stopAction,
051: refreshAction;
052:
053: public BrowserActionBarAdvisor(IActionBarConfigurer configurer) {
054: super (configurer);
055: }
056:
057: protected void makeActions(final IWorkbenchWindow window) {
058: ISharedImages images = window.getWorkbench().getSharedImages();
059:
060: newWindowAction = ActionFactory.OPEN_NEW_WINDOW.create(window);
061: newWindowAction.setText("&New Window");
062: register(newWindowAction);
063:
064: newTabAction = new Action("New &Tab") { //$NON-NLS-1$
065: int counter = 0;
066: {
067: setId("newTab");
068: setActionDefinitionId(IBrowserConstants.COMMAND_PREFIX
069: + "newTab");} //$NON-NLS-1$
070:
071: public void run() {
072: try {
073: String secondaryId = Integer.toString(++counter);
074: IWorkbenchPage page = window.getActivePage();
075: if (page != null) {
076: page.showView(
077: IBrowserConstants.BROWSER_VIEW_ID,
078: secondaryId,
079: IWorkbenchPage.VIEW_ACTIVATE);
080: }
081: } catch (PartInitException e) {
082: e.printStackTrace();
083: }
084: }
085: };
086: register(newTabAction);
087:
088: quitAction = ActionFactory.QUIT.create(window);
089: register(quitAction);
090:
091: backAction = new RetargetAction("back", "&Back");
092: backAction
093: .setActionDefinitionId(IBrowserConstants.COMMAND_PREFIX
094: + "back"); //$NON-NLS-1$
095: backAction.setToolTipText("Back");
096: backAction.setImageDescriptor(images
097: .getImageDescriptor(ISharedImages.IMG_TOOL_BACK));
098: window.getPartService().addPartListener(backAction);
099: register(backAction);
100:
101: forwardAction = new RetargetAction("forward", "&Forward");
102: forwardAction
103: .setActionDefinitionId(IBrowserConstants.COMMAND_PREFIX
104: + "forward"); //$NON-NLS-1$
105: forwardAction.setToolTipText("Forward");
106: forwardAction.setImageDescriptor(images
107: .getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD));
108: window.getPartService().addPartListener(forwardAction);
109: register(forwardAction);
110:
111: stopAction = new RetargetAction("stop", "Sto&p");
112: stopAction
113: .setActionDefinitionId(IBrowserConstants.COMMAND_PREFIX
114: + "stop"); //$NON-NLS-1$
115: stopAction.setToolTipText("Stop");
116: window.getPartService().addPartListener(stopAction);
117: register(stopAction);
118:
119: refreshAction = new RetargetAction("refresh", "&Refresh");
120: refreshAction
121: .setActionDefinitionId(IBrowserConstants.COMMAND_PREFIX
122: + "refresh"); //$NON-NLS-1$
123: refreshAction.setToolTipText("Refresh");
124: window.getPartService().addPartListener(refreshAction);
125: register(refreshAction);
126:
127: historyAction = new Action("History", IAction.AS_CHECK_BOX) { //$NON-NLS-1$
128: {
129: setId("history");
130: setActionDefinitionId(IBrowserConstants.COMMAND_PREFIX
131: + "history");} //$NON-NLS-1$
132:
133: public void run() {
134: try {
135: IWorkbenchPage page = window.getActivePage();
136: if (page != null) {
137: IViewPart historyView = page
138: .findView(IBrowserConstants.HISTORY_VIEW_ID);
139: if (historyView == null) {
140: page
141: .showView(IBrowserConstants.HISTORY_VIEW_ID);
142: setChecked(true);
143: } else {
144: page.hideView(historyView);
145: setChecked(false);
146: }
147: }
148: } catch (PartInitException e) {
149: e.printStackTrace();
150: }
151: }
152: };
153: register(historyAction);
154:
155: aboutAction = ActionFactory.ABOUT.create(window);
156: register(aboutAction);
157: }
158:
159: protected void fillMenuBar(IMenuManager menuBar) {
160: IMenuManager fileMenu = new MenuManager("&File", "file"); //$NON-NLS-2$
161: menuBar.add(fileMenu);
162: fileMenu.add(newWindowAction);
163: fileMenu.add(newTabAction);
164: fileMenu.add(new Separator());
165: fileMenu.add(quitAction);
166:
167: IMenuManager viewMenu = new MenuManager("&View", "view"); //$NON-NLS-2$
168: menuBar.add(viewMenu);
169: viewMenu.add(backAction);
170: viewMenu.add(forwardAction);
171: viewMenu.add(stopAction);
172: viewMenu.add(refreshAction);
173: viewMenu.add(new Separator("views")); //$NON-NLS-1$
174: viewMenu.add(historyAction);
175:
176: IMenuManager helpMenu = new MenuManager("&Help", "help"); //$NON-NLS-2$
177: menuBar.add(helpMenu);
178: helpMenu.add(aboutAction);
179: }
180:
181: protected void fillCoolBar(ICoolBarManager coolBar) {
182: IToolBarManager toolBar = new ToolBarManager(SWT.FLAT
183: | SWT.RIGHT);
184: coolBar.add(new ToolBarContributionItem(toolBar, "standard")); //$NON-NLS-1$
185:
186: // For the Back and Forward actions, force their text to be shown on the toolbar,
187: // not just their image. For the remaining actions, the ActionContributionItem
188: // is created implicitly with the default presentation mode.
189: ActionContributionItem backCI = new ActionContributionItem(
190: backAction);
191: backCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);
192: toolBar.add(backCI);
193:
194: ActionContributionItem forwardCI = new ActionContributionItem(
195: forwardAction);
196: forwardCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);
197: toolBar.add(forwardCI);
198:
199: toolBar.add(stopAction);
200: toolBar.add(refreshAction);
201: }
202: }
|