Skip to main content
summaryrefslogtreecommitdiffstats
blob: 59a6f3d055d713feada18ea25a1e90e5d135435c (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
/*******************************************************************************
 * Copyright (c) 2003, 2004 IBM Corporation 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:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 * Created on Mar 25, 2004
 */
package org.eclipse.jst.common.internal.annotations.controller;

import java.util.Iterator;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.WeakHashMap;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.jem.util.RegistryReader;
import org.eclipse.jem.util.logger.proxy.Logger;
import org.eclipse.jst.common.internal.annotations.registry.AnnotationsControllerResources;
import org.eclipse.wst.common.frameworks.internal.enablement.EnablementIdentifier;
import org.eclipse.wst.common.frameworks.internal.enablement.EnablementIdentifierEvent;
import org.eclipse.wst.common.frameworks.internal.enablement.EnablementManager;
import org.eclipse.wst.common.frameworks.internal.enablement.IEnablementIdentifier;
import org.eclipse.wst.common.frameworks.internal.enablement.IEnablementIdentifierListener;
import org.eclipse.wst.common.frameworks.internal.enablement.Identifiable;
import org.eclipse.wst.common.frameworks.internal.enablement.IdentifiableComparator;
import org.eclipse.wst.common.internal.emf.utilities.Assert;


/**
 * AnnotationsControllerRegistry for reading annotations controller extensions
 */
public class AnnotationsControllerManager extends RegistryReader implements IEnablementIdentifierListener {

	public static final AnnotationsControllerManager INSTANCE = new AnnotationsControllerManager();
	
	static {
		INSTANCE.readRegistry();
	}

	private SortedSet descriptors;

	private Map annotationsControllers;

	public static class Descriptor implements Identifiable {

		public static final String ANNOTATIONS_CONTROLLER = "annotationsController"; //$NON-NLS-1$

		public static final String ATT_ID = "id"; //$NON-NLS-1$

		public static final String CLASS = "class"; //$NON-NLS-1$
		
		public static final String BUILDER_ID = "builderID"; //$NON-NLS-1$

		private final IConfigurationElement configElement;
		private final String ID;
		private String builderID;
		private final int loadOrder;
		private static int loadOrderCounter = 0;

		public Descriptor(IConfigurationElement aConfigElement) {
			super();
			Assert.isLegal(ANNOTATIONS_CONTROLLER.equals(aConfigElement.getName()), AnnotationsControllerResources.AnnotationsControllerManager_ERROR_0);
			configElement = aConfigElement;
			ID = configElement.getAttribute(ATT_ID);
			builderID = configElement.getAttribute(BUILDER_ID);
			loadOrder = loadOrderCounter++;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.wst.common.frameworks.internal.enablement.Identifiable#getID()
		 */
		public String getID() {
			return ID;
		}
		
		public String getBuilderID() {
			return builderID;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.wst.common.frameworks.internal.enablement.Identifiable#getLoadOrder()
		 */
		public int getLoadOrder() {
			return loadOrder;
		}

		public AnnotationsController createInstance() {
			AnnotationsController instance = null;
			try {
				instance = (AnnotationsController) configElement.createExecutableExtension(CLASS);
			} catch (CoreException e) {
				Logger.getLogger().logError(e);
			}
			return instance;
		}
	}

	/**
	 * Default constructor
	 */
	public AnnotationsControllerManager() {
		super("org.eclipse.jst.common.annotations.controller", "annotationsController"); //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * read extension element
	 */
	public boolean readElement(IConfigurationElement element) {
		if (!element.getName().equals(Descriptor.ANNOTATIONS_CONTROLLER))
			return false;
		addAnnotationController(new Descriptor(element));
		return true;
	}

	/**
	 * @param descriptor
	 */
	protected void addAnnotationController(Descriptor descriptor) {
		EnablementManager.INSTANCE.getIdentifier(descriptor.getID(), null).addIdentifierListener(this);
		getDescriptors().add(descriptor);
	}

	/**
	 * @return Returns the annotationControllers.
	 */
	protected SortedSet getDescriptors() {
		if (descriptors == null)
			descriptors = new TreeSet(IdentifiableComparator.getInstance());
		return descriptors;
	}

	public Descriptor getDescriptor(IProject project) {
		for (Iterator iter = getDescriptors().iterator(); iter.hasNext();) {
			Descriptor descriptor = (Descriptor) iter.next();
			IEnablementIdentifier identifier = EnablementManager.INSTANCE.getIdentifier(descriptor.getID(), project);
			if (identifier.isEnabled())
				return descriptor;
		}
		return null;
	}

	/**
	 * Determine if any annotations are supported
	 */
	public boolean isAnyAnnotationsSupported() {
		return getDescriptors().size() > 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.common.frameworks.internal.enablement.IEnablementIdentifierListener#identifierChanged(org.eclipse.wst.common.frameworks.internal.enablement.EnablementIdentifierEvent)
	 */
	public void identifierChanged(EnablementIdentifierEvent identifierEvent) {
		IProject project = ((EnablementIdentifier) identifierEvent.getIdentifier()).getProject();
		getAnnotationsControllers().remove(project);
	}

	/**
	 * Return the annotations controller for the specified project
	 */
	public AnnotationsController getAnnotationsController(IProject project) {
		AnnotationsController controller = (AnnotationsController) getAnnotationsControllers().get(project);
		if (controller == null) {
			if (!hasAnnotationsBuilder(project))
				return null;
			Descriptor descriptor = getDescriptor(project);
			if (descriptor != null)
				getAnnotationsControllers().put(project, (controller = descriptor.createInstance()));
		}

		return controller;
	}

	/**
	 * @return Returns the annotationControllers.
	 */
	public Map getAnnotationsControllers() {
		if (annotationsControllers == null)
			annotationsControllers = new WeakHashMap();
		return annotationsControllers;
	}
	
	public boolean hasAnnotationsBuilder(IProject project) {
		Descriptor annotationsDescriptor = getDescriptor(project);
		if (annotationsDescriptor==null)
			return false;
		return hasBuilder(project, annotationsDescriptor.getBuilderID());
	}

	public boolean hasBuilder(IProject project, String builderName) {
		try {
			ICommand[] builders = project.getDescription().getBuildSpec();
			for (int i = 0; i < builders.length; i++) {
				ICommand builder = builders[i];
				if (builder != null) {
					if (builder.getBuilderName().equals(builderName))
						return true;
				}
			}
		} catch (Exception e) {
			// Do nothing
		}
		return false;
	}
}

Back to the top