KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > scheduler > UpdateScheduler


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
24  * This plug-in is loaded on startup to fork a job that
25  * searches for new plug-ins.
26  */

27 public class UpdateScheduler extends AbstractUIPlugin{
28     // Preferences
29
public static final String JavaDoc P_ENABLED = "enabled"; //$NON-NLS-1$
30
public static final String JavaDoc P_SCHEDULE = "schedule"; //$NON-NLS-1$
31
public static final String JavaDoc VALUE_ON_STARTUP = "on-startup"; //$NON-NLS-1$
32
public static final String JavaDoc VALUE_ON_SCHEDULE = "on-schedule"; //$NON-NLS-1$
33
public static final String JavaDoc P_DOWNLOAD = "download"; // value is true or false, default is false //$NON-NLS-1$
34

35     //The shared instance.
36
private static UpdateScheduler plugin;
37     //Resource bundle.
38
private ResourceBundle resourceBundle;
39     
40     // singleton
41
private static SchedulerStartup scheduler;
42
43     /**
44      * The constructor.
45      */

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"); //$NON-NLS-1$
55
} catch (MissingResourceException x) {
56                 resourceBundle = null;
57             }
58         return resourceBundle;
59     }
60
61     /**
62      * Returns the shared instance.
63      */

64     public static UpdateScheduler getDefault() {
65         return plugin;
66     }
67
68     /**
69      * Returns the string from the plugin's resource bundle,
70      * or 'key' if not found.
71      */

72     public static String JavaDoc getString(String JavaDoc 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 JavaDoc getFormattedMessage(String JavaDoc key, String JavaDoc[] args) {
83         String JavaDoc text = getString(key);
84         return java.text.MessageFormat.format(text, args);
85     }
86
87     public static String JavaDoc getFormattedMessage(String JavaDoc key, String JavaDoc arg) {
88         String JavaDoc text = getString(key);
89         return java.text.MessageFormat.format(text, new String JavaDoc[] { arg });
90     }
91
92     public static String JavaDoc getPluginId() {
93         return getDefault().getBundle().getSymbolicName();
94     }
95
96     public static void logException(Throwable JavaDoc e) {
97         logException(e, true);
98     }
99
100     public static void logException(Throwable JavaDoc 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 JavaDoc 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 // Should log on the update plugin's log
132
// Platform.getPlugin("org.eclipse.core.runtime").getLog().log(status); //$NON-NLS-1$
133
Bundle bundle = Platform.getBundle("org.eclipse.update.scheduler"); //$NON-NLS-1$
134
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     /* (non-Javadoc)
170      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
171      */

172     public void start(BundleContext context) throws Exception JavaDoc {
173         super.start(context);
174         initializeDefaultPreferences();
175     }
176     
177     public static SchedulerStartup getScheduler() {
178         // If the scheduler was disabled, it does not get initialized
179
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