📄 dragsource.java
字号:
if (!event.doit || transferAgents == null || transferAgents.length == 0 ) return; int[] pdwEffect = new int[1]; int operations = opToOs(getStyle()); int result = COM.DoDragDrop(iDataObject.getAddress(), iDropSource.getAddress(), operations, pdwEffect); int operation = osToOp(pdwEffect[0]); if (dataEffect == DND.DROP_MOVE) { operation = (operation == DND.DROP_NONE || operation == DND.DROP_COPY) ? DND.DROP_TARGET_MOVE : DND.DROP_MOVE; } else { if (dataEffect != DND.DROP_NONE) { operation = dataEffect; } } event = new DNDEvent(); event.widget = this; event.time = OS.GetMessageTime(); event.doit = (result == COM.DRAGDROP_S_DROP); event.detail = operation; try { notifyListeners(DND.DragEnd,event); } catch (Throwable e) {} dataEffect = DND.DROP_NONE;}private int EnumFormatEtc(int dwDirection, int ppenumFormatetc) { // only allow getting of data - SetData is not currently supported if (dwDirection == COM.DATADIR_SET) return COM.E_NOTIMPL; // what types have been registered? TransferData[] allowedDataTypes = new TransferData[0]; for (int i = 0; i < transferAgents.length; i++){ TransferData[] formats = transferAgents[i].getSupportedTypes(); TransferData[] newAllowedDataTypes = new TransferData[allowedDataTypes.length + formats.length]; System.arraycopy(allowedDataTypes, 0, newAllowedDataTypes, 0, allowedDataTypes.length); System.arraycopy(formats, 0, newAllowedDataTypes, allowedDataTypes.length, formats.length); allowedDataTypes = newAllowedDataTypes; } OleEnumFORMATETC enumFORMATETC = new OleEnumFORMATETC(); enumFORMATETC.AddRef(); FORMATETC[] formats = new FORMATETC[allowedDataTypes.length]; for (int i = 0; i < formats.length; i++){ formats[i] = allowedDataTypes[i].formatetc; } enumFORMATETC.setFormats(formats); COM.MoveMemory(ppenumFormatetc, new int[] {enumFORMATETC.getAddress()}, 4); return COM.S_OK;}/** * Returns the Control which is registered for this DragSource. This is the control that the * user clicks in to initiate dragging. * * @return the Control which is registered for this DragSource */public Control getControl () { return control;}private int GetData(int pFormatetc, int pmedium) { /* Called by a data consumer to obtain data from a source data object. The GetData method renders the data described in the specified FORMATETC structure and transfers it through the specified STGMEDIUM structure. The caller then assumes responsibility for releasing the STGMEDIUM structure. */ if (pFormatetc == 0 || pmedium == 0) return COM.E_INVALIDARG; if (QueryGetData(pFormatetc) != COM.S_OK) return COM.DV_E_FORMATETC; TransferData transferData = new TransferData(); transferData.formatetc = new FORMATETC(); COM.MoveMemory(transferData.formatetc, pFormatetc, FORMATETC.sizeof); transferData.type = transferData.formatetc.cfFormat; transferData.stgmedium = new STGMEDIUM(); transferData.result = COM.E_FAIL; DNDEvent event = new DNDEvent(); event.widget = this; event.time = OS.GetMessageTime(); event.dataType = transferData; try { notifyListeners(DND.DragSetData,event); } catch (Throwable e) { return COM.E_FAIL; } if (event.data == null) return COM.DV_E_FORMATETC; // get matching transfer agent to perform conversion Transfer transfer = null; for (int i = 0; i < transferAgents.length; i++){ if (transferAgents[i].isSupportedType(transferData)){ transfer = transferAgents[i]; break; } } if (transfer == null) return COM.DV_E_FORMATETC; transfer.javaToNative(event.data, transferData); if (transferData.result != COM.S_OK) return transferData.result; COM.MoveMemory(pmedium, transferData.stgmedium, STGMEDIUM.sizeof); return transferData.result;}/** * Returns the list of data types that can be transferred by this DragSource. * * @return the list of data types that can be transferred by this DragSource */public Transfer[] getTransfer(){ return transferAgents;}private int GiveFeedback(int dwEffect) { return COM.DRAGDROP_S_USEDEFAULTCURSORS;}private int QueryContinueDrag(int fEscapePressed, int grfKeyState) { if (fEscapePressed != 0) return COM.DRAGDROP_S_CANCEL; if ((grfKeyState & (OS.MK_LBUTTON | OS.MK_MBUTTON | OS.MK_RBUTTON)) == 0) return COM.DRAGDROP_S_DROP; return COM.S_OK;}private void onDispose() { if (control == null) return; this.Release(); if (controlListener != null){ control.removeListener(SWT.Dispose, controlListener); control.removeListener(SWT.DragDetect, controlListener); } controlListener = null; control.setData(DRAGSOURCEID, null); control = null; transferAgents = null;}private int opToOs(int operation) { int osOperation = 0; if ((operation & DND.DROP_COPY) != 0){ osOperation |= COM.DROPEFFECT_COPY; } if ((operation & DND.DROP_LINK) != 0) { osOperation |= COM.DROPEFFECT_LINK; } if ((operation & DND.DROP_MOVE) != 0) { osOperation |= COM.DROPEFFECT_MOVE; } return osOperation;}private int osToOp(int osOperation){ int operation = 0; if ((osOperation & COM.DROPEFFECT_COPY) != 0){ operation |= DND.DROP_COPY; } if ((osOperation & COM.DROPEFFECT_LINK) != 0) { operation |= DND.DROP_LINK; } if ((osOperation & COM.DROPEFFECT_MOVE) != 0) { operation |= DND.DROP_MOVE; } return operation;}private int QueryGetData(int pFormatetc) { if (transferAgents == null) return COM.E_FAIL; TransferData transferData = new TransferData(); transferData.formatetc = new FORMATETC(); COM.MoveMemory(transferData.formatetc, pFormatetc, FORMATETC.sizeof); transferData.type = transferData.formatetc.cfFormat; // is this type supported by the transfer agent? for (int i = 0; i < transferAgents.length; i++){ if (transferAgents[i].isSupportedType(transferData)) return COM.S_OK; } return COM.DV_E_FORMATETC;}private int QueryInterface(int riid, int ppvObject) { if (riid == 0 || ppvObject == 0) return COM.E_INVALIDARG; GUID guid = new GUID(); COM.MoveMemory(guid, riid, GUID.sizeof); if (COM.IsEqualGUID(guid, COM.IIDIUnknown) || COM.IsEqualGUID(guid, COM.IIDIDropSource)) { COM.MoveMemory(ppvObject, new int[] {iDropSource.getAddress()}, 4); AddRef(); return COM.S_OK; } if (COM.IsEqualGUID(guid, COM.IIDIDataObject) ) { COM.MoveMemory(ppvObject, new int[] {iDataObject.getAddress()}, 4); AddRef(); return COM.S_OK; } COM.MoveMemory(ppvObject, new int[] {0}, 4); return COM.E_NOINTERFACE;}private int Release() { refCount--; if (refCount == 0) { disposeCOMInterfaces(); COM.CoFreeUnusedLibraries(); } return refCount;}/** * Removes the listener from the collection of listeners who will * be notified when a drag and drop operation is in progress. * * @param listener the listener which should be notified * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> * </ul> * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * * @see DragSourceListener * @see #addDragListener */public void removeDragListener(DragSourceListener listener) { if (listener == null) DND.error (SWT.ERROR_NULL_ARGUMENT); removeListener (DND.DragStart, listener); removeListener (DND.DragSetData, listener); removeListener (DND.DragEnd, listener);}private int SetData(int pFormatetc, int pmedium, int fRelease) { if (pFormatetc == 0 || pmedium == 0) return COM.E_INVALIDARG; FORMATETC formatetc = new FORMATETC(); COM.MoveMemory(formatetc, pFormatetc, FORMATETC.sizeof); if (formatetc.cfFormat == CFSTR_PERFORMEDDROPEFFECT && formatetc.tymed == COM.TYMED_HGLOBAL) { STGMEDIUM stgmedium = new STGMEDIUM(); COM.MoveMemory(stgmedium, pmedium,STGMEDIUM.sizeof); int[] ptrEffect = new int[1]; OS.MoveMemory(ptrEffect, stgmedium.unionField,4); int[] effect = new int[1]; OS.MoveMemory(effect, ptrEffect[0],4); dataEffect = osToOp(effect[0]); } if (fRelease == 1) { COM.ReleaseStgMedium(pmedium); } return COM.S_OK;}/** * Specifies the list of data types that can be transferred by this DragSource. * The application must be able to provide data to match each of these types when * a successful drop has occurred. * * @param transferAgents a list of Transfer objects which define the types of data that can be * dragged from this source */public void setTransfer(Transfer[] transferAgents){ this.transferAgents = transferAgents;}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -