在RCP中实现用托拽打开Editor

工作台窗口的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()

<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard [email protected] = --><!-- = Further information: http://www.java2html.de = -->
<!-- start source code --> <!-- end source code -->
01publicvoidpreWindowOpen(){
02 ......
03configurer.addEditorAreaTransfer(LocalSelectionTransfer.getInstance());
04configurer.configureEditorAreaDropListener(newEditorAreaDropAdapter(
05configurer.getWindow()));
06}

Row 03定义了Editor Area支持的Transfer为LocalSelectionTransfer, LocalSelectionTransfer为自己实现的传输介质类。Row 04定义了对拖入Editor Area的对象的处理方法。EditorAreaDropAdapter实现了DropTargetListener接口,用来监听Editor Area中的Drop事件,它会打开拖入Editor Area的对象所对应的Editor,如果同时拖入多个对象,它会打开每个对象对应的Editor.

<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard [email protected] = --><!-- = Further information: http://www.java2html.de = -->
<!-- start headline --> <!-- end headline --> <!-- start source code --> <!-- end source code -->
EditorAreaDropAdapter.java
01publicclassEditorAreaDropAdapterextendsDropTargetAdapter{
02privateIWorkbenchWindowwindow;
03
04publicEditorAreaDropAdapter(IWorkbenchWindowwindow){
05this.window=window;
06}
07
08publicvoiddragEnter(DropTargetEventevent){
09//alwaysindicateacopy
10event.detail=DND.DROP_COPY;
11}
12
13publicvoiddragOperationChanged(DropTargetEventevent){
14//alwaysindicateacopy
15event.detail=DND.DROP_COPY;
16}
17
18publicvoiddrop(finalDropTargetEventevent){
19Displayd=window.getShell().getDisplay();
20finalIWorkbenchPagepage=window.getActivePage();
21if(page!=null){
22d.asyncExec(newRunnable(){
23publicvoidrun(){
24asyncDrop(event,page);
25}
26});
27}
28}
29
30privatevoidasyncDrop(DropTargetEventevent,IWorkbenchPagepage){
31if(LocalSelectionTransfer.getInstance().isSupportedType(
32event.currentDataType)){
33StructuredSelectionselection=(StructuredSelection)event.data;
34for(Iteratoriter=selection.iterator();iter.hasNext();){
35Objecto=iter.next();
36if(oinstanceofRecord){
37IEditorInputinput=newRecordEditorInput((Record)o);
38try{
39page.openEditor(input,RecordEditor.ID);
40}catch(Exceptione){
41PwdgatePlugin.log("openediotrRecordEditor",e);
42}
43}elseif(oinstanceofGroup){
44IEditorInputinput=newGroupEditorInput((Group)o);
45try{
46page.openEditor(input,GroupEditor.ID);
47}catch(PartInitExceptione){
48PwdgatePlugin.log("openediotrGroupEditor",e);
49}
50}
51}
52}
53}
54}

Row 18,用来处理Drop事件,Row 30的asyncDrop()方法用来打来相应对象的Editor.

现在Editor Area已经可以接收拖入对象了。下一步要使一个View支持拖出对象的功能。 下面的例子是在 Pass Gate View 中的 createPartControl() 方法中为一个 TreeViewer 添加拖出功能。
<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard [email protected] = --><!-- = Further information: http://www.java2html.de = -->
<!-- start headline --> <!-- end headline --> <!-- start source code --> <!-- end source code -->
PassGateView.java
01privatevoidinitDragAndDrop(){
02Transfer[]transfer=newTransfer[]{LocalSelectionTransfer
03.getInstance()};
04LocalSelectionDragAdapteradapter=newLocalSelectionDragAdapter(
05viewer);
06viewer.addDragSupport(DND.DROP_MOVE|DND.DROP_COPY,transfer,adapter);
07
08LocalSelectionDropAdapterdropAdapter=newLocalSelectionDropAdapter(
09viewer);
10viewer.addDropSupport(DND.DROP_MOVE|DND.DROP_COPY,transfer,
11dropAdapter);
12}
Row 06 为 TreeViewer 添加了监听拖动的事件,当在 TreeViewer 中有拖动时, LocalSelectionDragAdapter 把拖动的对象放入 LocalSelectionTransfer 中,通过其传到 Editor Area 中。
<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard [email protected] = --><!-- = Further information: http://www.java2html.de = -->
<!-- start headline --> <!-- end headline --> <!-- start source code --> <!-- end source code -->
LocalSelectionDragAdapter.java
01publicclassLocalSelectionDragAdapterextendsDragSourceAdapter{
02
03ISelectionProviderselectionProvider;
04
05publicLocalSelectionDragAdapter(ISelectionProviderprovider){
06selectionProvider=provider;
07}
08
09publicvoiddragFinished(DragSourceEventevent){
10//TODOAuto-generatedmethodstub
11super.dragFinished(event);
12System.out
13.println("DragSourceListener.dragFinished(DragSourceEventevent)");
14}
15
16publicvoiddragSetData(DragSourceEventevent){
17System.out
18.println("DragSourceListener.dragSetData(DragSourceEventevent)");
19DragSourcedragSource=(DragSource)event.widget;
20Controlcontrol=dragSource.getControl();
21if(control!=control.getDisplay().getFocusControl()){
22event.doit=false;
23return;
24}
25
26IStructuredSelectionselection=(IStructuredSelection)selectionProvider
27.getSelection();
28
29if(selection.isEmpty()){
30event.doit=false;
31return;
32}
33LocalSelectionTransfer.getInstance().setSelection(selection);
34event.doit=true;
35}
36
37publicvoiddragStart(DragSourceEventevent){
38System.out
39.println("DragSourceListener.dragStart(DragSourceEventevent)");
40}
41}

