12
返回列表 发新帖
楼主: Sky-Tiger

Eclipse Workbench: Using the Selection Service

[复制链接]
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
11#
 楼主| 发表于 2009-5-7 20:58 | 只看该作者
Multiple Selection Providers Within a Part

Be aware that the part's site accepts a single selection provider only, which should be registered within the createPartControl() method only:

    getSite().setSelectionProvider(provider);

Replacing the selection provider during the lifetime of the part is not properly supported by the workbench. If a part contains multiple viewers providing selections, like the "Java Hierarchy" view does, a intermediate ISelectionProvider implementation has to be provided that allows dynamically delegating to the currently active viewer within the part. As a starting point you may look into SelectionProviderIntermediate.java provided with this article.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
12#
 楼主| 发表于 2009-5-7 20:58 | 只看该作者
What to do With the Selected Objects?

This article claims that the selection service helps to decouple views reacting on each others selection. But the view handling a selection still needs to deal with the selected objects to provide any useful functionality. Check out the adapter pattern provided by the Eclipse runtime core, which allows attaching new functionality to existing model objects or the other way round providing required functionality for newly contributed objects.

Actually our example plug-in does exactly this by using the workbench label provider which in turn relies on the IWorkbenchAdapter to retrieve icons and text labels for the listed objects. The same mechanism is utilized by the "Properties" view, see article "Take control of your properties" for details.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
13#
 楼主| 发表于 2009-5-7 20:59 | 只看该作者
Example Plug-in

This article comes with a small example plug-in that demonstrates the explained techniques. Additionally the plug-in may serve for debugging your selection providers. The example contributes a new view "Workbench Selection" which simply mirrors the current selection in the workbench. It works for element selections as well as for text selections.
Getting and Running the Example

Download the plug-in com.mountainminds.eclipse.selectionsample_1.1.0.jar and import it into your workspace via the "Import..." wizard from the "File" menu. Select "Existing Project into Workspace" on the first wizard page. On the second page simply use the option "Select archive file" to import the downloaded archive. The fastest way to launch the example is right-clicking the plug-in project and select "Run As" → "Eclipse Application" in the context menu.

If don't want to play with the source code you can also drop the bundle into an Eclipse installation and use it directly.

From the menu of the launched workbench activate "Window" → "Show View" → "Other..." and select our view "Workbench Selection" in the category "Other". Now you can play around with selections in various workbench parts and see how our "Workbench Selection" view reflects these selections:
[Screen 2]

IMG01.gif (12.01 KB, 下载次数: 12)

IMG01.gif

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
14#
 楼主| 发表于 2009-5-7 20:59 | 只看该作者
The same works for text selections:
[Screen 3]

The example is implemented in a single class SelectionView.java applying the techniques discussed in this article. When reading through this short piece of code you may note that:

    * The ISelectionListener implementation makes sure that we do not react on our own selections.
    * The title of the source part of the current selection as well as the implementation class of ISelection is shown in the view description (the optional grey bar at the top).
    * The view uses two viewers: One for object lists and one for text blocks. Switching between the viewers is implemented done with a PageBook.
    *
      The viewer for object lists itself registeres as a selection provider. Check it out: If you select a element listed in our "Workbench Selection" view its properties are shown in turn in the "Properties" view.

IMG01.gif (7.75 KB, 下载次数: 9)

IMG01.gif

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
15#
 楼主| 发表于 2009-5-7 20:59 | 只看该作者
Conclusion

The mechanisms provided by the Eclipse workbench are simple to use and powerful. Not using this mechanisms result in plug-ins that poorly integrate with the rest of the workbench and will be hard to extend. To avoid such pitfalls simply follow these rules when selections come into picture:

    * Avoid direct hard-wired inter-view communication. If one view needs to react on the selection in another view use the ISelectionService.
    * Be cooperative and open for future extensions: Always register your viewers as selection providers with the part site.
    * Use existing selection specific views like the "Properties" view when appropriate instead of creating new ones.

使用道具 举报

回复
论坛徽章:
43
ITPUB元老
日期:2007-01-14 09:32:112011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:56管理团队成员
日期:2011-05-07 01:45:08开发板块每日发贴之星
日期:2011-08-29 01:01:012012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
16#
发表于 2009-5-8 02:42 | 只看该作者

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表