Skip to main content
summaryrefslogtreecommitdiffstats
blob: 862fb04e732b9e4341c42ca6218a24e9f18cc3e8 (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
/*******************************************************************************
 *  Copyright (c) 2010  Oracle. 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: 
 *  	Oracle - initial API and implementation
 *******************************************************************************/
package org.eclipse.jpt.common.core.internal.resource;

import static org.eclipse.jst.common.project.facet.core.internal.FacetedProjectFrameworkJavaPlugin.log;
import org.eclipse.core.expressions.EvaluationContext;
import org.eclipse.core.expressions.EvaluationResult;
import org.eclipse.core.expressions.Expression;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jpt.common.core.JptCommonCorePlugin;
import org.eclipse.jpt.common.core.internal.XPointUtil;
import org.eclipse.jpt.common.core.resource.ResourceLocator;
import org.eclipse.jpt.common.utility.internal.StringTools;

public class ResourceLocatorConfig 
		implements Comparable<ResourceLocatorConfig> {
	
	public static final String PROJECT_ENABLEMENT_VARIABLE = "project";  //$NON-NLS-1$
	
	private String id;
	private String pluginId;
	private String className;
	private Priority priority;
	private Expression enablementCondition;
	
	
	ResourceLocatorConfig() {
		super();
	}
	
	
	public String getId() {
		return this.id;
	}
	
	void setId(String id) {
		this.id = id;
	}
	
	public String getPluginId() {
		return this.pluginId;
	}
	
	void setPluginId(String pluginId) {
		this.pluginId = pluginId;
	}
	
	public String getClassName() {
		return this.className;
	}
	
	void setClassName(String className) {
		this.className = className;
	}
	
	public Priority getPriority() {
		return this.priority;
	}
	
	void setPriority(Priority priority) {
		this.priority = priority;
	}
	
	public Expression getEnablementCondition() {
		return this.enablementCondition;
	}
	
	void setEnablementCondition(Expression enablementCondition) {
		this.enablementCondition = enablementCondition;
	}
	
	public ResourceLocator getResourceLocator() {
		return XPointUtil.instantiate(
				JptCommonCorePlugin.PLUGIN_ID, ResourceLocatorManager.QUALIFIED_EXTENSION_POINT_ID, 
				this.className, ResourceLocator.class);
	}
	
	public boolean isEnabledFor(IProject project) {
		EvaluationContext evalContext = new EvaluationContext(null, project);
		evalContext.setAllowPluginActivation(true);
		evalContext.addVariable(PROJECT_ENABLEMENT_VARIABLE, project);
		
		if (this.enablementCondition != null) {
			try {
				EvaluationResult evalResult = this.enablementCondition.evaluate(evalContext);
				
				if (evalResult == EvaluationResult.FALSE) {
					return false;
				}
			}
			catch (CoreException e) {
				log(e);
			}
		}
		
		return true;
	}
	
	public int compareTo(ResourceLocatorConfig other) {
		return Priority.compare(this.priority, other.priority);
	}
	
	
	public static enum Priority {
		
		/* The lowest priority for a resource locator */
		LOWEST(6, "lowest"), //$NON-NLS-1$
		
		/* The second lowest priority for a resource locator */
		LOWER(5, "lower"), //$NON-NLS-1$
		
		/* The third lowest priority for a resource locator */
		LOW(4, "low"), //$NON-NLS-1$
		
		/* The default priority for a resource locator */
		NORMAL(3, "normal"), //$NON-NLS-1$
		
		/* The third highest priority for a resource locator */
		HIGH(2, "high"), //$NON-NLS-1$
		
		/* The second highest priority for a resource locator */
		HIGHER(1, "higher"), //$NON-NLS-1$
		
		/* The highest priority for a resource locator */
		HIGHEST(0, "highest"); //$NON-NLS-1$
		
		
		public static int compare(Priority priority1, Priority priority2) {
			return priority1.value.compareTo(priority2.value);
		}
		
		public static Priority get(String literal) {
			if (literal == null) {
				return NORMAL;
			}
			for (Priority priority : values()) {
				if (StringTools.stringsAreEqual(literal, priority.literal)) {
					return priority;
				}
			}
			return null;
		}
		
		
		private Integer value;
		private String literal;
		
		
		private Priority(int value, String literal) {
			this.value = Integer.valueOf(value);
			this.literal = literal;
		}
		
		
		@Override
		public String toString() {
			return this.literal;
		}
	}
}

Back to the top