<!-- = END of automatically generated HTML code = --><!-- ======================================================== --> Row 33 ,将拖动的对象放入LocalSelectionTransfer中。Row 44 设置拖动有效,设为false的话,拖动无效。下面是LocalSelectionTransfer的实现。很容易理解。

<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt--><style type="text/css"> <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }--> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard [email protected] = --><!-- = Further information: http://www.java2html.de = -->
<!-- start headline --> <!-- end headline --> <!-- start source code --> <!-- end source code -->
LocalSelectionTransfer.java
001publicclassLocalSelectionTransferextendsByteArrayTransfer{
002
003privatestaticfinalStringTYPE_NAME="local-selection-transfer-format"+(newLong(System.currentTimeMillis())).toString();//$NON-NLS-1$;
004
005privatestaticfinalintTYPEID=registerType(TYPE_NAME);
006
007privatestaticfinalLocalSelectionTransferINSTANCE=newLocalSelectionTransfer();
008
009privateISelectionselection;
010
011privatelongselectionSetTime;
012
013/**
014*Onlythesingletoninstanceofthisclassmaybeused.
015*/
016privateLocalSelectionTransfer(){
017}
018
019/**
020*Returnsthesingleton.
021*
022*@returnLocalSelectionTransfer
023*/
024publicstaticLocalSelectionTransfergetInstance(){
025returnINSTANCE;
026}
027
028/**
029*Returnsthelocaltransferdata.
030*
031*@returnthelocaltransferdata
032*/
033publicISelectiongetSelection(){
034returnselection;
035}
036
037/**
038*Testswhethernativedropdatamatchesthistransfertype.
039*
040*@paramresult
041*resultofconvertingthenativedropdatatoJava
042*@returntrueifthenativedropdatadoesnotmatchthistransfertype.
043*falseotherwise.
044*/
045privatebooleanisInvalidNativeType(Objectresult){
046return!(resultinstanceofbyte[])
047||!TYPE_NAME.equals(newString((byte[])result));
048}
049
050/**
051*Returnsthetypeidusedtoidentifythistransfer.
052*
053*@returnthetypeidusedtoidentifythistransfer.
054*/
055protectedint[]getTypeIds(){
056returnnewint[]{TYPEID};
057}
058
059/**
060*Returnsthetypenameusedtoidentifythistransfer.
061*
062*@returnthetypenameusedtoidentifythistransfer.
063*/
064protectedString[]getTypeNames(){
065returnnewString[]{TYPE_NAME};
066}
067
068/**
069*Overridesorg.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(Object,
070*TransferData).Onlyencodethetransfertypenamesincetheselectionis
071*readandwritteninthesameprocess.
072*
073*@seeorg.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(java.lang.Object,
074*org.eclipse.swt.dnd.TransferData)
075*/
076publicvoidjavaToNative(Objectobject,TransferDatatransferData){
077byte[]check=TYPE_NAME.getBytes();
078super.javaToNative(check,transferData);
079}
080
081/**
082*Overrides
083*org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(TransferData).Testif
084*thenativedropdatamatchesthistransfertype.
085*
086*@seeorg.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(TransferData)
087*/
088publicObjectnativeToJava(TransferDatatransferData){
089Objectresult=super.nativeToJava(transferData);
090if(isInvalidNativeType(result)){
091PwdgatePlugin.log("invalidnattivetype",newException(
092"invalidnattivetype"));
093}
094returnselection;
095}
096
097/**
098*Setsthetransferdataforlocaluse.
099*
100*@params
101*thetransferdata
102*/
103publicvoidsetSelection(ISelections){
104selection=s;
105}
106
107/**
108*Returnsthetimewhentheselectionoperationthistransferisassociated
109*withwasstarted.
110*
111*@returnthetimewhentheselectionoperationhasstarted
112*
113*@seeorg.eclipse.swt.events.TypedEvent#time
114*/
115publiclonggetSelectionSetTime(){
116returnselectionSetTime;
117}
118
119/**
120*Setsthetimewhentheselectionoperationthistransferisassociated
121*withwasstarted.IfassigningthisfromanSWTevent,besuretouse
122*<code>setSelectionTime(event.time&0xFFFF)</code>
123*
124*@paramtime
125*thetimewhentheselectionoperationwasstarted
126*
127*@seeorg.eclipse.swt.events.TypedEvent#time
128*/
129publicvoidsetSelectionSetTime(longtime){
130selectionSetTime=time;
131}
132}
其实不光是拖到Area Editor,以上例子稍加改造,也可以实现拖到别的控件中,任何Control都可以添加托拽功能,例如可以从一个表格拖到另一个表格中,或拖到一个面板中。托拽功能让软件的可用性又提高了一个档次。

你可能感兴趣的:(eclipse,html,css,XHTML)