论坛首页 Java企业应用论坛

eclipse popupMene高级教程:使用Visibility定制弹出菜单

浏览 5484 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-12-26  
这几天一直在研究popupMenu的Action,想要达到的目是选中不同的东西弹出不同的菜单。网上有很多介绍的文章,不过很多文章都是一些翻译过来并且作者没有自己实践过的东西,浪费了我很多时间,最后自己通过摸索,终于找出了这个问题的解决办法。

1.文章类型:
这是一篇解决问题的教程,不讨论技术的背景知识,比如运用到何种设计模式,以及实现背后的原理等问题。

2.此文对您的帮助:
看完此文后您将能够创建一个由选择内容指定弹出项的动态菜单。

setp1.创建一个rcp工程,模板使用有一个view的

step2.创建popupMenu
step2_1.新建类Action1:
package solonote.example.popupmenu;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

public class Action1 implements IObjectActionDelegate {

	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		// TODO Auto-generated method stub

	}

	@Override
	public void run(IAction action) {
		// TODO Auto-generated method stub

	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub

	}

}


step2_2.添加扩展点:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>

   <extension
         id="application"
         point="org.eclipse.core.runtime.applications">
      <application>
         <run
               class="solonote.example.popupmenu.Application">
         </run>
      </application>
   </extension>
   <extension
         point="org.eclipse.ui.perspectives">
      <perspective
            name="Perspective"
            class="solonote.example.popupmenu.Perspective"
            id="solonote.example.popupMenu.perspective">
      </perspective>
   </extension>
   <extension
         point="org.eclipse.ui.views">
      <view
            name="View"
            class="solonote.example.popupmenu.View"
            id="solonote.example.popupMenu.view">
      </view>
   </extension>
   <!-- 弹出菜单的定义 -->
   <extension
         point="org.eclipse.ui.popupMenus">
      <objectContribution
            id="objectContribution"
            objectClass="java.lang.Object">
         <action
               class="solonote.example.popupmenu.Action1"
               id="solonote.example.popupMenu.action1"
               label="label">
         </action>
      </objectContribution>
   </extension>

</plugin>


step2_3设置弹出菜单的弹出位置
修改View类
package solonote.example.popupmenu;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

public class View extends ViewPart {
	public static final String ID = "solonote.example.popupMenu.view";

	private TableViewer viewer;

	/**
	 * The content provider class is responsible for providing objects to the
	 * view. It can wrap existing objects in adapters or simply return objects
	 * as-is. These objects may be sensitive to the current input of the view,
	 * or ignore it and always show the same content (like Task List, for
	 * example).
	 */
	class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}

		public void dispose() {
		}

		public Object[] getElements(Object parent) {
			return new SimpleDTO[] { new SimpleDTO("One"), new SimpleDTO("Two"),  new SimpleDTO("Three") };
			
		}
	}

	class ViewLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			SimpleDTO dto = (SimpleDTO) obj;
			return dto.getName();
		}

		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}

		public Image getImage(Object obj) {
			return PlatformUI.getWorkbench().getSharedImages().getImage(
					ISharedImages.IMG_OBJ_ELEMENT);
		}
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL);
		viewer.setContentProvider(new ViewContentProvider());
		viewer.setLabelProvider(new ViewLabelProvider());
		viewer.setInput(getViewSite());
		
		//初始化弹出菜单
		MenuManager popupMenuManager = new MenuManager("#PopupMenu");
		popupMenuManager.setRemoveAllWhenShown(true);
		Menu popupMenu = popupMenuManager.createContextMenu(viewer.getTable());
		viewer.getTable().setMenu(popupMenu);
		//设置选择提供者和弹出菜单
		getSite().setSelectionProvider(viewer);
		getSite().registerContextMenu(popupMenuManager, viewer);
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}
}


好的step2已经结束了,现在您可以运行一下程序,看看效果.

接下来将要进行的是,只有选中Three时菜单才弹出,选择其他两个则不弹出。
确切的说是只有选择Three时     
<objectContribution
            id="objectContribution"
            objectClass="java.lang.Object">
节点中的Action才显示.

接下来:
step3_1修改扩展点 增加visibility项
  <!-- 弹出菜单的定义 -->
   <extension
         point="org.eclipse.ui.popupMenus">
      <objectContribution
            id="objectContribution"
            objectClass="java.lang.Object">
         <action
               class="solonote.example.popupmenu.Action1"
               id="solonote.example.popupMenu.action1"
               label="label">
         </action>
         <visibility>
            <objectState
                  name="name"
                  value="Three">
            </objectState>
         </visibility>
      </objectContribution>
   </extension>


修改被选中的DTO,让其实现IActionFilter接口
package solonote.example.popupmenu;

import org.eclipse.ui.IActionFilter;

/**
 * Viewer显示的项
 * @author solonote
 * @version 0.1.0 2007-12-26 上午11:49:41
 */
public class SimpleDTO implements IActionFilter{
	/**
	 * 显示的名字
	 */
	private String name;

	/**
	 * 返回显示的名字
	 * @return 显示的名字
	 */
	public String getName() {
		return name;
	}

	/**
	 * 设置显示的名字
	 * @param name 显示的名字
	 */
	public void setName(String name) {
		this.name = name;
	}

	public SimpleDTO(String name) {
		super();
		this.name = name;
	}

	/**
	 * 选择时候是否显示Action的判断
	 * <visibility>
            <objectState
                  name="name"
                  value="Three">
            </objectState>
         </visibility>
	 * @param target 选择的东西
	 * @param name plugin.xml里配置的name
	 * @param value plugin.xml里配置的value
	 */
	@Override
	public boolean testAttribute(Object target, String name, String value) {
		System.out.println(name);
		System.out.println(value);
		System.out.println(target);
		SimpleDTO dto = (SimpleDTO) target;
		if(dto.getName().equals(value))
			return true;	
		return false;
	}
	
	
}


然后就完成了,当然实际运用中我运用了Adapter将DTO适配成IActionFilter接口,这样DTO就不用和IActionFilter接口耦合了,或许以后的文章会接受如何适配。这里已经解决了问题,好的如果有任何疑问请联系我
solonote@163.com
这篇文章欢迎转载和分享,转载时请写明作者和出处,感谢.
  • 描述: step2结束效果
  • 大小: 13.1 KB
  • popupMenu.zip (21.2 KB)
  • 描述: eclipse工程
  • 下载次数: 333
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics