怎么获取Eclipse里view的id

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么获取Eclipse里view的id相关的知识,希望对你有一定的参考价值。

在制作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。
参考技术A

1、android开发中获取控件View的id通过findViewById(R.id.view_id)方法


2、在xml布局文件中需要设置对应View控件id,使用android:id="@+id/view_id"


3、下面是一个Demo例子:

<TextView
android:id="view_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="获取TextView的id"/>

public class MainActivity extends Activity 
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState)  
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite);
        //这一步获取对应View的id
        mTextView=(TextView)findViewById(R.id.view_id);
        
        
          

asp.net mvc 在View中获取Url参数的值

如果url是 /home/index?id=3 直接Request就ok。

但是如果路由设定为:{controller}/{action}/{id} 

url是 /home/index/3   
这时想在页面View中获取参数id的值,该怎么获取?

 

查了下资料好多人都利用Action获取到参数值后,用Viewdata传到View中
例如
Controlers中的phonelist这样定义  
public ActionResult phonelist(int id)  
  {  
  ViewData["id"] = id;   
  return View();  
  }  

其实,没有必要,只要在view中这样获取就可以:

<%=Html.ViewContext.RouteData.Values["id"]%>

就算没有id的参数也不会报错。

 

同样:<%=Request.RequestContext.RouteData.Values["id"] %>和
<%=Html.ViewContext.RouteData.Route.GetRouteData(Html.ViewContext.HttpContext).Values["id"]%>

也可以取到。

在用户控件中是无法直接访问到RouteData,RouteData是Page对象中的属性,

所以需要在用户控件中使用this.Page.RouteData来获取参数

使用this.Page.RouteData.Values["id"]来获取参数的值

以上是关于怎么获取Eclipse里view的id的主要内容,如果未能解决你的问题,请参考以下文章

eclipse怎么搭建ssh框架

eclipse里如果想要改变文件路径应怎么做?

eclipse如何创建文本框并获取它的信息

eclipse 的show view没有logcat怎么办

请问微信小程序里 web-view内嵌网页 如何获取内页的标题并分享

Java web项目访问路径问题。求解啊!