KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > UpdateUI


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.update.internal.ui;
12
13 import java.io.UnsupportedEncodingException JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.Vector JavaDoc;
18
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.jface.dialogs.*;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.*;
24 import org.eclipse.ui.browser.IWebBrowser;
25 import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
26 import org.eclipse.ui.plugin.AbstractUIPlugin;
27 import org.eclipse.update.configuration.*;
28 import org.eclipse.update.core.*;
29 import org.eclipse.update.internal.core.UpdateCore;
30 import org.eclipse.update.internal.ui.model.UpdateModel;
31 import org.osgi.framework.Bundle;
32 import org.osgi.framework.BundleContext;
33
34 /**
35  * The main plugin class to be used in the desktop.
36  */

37 public class UpdateUI extends AbstractUIPlugin {
38     public static final String JavaDoc PLUGIN_ID = "org.eclipse.update.ui"; //$NON-NLS-1$
39
public static final String JavaDoc WEB_APP_ID = "org.eclipse.update"; //$NON-NLS-1$
40
// preference key
41
public static final String JavaDoc P_DISCOVERY_SITES_ENABLED = "discoverySitesEnabled"; //$NON-NLS-1$
42
//The shared instance.
43
private static UpdateUI plugin;
44     private UpdateModel model;
45     private String JavaDoc appServerHost;
46     private int appServerPort;
47     private UpdateLabelProvider labelProvider;
48     private static boolean remindOnCancel = true;
49
50     /**
51      * The constructor.
52      */

53     public UpdateUI() {
54
55         plugin = this;
56     }
57
58     /**
59      * Returns the shared instance.
60      */

61     public static UpdateUI getDefault() {
62         return plugin;
63     }
64
65     public static IWorkbenchPage getActivePage() {
66         return getDefault().internalGetActivePage();
67     }
68
69     private IWorkbenchPage internalGetActivePage() {
70         IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
71         if (window != null)
72             return window.getActivePage();
73         return null;
74     }
75
76     public static Shell getActiveWorkbenchShell() {
77         IWorkbenchWindow window = getActiveWorkbenchWindow();
78         return window != null ? window.getShell() : getStandardDisplay().getActiveShell();
79     }
80
81     public static IWorkbenchWindow getActiveWorkbenchWindow() {
82         return getDefault().getWorkbench().getActiveWorkbenchWindow();
83     }
84
85     public static String JavaDoc getPluginId() {
86         return getDefault().getBundle().getSymbolicName();
87     }
88
89     public UpdateLabelProvider getLabelProvider() {
90         if (labelProvider == null)
91             labelProvider = new UpdateLabelProvider();
92         return labelProvider;
93     }
94
95     /* (non-Javadoc)
96      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
97      */

98     public void start(BundleContext context) throws Exception JavaDoc {
99         super.start(context);
100         model = new UpdateModel();
101         int historyPref =
102             getPluginPreferences().getInt(UpdateCore.P_HISTORY_SIZE);
103         if (historyPref > 0) {
104             UpdateCore.DEFAULT_HISTORY = historyPref;
105         }
106     }
107     
108     
109
110     public boolean isWebAppStarted() {
111         return appServerHost != null;
112     }
113
114     public String JavaDoc getAppServerHost() {
115         return appServerHost;
116     }
117
118     public int getAppServerPort() {
119         return appServerPort;
120     }
121
122     /* (non-Javadoc)
123      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
124      */

125     public void stop(BundleContext context) throws Exception JavaDoc {
126         if (model != null)
127             model.shutdown();
128
129         if (labelProvider != null)
130             labelProvider.dispose();
131         super.stop(context);
132
133     }
134
135     public UpdateModel getUpdateModel() {
136         return model;
137     }
138
139     public static void logException(Throwable JavaDoc e) {
140         logException(e, true);
141     }
142
143     public static void logException(Throwable JavaDoc e, boolean showErrorDialog) {
144         if (e instanceof InvocationTargetException JavaDoc) {
145             e = ((InvocationTargetException JavaDoc) e).getTargetException();
146         }
147
148         IStatus status = null;
149         if (e instanceof CoreException) {
150             status = ((CoreException) e).getStatus();
151         } else {
152             String JavaDoc message = e.getMessage();
153             if (message == null)
154                 message = e.toString();
155             status =
156                 new Status(
157                     IStatus.ERROR,
158                     getPluginId(),
159                     IStatus.OK,
160                     message,
161                     e);
162         }
163         log(status, showErrorDialog);
164     }
165
166     public static void log(IStatus status, boolean showErrorDialog) {
167         if (status.getSeverity() != IStatus.INFO) {
168             if (showErrorDialog)
169                 ErrorDialog.openError(
170                     getActiveWorkbenchShell(),
171                     null,
172                     null,
173                     status);
174             //ResourcesPlugin.getPlugin().getLog().log(status);
175
// Should log on the update plugin's log
176
// Platform.getPlugin("org.eclipse.core.runtime").getLog().log(status);
177
Bundle bundle = Platform.getBundle("org.eclipse.update.ui"); //$NON-NLS-1$
178
Platform.getLog(bundle).log(status);
179         } else {
180             MessageDialog.openInformation(
181                 getActiveWorkbenchShell(),
182                 null,
183                 status.getMessage());
184         }
185     }
186
187     public static IFeature[] searchSite(
188         String JavaDoc featureId,
189         IConfiguredSite site,
190         boolean onlyConfigured)
191         throws CoreException {
192         IFeatureReference[] references = null;
193
194         if (onlyConfigured)
195             references = site.getConfiguredFeatures();
196         else
197             references = site.getSite().getFeatureReferences();
198         Vector JavaDoc result = new Vector JavaDoc();
199
200         for (int i = 0; i < references.length; i++) {
201             IFeature feature = references[i].getFeature(null);
202             String JavaDoc id = feature.getVersionedIdentifier().getIdentifier();
203             if (featureId.equals(id)) {
204                 result.add(feature);
205             }
206         }
207         return (IFeature[]) result.toArray(new IFeature[result.size()]);
208     }
209
210     public static IFeature[] getInstalledFeatures(IFeature feature) {
211         return getInstalledFeatures(feature, true);
212     }
213
214     public static IFeature[] getInstalledFeatures(
215         IFeature feature,
216         boolean onlyConfigured) {
217         Vector JavaDoc features = new Vector JavaDoc();
218         try {
219             ILocalSite localSite = SiteManager.getLocalSite();
220             IInstallConfiguration config = localSite.getCurrentConfiguration();
221             IConfiguredSite[] isites = config.getConfiguredSites();
222             VersionedIdentifier vid = feature.getVersionedIdentifier();
223             String JavaDoc id = vid.getIdentifier();
224
225             for (int i = 0; i < isites.length; i++) {
226                 IConfiguredSite isite = isites[i];
227                 IFeature[] result =
228                     UpdateUI.searchSite(id, isite, onlyConfigured);
229                 for (int j = 0; j < result.length; j++) {
230                     IFeature installedFeature = result[j];
231                     features.add(installedFeature);
232                 }
233             }
234         } catch (CoreException e) {
235             UpdateUI.logException(e);
236         }
237         return (IFeature[]) features.toArray(new IFeature[features.size()]);
238     }
239
240     public static URL JavaDoc getOriginatingURL(String JavaDoc id) {
241         IDialogSettings section = getOriginatingURLSection();
242         String JavaDoc value = section.get(id);
243         if (value != null) {
244             try {
245                 return new URL JavaDoc(value);
246             } catch (MalformedURLException JavaDoc e) {
247             }
248         }
249         return null;
250     }
251
252     public static void setOriginatingURL(String JavaDoc id, URL JavaDoc url) {
253         IDialogSettings section = getOriginatingURLSection();
254         section.put(id, url.toString());
255     }
256
257     private static IDialogSettings getOriginatingURLSection() {
258         IDialogSettings settings = getDefault().getDialogSettings();
259         IDialogSettings section = settings.getSection("originatingURLs"); //$NON-NLS-1$
260
if (section == null)
261             section = settings.addNewSection("originatingURLs"); //$NON-NLS-1$
262
return section;
263     }
264
265     
266     /**
267      * Prompts the user to restart
268      * @param restartIsReallyNeeded true when a restart is needed, false if the user feels lucky (tm) and wants the changes
269      * applied to the current config
270      */

271     public static void requestRestart(boolean restartIsReallyNeeded) {
272         boolean restart =
273             RestartDialog.openQuestion(
274                 getActiveWorkbenchShell(),
275                 restartIsReallyNeeded);
276         if (restart)
277             PlatformUI.getWorkbench().restart();
278     }
279
280     public static void showURL(String JavaDoc url) {
281         showURL(url, false);
282     }
283
284     public static void showURL(String JavaDoc url, boolean encodeHostAndPort) {
285         if (encodeHostAndPort)
286             url = encodeHostAndPort(url);
287
288         IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport();
289         try {
290             IWebBrowser browser = support.getExternalBrowser();
291             browser.openURL(new URL JavaDoc(url));
292         }
293         catch (MalformedURLException JavaDoc e) {
294             UpdateUI.logException(e);
295         }
296         catch (PartInitException e) {
297             UpdateUI.logException(e);
298         }
299     }
300
301     private static String JavaDoc encodeHostAndPort(String JavaDoc urlName) {
302         String JavaDoc callbackURL = getCallbackURLAsString();
303         if (callbackURL == null)
304             return urlName;
305         String JavaDoc callbackParameter = "updateURL=" + callbackURL; //$NON-NLS-1$
306
if (urlName.indexOf('?') != -1)
307             return urlName + "&" + callbackParameter; //$NON-NLS-1$
308
else
309             return urlName + "?" + callbackParameter; //$NON-NLS-1$
310
}
311     
312     private static String JavaDoc getCallbackURLAsString() {
313         String JavaDoc host = getDefault().getAppServerHost();
314         int port = getDefault().getAppServerPort();
315         if (host == null || port == 0)
316             return null;
317         else {
318             String JavaDoc value =
319                 "http://" //$NON-NLS-1$
320
+ host
321                     + ":" //$NON-NLS-1$
322
+ port
323                     + "/" //$NON-NLS-1$
324
+ WEB_APP_ID
325                     + "/install"; //$NON-NLS-1$
326
try {
327                 value = URLCoder.encode(value);
328             } catch (UnsupportedEncodingException JavaDoc e) {
329             }
330             return value;
331         }
332     }
333     
334     public static boolean getRemindOnCancel() {
335         return remindOnCancel;
336     }
337     
338     public static void setRemindOnCancel(boolean remind) {
339         remindOnCancel = remind;
340     }
341     
342     
343     /**
344      * Returns the standard display to be used. The method first checks, if
345      * the thread calling this method has an associated disaply. If so, this
346      * display is returned. Otherwise the method returns the default display.
347      */

348     public static Display getStandardDisplay() {
349         Display display;
350         display = Display.getCurrent();
351         if (display == null)
352             display = Display.getDefault();
353         return display;
354     }
355     
356     
357     /* (non-Javadoc)
358      * @see org.eclipse.core.runtime.Plugin#initializeDefaultPluginPreferences()
359      */

360     protected void initializeDefaultPluginPreferences() {
361         Preferences store = getPluginPreferences();
362         store.setDefault(P_DISCOVERY_SITES_ENABLED, true);
363     }
364 }
365
Popular Tags