KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > browser > Download_1_8


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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.browser;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.internal.*;
15 import org.eclipse.swt.internal.mozilla.*;
16 import org.eclipse.swt.layout.*;
17 import org.eclipse.swt.widgets.*;
18
19 class Download_1_8 {
20     XPCOMObject supports;
21     XPCOMObject download;
22     XPCOMObject progressDialog;
23     XPCOMObject webProgressListener;
24     nsICancelable cancelable;
25     int refCount = 0;
26
27     Shell shell;
28     Label status;
29     Button cancel;
30
31     static final boolean is32 = C.PTR_SIZEOF == 4;
32
33 public Download_1_8 () {
34     createCOMInterfaces ();
35 }
36
37 int AddRef () {
38     refCount++;
39     return refCount;
40 }
41
42 void createCOMInterfaces () {
43     /* Create each of the interfaces that this object implements */
44     supports = new XPCOMObject (new int[] {2, 0, 0}) {
45         public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
46         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
47         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
48     };
49
50     download = new XPCOMObject (new int[] {2, 0, 0, 4, 6, 3, 4, 3, is32 ? 10 : 6, is32 ? 8 : 7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}) {
51         public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
52         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
53         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
54         public int /*long*/ method3 (int /*long*/[] args) {return OnStateChange (args[0], args[1], args[2], args[3]);}
55         public int /*long*/ method4 (int /*long*/[] args) {return OnProgressChange (args[0], args[1], args[2], args[3], args[4], args[5]);}
56         public int /*long*/ method5 (int /*long*/[] args) {return OnLocationChange (args[0], args[1], args[2]);}
57         public int /*long*/ method6 (int /*long*/[] args) {return OnStatusChange (args[0], args[1], args[2], args[3]);}
58         public int /*long*/ method7 (int /*long*/[] args) {return OnSecurityChange (args[0], args[1], args[2]);}
59         public int /*long*/ method8 (int /*long*/[] args) {
60             if (args.length == 10) {
61                 return OnProgressChange64_32 (args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
62             } else {
63                 return OnProgressChange64 (args[0], args[1], args[2], args[3], args[4], args[5]);
64             }
65         }
66         public int /*long*/ method9 (int /*long*/[] args) {
67             if (args.length == 8) {
68                 return Init_32 (args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
69             } else {
70                 return Init (args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
71             }
72         }
73         public int /*long*/ method10 (int /*long*/[] args) {return GetTargetFile (args[0]);}
74         public int /*long*/ method11 (int /*long*/[] args) {return GetPercentComplete (args[0]);}
75         public int /*long*/ method12 (int /*long*/[] args) {return GetAmountTransferred (args[0]);}
76         public int /*long*/ method13 (int /*long*/[] args) {return GetSize (args[0]);}
77         public int /*long*/ method14 (int /*long*/[] args) {return GetSource (args[0]);}
78         public int /*long*/ method15 (int /*long*/[] args) {return GetTarget (args[0]);}
79         public int /*long*/ method16 (int /*long*/[] args) {return GetCancelable (args[0]);}
80         public int /*long*/ method17 (int /*long*/[] args) {return GetDisplayName (args[0]);}
81         public int /*long*/ method18 (int /*long*/[] args) {return GetStartTime (args[0]);}
82         public int /*long*/ method19 (int /*long*/[] args) {return GetMIMEInfo (args[0]);}
83     };
84
85     progressDialog = new XPCOMObject (new int[] {2, 0, 0, 4, 6, 3, 4, 3, is32 ? 10 : 6, is32 ? 8 : 7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}) {
86         public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
87         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
88         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
89         public int /*long*/ method3 (int /*long*/[] args) {return OnStateChange (args[0], args[1], args[2], args[3]);}
90         public int /*long*/ method4 (int /*long*/[] args) {return OnProgressChange (args[0], args[1], args[2], args[3], args[4], args[5]);}
91         public int /*long*/ method5 (int /*long*/[] args) {return OnLocationChange (args[0], args[1], args[2]);}
92         public int /*long*/ method6 (int /*long*/[] args) {return OnStatusChange (args[0], args[1], args[2], args[3]);}
93         public int /*long*/ method7 (int /*long*/[] args) {return OnSecurityChange (args[0], args[1], args[2]);}
94         public int /*long*/ method8 (int /*long*/[] args) {
95             if (args.length == 10) {
96                 return OnProgressChange64_32 (args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
97             } else {
98                 return OnProgressChange64 (args[0], args[1], args[2], args[3], args[4], args[5]);
99             }
100         }
101         public int /*long*/ method9 (int /*long*/[] args) {
102             if (args.length == 8) {
103                 return Init_32 (args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
104             } else {
105                 return Init (args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
106             }
107         }
108         public int /*long*/ method10 (int /*long*/[] args) {return GetTargetFile (args[0]);}
109         public int /*long*/ method11 (int /*long*/[] args) {return GetPercentComplete (args[0]);}
110         public int /*long*/ method12 (int /*long*/[] args) {return GetAmountTransferred (args[0]);}
111         public int /*long*/ method13 (int /*long*/[] args) {return GetSize (args[0]);}
112         public int /*long*/ method14 (int /*long*/[] args) {return GetSource (args[0]);}
113         public int /*long*/ method15 (int /*long*/[] args) {return GetTarget (args[0]);}
114         public int /*long*/ method16 (int /*long*/[] args) {return GetCancelable (args[0]);}
115         public int /*long*/ method17 (int /*long*/[] args) {return GetDisplayName (args[0]);}
116         public int /*long*/ method18 (int /*long*/[] args) {return GetStartTime (args[0]);}
117         public int /*long*/ method19 (int /*long*/[] args) {return GetMIMEInfo (args[0]);}
118         public int /*long*/ method20 (int /*long*/[] args) {return Open (args[0]);}
119         public int /*long*/ method21 (int /*long*/[] args) {return GetCancelDownloadOnClose (args[0]);}
120         public int /*long*/ method22 (int /*long*/[] args) {return SetCancelDownloadOnClose (args[0]);}
121         public int /*long*/ method23 (int /*long*/[] args) {return GetObserver (args[0]);}
122         public int /*long*/ method24 (int /*long*/[] args) {return SetObserver (args[0]);}
123         public int /*long*/ method25 (int /*long*/[] args) {return GetDialog (args[0]);}
124         public int /*long*/ method26 (int /*long*/[] args) {return SetDialog (args[0]);}
125     };
126
127     webProgressListener = new XPCOMObject (new int[] {2, 0, 0, 4, 6, 3, 4, 3}) {
128         public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
129         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
130         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
131         public int /*long*/ method3 (int /*long*/[] args) {return OnStateChange (args[0], args[1], args[2],args[3]);}
132         public int /*long*/ method4 (int /*long*/[] args) {return OnProgressChange (args[0], args[1], args[2], args[3], args[4], args[5]);}
133         public int /*long*/ method5 (int /*long*/[] args) {return OnLocationChange (args[0], args[1], args[2]);}
134         public int /*long*/ method6 (int /*long*/[] args) {return OnStatusChange (args[0], args[1], args[2], args[3]);}
135         public int /*long*/ method7 (int /*long*/[] args) {return OnSecurityChange (args[0], args[1], args[2]);}
136     };
137 }
138
139 void disposeCOMInterfaces() {
140     if (supports != null) {
141         supports.dispose ();
142         supports = null;
143     }
144     if (download != null) {
145         download.dispose ();
146         download = null;
147     }
148     if (progressDialog != null) {
149         progressDialog.dispose ();
150         progressDialog = null;
151     }
152     if (webProgressListener != null) {
153         webProgressListener.dispose ();
154         webProgressListener = null;
155     }
156 }
157
158 int /*long*/ getAddress () {
159     return progressDialog.getAddress ();
160 }
161
162 int /*long*/ QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
163     if (riid == 0 || ppvObject == 0) return XPCOM.NS_ERROR_NO_INTERFACE;
164     nsID guid = new nsID ();
165     XPCOM.memmove (guid, riid, nsID.sizeof);
166
167     if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
168         XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF);
169         AddRef();
170         return XPCOM.NS_OK;
171     }
172     if (guid.Equals (nsIDownload_1_8.NS_IDOWNLOAD_IID)) {
173         XPCOM.memmove (ppvObject, new int /*long*/[] {download.getAddress ()}, C.PTR_SIZEOF);
174         AddRef();
175         return XPCOM.NS_OK;
176     }
177     if (guid.Equals (nsIProgressDialog_1_8.NS_IPROGRESSDIALOG_IID)) {
178         XPCOM.memmove (ppvObject, new int /*long*/[] {progressDialog.getAddress ()}, C.PTR_SIZEOF);
179         AddRef();
180         return XPCOM.NS_OK;
181     }
182     if (guid.Equals (nsIWebProgressListener.NS_IWEBPROGRESSLISTENER_IID)) {
183         XPCOM.memmove (ppvObject, new int /*long*/[] {webProgressListener.getAddress ()}, C.PTR_SIZEOF);
184         AddRef();
185         return XPCOM.NS_OK;
186     }
187     XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
188     return XPCOM.NS_ERROR_NO_INTERFACE;
189 }
190
191 int Release () {
192     refCount--;
193     if (refCount == 0) disposeCOMInterfaces ();
194     return refCount;
195 }
196
197 /* nsIDownload */
198
199 /* Note. The argument startTime is defined as a PRInt64. This translates into two java ints. */
200 public int Init_32 (int /*long*/ aSource, int /*long*/ aTarget, int /*long*/ aDisplayName, int /*long*/ aMIMEInfo, int /*long*/ startTime1, int /*long*/ startTime2, int /*long*/ aTempFile, int /*long*/ aCancelable) {
201     long startTime = (startTime2 << 32) + startTime1;
202     return Init (aSource, aTarget, aDisplayName, aMIMEInfo, startTime, aTempFile, aCancelable);
203 }
204
205 public int Init (int /*long*/ aSource, int /*long*/ aTarget, int /*long*/ aDisplayName, int /*long*/ aMIMEInfo, long startTime, int /*long*/ aTempFile, int /*long*/ aCancelable) {
206     cancelable = new nsICancelable (aCancelable);
207     nsIURI source = new nsIURI (aSource);
208     int /*long*/ aSpec = XPCOM.nsEmbedCString_new ();
209     int rc = source.GetHost (aSpec);
210     if (rc != XPCOM.NS_OK) Mozilla.error(rc);
211     int length = XPCOM.nsEmbedCString_Length (aSpec);
212     int /*long*/ buffer = XPCOM.nsEmbedCString_get (aSpec);
213     byte[] dest = new byte[length];
214     XPCOM.memmove (dest, buffer, length);
215     XPCOM.nsEmbedCString_delete (aSpec);
216     String JavaDoc url = new String JavaDoc (dest);
217
218     nsIURI target = new nsIURI (aTarget);
219     int /*long*/ aPath = XPCOM.nsEmbedCString_new ();
220     rc = target.GetPath (aPath);
221     if (rc != XPCOM.NS_OK) Mozilla.error (rc);
222     length = XPCOM.nsEmbedCString_Length (aPath);
223     buffer = XPCOM.nsEmbedCString_get (aPath);
224     dest = new byte[length];
225     XPCOM.memmove (dest, buffer, length);
226     XPCOM.nsEmbedCString_delete (aPath);
227     String JavaDoc filename = new String JavaDoc (dest);
228     int separator = filename.lastIndexOf (System.getProperty ("file.separator")); //$NON-NLS-1$
229
filename = filename.substring (separator + 1);
230
231     Listener listener = new Listener() {
232         public void handleEvent (Event event) {
233             if (event.widget == cancel) {
234                 shell.close ();
235             }
236             if (cancelable != null) {
237                 int rc = cancelable.Cancel (XPCOM.NS_BINDING_ABORTED);
238                 if (rc != XPCOM.NS_OK) Mozilla.error (rc);
239             }
240             shell = null;
241             cancelable = null;
242         }
243     };
244     shell = new Shell (SWT.DIALOG_TRIM);
245     String JavaDoc msg = Compatibility.getMessage ("SWT_Download_File", new Object JavaDoc[] {filename}); //$NON-NLS-1$
246
shell.setText (msg);
247     GridLayout gridLayout = new GridLayout ();
248     gridLayout.marginHeight = 15;
249     gridLayout.marginWidth = 15;
250     gridLayout.verticalSpacing = 20;
251     shell.setLayout (gridLayout);
252     msg = Compatibility.getMessage ("SWT_Download_Location", new Object JavaDoc[] {filename, url}); //$NON-NLS-1$
253
new Label (shell, SWT.SIMPLE).setText (msg);
254     status = new Label (shell, SWT.SIMPLE);
255     msg = Compatibility.getMessage ("SWT_Download_Started"); //$NON-NLS-1$
256
status.setText (msg);
257     GridData data = new GridData ();
258     data.grabExcessHorizontalSpace = true;
259     data.grabExcessVerticalSpace = true;
260     status.setLayoutData (data);
261     
262     cancel = new Button (shell, SWT.PUSH);
263     cancel.setText (SWT.getMessage ("SWT_Cancel")); //$NON-NLS-1$
264
data = new GridData ();
265     data.horizontalAlignment = GridData.CENTER;
266     cancel.setLayoutData (data);
267     cancel.addListener (SWT.Selection, listener);
268     shell.addListener (SWT.Close, listener);
269     shell.pack ();
270     shell.open ();
271     return XPCOM.NS_OK;
272 }
273
274 int /*long*/ GetAmountTransferred (int /*long*/ arg0) {
275     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
276 }
277
278 int /*long*/ GetCancelable (int /*long*/ arg0) {
279     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
280 }
281
282 public int /*long*/ GetDisplayName (int /*long*/ aDisplayName) {
283     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
284 }
285
286 public int /*long*/ GetMIMEInfo (int /*long*/ aMIMEInfo) {
287     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
288 }
289
290 public int /*long*/ GetPercentComplete (int /*long*/ aPercentComplete) {
291     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
292 }
293
294 int /*long*/ GetSize (int /*long*/ arg0) {
295     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
296 }
297
298 public int /*long*/ GetSource (int /*long*/ aSource) {
299     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
300 }
301
302 public int /*long*/ GetStartTime (int /*long*/ aStartTime) {
303     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
304 }
305
306 public int /*long*/ GetTarget (int /*long*/ aTarget) {
307     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
308 }
309
310 int /*long*/ GetTargetFile (int /*long*/ arg0) {
311     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
312 }
313
314 /* nsIProgressDialog */
315 public int /*long*/ GetCancelDownloadOnClose (int /*long*/ aCancelDownloadOnClose) {
316     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
317 }
318
319 public int /*long*/ GetDialog (int /*long*/ aDialog) {
320     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
321 }
322
323 public int /*long*/ GetObserver (int /*long*/ aObserver) {
324     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
325 }
326
327 public int /*long*/ Open (int /*long*/ aParent) {
328     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
329 }
330
331 public int /*long*/ SetCancelDownloadOnClose (int /*long*/ aCancelDownloadOnClose) {
332     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
333 }
334
335 public int /*long*/ SetDialog (int /*long*/ aDialog) {
336     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
337 }
338
339 public int /*long*/ SetObserver (int /*long*/ aObserver) {
340     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
341 }
342
343 /* nsIWebProgressListener */
344
345 int /*long*/ OnLocationChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int /*long*/ aLocation) {
346     return XPCOM.NS_OK;
347 }
348
349 int /*long*/ OnProgressChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int /*long*/ aCurSelfProgress, int /*long*/ aMaxSelfProgress, int /*long*/ aCurTotalProgress, int /*long*/ aMaxTotalProgress) {
350     return OnProgressChange64 (aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
351 }
352
353 /* Note. The last 4 args in the original interface are defined as PRInt64. These each translate into two java ints. */
354 int /*long*/ OnProgressChange64_32 (int /*long*/ aWebProgress, int /*long*/ aRequest, int /*long*/ aCurSelfProgress1, int /*long*/ aCurSelfProgress2, int /*long*/ aMaxSelfProgress1, int /*long*/ aMaxSelfProgress2, int /*long*/ aCurTotalProgress1, int /*long*/ aCurTotalProgress2, int /*long*/ aMaxTotalProgress1, int /*long*/ aMaxTotalProgress2) {
355     long aCurSelfProgress = (aCurSelfProgress2 << 32) + aCurSelfProgress1;
356     long aMaxSelfProgress = (aMaxSelfProgress2 << 32) + aMaxSelfProgress1;
357     long aCurTotalProgress = (aCurTotalProgress2 << 32) + aCurTotalProgress1;
358     long aMaxTotalProgress = (aMaxTotalProgress2 << 32) + aMaxTotalProgress1;
359     return OnProgressChange64 (aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
360 }
361
362 int /*long*/ OnProgressChange64 (int /*long*/ aWebProgress, int /*long*/ aRequest, long aCurSelfProgress, long aMaxSelfProgress, long aCurTotalProgress, long aMaxTotalProgress) {
363     long currentKBytes = aCurTotalProgress / 1024;
364     long totalKBytes = aMaxTotalProgress / 1024;
365     if (shell != null & !shell.isDisposed ()) {
366         Object JavaDoc[] arguments = {new Long JavaDoc (currentKBytes), new Long JavaDoc (totalKBytes)};
367         String JavaDoc statusMsg = Compatibility.getMessage ("SWT_Download_Status", arguments); //$NON-NLS-1$
368
status.setText (statusMsg);
369         shell.layout (true);
370         shell.getDisplay ().update ();
371     }
372     return XPCOM.NS_OK;
373 }
374
375 int /*long*/ OnSecurityChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int /*long*/ state) {
376     return XPCOM.NS_OK;
377 }
378
379 int /*long*/ OnStateChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int /*long*/ aStateFlags, int /*long*/ aStatus) {
380     if ((aStateFlags & nsIWebProgressListener.STATE_STOP) != 0) {
381         cancelable = null;
382         if (shell != null && !shell.isDisposed ()) shell.dispose ();
383         shell = null;
384     }
385     return XPCOM.NS_OK;
386 }
387
388 int /*long*/ OnStatusChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int /*long*/ aStatus, int /*long*/ aMessage) {
389     return XPCOM.NS_OK;
390 }
391 }
392
Popular Tags