KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > refresh > RefreshManager


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.refresh;
12
13 import org.eclipse.core.internal.resources.IManager;
14 import org.eclipse.core.internal.utils.Policy;
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.resources.refresh.IRefreshMonitor;
17 import org.eclipse.core.resources.refresh.IRefreshResult;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.Preferences;
20 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
21
22 /**
23  * Manages auto-refresh functionality, including maintaining the active
24  * set of monitors and controlling the job that performs periodic refreshes
25  * on out of sync resources.
26  *
27  * @since 3.0
28  */

29 public class RefreshManager implements IRefreshResult, IManager, Preferences.IPropertyChangeListener {
30     public static boolean DEBUG = Policy.DEBUG_AUTO_REFRESH;
31     public static final String JavaDoc DEBUG_PREFIX = "Auto-refresh: "; //$NON-NLS-1$
32
MonitorManager monitors;
33     private RefreshJob refreshJob;
34
35     /**
36      * The workspace.
37      */

38     private IWorkspace workspace;
39
40     public RefreshManager(IWorkspace workspace) {
41         this.workspace = workspace;
42     }
43
44     /*
45      * Starts or stops auto-refresh depending on the auto-refresh preference.
46      */

47     protected void manageAutoRefresh(boolean enabled) {
48         //do nothing if we have already shutdown
49
if (refreshJob == null)
50             return;
51         if (enabled) {
52             refreshJob.start();
53             monitors.start();
54         } else {
55             refreshJob.stop();
56             monitors.stop();
57         }
58     }
59
60     /* (non-Javadoc)
61      * @see org.eclipse.core.resources.refresh.IRefreshResult#monitorFailed(org.eclipse.core.resources.refresh.IRefreshMonitor, org.eclipse.core.resources.IResource)
62      */

63     public void monitorFailed(IRefreshMonitor monitor, IResource resource) {
64         monitors.monitorFailed(monitor, resource);
65     }
66
67     /**
68      * Checks for changes to the PREF_AUTO_UPDATE property.
69      * @see Preferences.IPropertyChangeListener#propertyChange(PropertyChangeEvent)
70      */

71     public void propertyChange(PropertyChangeEvent event) {
72         String JavaDoc property = event.getProperty();
73         if (ResourcesPlugin.PREF_AUTO_REFRESH.equals(property)) {
74             Preferences preferences = ResourcesPlugin.getPlugin().getPluginPreferences();
75             boolean autoRefresh = preferences.getBoolean(ResourcesPlugin.PREF_AUTO_REFRESH);
76             manageAutoRefresh(autoRefresh);
77         }
78     }
79
80     /* (non-Javadoc)
81      * @see org.eclipse.core.resources.refresh.IRefreshResult#refresh(org.eclipse.core.resources.IResource)
82      */

83     public void refresh(IResource resource) {
84         //do nothing if we have already shutdown
85
if (refreshJob != null)
86             refreshJob.refresh(resource);
87     }
88
89     /**
90      * Shuts down the refresh manager. This only happens when
91      * the resources plugin is going away.
92      */

93     public void shutdown(IProgressMonitor monitor) {
94         ResourcesPlugin.getPlugin().getPluginPreferences().removePropertyChangeListener(this);
95         if (monitors != null) {
96             monitors.stop();
97             monitors = null;
98         }
99         if (refreshJob != null) {
100             refreshJob.stop();
101             refreshJob = null;
102         }
103     }
104
105     /**
106      * Initializes the refresh manager. This does a minimal amount of work
107      * if auto-refresh is turned off.
108      */

109     public void startup(IProgressMonitor monitor) {
110         Preferences preferences = ResourcesPlugin.getPlugin().getPluginPreferences();
111         preferences.addPropertyChangeListener(this);
112
113         refreshJob = new RefreshJob();
114         monitors = new MonitorManager(workspace, this);
115         boolean autoRefresh = preferences.getBoolean(ResourcesPlugin.PREF_AUTO_REFRESH);
116         if (autoRefresh)
117             manageAutoRefresh(autoRefresh);
118     }
119 }
120
Popular Tags