Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 73d1fc198e14103452e212d92a0c3cc515a5d393 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*******************************************************************************
 * Copyright (c) 2008 Symbian Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Andrew Ferguson (Symbian) - Initial implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.text.doctools;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.osgi.service.prefs.Preferences;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.text.doctools.IDocCommentOwner;
import org.eclipse.cdt.ui.text.doctools.IDocCommentOwnershipListener;
import org.eclipse.cdt.ui.text.doctools.IDocCommentViewerConfiguration;

/**
 * This class manages which IDocCommentOwner's are available in the run-time, and how they map to resources
 * in projects.
 * @since 5.0
 */
public class DocCommentOwnerManager {
	/** Constants for attributes/elements from the DocCommentOwner extension point */
	private static final String ELEMENT_OWNER = "owner"; //$NON-NLS-1$
	private static final String ATTRKEY_OWNER_ID = "id"; //$NON-NLS-1$
	private static final String ATTRKEY_OWNER_NAME = "name"; //$NON-NLS-1$
	private static final String ATTRKEY_OWNER_SINGLELINE = "singleline"; //$NON-NLS-1$
	private static final String ATTRKEY_OWNER_MULTILINE = "multiline"; //$NON-NLS-1$

	private static final String QUALIFIER= CCorePlugin.PLUGIN_ID;
	private static final String WORKSPACE_DOC_TOOL_NODE= "doctool"; //$NON-NLS-1$
	private static final String PREFKEY_WORKSPACE_DEFAULT= "workspace.default"; //$NON-NLS-1$
	
	private static DocCommentOwnerManager singleton;

	public static DocCommentOwnerManager getInstance() {
		return singleton==null ? singleton= new DocCommentOwnerManager() : singleton;
	}

	private Map<String, IDocCommentOwner> fOwners;
	private IDocCommentOwner fWorkspaceOwner;
	private Map<IProject, ProjectMap> prj2map= new HashMap<IProject, ProjectMap>();
	private static List<IDocCommentOwnershipListener> fListeners;

	private DocCommentOwnerManager() {
		fOwners= getCommentOwnerExtensions();
		fListeners= new ArrayList<IDocCommentOwnershipListener>();

		Preferences prefs= new InstanceScope().getNode(QUALIFIER).node(WORKSPACE_DOC_TOOL_NODE);
		String id= prefs.get(PREFKEY_WORKSPACE_DEFAULT, NullDocCommentOwner.INSTANCE.getID());
		fWorkspaceOwner= getOwner(id);
		if(fWorkspaceOwner == null) {
			// this could occur if a plug-in is no longer available
			fWorkspaceOwner= NullDocCommentOwner.INSTANCE;
		}
	}

	/**
	 * @param project a non-null project
	 * @return whether the specified project defines any documentation owner association 
	 */
	public boolean projectDefinesOwnership(IProject project) {
		return !getProjectMap(project).isEmpty();
	}

	/**
	 * @param newOwner the non-null doc-comment owner
	 */
	public void setWorkspaceCommentOwner(IDocCommentOwner newOwner) {
		if(newOwner == null)
			throw new IllegalArgumentException();
		
		if(!fWorkspaceOwner.getID().equals(newOwner.getID())) {
			IDocCommentOwner oldOwner= fWorkspaceOwner;
			fWorkspaceOwner= newOwner;

			Preferences prefs= new InstanceScope().getNode(QUALIFIER).node(WORKSPACE_DOC_TOOL_NODE);
			prefs.put(PREFKEY_WORKSPACE_DEFAULT, newOwner.getID());
			
			fireWorkspaceOwnershipChanged(oldOwner, fWorkspaceOwner);
		}
	}

	/**
	 * @return the doc comment owner associated with the workspace. If non
	 * is set, the {@link NullDocCommentOwner} is returned.
	 */
	public IDocCommentOwner getWorkspaceCommentOwner() {
		return fWorkspaceOwner;
	}

	/**
	 * 
	 * @param resource May be null.
	 * @return a non-null IDocCommentOwner. If the resource was null, the {@link NullDocCommentOwner} is returned.
	 */
	public IDocCommentOwner getCommentOwner(IResource resource) {
		if(resource==null)
			return NullDocCommentOwner.INSTANCE;

		if(ResourcesPlugin.getWorkspace().getRoot().equals(resource))
			return getWorkspaceCommentOwner();

		ProjectMap pm= getProjectMap(resource);
		String ownerID= pm.getOwnerID(resource);
		IDocCommentOwner result= getOwner(ownerID);
		return result == null ? fWorkspaceOwner : result;
	}

	/**
	 * @param id
	 * @return the {@link IDocCommentOwner} with the specified id, or null
	 */
	public IDocCommentOwner getOwner(String id) {
		if(NullDocCommentOwner.INSTANCE.getID().equals(id)) {
			return NullDocCommentOwner.INSTANCE;
		}
		return fOwners.get(id);
	}

