在RCP中实现用托拽打开Editor
2008-01-05 18:13:02 来源:WEB开发网核心提示: Java align=left> 工作台窗口的Editor区域默认是显示的,而且它支持拖拽操作,在RCP中实现用托拽打开Editor,在Eclipse里面,把一个文件拖到Editor区域,用来监听Editor Area中的Drop事件,它会打开拖入Editor Area的对象所对应的Editor,就会自动
Java align=left> 工作台窗口的Editor区域默认是显示的,而且它支持拖拽操作。在Eclipse里面,把一个文件拖到Editor区域,就会自动打开该文件的Editor.该特性是在IWorkbenchWindowConfigurer 中设置。
在PassWord Gate中,当拖动Password Gate View中的一个Group 或者 Service到Editor区域,会在Editor显示该项的属性。
要实现此特性,有四部分是必须的。
1 实现必要的Transfer类型,而且定义Editor要支持哪些类型。在Password Gate中,Transfer是LocalSelectionTransfer.Transfer用来进行数据的序列化,它可以支持在同一个程序,或不同程序间拖拽。
2 因为在拖拽传递的过程中Editor Input 要实现序列化,所以要实现IPersistableElement接口。
3 加入一个释放适配器,当一个元素被扔到Editor区域,它可以知道如何进行操作,其实就是打开该元素的Editor.
4 使Password View能够进行拖操作,它要提供供拖的元素。
下面进行代码实现。
在RCP的WorkbenchWindowAdvisor. PReWindowOpen中定义要求Editor Area支持的拖入对象的类型,以及打开相应Editor的事件。
applicationWorkbenchWindowAdvisor.preWindowOpen()
01 public void preWindowOpen() {
02 ......
03 configurer.addEditorAreaTransfer(LocalSelectionTransfer.getInstance());
04 configurer.configureEditorAreaDropListener(new EditorAreaDropAdapter(
05 configurer.getWindow()));
06 } Row 03定义了Editor Area支持的Transfer为LocalSelectionTransfer, LocalSelectionTransfer为自己实现的传输介质类。Row 04定义了对拖入Editor Area的对象的处理方法。EditorAreaDropAdapter实现了DropTargetListener接口,用来监听Editor Area中的Drop事件,它会打开拖入Editor Area的对象所对应的Editor,假如同时拖入多个对象,它会打开每个对象对应的Editor.
EditorAreaDropAdapter.java
01 public class EditorAreaDropAdapter extends DropTargetAdapter {
02 private IWorkbenchWindow window;
03
04 public EditorAreaDropAdapter(IWorkbenchWindow window) {
05 this.window = window;
06 }
07
08 public void dragEnter(DropTargetEvent event) {
09 // always indicate a copy
10 event.detail = DND.DROP_COPY;
11 }
12
13 public void dragOperationChanged(DropTargetEvent event) {
14 // always indicate a copy
15 event.detail = DND.DROP_COPY;
16 }
17
18 public void drop(final DropTargetEvent event) {
19 Display d = window.getShell().getDisplay();
20 final IWorkbenchPage page = window.getActivePage();
21 if (page != null) {
22 d.asyncExec(new Runnable() {
23 public void run() {
24 asyncDrop(event, page);
25 }
26 });
27 }
28 }
29
30 private void asyncDrop(DropTargetEvent event, IWorkbenchPage page) {
31 if (LocalSelectionTransfer.getInstance().isSupportedType(
32 event.currentDataType)) {
33 StrUCturedSelection selection = (StructuredSelection) event.data;
34 for (Iterator iter = selection.iterator(); iter.hasNext();) {
35 Object o = iter.next();
36 if (o instanceof Record) {
37 IEditorInput input = new RecordEditorInput((Record) o);
38 try {
39 page.openEditor(input, RecordEditor.ID);
40 } catch (Exception e) {
41 PwdgatePlugin.log("open ediotr RecordEditor", e);
42 }
43 } else if (o instanceof Group) {
44 IEditorInput input = new GroupEditorInput((Group) o);
45 try {
46 page.openEditor(input, GroupEditor.ID);
47 } catch (PartInitException e) {
48 PwdgatePlugin.log("open ediotr GroupEditor", e);
49 }
50 }
51 }
52 }
53 }
54 }
02 ......
03 configurer.addEditorAreaTransfer(LocalSelectionTransfer.getInstance());
04 configurer.configureEditorAreaDropListener(new EditorAreaDropAdapter(
05 configurer.getWindow()));
06 } Row 03定义了Editor Area支持的Transfer为LocalSelectionTransfer, LocalSelectionTransfer为自己实现的传输介质类。Row 04定义了对拖入Editor Area的对象的处理方法。EditorAreaDropAdapter实现了DropTargetListener接口,用来监听Editor Area中的Drop事件,它会打开拖入Editor Area的对象所对应的Editor,假如同时拖入多个对象,它会打开每个对象对应的Editor.
02 private IWorkbenchWindow window;
03
04 public EditorAreaDropAdapter(IWorkbenchWindow window) {
05 this.window = window;
06 }
07
08 public void dragEnter(DropTargetEvent event) {
09 // always indicate a copy
10 event.detail = DND.DROP_COPY;
11 }
12
13 public void dragOperationChanged(DropTargetEvent event) {
14 // always indicate a copy
15 event.detail = DND.DROP_COPY;
16 }
17
18 public void drop(final DropTargetEvent event) {
19 Display d = window.getShell().getDisplay();
20 final IWorkbenchPage page = window.getActivePage();
21 if (page != null) {
22 d.asyncExec(new Runnable() {
23 public void run() {
24 asyncDrop(event, page);
25 }
26 });
27 }
28 }
29
30 private void asyncDrop(DropTargetEvent event, IWorkbenchPage page) {
31 if (LocalSelectionTransfer.getInstance().isSupportedType(
32 event.currentDataType)) {
33 StrUCturedSelection selection = (StructuredSelection) event.data;
34 for (Iterator iter = selection.iterator(); iter.hasNext();) {
35 Object o = iter.next();
36 if (o instanceof Record) {
37 IEditorInput input = new RecordEditorInput((Record) o);
38 try {
39 page.openEditor(input, RecordEditor.ID);
40 } catch (Exception e) {
41 PwdgatePlugin.log("open ediotr RecordEditor", e);
42 }
43 } else if (o instanceof Group) {
44 IEditorInput input = new GroupEditorInput((Group) o);
45 try {
46 page.openEditor(input, GroupEditor.ID);
47 } catch (PartInitException e) {
48 PwdgatePlugin.log("open ediotr GroupEditor", e);
49 }
50 }
51 }
52 }
53 }
54 }
更多精彩
赞助商链接