KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > resources > ResourcesPlugin


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.core.resources;
12
13 import org.eclipse.core.internal.resources.*;
14 import org.eclipse.core.internal.utils.Messages;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.core.runtime.jobs.IJobManager;
17 import org.eclipse.core.runtime.jobs.Job;
18 import org.osgi.framework.BundleActivator;
19 import org.osgi.framework.BundleContext;
20
21 /**
22  * The plug-in runtime class for the Resources plug-in. This is
23  * the starting point for all workspace and resource manipulation.
24  * A typical sequence of events would be for a dependent plug-in
25  * to call <code>ResourcesPlugin.getWorkspace()</code>.
26  * Doing so would cause this plug-in to be activated and the workspace
27  * (if any) to be loaded from disk and initialized.
28  */

29 public final class ResourcesPlugin extends Plugin {
30     /**
31      * Unique identifier constant (value <code>"org.eclipse.core.resources"</code>)
32      * for the standard Resources plug-in.
33      */

34     public static final String JavaDoc PI_RESOURCES = "org.eclipse.core.resources"; //$NON-NLS-1$
35

36     /*====================================================================
37      * Constants defining the ids of the standard workspace extension points:
38      *====================================================================*/

39
40     /**
41      * Simple identifier constant (value <code>"builders"</code>)
42      * for the builders extension point.
43      */

44     public static final String JavaDoc PT_BUILDERS = "builders"; //$NON-NLS-1$
45

46     /**
47      * Simple identifier constant (value <code>"natures"</code>)
48      * for the natures extension point.
49      */

50     public static final String JavaDoc PT_NATURES = "natures"; //$NON-NLS-1$
51

52     /**
53      * Simple identifier constant (value <code>"markers"</code>)
54      * for the markers extension point.
55      */

56     public static final String JavaDoc PT_MARKERS = "markers"; //$NON-NLS-1$
57

58     /**
59      * Simple identifier constant (value <code>"fileModificationValidator"</code>)
60      * for the file modification validator extension point.
61      */

62     public static final String JavaDoc PT_FILE_MODIFICATION_VALIDATOR = "fileModificationValidator"; //$NON-NLS-1$
63

64     /**
65      * Simple identifier constant (value <code>"moveDeleteHook"</code>)
66      * for the move/delete hook extension point.
67      *
68      * @since 2.0
69      */

70     public static final String JavaDoc PT_MOVE_DELETE_HOOK = "moveDeleteHook"; //$NON-NLS-1$
71

72     /**
73      * Simple identifier constant (value <code>"teamHook"</code>)
74      * for the team hook extension point.
75      *
76      * @since 2.1
77      */

78     public static final String JavaDoc PT_TEAM_HOOK = "teamHook"; //$NON-NLS-1$
79

80     /**
81      * Simple identifier constant (value <code>"refreshProviders"</code>)
82      * for the auto-refresh refresh providers extension point.
83      *
84      * @since 3.0
85      */

86     public static final String JavaDoc PT_REFRESH_PROVIDERS = "refreshProviders"; //$NON-NLS-1$
87

88     /**
89      * Simple identifier constant (value <code>"modelProviders"</code>)
90      * for the model providers extension point.
91      *
92      * @since 3.2
93      */

94     public static final String JavaDoc PT_MODEL_PROVIDERS = "modelProviders"; //$NON-NLS-1$
95

96     /**
97      * Constant identifying the job family identifier for the background autobuild job.
98      *
99      * @see IJobManager#join(Object, IProgressMonitor)
100      * @since 3.0
101      */

102     public static final Object JavaDoc FAMILY_AUTO_BUILD = new Object JavaDoc();
103     
104     /**
105      * Constant identifying the job family identifier for the background auto-refresh job.
106      *
107      * @see IJobManager#join(Object, IProgressMonitor)
108      * @since 3.1
109      */

110     public static final Object JavaDoc FAMILY_AUTO_REFRESH = new Object JavaDoc();
111     
112     /**
113      * Constant identifying the job family identifier for a background build job. All clients
114      * that schedule background jobs for performing builds should include this job
115      * family in their implementation of <code>belongsTo</code>.
116      *
117      * @see IJobManager#join(Object, IProgressMonitor)
118      * @see Job#belongsTo(Object)
119      * @since 3.0
120      */

121     public static final Object JavaDoc FAMILY_MANUAL_BUILD = new Object JavaDoc();
122
123     /**
124      * Name of a preference indicating the encoding to use when reading text
125      * files in the workspace. The value is a string, and may
126      * be the default empty string, indicating that the file system encoding should
127      * be used instead. The file system encoding can be retrieved using
128      * <code>System.getProperty("file.encoding")</code>.
129      * There is also a convenience method <code>getEncoding</code> which returns
130      * the value of this preference, or the file system encoding if this
131      * preference is not set.
132      * <p>
133      * Note that there is no guarantee that the value is a supported encoding.
134      * Callers should be prepared to handle <code>UnsupportedEncodingException</code>
135      * where this encoding is used.
136      * </p>
137      *
138      * @see #getEncoding()
139      * @see java.io.UnsupportedEncodingException
140      */

141     public static final String JavaDoc PREF_ENCODING = "encoding"; //$NON-NLS-1$
142

143     /**
144      * Common prefix for workspace preference names.
145      * @since 2.1
146      */

147     private static final String JavaDoc PREF_DESCRIPTION_PREFIX = "description."; //$NON-NLS-1$
148

149     /**
150      * @deprecated Do not use.
151      * @since 3.0
152      */

153     public static final String JavaDoc PREF_MAX_NOTIFICATION_DELAY = "maxnotifydelay"; //$NON-NLS-1$
154

155     /**
156      * Name of a preference for configuring whether the workspace performs auto-
157      * builds.
158      *
159      * @see IWorkspaceDescription#isAutoBuilding()
160      * @see IWorkspaceDescription#setAutoBuilding(boolean)
161      * @since 2.1
162      */

163     public static final String JavaDoc PREF_AUTO_BUILDING = PREF_DESCRIPTION_PREFIX + "autobuilding"; //$NON-NLS-1$
164

165     /**
166      * Name of a preference for configuring the order projects in the workspace
167      * are built.
168      *
169      * @see IWorkspaceDescription#getBuildOrder()
170      * @see IWorkspaceDescription#setBuildOrder(String[])
171      * @since 2.1
172      */

173     public static final String JavaDoc PREF_BUILD_ORDER = PREF_DESCRIPTION_PREFIX + "buildorder"; //$NON-NLS-1$
174

175     /**
176      * Name of a preference for configuring whether to use the workspace's
177      * default order for building projects.
178      * @since 2.1
179      */

180     public static final String JavaDoc PREF_DEFAULT_BUILD_ORDER = PREF_DESCRIPTION_PREFIX + "defaultbuildorder"; //$NON-NLS-1$
181

182     /**
183      * Name of a preference for configuring the maximum number of times that the
184      * workspace should rebuild when builders affect projects that have already
185      * been built.
186      *
187      * @see IWorkspaceDescription#getMaxBuildIterations()
188      * @see IWorkspaceDescription#setMaxBuildIterations(int)
189      * @since 2.1
190      */

191     public static final String JavaDoc PREF_MAX_BUILD_ITERATIONS = PREF_DESCRIPTION_PREFIX + "maxbuilditerations"; //$NON-NLS-1$
192

193     /**
194      * Name of a preference for configuring the maximum number of milliseconds a
195      * file state should be kept in the local history
196      *
197      * @see IWorkspaceDescription#getFileStateLongevity()
198      * @see IWorkspaceDescription#setFileStateLongevity(long)
199      * @since 2.1
200      */

201     public static final String JavaDoc PREF_FILE_STATE_LONGEVITY = PREF_DESCRIPTION_PREFIX + "filestatelongevity"; //$NON-NLS-1$
202

203     /**
204      * Name of a preference for configuring the maximum permitted size of a file
205      * to be stored in the local history
206      *
207      * @see IWorkspaceDescription#getMaxFileStateSize()
208      * @see IWorkspaceDescription#setMaxFileStateSize(long)
209      * @since 2.1
210      */

211     public static final String JavaDoc PREF_MAX_FILE_STATE_SIZE = PREF_DESCRIPTION_PREFIX + "maxfilestatesize"; //$NON-NLS-1$
212

213     /**
214      * Name of a preference for configuring the maximum number of states per
215      * file that can be stored in the local history.
216      *
217      * @see IWorkspaceDescription#getMaxFileStates()
218      * @see IWorkspaceDescription#setMaxFileStates(int)
219      * @since 2.1
220      */

221     public static final String JavaDoc PREF_MAX_FILE_STATES = PREF_DESCRIPTION_PREFIX + "maxfilestates"; //$NON-NLS-1$
222
/**
223      * Name of a preference for configuring the amount of time in milliseconds
224      * between automatic workspace snapshots
225      *
226      * @see IWorkspaceDescription#getSnapshotInterval()
227      * @see IWorkspaceDescription#setSnapshotInterval(long)
228      * @since 2.1
229      */

230     public static final String JavaDoc PREF_SNAPSHOT_INTERVAL = PREF_DESCRIPTION_PREFIX + "snapshotinterval"; //$NON-NLS-1$
231

232     /**
233      * Name of a preference for turning off support for linked resources. When
234      * this preference is set to "true", attempting to create linked resources will fail.
235      * @since 2.1
236      */

237     public static final String JavaDoc PREF_DISABLE_LINKING = PREF_DESCRIPTION_PREFIX + "disableLinking";//$NON-NLS-1$
238

239     /**
240      * Name of a preference for configuring whether the workspace performs auto-
241      * refresh.
242      * @since 3.0
243      */

244     public static final String JavaDoc PREF_AUTO_REFRESH = "refresh.enabled"; //$NON-NLS-1$
245

246     /**
247      * The single instance of this plug-in runtime class.
248      */

249     private static ResourcesPlugin plugin;
250
251     /**
252      * The workspace managed by the single instance of this
253      * plug-in runtime class, or <code>null</code> is there is none.
254      */

255     private static Workspace workspace = null;
256
257     /**
258      * Constructs an instance of this plug-in runtime class.
259      * <p>
260      * An instance of this plug-in runtime class is automatically created
261      * when the facilities provided by the Resources plug-in are required.
262      * <b>Clients must never explicitly instantiate a plug-in runtime class.</b>
263      * </p>
264      */

265     public ResourcesPlugin() {
266         plugin = this;
267     }
268
269     /**
270      * Constructs a brand new workspace structure at the location in the local file system
271      * identified by the given path and returns a new workspace object.
272      *
273      * @exception CoreException if the workspace structure could not be constructed.
274      * Reasons include:
275      * <ul>
276      * <li> There is an existing workspace structure on at the given location
277      * in the local file system.
278      * <li> A file exists at the given location in the local file system.
279      * <li> A directory could not be created at the given location in the
280      * local file system.
281      * </ul>
282      */

283     private static void constructWorkspace() throws CoreException {
284         new LocalMetaArea().createMetaArea();
285     }
286
287     /**
288      * Returns the encoding to use when reading text files in the workspace.
289      * This is the value of the <code>PREF_ENCODING</code> preference, or the
290      * file system encoding (<code>System.getProperty("file.encoding")</code>)
291      * if the preference is not set.
292      * <p>
293      * Note that this method does not check whether the result is a supported
294      * encoding. Callers should be prepared to handle
295      * <code>UnsupportedEncodingException</code> where this encoding is used.
296      *
297      * @return the encoding to use when reading text files in the workspace
298      * @see java.io.UnsupportedEncodingException
299      */

300     public static String JavaDoc getEncoding() {
301         String JavaDoc enc = getPlugin().getPluginPreferences().getString(PREF_ENCODING);
302         if (enc == null || enc.length() == 0) {
303             enc = System.getProperty("file.encoding"); //$NON-NLS-1$
304
}
305         return enc;
306     }
307
308     /**
309      * Returns the Resources plug-in.
310      *
311      * @return the single instance of this plug-in runtime class
312      */

313     public static ResourcesPlugin getPlugin() {
314         return plugin;
315     }
316
317     /**
318      * Returns the workspace. The workspace is not accessible after the resources
319      * plug-in has shutdown.
320      *
321      * @return the workspace that was created by the single instance of this
322      * plug-in class.
323      */

324     public static IWorkspace getWorkspace() {
325         if (workspace == null)
326             throw new IllegalStateException JavaDoc(Messages.resources_workspaceClosed);
327         return workspace;
328     }
329
330     /**
331      * This implementation of the corresponding {@link BundleActivator} method
332      * closes the workspace without saving.
333      * @see BundleActivator#stop(BundleContext)
334      */

335     public void stop(BundleContext context) throws Exception JavaDoc {
336         super.stop(context);
337         if (workspace == null)
338             return;
339         // save the preferences for this plug-in
340
getPlugin().savePluginPreferences();
341         workspace.close(null);
342
343         // Forget workspace only if successfully closed, to
344
// make it easier to debug cases where close() is failing.
345
workspace = null;
346     }
347
348     /**
349      * This implementation of the corresponding {@link BundleActivator} method
350      * opens the workspace.
351      * @see BundleActivator#start(BundleContext)
352      */

353     public void start(BundleContext context) throws Exception JavaDoc {
354         super.start(context);
355         if (!new LocalMetaArea().hasSavedWorkspace()) {
356             constructWorkspace();
357         }
358         Workspace.DEBUG = ResourcesPlugin.getPlugin().isDebugging();
359         // Remember workspace before opening, to
360
// make it easier to debug cases where open() is failing.
361
workspace = new Workspace();
362         PlatformURLResourceConnection.startup(workspace.getRoot().getLocation());
363         IStatus result = workspace.open(null);
364         if (!result.isOK())
365             getLog().log(result);
366     }
367 }
368
Popular Tags