blob: f00c7f11d1544e788ac87ad2dca85a93ad31b0a9 [file] [log] [blame]
rsrinivasancf824322008-05-02 19:51:19 +00001/*******************************************************************************
2 * Copyright (c) 2001, 2008 Oracle 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 * Oracle Corporation - initial API and implementation
10 *******************************************************************************/
cbateman123ece32008-03-25 19:46:06 +000011package org.eclipse.jst.jsf.validation.internal;
12
13import org.eclipse.core.runtime.preferences.IScopeContext;
14import org.eclipse.emf.common.util.Diagnostic;
15import org.eclipse.jface.preference.IPreferenceStore;
16import org.eclipse.jst.jsf.common.internal.types.TypeComparatorDiagnosticFactory;
17import org.eclipse.jst.jsf.common.internal.types.TypeComparatorPreferences;
18import org.eclipse.jst.jsf.core.internal.IJSFPreferenceModel;
19import org.eclipse.jst.jsf.core.internal.JSFCorePlugin;
20
21/**
22 * Type comparator preferences for JSF.
23 *
24 * @author cbateman
25 *
26 */
27public class JSFTypeComparatorPreferences extends TypeComparatorPreferences
28 implements IJSFPreferenceModel
29{
30
31 private int[] _severities;
32
33 /**
34 * Loads the object from the preference store provided
35 *
36 * @param prefStore
37 */
38 public void load(IPreferenceStore prefStore)
39 {
40 loadSeverities(prefStore);
41 }
42
43 private void loadSeverities(final IPreferenceStore prefStore)
44 {
45 final int severities[] = getSeverities();
46
47 for (int i = 0; i < TypeComparatorDiagnosticFactory.NUM_IDS; i++)
48 {
49 final String key = getKeyById(i);
50
51 if (!prefStore.contains(key))
52 {
53 final int diagSeverity = getDefaultSeverity(i);
54 final Severity severity = mapDiagToSeverity(diagSeverity);
55
56 prefStore.setDefault(key, severity.toString());
57 }
58 final String storedSeverity = prefStore.getString(key);
59 severities[i] = mapSeverityToDiag(storedSeverity);
60 }
61 }
62
63 /**
64 * Copies the object into the preference store but DOES NOT SAVE IT
65 *
66 * @param prefStore
67 */
68 public void commit(IPreferenceStore prefStore)
69 {
70 commitSeverities(prefStore);
71 }
72
73 private void commitSeverities(final IPreferenceStore prefStore)
74 {
75 final int severities[] = getSeverities();
76
77 for (int i = 0; i < severities.length; i++)
78 {
79 final String key = getKeyById(i);
80 prefStore
81 .setValue(key, mapDiagToSeverity(severities[i]).toString());
82 }
83 }
84
85 /**
86 * Reverts the model to it's defaults. Does not commit to pref store.
87 */
88 public void setDefaults()
89 {
90 setProblemSeverityDefaults();
91 }
92
93 private void setProblemSeverityDefaults()
94 {
95 final int[] severities = getSeverities();
96
97 for (int i = 0; i < TypeComparatorDiagnosticFactory.NUM_IDS; i++)
98 {
99 severities[i] = getDefaultSeverity(i);
100 }
101 }
102
103 public Object getValueByKey(IScopeContext context, String key)
104 {
105 try
106 {
107 final Severity severity = getSeverity(key);
108 return severity.toString();
109 }
110 catch (IllegalArgumentException e)
111 {
112 // getIdByKey will throw this exception if key is not a valid
113 // severity key. Ignore the exception here and fall-through
114 }
115
116 return null; // not found
117 }
118
119 /*
120 * (non-Javadoc)
121 *
122 * @see org.eclipse.jst.jsf.core.internal.IJSFPreferenceModel#getStoredValueByKey(org.eclipse.core.runtime.preferences.IScopeContext,
123 * java.lang.String)
124 */
125 public Object getStoredValueByKey(IScopeContext context, String key)
126 {
127 try
128 {
gkesslercfc53082008-11-18 22:25:04 +0000129 return context.getNode("org.eclipse.jst.jsf.core").get( //$NON-NLS-1$
cbateman123ece32008-03-25 19:46:06 +0000130 key,
131 mapDiagToSeverity(getDefaultSeverity(getIdByKey(key)))
132 .toString());
133 }
134 catch (IllegalArgumentException e)
135 {
136 // getIdByKey will throw this exception if key is not a valid
137 // severity key. Ignore the exception here and fall-through
138 }
139
140 return null; // not found
141 }
142
143 /*
144 * (non-Javadoc)
145 *
146 * @see org.eclipse.jst.jsf.core.internal.IJSFPreferenceModel#setValueByKey(org.eclipse.core.runtime.preferences.IScopeContext,
147 * java.lang.String, java.lang.Object)
148 */
149 public Object setValueByKey(IScopeContext context, String key, Object value)
150 {
151 final Severity oldValue = getSeverity(key);
152 setSeverity(key, (Severity) value);
153 return oldValue;
154 }
155
156 /**
157 * @param key
158 * @return the severity
159 */
160 public Severity getSeverity(final String key)
161 {
162 final int severityDiag = _severities[getIdByKey(key)];
163 final Severity severity = mapDiagToSeverity(severityDiag);
164 return severity;
165 }
166
167 /**
168 * @param key
169 * @param severity
170 */
171 public void setSeverity(final String key, final Severity severity)
172 {
173 final int newSeverityDiag = mapSeverityToDiag(severity.toString());
174 final int diagId = getIdByKey(key);
175 _severities[diagId] = newSeverityDiag;
176 }
177
178 /**
179 * @param diagnosticId
180 * @return the severity as configured for diagnosticId. The value is
181 * relative to the Diagnostic class severity scheme
182 */
183 public final int getDiagnosticSeverity(final int diagnosticId)
184 {
185 return getSeverities()[diagnosticId];
186 }
187
188 private int[] getSeverities()
189 {
190 if (_severities == null)
191 {
192 _severities = new int[TypeComparatorDiagnosticFactory.NUM_IDS];
193 }
194
195 return _severities;
196 }
197
198 /**
199 * @param diagSeverity
200 * @return a Severity preference value for a diagnostic severity
201 */
202 public static Severity mapDiagToSeverity(int diagSeverity)
203 {
204 switch (diagSeverity)
205 {
206 case Diagnostic.ERROR:
207 return Severity.ERROR;
208 case Diagnostic.WARNING:
209 return Severity.WARNING;
210 default:
211 return Severity.IGNORE;
212 }
213 }
214
215 /**
216 * @param severity
217 * @return a Diagnostic severity level for a severity pref string
218 */
219 public static int mapSeverityToDiag(String severity)
220 {
gkesslercfc53082008-11-18 22:25:04 +0000221 if ("error".equals(severity)) //$NON-NLS-1$
cbateman123ece32008-03-25 19:46:06 +0000222 {
223 return Diagnostic.ERROR;
224 }
gkesslercfc53082008-11-18 22:25:04 +0000225 else if ("warning".equals(severity)) //$NON-NLS-1$
cbateman123ece32008-03-25 19:46:06 +0000226 {
227 return Diagnostic.WARNING;
228 }
gkesslercfc53082008-11-18 22:25:04 +0000229 else if ("ignore".equals(severity)) //$NON-NLS-1$
cbateman123ece32008-03-25 19:46:06 +0000230 {
231 return Diagnostic.OK;
232 }
233 else
234 {
gkesslercfc53082008-11-18 22:25:04 +0000235 throw new IllegalArgumentException("Invalid enum name: " + severity); //$NON-NLS-1$
cbateman123ece32008-03-25 19:46:06 +0000236 }
237 }
238
239 /**
240 * @param diagnosticId
241 * @return the preference key for the corresponding diagnosticId in the el
242 * DiagnosticFactory
243 */
244 public static String getKeyById(final int diagnosticId)
245 {
246 switch (diagnosticId)
247 {
248 case TypeComparatorDiagnosticFactory.INCOMPATIBLE_METHOD_TYPES_ID:
249 return INCOMPATIBLE_METHOD_TYPES;
250 case TypeComparatorDiagnosticFactory.INCOMPATIBLE_TYPES_ID:
251 return INCOMPATIBLE_TYPES;
252 case TypeComparatorDiagnosticFactory.METHOD_EXPRESSION_EXPECTED_ID:
253 return METHOD_EXPRESSION_EXPECTED;
254 case TypeComparatorDiagnosticFactory.PROPERTY_NOT_READABLE_ID:
255 return PROPERTY_NOT_READABLE;
256 case TypeComparatorDiagnosticFactory.PROPERTY_NOT_WRITABLE_ID:
257 return PROPERTY_NOT_WRITABLE;
258 case TypeComparatorDiagnosticFactory.VALUE_EXPRESSION_EXPECTED_ID:
259 return VALUE_EXPRESSION_EXPECTED;
260 default:
gkesslercfc53082008-11-18 22:25:04 +0000261 throw new IllegalArgumentException("Diagnostic Id: " //$NON-NLS-1$
262 + diagnosticId + " is out of range"); //$NON-NLS-1$
cbateman123ece32008-03-25 19:46:06 +0000263 }
264 }
265
266 /**
267 * @param key
268 * @return the preference key for the corresponding diagnosticId in the el
269 * DiagnosticFactory
270 */
271 public static int getIdByKey(final String key)
272 {
273 if (INCOMPATIBLE_METHOD_TYPES.equals(key))
274 {
275 return TypeComparatorDiagnosticFactory.INCOMPATIBLE_METHOD_TYPES_ID;
276 }
277 else if (INCOMPATIBLE_TYPES.equals(key))
278 {
279 return TypeComparatorDiagnosticFactory.INCOMPATIBLE_TYPES_ID;
280 }
281 else if (METHOD_EXPRESSION_EXPECTED.equals(key))
282 {
283 return TypeComparatorDiagnosticFactory.METHOD_EXPRESSION_EXPECTED_ID;
284 }
285 else if (PROPERTY_NOT_READABLE.equals(key))
286 {
287 return TypeComparatorDiagnosticFactory.PROPERTY_NOT_READABLE_ID;
288 }
289 else if (PROPERTY_NOT_WRITABLE.equals(key))
290 {
291 return TypeComparatorDiagnosticFactory.PROPERTY_NOT_WRITABLE_ID;
292 }
293 else if (VALUE_EXPRESSION_EXPECTED.equals(key))
294 {
295 return TypeComparatorDiagnosticFactory.VALUE_EXPRESSION_EXPECTED_ID;
296 }
297 else
298 {
gkesslercfc53082008-11-18 22:25:04 +0000299 throw new IllegalArgumentException("Severity Key: " + key); //$NON-NLS-1$
cbateman123ece32008-03-25 19:46:06 +0000300 }
301 }
302
303 /**
304 * e.g. createQualifiedKeyName("foo") -> org.eclipse.jst.jsf.core.foo
305 *
306 * @param baseName
307 * @return a plugin qualified key given the baseName
308 *
309 */
310 private static String createQualifiedKeyName(final String baseName)
311 {
gkesslercfc53082008-11-18 22:25:04 +0000312 return JSFCorePlugin.PLUGIN_ID + "." + baseName; //$NON-NLS-1$
cbateman123ece32008-03-25 19:46:06 +0000313 }
314
315 /**
316 * preference key. Match to DiagnosticFactory constants
317 */
gkesslercfc53082008-11-18 22:25:04 +0000318 public final static String INCOMPATIBLE_METHOD_TYPES = createQualifiedKeyName("INCOMPATIBLE_METHOD_TYPES"); //$NON-NLS-1$
cbateman123ece32008-03-25 19:46:06 +0000319 /**
320 * preference key. Match to DiagnosticFactory constants
321 */
gkesslercfc53082008-11-18 22:25:04 +0000322 public final static String INCOMPATIBLE_TYPES = createQualifiedKeyName("INCOMPATIBLE_TYPES"); //$NON-NLS-1$
cbateman123ece32008-03-25 19:46:06 +0000323 /**
324 * preference key. Match to DiagnosticFactory constants
325 */
gkesslercfc53082008-11-18 22:25:04 +0000326 public final static String METHOD_EXPRESSION_EXPECTED = createQualifiedKeyName("METHOD_EXPRESSION_EXPECTED"); //$NON-NLS-1$
cbateman123ece32008-03-25 19:46:06 +0000327 /**
328 * preference key. Match to DiagnosticFactory constants
329 */
gkesslercfc53082008-11-18 22:25:04 +0000330 public final static String PROPERTY_NOT_READABLE = createQualifiedKeyName("PROPERTY_NOT_READABLE"); //$NON-NLS-1$
cbateman123ece32008-03-25 19:46:06 +0000331 /**
332 * preference key. Match to DiagnosticFactory constants
333 */
gkesslercfc53082008-11-18 22:25:04 +0000334 public final static String PROPERTY_NOT_WRITABLE = createQualifiedKeyName("PROPERTY_NOT_WRITABLE"); //$NON-NLS-1$
cbateman123ece32008-03-25 19:46:06 +0000335 /**
336 * preference key. Match to DiagnosticFactory constants
337 */
gkesslercfc53082008-11-18 22:25:04 +0000338 public final static String VALUE_EXPRESSION_EXPECTED = createQualifiedKeyName("VALUE_EXPRESSION_EXPECTED"); //$NON-NLS-1$
cbateman123ece32008-03-25 19:46:06 +0000339}