1 11 package org.eclipse.update.internal.ui; 12 13 import java.io.UnsupportedEncodingException ; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 import java.util.Vector ; 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 37 public class UpdateUI extends AbstractUIPlugin { 38 public static final String PLUGIN_ID = "org.eclipse.update.ui"; public static final String WEB_APP_ID = "org.eclipse.update"; public static final String P_DISCOVERY_SITES_ENABLED = "discoverySitesEnabled"; private static UpdateUI plugin; 44 private UpdateModel model; 45 private String appServerHost; 46 private int appServerPort; 47 private UpdateLabelProvider labelProvider; 48 private static boolean remindOnCancel = true; 49 50 53 public UpdateUI() { 54 55 plugin = this; 56 } 57 58 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 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 98 public void start(BundleContext context) throws Exception { 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 getAppServerHost() { 115 return appServerHost; 116 } 117 118 public int getAppServerPort() { 119 return appServerPort; 120 } 121 122 125 public void stop(BundleContext context) throws Exception { 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 e) { 140 logException(e, true); 141 } 142 143 public static void logException(Throwable e, boolean showErrorDialog) { 144 if (e instanceof InvocationTargetException ) { 145 e = ((InvocationTargetException ) e).getTargetException(); 146 } 147 148 IStatus status = null; 149 if (e instanceof CoreException) { 150 status = ((CoreException) e).getStatus(); 151 } else { 152 String 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 Bundle bundle = Platform.getBundle("org.eclipse.update.ui"); 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 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 result = new Vector (); 199 200 for (int i = 0; i < references.length; i++) { 201 IFeature feature = references[i].getFeature(null); 202 String 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 features = new Vector (); 218 try { 219 ILocalSite localSite = SiteManager.getLocalSite(); 220 IInstallConfiguration config = localSite.getCurrentConfiguration(); 221 IConfiguredSite[] isites = config.getConfiguredSites(); 222 VersionedIdentifier vid = feature.getVersionedIdentifier(); 223 String 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 getOriginatingURL(String id) { 241 IDialogSettings section = getOriginatingURLSection(); 242 String value = section.get(id); 243 if (value != null) { 244 try { 245 return new URL (value); 246 } catch (MalformedURLException e) { 247 } 248 } 249 return null; 250 } 251 252 public static void setOriginatingURL(String id, URL 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"); if (section == null) 261 section = settings.addNewSection("originatingURLs"); return section; 263 } 264 265 266 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 url) { 281 showURL(url, false); 282 } 283 284 public static void showURL(String 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 (url)); 292 } 293 catch (MalformedURLException e) { 294 UpdateUI.logException(e); 295 } 296 catch (PartInitException e) { 297 UpdateUI.logException(e); 298 } 299 } 300 301 private static String encodeHostAndPort(String urlName) { 302 String callbackURL = getCallbackURLAsString(); 303 if (callbackURL == null) 304 return urlName; 305 String callbackParameter = "updateURL=" + callbackURL; if (urlName.indexOf('?') != -1) 307 return urlName + "&" + callbackParameter; else 309 return urlName + "?" + callbackParameter; } 311 312 private static String getCallbackURLAsString() { 313 String host = getDefault().getAppServerHost(); 314 int port = getDefault().getAppServerPort(); 315 if (host == null || port == 0) 316 return null; 317 else { 318 String value = 319 "http://" + host 321 + ":" + port 323 + "/" + WEB_APP_ID 325 + "/install"; try { 327 value = URLCoder.encode(value); 328 } catch (UnsupportedEncodingException 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 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 360 protected void initializeDefaultPluginPreferences() { 361 Preferences store = getPluginPreferences(); 362 store.setDefault(P_DISCOVERY_SITES_ENABLED, true); 363 } 364 } 365 | Popular Tags |