怎么获取Eclipse里view的id

 我来答
福喜900
2015-01-16 · TA获得超过6.1万个赞
知道大有可为答主
回答量:1.1万
采纳率:0%
帮助的人:1亿
展开全部
在制作eclipse插件的时候,时常需要对已经存在的视图做一些扩展,例如在project explorer中增加一个右键菜单,或者需要获取outline中当前选中项,等等。而这些操作的前提,是获取这些视图的id,然后通过获取视图的语句获取到视图,并进行下一步操作。
例如:如果是在view中:

IViewPart?part?=?getViewSite().getPage().findView("org.eclipse.ui.views.ContentOutline");
如果是在action中:
IViewPart?part?=?Plugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("org.eclipse.ui.views.ContentOutline");
可见获取到视图的id是很重要的。如何获取id呢,有很多方法,我觉得比较常用的是以下两种:
1.通过手动查找目标视图所在的插件的plugin.xml中对改视图的定义,获得视图id。
描述:我觉得这是最保险的办法,因为所有的视图都必定有一个plugin.xml定义,但是找到这个plugin.xml还是需要一些操作,可能会耗费一些时间。
步骤:
a.点击“工具栏”->“Search”->“Search”,或者热键“ctrl+h”。选“ Plugin Search”。
b.Search for 选"Extention Point",Limit to 选“references”,External Scope 选“Enabled Plug-ins only”。
c.下面的scope选“Worksapaces”。
d.上面的Search String框填入我们要搜索的插件的扩展点的名字,这里我们要找视图插件,所以填入:org.eclipse.ui.views
e.点击search以后,在底部Search视图内列出很多搜索结果,任意选中一个,双击,可以看到editor内打开了该plugin.xml,选中编辑器底部的Extentions,用图形化的方式来查看会更快捷。
f.可以看到该插件使用的扩展点已经以列表的形式列出,我们查找的org.eclipse.ui.views扩展点也已经选中,展开选中的扩展点,可以看到它实现了哪些view,点击某一个,可以在页面右边看到该视图的详细信息,第一项即是视图的id。
我们无法知道目标视图在搜索结果中的哪个plugin中,所以可能会耗费一些时间,建议在查看搜索结果时,通过对视图的分类分析,确定一个大概范围再进行查看。
同时,用这种方法也可以查找其他扩展点的实现插件。
2.通过代码直接读出该视图的id。

