您的位置:首页 > 编程语言 > Java开发

eclipse财务管理小插件

2005-12-24 06:49 316 查看
说到就要做到,一晚上把插件搞定。因为主要部分的代码已经写好了,要做的不过是把它做成插件,所以一小时不到就弄的差不多了,没想到
后来出了两个小问题,很是烦人,浪费了不少,其实是很多很多时间...
第一个问题就是关于生成Image的问题,我开始想的是下面这个方法:
private ImageDescriptor getImageDescriptor(String relativePath) {
  String iconPath = "icons/";
  try {
   ViewsPlugin plugin = ViewsPlugin.getDefault();
   URL installURL = plugin.getDescriptor().getInstallURL();
   URL url = new URL(installURL, iconPath + relativePath);
   return ImageDescriptor.createFromURL(url);
  }
  catch (MalformedURLException e) {
   // should not happen
   return ImageDescriptor.getMissingImageDescriptor();
  }
 }
然后要用到某个图片只需要这样的代码就可以了:
deleteItemAction.setImageDescriptor(getImageDescriptor("delete.gif"));
我的编译器给出了警告,说.getDescriptor().getInstallURL()这两个方法都已经deprecated了,我不听,希望能这样运行通过,可是Viewer
就是出不来,没办法,我去网上查,在eclipseplugin网站上下了个小插件,那个作者也是这样弄的,郁闷...
最后只有查API了,既然这两个方法不行,那么对 AbstractUIPlugin这个类一定有好的新办法了,果然,我找到这个方法:
imageDescriptorFromPlugin
public static ImageDescriptor imageDescriptorFromPlugin(String pluginId,
                                                        String imageFilePath)Creates and returns a new image descriptor for
an image file located within the specified plug-in.
This is a convenience method that simply locates the image file in within the plug-in (no image registries are involved). The
path is relative to the root of the plug-in, and takes into account files coming from plug-in fragments. The path may include
$arg$ elements. However, the path must not have a leading "." or path separator. Clients should use a path like
"icons/mysample.gif" rather than "./icons/mysample.gif" or "/icons/mysample.gif".

Parameters:
pluginId - the id of the plug-in containing the image file; null is returned if the plug-in does not exist
imageFilePath - the relative path of the image file, relative to the root of the plug-in; the path must be legal
Returns:
an image descriptor, or null if no image could be found
Since:
3.0
在我的代码是这样用的:
private String FINANCE_PLUGIN_ID="starshus.csdn.blog.finance2";
public ImageDescriptor getImageDescriptor(String relativePath) {
  String iconPath = "icons/";
  try {
   return FinancePlugin.imageDescriptorFromPlugin(FINANCE_PLUGIN_ID, iconPath+relativePath);
  }
  catch (Exception e) {
   // should not happen
   return ImageDescriptor.getMissingImageDescriptor(); 
  }
 }
调用图片的时候这样:
saveAction.setImageDescriptor(getImageDescriptor("save.gif"));

image的问题解决了.还有个问题是我在View的createPartControl()方法里生成一个TableTreeView后,直接自己设定layoutData,结果这个插件怎么都无法初始化...也花了不少时间来debug...
还有个小技巧,在写插件,如果要启动一个Dialog,往往需要向dialog里传入一个Shell,我找了不少时间才找到这个方法:
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
不要忘记把org.eclise.ui放入build_path的:P
这个插件在3.0和3.1下运行都没有问题,把我的插件直接解压到eclipse的plugin目录,重启动eclipse就可以了.
打开这个View方法是:Window--> Show View--> other--> Finance --> Table View
是这个样子:
打开:



插件界面:



在eclipse里就是爽:P



插件下载地址:
http://rapidshare.de/files/9717855/Finance.zip.html

JUnit Plugin test的代码:
public class SomeTest extends TestCase {

    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
    }

    /*
     * @see TestCase#tearDown()
     */
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    /**
     * Constructor for SomeTest.
     * @param name
     */
    public SomeTest(String name) {
        super(name);
    }
    private IWorkbenchPage getPage(){
        IWorkbench workbench = PlatformUI.getWorkbench();
        IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
        return window.getActivePage();
    }
    public void test()throws PartInitException{
        //System.out.println(FinancePlugin.getDefault());
        IViewPart view = getPage().showView("starshus.csdn.blog.finance.MyTableView");
        getPage().hideView(view);
    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息