KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
14 import org.eclipse.swt.internal.C;
15 import org.eclipse.swt.internal.mozilla.*;
16 import org.eclipse.swt.widgets.*;
17
18 class HelperAppLauncherDialog {
19     XPCOMObject supports;
20     XPCOMObject helperAppLauncherDialog;
21     int refCount = 0;
22
23 public HelperAppLauncherDialog () {
24     createCOMInterfaces ();
25 }
26
27 int AddRef () {
28     refCount++;
29     return refCount;
30 }
31
32 void createCOMInterfaces () {
33     /* Create each of the interfaces that this object implements */
34     supports = new XPCOMObject (new int[] {2, 0, 0}) {
35         public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
36         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
37         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
38     };
39     
40     helperAppLauncherDialog = new XPCOMObject (new int[] {2, 0, 0, 3, 5}) {
41         public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
42         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
43         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
44         public int /*long*/ method3 (int /*long*/[] args) {return Show (args[0], args[1], args[2]);}
45         public int /*long*/ method4 (int /*long*/[] args) {return PromptForSaveToFile (args[0], args[1], args[2], args[3], args[4]);}
46     };
47 }
48
49 void disposeCOMInterfaces () {
50     if (supports != null) {
51         supports.dispose ();
52         supports = null;
53     }
54     if (helperAppLauncherDialog != null) {
55         helperAppLauncherDialog.dispose ();
56         helperAppLauncherDialog = null;
57     }
58 }
59
60 int /*long*/ getAddress () {
61     return helperAppLauncherDialog.getAddress ();
62 }
63
64 int /*long*/ QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
65     if (riid == 0 || ppvObject == 0) return XPCOM.NS_ERROR_NO_INTERFACE;
66     nsID guid = new nsID ();
67     XPCOM.memmove (guid, riid, nsID.sizeof);
68     
69     if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
70         XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF);
71         AddRef ();
72         return XPCOM.NS_OK;
73     }
74     if (guid.Equals (nsIHelperAppLauncherDialog.NS_IHELPERAPPLAUNCHERDIALOG_IID)) {
75         XPCOM.memmove (ppvObject, new int /*long*/[] {helperAppLauncherDialog.getAddress ()}, C.PTR_SIZEOF);
76         AddRef ();
77         return XPCOM.NS_OK;
78     }
79     
80     XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
81     return XPCOM.NS_ERROR_NO_INTERFACE;
82 }
83             
84 int Release () {
85     refCount--;
86     /*
87     * Note. This instance lives as long as the download it is binded to.
88     * Its reference count is expected to go down to 0 when the download
89     * has completed or when it has been cancelled. E.g. when the user
90     * cancels the File Dialog, cancels or closes the Download Dialog
91     * and when the Download Dialog goes away after the download is completed.
92     */

93     if (refCount == 0) disposeCOMInterfaces ();
94     return refCount;
95 }
96
97 /* nsIHelperAppLauncherDialog */
98
99 public int /*long*/ Show (int /*long*/ aLauncher, int /*long*/ aContext, int /*long*/ aReason) {
100     /*
101      * The interface for nsIHelperAppLauncher changed as of mozilla 1.8. Query the received
102      * nsIHelperAppLauncher for the new interface, and if it is not found then fall back to
103      * the old interface.
104      */

105     nsISupports supports = new nsISupports (aLauncher);
106     int /*long*/[] result = new int /*long*/[1];
107     int rc = supports.QueryInterface (nsIHelperAppLauncher_1_8.NS_IHELPERAPPLAUNCHER_IID, result);
108     if (rc == 0) { /* >= 1.8 */
109         nsIHelperAppLauncher_1_8 helperAppLauncher = new nsIHelperAppLauncher_1_8 (aLauncher);
110         rc = helperAppLauncher.SaveToDisk (0, false);
111         helperAppLauncher.Release ();
112         return rc;
113     }
114     nsIHelperAppLauncher helperAppLauncher = new nsIHelperAppLauncher (aLauncher); /* < 1.8 */
115     return helperAppLauncher.SaveToDisk (0, false);
116 }
117
118 public int /*long*/ PromptForSaveToFile (int /*long*/ arg0, int /*long*/ arg1, int /*long*/ arg2, int /*long*/ arg3, int /*long*/ arg4) {
119     int /*long*/ aDefaultFile, aSuggestedFileExtension, _retval;
120     boolean hasLauncher = false;
121
122     /*
123     * The interface for nsIHelperAppLauncherDialog changed as of mozilla 1.5 when an
124     * extra argument was added to the PromptForSaveToFile method (this resulted in all
125     * subsequent arguments shifting right). The workaround is to provide an XPCOMObject
126     * that fits the newer API, and to use the first argument's type to infer whether
127     * the old or new nsIHelperAppLauncherDialog interface is being used (and by extension
128     * the ordering of the arguments). In mozilla >= 1.5 the first argument is an
129     * nsIHelperAppLauncher.
130     */

131     /*
132      * The interface for nsIHelperAppLauncher changed as of mozilla 1.8, so the first
133      * argument must be queried for both the old and new nsIHelperAppLauncher interfaces.
134      */

135     nsISupports support = new nsISupports (arg0);
136     int /*long*/[] result = new int /*long*/[1];
137     int rc = support.QueryInterface (nsIHelperAppLauncher_1_8.NS_IHELPERAPPLAUNCHER_IID, result);
138     boolean usingMozilla18 = rc == 0;
139     if (usingMozilla18) {
140         hasLauncher = true;
141         nsISupports supports = new nsISupports (result[0]);
142         supports.Release ();
143     } else {
144         result[0] = 0;
145         rc = support.QueryInterface (nsIHelperAppLauncher.NS_IHELPERAPPLAUNCHER_IID, result);
146         if (rc == 0) {
147             hasLauncher = true;
148             nsISupports supports = new nsISupports (result[0]);
149             supports.Release ();
150         }
151     }
152     result[0] = 0;
153
154     if (hasLauncher) { /* >= 1.5 */
155         aDefaultFile = arg2;
156         aSuggestedFileExtension = arg3;
157         _retval = arg4;
158     } else { /* 1.4 */
159         aDefaultFile = arg1;
160         aSuggestedFileExtension = arg2;
161         _retval = arg3;
162     }
163
164     int length = XPCOM.strlen_PRUnichar (aDefaultFile);
165     char[] dest = new char[length];
166     XPCOM.memmove (dest, aDefaultFile, length * 2);
167     String JavaDoc defaultFile = new String JavaDoc (dest);
168
169     length = XPCOM.strlen_PRUnichar (aSuggestedFileExtension);
170     dest = new char[length];
171     XPCOM.memmove (dest, aSuggestedFileExtension, length * 2);
172     String JavaDoc suggestedFileExtension = new String JavaDoc (dest);
173
174     Shell shell = new Shell ();
175     FileDialog fileDialog = new FileDialog (shell, SWT.SAVE);
176     fileDialog.setFileName (defaultFile);
177     fileDialog.setFilterExtensions (new String JavaDoc[] {suggestedFileExtension});
178     String JavaDoc name = fileDialog.open ();
179     shell.close ();
180     if (name == null) {
181         if (hasLauncher) {
182             if (usingMozilla18) {
183                 nsIHelperAppLauncher_1_8 launcher = new nsIHelperAppLauncher_1_8 (arg0);
184                 rc = launcher.Cancel (XPCOM.NS_BINDING_ABORTED);
185             } else {
186                 nsIHelperAppLauncher launcher = new nsIHelperAppLauncher (arg0);
187                 rc = launcher.Cancel ();
188             }
189             if (rc != XPCOM.NS_OK) Mozilla.error (rc);
190             return XPCOM.NS_OK;
191         }
192         return XPCOM.NS_ERROR_FAILURE;
193     }
194     nsEmbedString path = new nsEmbedString (name);
195     rc = XPCOM.NS_NewLocalFile (path.getAddress (), true, result);
196     path.dispose ();
197     if (rc != XPCOM.NS_OK) Mozilla.error (rc);
198     if (result[0] == 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
199     /* Our own nsIDownload has been registered during the Browser initialization. It will be invoked by Mozilla. */
200     XPCOM.memmove (_retval, result, C.PTR_SIZEOF);
201     return XPCOM.NS_OK;
202 }
203 }
204
Popular Tags