工作台窗口的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 -->
01publicvoidpreWindowOpen(){ 02 ...... 03configurer.addEditorAreaTransfer(LocalSelectionTransfer.getInstance()); 04configurer.configureEditorAreaDropListener(newEditorAreaDropAdapter( 05configurer.getWindow())); 06} | <!-- end source code -->
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 -->
EditorAreaDropAdapter.java |
<!-- end headline -->
<!-- start source code -->
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} | <!-- end source code -->
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 -->
PassGateView.java |
<!-- end headline -->
<!-- start source code -->
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} | <!-- end source code -->
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 -->
LocalSelectionDragAdapter.java |
<!-- end headline -->
<!-- start source code -->
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 source code -->
<!-- = 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 -->
LocalSelectionTransfer.java |
<!-- end headline -->
<!-- start source code -->
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} | <!-- end source code -->
å
¶å®ä¸å
æ¯æå°Area Editorï¼ä»¥ä¸ä¾åç¨å æ¹é ï¼ä¹å¯ä»¥å®ç°æå°å«çæ§ä»¶ä¸ï¼ä»»ä½Controlé½å¯ä»¥æ·»å ææ½åè½ï¼ä¾å¦å¯ä»¥ä»ä¸ä¸ªè¡¨æ ¼æå°å¦ä¸ä¸ªè¡¨æ ¼ä¸ï¼ææå°ä¸ä¸ªé¢æ¿ä¸ãææ½åè½è®©è½¯ä»¶çå¯ç¨æ§åæé«äºä¸ä¸ªæ¡£æ¬¡ã