001: package net.refractions.udig.internal.ui;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.IOException;
005: import java.io.InputStreamReader;
006: import java.util.ArrayList;
007: import java.util.Iterator;
008: import java.util.List;
009:
010: import org.eclipse.jface.viewers.IStructuredSelection;
011: import org.eclipse.swt.dnd.ByteArrayTransfer;
012: import org.eclipse.swt.dnd.Transfer;
013: import org.eclipse.swt.dnd.TransferData;
014:
015: public class UDigByteAndLocalTransfer extends ByteArrayTransfer
016: implements UDIGTransfer {
017: private static UDigByteAndLocalTransfer _instance = new UDigByteAndLocalTransfer();
018:
019: static final String CFSTR_INETURL = "UniformResourceLocator"; //$NON-NLS-1$
020:
021: private static final int CFSTR_INETURLID = Transfer
022: .registerType(CFSTR_INETURL);
023:
024: private long startTime;
025:
026: public Object object;
027:
028: @Override
029: public boolean isSupportedType(TransferData transferData) {
030: return super .isSupportedType(transferData);
031: }
032:
033: public UDigByteAndLocalTransfer() {
034:
035: // do nothing.
036: }
037:
038: public static UDigByteAndLocalTransfer getInstance() {
039:
040: return _instance;
041: }
042:
043: protected int[] getTypeIds() {
044:
045: return new int[] { CFSTR_INETURLID };
046: }
047:
048: public String[] getTypeNames() {
049:
050: return new String[] { CFSTR_INETURL };
051: }
052:
053: @Override
054: public TransferData[] getSupportedTypes() {
055: return super .getSupportedTypes();
056: }
057:
058: @SuppressWarnings("unchecked")
059: @Override
060: public void javaToNative(Object object, TransferData transferData) {
061:
062: startTime = System.currentTimeMillis();
063: if (object instanceof IStructuredSelection) {
064: IStructuredSelection selection = (IStructuredSelection) object;
065: List<Object> elements = new ArrayList<Object>();
066: for (Iterator<Object> iter = selection.iterator(); iter
067: .hasNext();) {
068: elements.add(iter.next());
069: }
070: this .object = elements.toArray();
071: }
072: this .object = object;
073: if (transferData != null) {
074: super .javaToNative(String.valueOf(startTime).getBytes(),
075: transferData);
076: }
077: }
078:
079: /**
080: * This implementation of <code>nativeToJava</code> converts a platform
081: * specific representation of a URL and optionally, a title to a java
082: * <code>String[]</code>. For additional information see
083: * <code>Transfer#nativeToJava</code>.
084: *
085: * @param transferData
086: * the platform specific representation of the data to be been
087: * converted
088: * @return a java <code>String[]</code> containing a URL and optionally a
089: * title if the conversion was successful; otherwise null
090: */
091: public Object nativeToJava(TransferData transferData) {
092:
093: byte[] bytes = (byte[]) super .nativeToJava(transferData);
094: if (bytes == null)
095: return null;
096:
097: try {
098: long startTime = Long.valueOf(new String(bytes))
099: .longValue();
100: return this .startTime == startTime ? object : null;
101: } catch (NumberFormatException exception) {
102: InputStreamReader reader = new InputStreamReader(
103: new ByteArrayInputStream(bytes));
104: StringBuffer buf = new StringBuffer();
105: char[] chars = new char[bytes.length / 2];
106: int read = 0;
107: try {
108: read = reader.read(chars);
109: } catch (IOException e) {
110: UiPlugin.log("Error reading transfer data", e); //$NON-NLS-1$
111: }
112: buf.append(chars, 0, read);
113: return buf.toString().trim();
114: }
115:
116: }
117:
118: public boolean validate(Object object) {
119: return true;
120: }
121: }
|