KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > dnd > URLTransfer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.dnd;
12
13 import java.net.*;
14
15 import org.eclipse.swt.internal.ole.win32.*;
16 import org.eclipse.swt.internal.win32.*;
17
18 /**
19  * The class <code>URLTransfer</code> provides a platform specific mechanism
20  * for converting text in URL format represented as a java <code>String[]</code>
21  * to a platform specific representation of the data and vice versa. See
22  * <code>Transfer</code> for additional information. The first string in the
23  * array is mandatory and must contain the fully specified url. The second
24  * string in the array is optional and if present contains the title for the
25  * page.
26  *
27  * <p>An example of a java <code>String[]</code> containing a URL is shown
28  * below:</p>
29  *
30  * <code><pre>
31  * String[] urlData = new String[] {"http://www.eclipse.org", "Eclipse.org Main Page"};
32  * </code></pre>
33  */

34 /*public*/ class URLTransfer extends ByteArrayTransfer {
35
36     static URLTransfer _instance = new URLTransfer();
37     static final String JavaDoc CFSTR_INETURL = "UniformResourceLocator"; //$NON-NLS-1$
38
static final int CFSTR_INETURLID = registerType(CFSTR_INETURL);
39
40 private URLTransfer() {}
41
42 /**
43  * Returns the singleton instance of the URLTransfer class.
44  *
45  * @return the singleton instance of the URLTransfer class
46  */

47 public static URLTransfer getInstance () {
48     return _instance;
49 }
50
51 /**
52  * This implementation of <code>javaToNative</code> converts a URL and optionally a title
53  * represented by a java <code>String[]</code> to a platform specific representation.
54  * For additional information see <code>Transfer#javaToNative</code>.
55  *
56  * @param object a java <code>String[]</code> containing a URL and optionally, a title
57  * @param transferData an empty <code>TransferData</code> object; this
58  * object will be filled in on return with the platform specific format of the data
59  */

60 public void javaToNative (Object JavaDoc object, TransferData transferData){
61     if (!checkURL(object) || !isSupportedType(transferData)) {
62         DND.error(DND.ERROR_INVALID_DATA);
63     }
64     transferData.result = COM.E_FAIL;
65     // URL is stored as a null terminated byte array
66
String JavaDoc url = ((String JavaDoc[])object)[0];
67     int count = url.length();
68     char[] chars = new char[count + 1];
69     url.getChars(0, count, chars, 0);
70     int codePage = OS.GetACP();
71     int cchMultiByte = OS.WideCharToMultiByte(codePage, 0, chars, -1, null, 0, null, null);
72     if (cchMultiByte == 0) {
73         transferData.stgmedium = new STGMEDIUM();
74         transferData.result = COM.DV_E_STGMEDIUM;
75         return;
76     }
77     int lpMultiByteStr = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, cchMultiByte);
78     OS.WideCharToMultiByte(codePage, 0, chars, -1, lpMultiByteStr, cchMultiByte, null, null);
79     transferData.stgmedium = new STGMEDIUM();
80     transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
81     transferData.stgmedium.unionField = lpMultiByteStr;
82     transferData.stgmedium.pUnkForRelease = 0;
83     transferData.result = COM.S_OK;
84     return;
85 }
86
87 /**
88  * This implementation of <code>nativeToJava</code> converts a platform specific
89  * representation of a URL and optionally, a title to a java <code>String[]</code>.
90  * For additional information see <code>Transfer#nativeToJava</code>.
91  *
92  * @param transferData the platform specific representation of the data to be
93  * been converted
94  * @return a java <code>String[]</code> containing a URL and optionally a title if the
95  * conversion was successful; otherwise null
96  */

97 public Object JavaDoc nativeToJava(TransferData transferData){
98     if (!isSupportedType(transferData) || transferData.pIDataObject == 0) return null;
99     IDataObject data = new IDataObject(transferData.pIDataObject);
100     data.AddRef();
101     STGMEDIUM stgmedium = new STGMEDIUM();
102     FORMATETC formatetc = transferData.formatetc;
103     stgmedium.tymed = COM.TYMED_HGLOBAL;
104     transferData.result = data.GetData(formatetc, stgmedium);
105     data.Release();
106     if (transferData.result != COM.S_OK) return null;
107     int hMem = stgmedium.unionField;
108     try {
109         int lpMultiByteStr = OS.GlobalLock(hMem);
110         if (lpMultiByteStr == 0) return null;
111         try {
112             int codePage = OS.GetACP();
113             int cchWideChar = OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, null, 0);
114             if (cchWideChar == 0) return null;
115             char[] lpWideCharStr = new char [cchWideChar - 1];
116             OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, lpWideCharStr, lpWideCharStr.length);
117             return new String JavaDoc[]{new String JavaDoc(lpWideCharStr)};
118         } finally {
119             OS.GlobalUnlock(hMem);
120         }
121     } finally {
122         OS.GlobalFree(hMem);
123     }
124 }
125
126 protected int[] getTypeIds(){
127     return new int[] {CFSTR_INETURLID};
128 }
129
130 protected String JavaDoc[] getTypeNames(){
131     return new String JavaDoc[] {CFSTR_INETURL};
132 }
133
134 boolean checkURL(Object JavaDoc object) {
135     if (object == null || !(object instanceof String JavaDoc[]) || ((String JavaDoc[])object).length == 0) return false;
136     String JavaDoc[] strings = (String JavaDoc[])object;
137     if (strings[0] == null || strings[0].length() == 0) return false;
138     try {
139         new URL(strings[0]);
140     } catch (java.net.MalformedURLException JavaDoc e) {
141         return false;
142     }
143     return true;
144 }
145
146 protected boolean validate(Object JavaDoc object) {
147     return checkURL(object);
148 }
149 }
150
Popular Tags