Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 819a30a6ac8f58d5040ec354de8fc5b73e0acbae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*******************************************************************************
 * Copyright (c) 2000, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.examples.pessimistic;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The plugin for the <code>PessimisticFilesystemProvider</code>.
 */
public class PessimisticFilesystemProviderPlugin extends AbstractUIPlugin {
	/*
	 * Singleton instance.
	 */
	private static PessimisticFilesystemProviderPlugin instance;
	/*
	 * The resource change listener which notifies the provider of
	 * added and deleted files.
	 */
	private ResourceChangeListener fListener;
	/*
	 * The provider listeners
	 */
	private List<IResourceStateListener> fListeners;

	/**
	 * The plugin identifier
	 */
	public static final String PLUGIN_ID = "org.eclipse.team.examples.pessimistic";
	/**
	 * The nature identifier.
	 */
	public static final String NATURE_ID = PLUGIN_ID + ".pessimisticnature";

	/**
	 * Constructor required by plugin lifecycle.
	 */
	public PessimisticFilesystemProviderPlugin() {
		super();
		instance = this;
		fListeners = new ArrayList<>(1);
		//setDebugging(true);
	}

	/**
	 * Answers the singleton instance of this plugin.
	 */
	public static PessimisticFilesystemProviderPlugin getInstance() {
		return instance;
	}

	/**
	 * Initializes the default preferences for this plugin.
	 */
	protected void initializeDefaultPreferences() {
		IPreferenceStore store = getPreferenceStore();

		store.setDefault(
				IPessimisticFilesystemConstants.PREF_CHECKED_IN_FILES_EDITED,
				IPessimisticFilesystemConstants.OPTION_PROMPT);
		store.setDefault(
				IPessimisticFilesystemConstants.PREF_CHECKED_IN_FILES_EDITED_NOPROMPT,
				IPessimisticFilesystemConstants.OPTION_AUTOMATIC);
		store.setDefault(
				IPessimisticFilesystemConstants.PREF_CHECKED_IN_FILES_SAVED,
				IPessimisticFilesystemConstants.OPTION_DO_NOTHING);
		store.setDefault(
				IPessimisticFilesystemConstants.PREF_ADD_TO_CONTROL,
				IPessimisticFilesystemConstants.OPTION_PROMPT);
		store.setDefault(IPessimisticFilesystemConstants.PREF_FAIL_VALIDATE_EDIT, false);
		store.setDefault(IPessimisticFilesystemConstants.PREF_TOUCH_DURING_VALIDATE_EDIT, true);
	}

	/**
	 * Convenience method for logging errors.
	 */
	public void logError(Throwable exception, String message) {
		String pluginId= getBundle().getSymbolicName();
		Status status= new Status(Status.ERROR, pluginId, Status.OK, message, exception);
		getLog().log(status);
		if (isDebugging()) {
			System.out.println(message);
			exception.printStackTrace();
		}
	}

	/**
	 * Starts the resource listener.
	 */
	@Override
	public void start(BundleContext context) throws Exception {
		fListener= new ResourceChangeListener();
		fListener.startup();
		initializeDefaultPreferences();
		super.start(context);
	}

	/**
	 * Stops the resource listener.
	 */
	@Override
	public void stop(BundleContext context) throws Exception {
		fListener.shutdown();
		fListener= null;
		super.stop(context);
	}

	/**
	 * Notifies the registered <code>IResourceStateListener</code> objects
	 * that the repository state for the resources has changed.
	 *
	 * @param resources	Collection of resources that have changed.
	 */
	public void fireResourcesChanged(IResource[] resources) {
		if (resources == null || resources.length == 0 || fListeners.isEmpty())
			return;
		for (Object element : fListeners) {
			IResourceStateListener listener= (IResourceStateListener) element;
			listener.stateChanged(resources);
		}
	}

	/**
	 * Adds the listener to the list of listeners that are notified when
	 * the repository state of resources change.
	 *
	 * @param listener
	 */
	public void addProviderListener(IResourceStateListener listener) {
		if (fListeners.contains(listener))
			return;
		fListeners.add(listener);
	}


	/**
	 * Removes the listener from the list of listeners that are notified when
	 * the repository state of resources change.
	 *
	 * @param listener
	 */
	public void removeProviderListener(IResourceStateListener listener) {
		fListeners.remove(listener);
	}
}

Back to the top