]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/dnd/URLTransfer.java
1581e80a18de3fd44db236e38f7018c0c144eeec
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / dnd / URLTransfer.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 20007 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.dnd;
15
16 import org.eclipse.swt.internal.ole.win32.*;
17 import org.eclipse.swt.internal.win32.*;
18
19 /**
20  * The class <code>URLTransfer</code> provides a platform specific mechanism
21  * for converting text in URL format represented as a java <code>String</code>
22  * to a platform specific representation of the data and vice versa. The string
23  * must contain a fully specified url.
24  *
25  * <p>An example of a java <code>String</code> containing a URL is shown below:</p>
26  *
27  * <pre><code>
28  *     String url = "http://www.eclipse.org";
29  * </code></pre>
30  *
31  * @see Transfer
32  * @since 3.4
33  */
34 public class URLTransfer extends ByteArrayTransfer {
35
36         static URLTransfer _instance = new URLTransfer();
37         static final String CFSTR_INETURLW = "UniformResourceLocatorW"; //$NON-NLS-1$
38         static final int CFSTR_INETURLIDW = registerType(CFSTR_INETURLW);
39         static final String CFSTR_INETURL = "UniformResourceLocator"; //$NON-NLS-1$
40         static final int CFSTR_INETURLID = registerType(CFSTR_INETURL);
41
42 private URLTransfer() {}
43
44 /**
45  * Returns the singleton instance of the URLTransfer class.
46  *
47  * @return the singleton instance of the URLTransfer class
48  */
49 public static URLTransfer getInstance () {
50         return _instance;
51 }
52
53 /**
54  * This implementation of <code>javaToNative</code> converts a URL
55  * represented by a java <code>String</code> to a platform specific representation.
56  *
57  * @param object a java <code>String</code> containing a URL
58  * @param transferData an empty <code>TransferData</code> object that will
59  *      be filled in on return with the platform specific format of the data
60  *
61  * @see Transfer#nativeToJava
62  */
63 @Override
64 public void javaToNative (Object object, TransferData transferData){
65         if (!checkURL(object) || !isSupportedType(transferData)) {
66                 DND.error(DND.ERROR_INVALID_DATA);
67         }
68         transferData.result = COM.E_FAIL;
69         // URL is stored as a null terminated byte array
70         String url = ((String)object);
71         if (transferData.type == CFSTR_INETURLIDW) {
72                 int charCount = url.length ();
73                 char[] chars = new char[charCount+1];
74                 url.getChars (0, charCount, chars, 0);
75                 int byteCount = chars.length * 2;
76                 long newPtr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, byteCount);
77                 OS.MoveMemory(newPtr, chars, byteCount);
78                 transferData.stgmedium = new STGMEDIUM();
79                 transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
80                 transferData.stgmedium.unionField = newPtr;
81                 transferData.stgmedium.pUnkForRelease = 0;
82                 transferData.result = COM.S_OK;
83         } else if (transferData.type == CFSTR_INETURLID) {
84                 int count = url.length();
85                 char[] chars = new char[count + 1];
86                 url.getChars(0, count, chars, 0);
87                 int codePage = OS.GetACP();
88                 int cchMultiByte = OS.WideCharToMultiByte(codePage, 0, chars, -1, null, 0, null, null);
89                 if (cchMultiByte == 0) {
90                         transferData.stgmedium = new STGMEDIUM();
91                         transferData.result = COM.DV_E_STGMEDIUM;
92                         return;
93                 }
94                 long lpMultiByteStr = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, cchMultiByte);
95                 OS.WideCharToMultiByte(codePage, 0, chars, -1, lpMultiByteStr, cchMultiByte, null, null);
96                 transferData.stgmedium = new STGMEDIUM();
97                 transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
98                 transferData.stgmedium.unionField = lpMultiByteStr;
99                 transferData.stgmedium.pUnkForRelease = 0;
100                 transferData.result = COM.S_OK;
101         }
102 }
103
104 /**
105  * This implementation of <code>nativeToJava</code> converts a platform
106  * specific representation of a URL to a java <code>String</code>.
107  *
108  * @param transferData the platform specific representation of the data to be converted
109  * @return a java <code>String</code> containing a URL if the conversion was successful;
110  *              otherwise null
111  *
112  * @see Transfer#javaToNative
113  */
114 @Override
115 public Object nativeToJava(TransferData transferData){
116         if (!isSupportedType(transferData) || transferData.pIDataObject == 0) return null;
117         IDataObject data = new IDataObject(transferData.pIDataObject);
118         data.AddRef();
119         STGMEDIUM stgmedium = new STGMEDIUM();
120         FORMATETC formatetc = transferData.formatetc;
121         stgmedium.tymed = COM.TYMED_HGLOBAL;
122         transferData.result = getData(data, formatetc, stgmedium);
123         data.Release();
124         if (transferData.result != COM.S_OK) return null;
125         long hMem = stgmedium.unionField;
126         try {
127                 if (transferData.type == CFSTR_INETURLIDW) {
128                         /* Ensure byteCount is a multiple of 2 bytes */
129                         int size = OS.GlobalSize(hMem) / 2 * 2;
130                         if (size == 0) return null;
131                         char[] chars = new char[size/2];
132                         long ptr = OS.GlobalLock(hMem);
133                         if (ptr == 0) return null;
134                         try {
135                                 OS.MoveMemory(chars, ptr, size);
136                                 int length = chars.length;
137                                 for (int i=0; i<chars.length; i++) {
138                                         if (chars [i] == '\0') {
139                                                 length = i;
140                                                 break;
141                                         }
142                                 }
143                                 return new String (chars, 0, length);
144                         } finally {
145                                 OS.GlobalUnlock(hMem);
146                         }
147                 } else if (transferData.type == CFSTR_INETURLID) {
148                         long lpMultiByteStr = OS.GlobalLock(hMem);
149                         if (lpMultiByteStr == 0) return null;
150                         try {
151                                 int codePage = OS.GetACP();
152                                 int cchWideChar  = OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, null, 0);
153                                 if (cchWideChar == 0) return null;
154                                 char[] lpWideCharStr = new char [cchWideChar - 1];
155                                 OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, lpWideCharStr, lpWideCharStr.length);
156                                 return new String(lpWideCharStr);
157                         } finally {
158                                 OS.GlobalUnlock(hMem);
159                         }
160                 }
161         } finally {
162                 OS.GlobalFree(hMem);
163         }
164         return null;
165 }
166
167 @Override
168 protected int[] getTypeIds(){
169         return new int[] {CFSTR_INETURLIDW, CFSTR_INETURLID};
170 }
171
172 @Override
173 protected String[] getTypeNames(){
174         return new String[] {CFSTR_INETURLW, CFSTR_INETURL};
175 }
176
177 boolean checkURL(Object object) {
178         return object != null && (object instanceof String) && ((String)object).length() > 0;
179 }
180
181 @Override
182 protected boolean validate(Object object) {
183         return checkURL(object);
184 }
185 }