How to extend p4eclipse plugin?

What’s p4eclipse?

Perforce provides is a very powerful and handy plugin for Eclipse – p4eclipse. With it, you can do everything in Eclipse enviornment, which is the same as in it p4 command line and p4v (a clicent of perforce).

Why to extend it?

Team also deployed ReviewBoard to do pre-check-in code review. And we have a command-line tool to post review request to it. The input is the id of perforce changelist.

Basically, we can see the changelist in Eclipse. The problem is how to combine them together and allow us to easily post the request from GUI directly.

2014-07-28_13-57-09

When you right-click on the changelist, you can see some actions in the context menu.

2014-07-28_14-02-55

Can we add another menu item into the context menu and post review request?

Ways to Extend p4eclipse

p4eclipse is also a Eclipse plugin and it follows the basic concept, such as extension, extension point, views and etc. It should be easily extended.

Find which view to extend

First, we need to find out the view class of P4 changelist. Simply move the mouse on view tab and click ATL+SHIFT+F1 to launch “Plug-in Selection Spy“. And the view name is PendingView, contained in plugin-in “com.perforce.team.ui” and the element in tree is P4PendingChangelist.

2014-07-28_14-08-36

Create plugin-in project

Now we know to extend which view. It’s time to create our own plugin-in project. And in the MANIFEST.MF, besides basic plugins, we need explicitly declare we need p4eclipse plugins,

  • com.perforce.team.ui
  • com.perforce.p4api
  • com.perfroce.team.core

2014-07-28_14-16-31

NOTE: Here is a bug of Eclipse PDE 3.x. Even we declare the com.perforce.team.ui is needed, but Eclipse can’t build through and there will be build errors. The root cause is com.perforce.team.ui is in a unpacking feature and PDE can’t revolve the dependency correctly in such case. To fix it, we have to download the jar file of com.perforce.team.ui to local and put it in to runtime class path manually. Remember, it’s only to fix PDE issue, we don’t need it in packaging.

2014-07-28_14-22-41

Add new menu item into the context menu

Open the MANIFEST.MF file and file the context menu extension declaration. It’s in org.eclipse.ui.popupMenus and the menu id is com.perforce.team.core.p4java.IP4PendingChangelist.

2014-07-28_14-26-35

Add new action into it,

2014-07-28_14-29-12

Next steps are easy

Now we finished the important part. If you are familiar with Eclipse RCP, you should understand next steps are easy. Basically, we need to launch a command line from Java code and implement an  action handler for the new created menu item. In my implementation, I also add a new preference page for team to setup the command location.

For details, please refer to my github project p4eclipse_ext.

Extened p4eclipse

After extension, the review request can be posted very easily, just like a build-in function of perforce. 😆

 

不要放过SWT里的Resource Leak

最近出了几个奇怪的问题,打开时间的GUI会莫名的抛Exception,报“no more SWT handle”. 句柄(Handle)资源以前做Win32/MFC接触的比较多,转到Java/SWT之后就很少接触到。

出错原因很清楚,Resource不够了!SWT和Swing的实现方法不同,它还是使用了Native的资源,所以句柄资源不会被GC回收。Eclipse提供了一个叫Sleak的工具来检查Leak的情况。情况果然非常严重,主要原因是我们生成了大量的Image。不仅仅是Image,在使用Font,Color,GC的时候也要非常注意,遵循“谁生成谁销毁”的原因。

在RCP中加入Sleak的方法是–Application.java的start方法中加入:

之后,Sleak的界面会随着RCP启动。

Reference