001 public class LocalSelectionTransfer extends ByteArrayTransfer { 002 003 private static final String TYPE_NAME = "local-selection-transfer-format" + (new Long(System.currentTimeMillis())).toString(); //$NON-NLS-1$; 004 005 private static final int TYPEID = registerType(TYPE_NAME); 006 007 private static final LocalSelectionTransfer INSTANCE = new LocalSelectionTransfer(); 008 009 private ISelection selection; 010 011 private long selectionSetTime; 012 013 /** 014 * Only the singleton instance of this class may be used. 015 */ 016 private LocalSelectionTransfer() { 017 } 018 019 /** 020 * Returns the singleton. 021 * 022 * @return LocalSelectionTransfer 023 */ 024 public static LocalSelectionTransfer getInstance() { 025 return INSTANCE; 026 } 027 028 /** 029 * Returns the local transfer data. 030 * 031 * @return the local transfer data 032 */ 033 public ISelection getSelection() { 034 return selection; 035 } 036 037 /** 038 * Tests whether native drop data matches this transfer type. 039 * 040 * @param result 041 * result of converting the native drop data to Java 042 * @return true if the native drop data does not match this transfer type. 043 * false otherwise. 044 */ 045 private boolean isInvalidNativeType(Object result) { 046 return !(result instanceof byte[]) 047 || !TYPE_NAME.equals(new String((byte[]) result)); 048 } 049 050 /** 051 * Returns the type id used to identify this transfer. 052 * 053 * @return the type id used to identify this transfer. 054 */ 055 protected int[] getTypeIds() { 056 return new int[] { TYPEID }; 057 } 058 059 /** 060 * Returns the type name used to identify this transfer. 061 * 062 * @return the type name used to identify this transfer. 063 */ 064 protected String[] getTypeNames() { 065 return new String[] { TYPE_NAME }; 066 } 067 068 /** 069 * Overrides org.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(Object, 070 * TransferData). Only encode the transfer type name since the selection is 071 * read and written in the same process. 072 * 073 * @see org.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(java.lang.Object, 074 * org.eclipse.swt.dnd.TransferData) 075 */ 076 public void javaToNative(Object object, TransferData transferData) { 077 byte[] check = TYPE_NAME.getBytes(); 078 super.javaToNative(check, transferData); 079 } 080 081 /** 082 * Overrides 083 * org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(TransferData). Test if 084 * the native drop data matches this transfer type. 085 * 086 * @see org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(TransferData) 087 */ 088 public Object nativeToJava(TransferData transferData) { 089 Object result = super.nativeToJava(transferData); 090 if (isInvalidNativeType(result)) { 091 PwdgatePlugin.log("invalid nattive type", new Exception( 092 "invalid nattive type")); 093 } 094 return selection; 095 } 096 097 /** 098 * Sets the transfer data for local use. 099 * 100 * @param s 101 * the transfer data 102 */ 103 public void setSelection(ISelection s) { 104 selection = s; 105 } 106 107 /** 108 * Returns the time when the selection operation this transfer is associated 109 * with was started. 110 * 111 * @return the time when the selection operation has started 112 * 113 * @see org.eclipse.swt.events.TypedEvent#time 114 */ 115 public long getSelectionSetTime() { 116 return selectionSetTime; 117 } 118 119 /** 120 * Sets the time when the selection operation this transfer is associated 121 * with was started. If assigning this from an SWT event, be sure to use 122 * <code>setSelectionTime(event.time & 0xFFFF)</code> 123 * 124 * @param time 125 * the time when the selection operation was started 126 * 127 * @see org.eclipse.swt.events.TypedEvent#time 128 */ 129 public void setSelectionSetTime(long time) { 130 selectionSetTime = time; 131 } 132 }