	/**
	 * @param resource a non-null resource to map a comment owner to
	 * @param newOwner the new owner to assign, or null to inherit the parent's mapping
	 * @param removeSubMappings if the resource is an {@link IContainer}, then remove any mappings
	 * children have. <em>This is currently unimplemented.</em>
	 */
	/*
	 * Note - this implementation currently ignores removeSubMappings.
	 */
	public void setCommentOwner(IResource resource, IDocCommentOwner newOwner, boolean removeSubMappings) {
		Assert.isNotNull(resource);

		if(ResourcesPlugin.getWorkspace().getRoot().equals(resource)) {
			setWorkspaceCommentOwner(newOwner);
			return;
		}

		ProjectMap pm= getProjectMap(resource);
		IDocCommentOwner oldOwner= getCommentOwner(resource);
		pm.setCommentOwner(resource, newOwner);
		
		IDocCommentOwner newLogicalOwner= getCommentOwner(resource);
		if(!newLogicalOwner.getID().equals(oldOwner.getID())) { 
			fireOwnershipChanged(resource, removeSubMappings, oldOwner, newLogicalOwner);
		}
	}

	/**
	 * @return any comment owners registered against the extension point. This does not include
	 * the null comment processor.
	 */
	public IDocCommentOwner[] getRegisteredOwners() {
		return fOwners.values().toArray(new IDocCommentOwner[fOwners.values().size()]);
	}


	/**
	 * @param listener registers a listener for doc-comment ownership events
	 */
	public void addListener(IDocCommentOwnershipListener listener) {
		if(!fListeners.contains(listener)) {
			fListeners.add(listener);
		}
	}

	/**
	 * @param listener removes a listener from those registered for doc-comment ownership events
	 */
	public void removeListener(IDocCommentOwnershipListener listener) {
		fListeners.remove(listener);
	}

	/*
	 * Utilities
	 */

	/**
	 * @param resource a non-null resource
	 * @return the cached (or newly obtained) ProjectMap for the resources
	 * associated project.
	 */
	private ProjectMap getProjectMap(IResource resource) {
		Assert.isNotNull(resource);
		IProject project= resource.getProject();

		if(!prj2map.containsKey(project)) {
			prj2map.put(project, new ProjectMap(project));
		}

		return prj2map.get(project);
	}

	/**
	 * @return a map of ID to {@link IDocCommentOwner} for comment owners registered
	 * via the DocCommentOwner extension point
	 */
	private static Map<String, IDocCommentOwner> getCommentOwnerExtensions() {
		Map<String, IDocCommentOwner> result= new HashMap<String, IDocCommentOwner>();

		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint indexProviders = registry.getExtensionPoint(CUIPlugin.ID_COMMENT_OWNER);
		IExtension[] extensions = indexProviders.getExtensions();
		for(int i=0; i<extensions.length; i++) {
			IExtension extension = extensions[i];
			try {
				IConfigurationElement[] ce = extension.getConfigurationElements();
				for(int j=0; j<ce.length; j++) {
					if(ce[j].getName().equals(ELEMENT_OWNER)) { 
						IDocCommentViewerConfiguration multi = (IDocCommentViewerConfiguration) ce[j].createExecutableExtension(ATTRKEY_OWNER_MULTILINE);
						IDocCommentViewerConfiguration single = (IDocCommentViewerConfiguration) ce[j].createExecutableExtension(ATTRKEY_OWNER_SINGLELINE);
						String id= ce[j].getAttribute(ATTRKEY_OWNER_ID);
						String name= ce[j].getAttribute(ATTRKEY_OWNER_NAME);
						if(result.put(id, new DocCommentOwner(id, name, multi, single))!=null) {
							String msg= MessageFormat.format(Messages.DocCommentOwnerManager_DuplicateMapping0, id);
							CCorePlugin.log(new Status(IStatus.WARNING, CUIPlugin.PLUGIN_ID, msg));
						}
					}
				}
			} catch(CoreException ce) {
				CCorePlugin.log(ce);
			}
		}

		return result;
	}

	private void fireOwnershipChanged(IResource resource, boolean submappingsRemoved, IDocCommentOwner oldOwner, IDocCommentOwner newOwner) {
		for(Iterator i= fListeners.iterator(); i.hasNext();) {
			((IDocCommentOwnershipListener)i.next()).ownershipChanged(resource, submappingsRemoved, oldOwner, newOwner);
		}
	}

	private void fireWorkspaceOwnershipChanged(IDocCommentOwner oldOwner, IDocCommentOwner newOwner) {
		for(Iterator i= fListeners.iterator(); i.hasNext();) {
			((IDocCommentOwnershipListener)i.next()).workspaceOwnershipChanged(oldOwner, newOwner);
		}
	}
}

Back to the top