描述:通过这种方法,可以直接用鼠标点击某个你想知道的视图,控制台会打印出该视图的id和所属插件的id。
步骤:
a.通过插件向导新建一个插件,并使用Available Templates中的Plugin with a view模板,点finish。
b.对自动创建的SampleView代码进行修改,修改结果如下。其中主要步骤有三个:实现IPartListener2接口,编写isActivePart()方法,调用isActivePart()方法。
package?com.raul.learn.sampleview.views;
import?org.eclipse.swt.widgets.Composite;
import?org.eclipse.ui.part.*;
import?org.eclipse.jface.viewers.*;
import?org.eclipse.swt.graphics.Image;
import?org.eclipse.jface.action.*;
import?org.eclipse.jface.dialogs.MessageDialog;
import?org.eclipse.ui.*;
import?org.eclipse.swt.widgets.Menu;
import?org.eclipse.swt.SWT;
import?com.raul.learn.sampleview.Activator;
/**
?*?This?sample?class?demonstrates?how?to?plug-in?a?new
?*?workbench?view.?The?view?shows?data?obtained?from?the
?*?model.?The?sample?creates?a?dummy?model?on?the?fly,
?*?but?a?real?implementation?would?connect?to?the?model
?*?available?either?in?this?or?another?plug-in?(e.g.?the?workspace).
?*?The?view?is?connected?to?the?model?using?a?content?provider.
?*?<p>
?*?The?view?uses?a?label?provider?to?define?how?model
?*?objects?should?be?presented?in?the?view.?Each
?*?view?can?present?the?same?model?objects?using
?*?different?labels?and?icons,?if?needed.?Alternatively,
?*?a?single?label?provider?can?be?shared?between?views
?*?in?order?to?ensure?that?objects?of?the?same?type?are
?*?presented?in?the?same?way?everywhere.
?*?<p>
?*/
public?class?SampleView?extends?ViewPart?implements?IPartListener2{
????private?TableViewer?viewer;
????private?Action?action1;
????private?Action?action2;
????private?Action?doubleClickAction;
????/*
?????*?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?String[]?{?"One",?"Two",?"Three"?};
????????}
????}
????class?ViewLabelProvider?extends?LabelProvider?implements?ITableLabelProvider?{
????????public?String?getColumnText(Object?obj,?int?index)?{
????????????return?getText(obj);
????????}
????????public?Image?getColumnImage(Object?obj,?int?index)?{
????????????return?getImage(obj);
????????}
????????public?Image?getImage(Object?obj)?{
????????????return?PlatformUI.getWorkbench().
????????????????????getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
????????}
????}
????class?NameSorter?extends?ViewerSorter?{
????}

????/**
?????*?The?constructor.
?????*/
????public?SampleView()?{
????????Activator.getDefault().getWorkbench().getActiveWorkbenchWindow()
????????.getActivePage().addPartListener(this);

????}

????@Override
????public?void?dispose()?{
????????//?TODO?Auto-generated?method?stub
????????Activator.getDefault().getWorkbench().getActiveWorkbenchWindow()
????????.getActivePage().removePartListener(this);
????????super.dispose();
????}
????
????/**
?????*?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.setSorter(new?NameSorter());
????????viewer.setInput(getViewSite());
????????makeActions();
????????hookContextMenu();
????????hookDoubleClickAction();
????????contributeToActionBars();
????}

????private?void?hookContextMenu()?{
????????MenuManager?menuMgr?=?new?MenuManager("#PopupMenu");
????????menuMgr.setRemoveAllWhenShown(true);
????????menuMgr.addMenuListener(new?IMenuListener()?{
????????????public?void?menuAboutToShow(IMenuManager?manager)?{
????????????????SampleView.this.fillContextMenu(manager);
????????????}
????????});
????????Menu?menu?=?menuMgr.createContextMenu(viewer.getControl());
????????viewer.getControl().setMenu(menu);
????????getSite().registerContextMenu(menuMgr,?viewer);
????}

????private?void?contributeToActionBars()?{
????????IActionBars?bars?=?getViewSite().getActionBars();
????????fillLocalPullDown(bars.getMenuManager());
????????fillLocalToolBar(bars.getToolBarManager());
????}

????private?void?fillLocalPullDown(IMenuManager?manager)?{
????????manager.add(action1);
????????manager.add(new?Separator());
????????manager.add(action2);
????}

????private?void?fillContextMenu(IMenuManager?manager)?{
????????manager.add(action1);
????????manager.add(action2);
????????//?Other?plug-ins?can?contribute?there?actions?here
????????manager.add(new?Separator(IWorkbenchActionConstants.MB_ADDITIONS));
????}
????
????private?void?fillLocalToolBar(IToolBarManager?manager)?{
????????manager.add(action1);
????????manager.add(action2);
????}

????@Override
????public?void?partBroughtToTop(IWorkbenchPartReference?partRef)?{
????????//?TODO?Auto-generated?method?stub
????????
????}

????@Override
????public?void?partClosed(IWorkbenchPartReference?partRef)?{
????????//?TODO?Auto-generated?method?stub
????????
????}

????@Override
????public?void?partDeactivated(IWorkbenchPartReference?partRef)?{
????????//?TODO?Auto-generated?method?stub
????????
????}

????@Override
????public?void?partHidden(IWorkbenchPartReference?partRef)?{
????????//?TODO?Auto-generated?method?stub
????????
????}

????@Override
????public?void?partInputChanged(IWorkbenchPartReference?partRef)?{
????????//?TODO?Auto-generated?method?stub
????????
????}

????@Override
????public?void?partOpened(IWorkbenchPartReference?partRef)?{
????????//?TODO?Auto-generated?method?stub
????????
????}

????private?void?isActivePart()?{
????????IWorkbenchPart?part?=?getViewSite().getPage().getActivePart();
????????//return?part?!=?null?&&?part.equals(this);????????
????????System.out.println(part.getSite().getId());
????????part.getSite().getPluginId();
????}
}

然后调插件,在运行环境中调出该视图,接着当你用鼠标点击你想知道的视图时,在开发环境中的控制台,就会打印出该视图的id以及所属插件的id。
huanglenzhi
2015-12-17 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117538 获赞数:517191
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
  在制作eclipse插件的时候,时常需要对已经存在的视图做一些扩展,例如在project explorer中增加一个右键菜单,或者需要获取outline中当前选中项,等等。而这些操作的前提,是获取这些视图的id,然后通过获取视图的语句获取到视图,并进行下一步操作。
  例如:如果是在view中:
  IViewPart part = getViewSite().getPage().findView("org.eclipse.ui.views.ContentOutline");
  如果是在action中:
  IViewPart part = Plugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("org.eclipse.ui.views.ContentOutline");
  可见获取到视图的id是很重要的。如何获取id呢,比较常用的是以下两种:
  1.通过手动查找目标视图所在的插件的plugin.xml中对改视图的定义,获得视图id。
  描述:这是最保险的办法,因为所有的视图都必定有一个plugin.xml定义,但是找到这个plugin.xml还是需要一些操作,可能会耗费一些时间。
  步骤:
  a.点击“工具栏”->“Search”->“Search”,或者热键“ctrl+h”。选“ Plugin Search”。
  b.Search for 选"Extention Point",Limit to 选“References”,External Scope 选“Enabled Plug-ins only”。
  c.下面的scope选“Worksapaces”。
  d.上面的Search String框填入我们要搜索的插件的扩展点的名字,这里要找视图插件,所以填入:org.eclipse.ui.views
  e.点击search以后,在底部Search视图内列出很多搜索结果,任意选中一个,双击,可以看到editor内打开了该plugin.xml,选中编辑器底部的Extentions,用图形化的方式来查看会更快捷。
  f.可以看到该插件使用的扩展点已经以列表的形式列出,查找的org.eclipse.ui.views扩展点也已经选中,展开选中的扩展点,可以看到它实现了哪些view,点击某一个,可以在页面右边看到该视图的详细信息,第一项即是视图的id。
  无法知道目标视图在搜索结果中的哪个plugin中,所以可能会耗费一些时间,建议在查看搜索结果时,通过对视图的分类分析,确定一个大概范围再进行查看。
  同时,用这种方法也可以查找其他扩展点的实现插件。
  2.通过代码直接读出该视图的id。
  描述:通过这种方法,可以直接用鼠标点击某个想知道的视图,控制台会打印出该视图的id和所属插件的id。
  步骤:
  a.通过插件向导新建一个插件,并使用Available Templates中的Plugin with a view模板,点finish。
  b.对自动创建的SampleView代码进行修改,修改结果如下。其中主要步骤有三个:实现IPartListener2接口,编写isActivePart()方法,调用isActivePart()方法。

  package com.raul.learn.sampleview.views;

  import org.eclipse.swt.widgets.Composite;
  import org.eclipse.ui.part.*;
  import org.eclipse.jface.viewers.*;
  import org.eclipse.swt.graphics.Image;
  import org.eclipse.jface.action.*;
  import org.eclipse.jface.dialogs.MessageDialog;
  import org.eclipse.ui.*;
  import org.eclipse.swt.widgets.Menu;
  import org.eclipse.swt.SWT;

  import com.raul.learn.sampleview.Activator;

  /**
  * This sample class demonstrates how to plug-in a new
  * workbench view. The view shows data obtained from the
  * model. The sample creates a dummy model on the fly,
  * but a real implementation would connect to the model
  * available either in this or another plug-in (e.g. the workspace).
  * The view is connected to the model using a content provider.
  * <p>
  * The view uses a label provider to define how model
  * objects should be presented in the view. Each
  * view can present the same model objects using
  * different labels and icons, if needed. Alternatively,
  * a single label provider can be shared between views
  * in order to ensure that objects of the same type are
  * presented in the same way everywhere.
  * <p>
  */

  public class SampleView extends ViewPart implements IPartListener2{
  private TableViewer viewer;
  private Action action1;
  private Action action2;
  private Action doubleClickAction;

  /*
  * 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 String[] { "One", "Two", "Three" };
  }
  }
  class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
  public String getColumnText(Object obj, int index) {
  return getText(obj);
  }
  public Image getColumnImage(Object obj, int index) {
  return getImage(obj);
  }
  public Image getImage(Object obj) {
  return PlatformUI.getWorkbench().
  getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
  }
  }
  class NameSorter extends ViewerSorter {
  }

  /**
  * The constructor.
  */
  public SampleView() {
  Activator.getDefault().getWorkbench().getActiveWorkbenchWindow()
  .getActivePage().addPartListener(this);

  }

  @Override
  public void dispose() {
  // TODO Auto-generated method stub
  Activator.getDefault().getWorkbench().getActiveWorkbenchWindow()
  .getActivePage().removePartListener(this);
  super.dispose();
  }
  
  /**
  * 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.setSorter(new NameSorter());
  viewer.setInput(getViewSite());
  makeActions();
  hookContextMenu();
  hookDoubleClickAction();
  contributeToActionBars();
  }

  private void hookContextMenu() {
  MenuManager menuMgr = new MenuManager("#PopupMenu");
  menuMgr.setRemoveAllWhenShown(true);
  menuMgr.addMenuListener(new IMenuListener() {
  public void menuAboutToShow(IMenuManager manager) {
  SampleView.this.fillContextMenu(manager);
  }
  });
  Menu menu = menuMgr.createContextMenu(viewer.getControl());
  viewer.getControl().setMenu(menu);
  getSite().registerContextMenu(menuMgr, viewer);
  }

  private void contributeToActionBars() {
  IActionBars bars = getViewSite().getActionBars();
  fillLocalPullDown(bars.getMenuManager());
  fillLocalToolBar(bars.getToolBarManager());
  }

  private void fillLocalPullDown(IMenuManager manager) {
  manager.add(action1);
  manager.add(new Separator());
  manager.add(action2);
  }

  private void fillContextMenu(IMenuManager manager) {
  manager.add(action1);
  manager.add(action2);
  // Other plug-ins can contribute there actions here
  manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
  }
  
  private void fillLocalToolBar(IToolBarManager manager) {
  manager.add(action1);
  manager.add(action2);
  }

  private void makeActions() {
  action1 = new Action() {
  public void run() {
  showMessage("Action 1 executed");
  }
  };
  action1.setText("Action 1");
  action1.setToolTipText("Action 1 tooltip");
  action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
  getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
  
  action2 = new Action() {
  public void run() {
  showMessage("Action 2 executed");
  }
  };
  action2.setText("Action 2");
  action2.setToolTipText("Action 2 tooltip");
  action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
  getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
  doubleClickAction = new Action() {
  public void run() {
  ISelection selection = viewer.getSelection();
  Object obj = ((IStructuredSelection)selection).getFirstElement();
  showMessage("Double-click detected on "+obj.toString());
  }
  };
  }

  private void hookDoubleClickAction() {
  viewer.addDoubleClickListener(new IDoubleClickListener() {
  public void doubleClick(DoubleClickEvent event) {
  doubleClickAction.run();
  }
  });
  }
  private void showMessage(String message) {
  MessageDialog.openInformation(
  viewer.getControl().getShell(),
  "Sample View",
  message);
  }

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

  @Override
  public void partActivated(IWorkbenchPartReference partRef) {
  // TODO Auto-generated method stub
  isActivePart();
  }

  @Override
  public void partBroughtToTop(IWorkbenchPartReference partRef) {
  // TODO Auto-generated method stub
  
  }

  @Override
  public void partClosed(IWorkbenchPartReference partRef) {
  // TODO Auto-generated method stub
  
  }

  @Override
  public void partDeactivated(IWorkbenchPartReference partRef) {
  // TODO Auto-generated method stub
  
  }

  @Override
  public void partHidden(IWorkbenchPartReference partRef) {
  // TODO Auto-generated method stub
  
  }

  @Override
  public void partInputChanged(IWorkbenchPartReference partRef) {
  // TODO Auto-generated method stub
  
  }

  @Override
  public void partOpened(IWorkbenchPartReference partRef) {
  // TODO Auto-generated method stub
  
  }

  @Override
  public void partVisible(IWorkbenchPartReference partRef) {
  // TODO Auto-generated method stub
  
  }
  
  private void isActivePart() {
  IWorkbenchPart part = getViewSite().getPage().getActivePart();
  //return part != null && part.equals(this);
  System.out.println(part.getSite().getId());
  part.getSite().getPluginId();
  }
  }

  然后调插件,在运行环境中调出该视图,接着当用鼠标点击你想知道的视图时,在开发环境中的控制台,就会打印出该视图的id以及所属插件的id。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式