1 11 package org.eclipse.update.internal.scheduler; 12 13 import java.lang.reflect.*; 14 import java.util.*; 15 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.jface.dialogs.*; 18 import org.eclipse.swt.widgets.*; 19 import org.eclipse.ui.*; 20 import org.eclipse.ui.plugin.*; 21 import org.osgi.framework.*; 22 23 27 public class UpdateScheduler extends AbstractUIPlugin{ 28 public static final String P_ENABLED = "enabled"; public static final String P_SCHEDULE = "schedule"; public static final String VALUE_ON_STARTUP = "on-startup"; public static final String VALUE_ON_SCHEDULE = "on-schedule"; public static final String P_DOWNLOAD = "download"; 35 private static UpdateScheduler plugin; 37 private ResourceBundle resourceBundle; 39 40 private static SchedulerStartup scheduler; 42 43 46 public UpdateScheduler() { 47 plugin = this; 48 } 49 50 51 public ResourceBundle getResourceBundle() { 52 if (resourceBundle == null) 53 try { 54 resourceBundle = ResourceBundle.getBundle("org.eclipse.update.internal.scheduler.UpdateSchedulerResources"); } catch (MissingResourceException x) { 56 resourceBundle = null; 57 } 58 return resourceBundle; 59 } 60 61 64 public static UpdateScheduler getDefault() { 65 return plugin; 66 } 67 68 72 public static String getString(String key) { 73 ResourceBundle bundle = 74 UpdateScheduler.getDefault().getResourceBundle(); 75 try { 76 return bundle.getString(key); 77 } catch (MissingResourceException e) { 78 return key; 79 } 80 } 81 82 public static String getFormattedMessage(String key, String [] args) { 83 String text = getString(key); 84 return java.text.MessageFormat.format(text, args); 85 } 86 87 public static String getFormattedMessage(String key, String arg) { 88 String text = getString(key); 89 return java.text.MessageFormat.format(text, new String [] { arg }); 90 } 91 92 public static String getPluginId() { 93 return getDefault().getBundle().getSymbolicName(); 94 } 95 96 public static void logException(Throwable e) { 97 logException(e, true); 98 } 99 100 public static void logException(Throwable e, boolean showErrorDialog) { 101 if (e instanceof InvocationTargetException) { 102 e = ((InvocationTargetException) e).getTargetException(); 103 } 104 105 IStatus status = null; 106 if (e instanceof CoreException) { 107 status = ((CoreException) e).getStatus(); 108 } else { 109 String message = e.getMessage(); 110 if (message == null) 111 message = e.toString(); 112 status = 113 new Status( 114 IStatus.ERROR, 115 getPluginId(), 116 IStatus.OK, 117 message, 118 e); 119 } 120 log(status, showErrorDialog); 121 } 122 123 public static void log(IStatus status, boolean showErrorDialog) { 124 if (status.getSeverity() != IStatus.INFO) { 125 if (showErrorDialog) 126 ErrorDialog.openError( 127 getActiveWorkbenchShell(), 128 null, 129 null, 130 status); 131 Bundle bundle = Platform.getBundle("org.eclipse.update.scheduler"); Platform.getLog(bundle).log(status); 135 } else { 136 MessageDialog.openInformation( 137 getActiveWorkbenchShell(), 138 null, 139 status.getMessage()); 140 } 141 } 142 143 public static IWorkbenchPage getActivePage() { 144 UpdateScheduler plugin = getDefault(); 145 IWorkbenchWindow window = 146 plugin.getWorkbench().getActiveWorkbenchWindow(); 147 if (window != null) 148 return window.getActivePage(); 149 return null; 150 } 151 152 public static Shell getActiveWorkbenchShell() { 153 IWorkbenchWindow window = getActiveWorkbenchWindow(); 154 return window != null ? window.getShell() : null; 155 } 156 157 public static IWorkbenchWindow getActiveWorkbenchWindow() { 158 return getDefault().getWorkbench().getActiveWorkbenchWindow(); 159 } 160 161 private void initializeDefaultPreferences() { 162 Preferences pref = getPluginPreferences(); 163 pref.setDefault(P_ENABLED, false); 164 pref.setDefault(P_SCHEDULE, VALUE_ON_STARTUP); 165 pref.setDefault(P_DOWNLOAD, false); 166 } 167 168 169 172 public void start(BundleContext context) throws Exception { 173 super.start(context); 174 initializeDefaultPreferences(); 175 } 176 177 public static SchedulerStartup getScheduler() { 178 if (scheduler == null) 180 scheduler = new SchedulerStartup(); 181 return scheduler; 182 } 183 184 static void setScheduler(SchedulerStartup scheduler) { 185 UpdateScheduler.scheduler = scheduler; 186 } 187 } 188 | Popular Tags |