Open File Dialog in the NetBeans Platform

http://blogs.oracle.com/geertjan/entry/open_file_dialog_in_the
 ——————————————————————————————————————————————————————————————————————

When you use the "NetBeans Platform Application" template in NetBeans IDE and you make no changes of any kind, simply run it, you'll find that your "File" menu has this content, by default:

 

Let's now imagine we want to add an "Open File" menu item to the "File" menu that you see above. Of course, you could create that menu item and the related GUI and functionality yourself.

Or, you could reuse the functionality from NetBeans IDE, which comes from a module named "User Utilities" (org.netbeans.modules.utilities)  and is found in the "ide" cluster. So, to use this module, right-click the application in the Projects window, choose Properties, click Libraries, expand the "ide" node, and put a check in the checkbox next to "User Utilities". The red "Resolve" button now becomes enabled. Click it and the Search API also becomes selected.

Now run your application again and, without having typed a single character of code, you will see the following:

 

 

 The "Open File" dialog looks as follows:

 

 

Above, notice that two filters are predefined, for text files and for Java files; however, you're not limited to accessing these files, simply use the "All Files" filter and you'll be able to access any file. Depending on which modules you have installed in your application, different things can happen to the files that you open. For example, if you have the "Image" module installed, the image opens into the Image Editor when you use the Open File dialog to open an image file.

Now, let's add our own filter to the Open File dialog! In one of your custom modules, set a dependency on the "User Utilities" module (it does not expose an API, so you need to set a dependency on the implementation and declare the User Utilities module as your friend), as well as the "Lookup API" module.

Then you can create a new Open File dialog filter class, like this:

 

package org.my.filter;

import org.netbeans.modules.openfile.OpenFileDialogFilter;
import org.openide.util.lookup.ServiceProvider;

@ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter. class)
public  class MyOpenFileDialogFilter  extends OpenFileDialogFilter {
    @Override
     public String getDescriptionString() {
         return "My Files";
    }
    @Override
     public String[] getSuffixes() {
         return  new String[]{".my"};
    }
}

 

When you run the application, your Open File dialog will now be as follows:

 

 

 

However, what if you only want your own filters to be displayed and not those provided by default by the "User Utilities" module? Here, thanks to this issue, is the solution, note the bit in bold:

 

@ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter. class,
supersedes = {
    "org.netbeans.modules.openfile.FileChooser$TxtFileFilter",
    "org.netbeans.modules.openfile.FileChooser$JavaFilesFilter"
})
public  class MyOpenFileDialogFilter  extends OpenFileDialogFilter {

    @Override
     public String getDescriptionString() {
         return "My Files";
    }

    @Override
     public String[] getSuffixes() {
         return  new String[]{".my"};
    }
}

 The result:

 

 Another consideration is that maybe you don't want all of the "User Utilities" module. I.e., there's quite some packages in there that do not relate to the Open File dialog. In that case, copy the "org.netbeans.modules.openfile"  package into your own module (optionally, delete the Handler class, which depends on the Command Line Parsing API, which might not be a dependency you'd like to take with you in your application) and then you have forked the Open File dialog functionality from NetBeans IDE into your own application. That will also save you the inconveniences associated with needing to set an implementation dependency on "User Utilities", which is also a win.

Many thanks to Sam Harwell from San Francisco for help and questions on this scenario! Hope the above helps, Sam.

 

 

 

 

 

 

转载于:https://www.cnblogs.com/cuizhf/archive/2011/09/24/2189549.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值