⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 browseractionbaradvisor.java

📁 基于eclipse|rcp的浏览器,另完成加入了收藏夹功能
💻 JAVA
字号:
/*******************************************************************************
 * Copyright (c) 2005, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.rcp.browser;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.RetargetAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

/**
 * Builds the actions and populates the menubar and toolbar when a new window is
 * opened. This work is factored into a separate class to avoid cluttering
 * <code>BrowserAdvisor</code>
 * <p>
 * This adds several actions to the menus and toolbar that are typical for web
 * browsers (e.g. Back, Forward, Stop, Refresh). These are defined as
 * retargetable actions, for which the <code>BrowserView</code> registers
 * handling actions.
 * 
 * @since 3.1
 */
public class BrowserActionBarAdvisor extends ActionBarAdvisor {

	private IAction newWindowAction, newTabAction, quitAction, receiveAction,
			aboutAction;

	private RetargetAction backAction, forwardAction, stopAction,
			refreshAction;
	
	private int count=0;
	/**
	 * to secondaryId
	 */
	
	/**
	 * @param configurer
	 */
	public BrowserActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
		
	}

	protected void makeActions(final IWorkbenchWindow window) {
		ISharedImages images = window.getWorkbench().getSharedImages();

		newWindowAction = ActionFactory.OPEN_NEW_WINDOW.create(window);
		newWindowAction.setText("新建窗体(&N)");
		register(newWindowAction);

		newTabAction = new Action("新建页面(&T)") { //$NON-NLS-1$
//			int counter = 100000;
			
			{
				setId("newTab");
				setActionDefinitionId(IBrowserConstants.COMMAND_PREFIX
						+ "newTab");
				setImageDescriptor(BrowserImages.getImageDescriptor("NEWTAB"));
				setToolTipText("新建页面");
			} //$NON-NLS-1$
			// 打开一个页面。重写Action类的方法。接口IAciotn中已经定义此方法.
			
			public void run() {
				try {
					
					String secondaryId = "advisor"+Integer.toString(++count);
					
					System.out.println("advisor:"+count);
					IWorkbenchPage page = window.getActivePage();
					if (page != null) {
						page.showView(IBrowserConstants.BROWSER_VIEW_ID,
								secondaryId, IWorkbenchPage.VIEW_ACTIVATE);
					}
				} catch (PartInitException e) {
					e.printStackTrace();
				}
			}
			
		};		
		register(newTabAction);
		// 退出按键
		quitAction = ActionFactory.QUIT.create(window);
		register(quitAction);
		// 返回按键
		backAction = new RetargetAction("back", "返回(&B)");
		backAction.setActionDefinitionId(IBrowserConstants.COMMAND_PREFIX
				+ "back"); //$NON-NLS-1$
		backAction.setToolTipText("返回");
		backAction.setImageDescriptor(images
				.getImageDescriptor(ISharedImages.IMG_TOOL_BACK));
//		backAction.setImageDescriptor(BrowserImages.getImageDescriptor("BACK"));
		window.getPartService().addPartListener(backAction);
		register(backAction);
		// 前进按键
		forwardAction = new RetargetAction("forward", "前进(&F)");
		forwardAction.setActionDefinitionId(IBrowserConstants.COMMAND_PREFIX
				+ "forward"); //$NON-NLS-1$
		forwardAction.setToolTipText("前进");
		forwardAction.setImageDescriptor(images
				.getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD));
		
		window.getPartService().addPartListener(forwardAction);
		register(forwardAction);
		// 停止按键
		stopAction = new RetargetAction("stop", "停止(&p)");
		stopAction.setActionDefinitionId(IBrowserConstants.COMMAND_PREFIX
				+ "stop"); //$NON-NLS-1$
		stopAction.setImageDescriptor(BrowserImages.getImageDescriptor("STOP"));
		stopAction.setToolTipText("停止");
		window.getPartService().addPartListener(stopAction);
		register(stopAction);
		// 刷新按键
		refreshAction = new RetargetAction("refresh", "刷新(&R)");
		refreshAction.setActionDefinitionId(IBrowserConstants.COMMAND_PREFIX
				+ "refresh"); //$NON-NLS-1$
		refreshAction.setImageDescriptor(BrowserImages.getImageDescriptor("FRESH"));
		refreshAction.setToolTipText("刷新");
		window.getPartService().addPartListener(refreshAction);
		register(refreshAction);
		// 历史按键,自定义
		receiveAction = new Action("收藏", IAction.AS_CHECK_BOX) { //$NON-NLS-1$
			{
				setId("receive");
				setActionDefinitionId(IBrowserConstants.COMMAND_PREFIX
						+ "receive");
				setImageDescriptor(BrowserImages.getImageDescriptor("RECEIVE"));
				
			} //$NON-NLS-1$

			public void run() {
				try {
					IWorkbenchPage page = window.getActivePage();
					if (page != null) {
						IViewPart receiveView = page
								.findView(IBrowserConstants.RECEIVE_VIEW_ID);
						if (receiveView == null) {
							page.showView(IBrowserConstants.RECEIVE_VIEW_ID);
							setChecked(true);
						} else {
							page.hideView(receiveView);
							setChecked(false);
						}
					}
				} catch (PartInitException e) {
					e.printStackTrace();
				}
			}
		};
		receiveAction.setToolTipText("收藏");
		register(receiveAction);
		// 关于按键
		// aboutAction = ActionFactory.ABOUT.create(window);
		aboutAction = new Action("关于MyBrowser") { //$NON-NLS-1$
			{
				setId("about");
				setActionDefinitionId(IBrowserConstants.COMMAND_PREFIX
						+ "about");
			} //$NON-NLS-1$

			public void run() {
				MessageDialog.openInformation(BrowserPlugin.getDefault()
						.getWorkbench().getActiveWorkbenchWindow().getShell(),
						"关于MyBrowser",
						"\tMyBrowser第一版\n\t基于eclipse RCP 平台\n\t作者:陈勇 email:chenyong301@tom.com\n\t个人主页www.i7766.com/blog/my/krf301 \n\t公用版本,出售源码");
			}
		};
		register(aboutAction);
	}

	protected void fillMenuBar(IMenuManager menuBar) {
		IMenuManager fileMenu = new MenuManager("文件(&F)", "file"); //$NON-NLS-2$
		menuBar.add(fileMenu);
		fileMenu.add(newWindowAction);
		fileMenu.add(newTabAction);
		fileMenu.add(new Separator());
		fileMenu.add(quitAction);

		IMenuManager viewMenu = new MenuManager("查看(&V)", "view"); //$NON-NLS-2$
		menuBar.add(viewMenu);
		viewMenu.add(backAction);
		viewMenu.add(forwardAction);
		viewMenu.add(stopAction);
		viewMenu.add(refreshAction);
		viewMenu.add(new Separator("views")); //$NON-NLS-1$
		viewMenu.add(receiveAction);

		IMenuManager helpMenu = new MenuManager("帮助(&H)", "help"); //$NON-NLS-2$
		menuBar.add(helpMenu);
		helpMenu.add(aboutAction);
	}

	protected void fillCoolBar(ICoolBarManager coolBar) {
		IToolBarManager toolBar = new ToolBarManager(SWT.FLAT|SWT.RIGHT);
		coolBar.add(new ToolBarContributionItem(toolBar, "standard")); //$NON-NLS-1$

		// For the Back and Forward actions, force their text to be shown on the
		// toolbar,
		// not just their image. For the remaining actions, the
		// ActionContributionItem
		// is created implicitly with the default presentation mode.
		ActionContributionItem backCI = new ActionContributionItem(backAction);
		backCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);
		toolBar.add(backCI);

		ActionContributionItem forwardCI = new ActionContributionItem(
				forwardAction);
		forwardCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);
		toolBar.add(forwardCI);

		toolBar.add(stopAction);
		toolBar.add(refreshAction);
//		toolBar.add(quitAction);
		IToolBarManager toolBar2=new ToolBarManager(SWT.FLAT | SWT.RIGHT);
		coolBar.add(new ToolBarContributionItem(toolBar2,"standard2"));
//		coolBar.add(toolBar2);
		ActionContributionItem newTabCI = new ActionContributionItem(newTabAction);
		newTabCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);
		toolBar2.add(newTabCI);
		ActionContributionItem receiveCI = new ActionContributionItem(receiveAction);
		receiveCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);
		toolBar2.add(receiveCI);
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -