rsrinivasan | cf82432 | 2008-05-02 19:51:19 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 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 | *******************************************************************************/ |
cbateman | 123ece3 | 2008-03-25 19:46:06 +0000 | [diff] [blame] | 11 | package org.eclipse.jst.jsf.validation.internal; |
| 12 | |
| 13 | import org.eclipse.core.runtime.preferences.IScopeContext; |
| 14 | import org.eclipse.emf.common.util.Diagnostic; |
| 15 | import org.eclipse.jface.preference.IPreferenceStore; |
| 16 | import org.eclipse.jst.jsf.common.internal.types.TypeComparatorDiagnosticFactory; |
| 17 | import org.eclipse.jst.jsf.common.internal.types.TypeComparatorPreferences; |
| 18 | import org.eclipse.jst.jsf.core.internal.IJSFPreferenceModel; |
| 19 | import org.eclipse.jst.jsf.core.internal.JSFCorePlugin; |
| 20 | |
| 21 | /** |
| 22 | * Type comparator preferences for JSF. |
| 23 | * |
| 24 | * @author cbateman |
| 25 | * |
| 26 | */ |
| 27 | public 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 | { |
gkessler | cfc5308 | 2008-11-18 22:25:04 +0000 | [diff] [blame] | 129 | return context.getNode("org.eclipse.jst.jsf.core").get( //$NON-NLS-1$ |
cbateman | 123ece3 | 2008-03-25 19:46:06 +0000 | [diff] [blame] | 130 | 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 | { |
gkessler | cfc5308 | 2008-11-18 22:25:04 +0000 | [diff] [blame] | 221 | if ("error".equals(severity)) //$NON-NLS-1$ |
cbateman | 123ece3 | 2008-03-25 19:46:06 +0000 | [diff] [blame] | 222 | { |
| 223 | return Diagnostic.ERROR; |
| 224 | } |
gkessler | cfc5308 | 2008-11-18 22:25:04 +0000 | [diff] [blame] | 225 | else if ("warning".equals(severity)) //$NON-NLS-1$ |
cbateman | 123ece3 | 2008-03-25 19:46:06 +0000 | [diff] [blame] | 226 | { |
| 227 | return Diagnostic.WARNING; |
| 228 | } |
gkessler | cfc5308 | 2008-11-18 22:25:04 +0000 | [diff] [blame] | 229 | else if ("ignore".equals(severity)) //$NON-NLS-1$ |
cbateman | 123ece3 | 2008-03-25 19:46:06 +0000 | [diff] [blame] | 230 | { |
| 231 | return Diagnostic.OK; |
| 232 | } |
| 233 | else |
| 234 | { |
gkessler | cfc5308 | 2008-11-18 22:25:04 +0000 | [diff] [blame] | 235 | throw new IllegalArgumentException("Invalid enum name: " + severity); //$NON-NLS-1$ |
cbateman | 123ece3 | 2008-03-25 19:46:06 +0000 | [diff] [blame] | 236 | } |
| 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: |
gkessler | cfc5308 | 2008-11-18 22:25:04 +0000 | [diff] [blame] | 261 | throw new IllegalArgumentException("Diagnostic Id: " //$NON-NLS-1$ |
| 262 | + diagnosticId + " is out of range"); //$NON-NLS-1$ |
cbateman | 123ece3 | 2008-03-25 19:46:06 +0000 | [diff] [blame] | 263 | } |
| 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 | { |
gkessler | cfc5308 | 2008-11-18 22:25:04 +0000 | [diff] [blame] | 299 | throw new IllegalArgumentException("Severity Key: " + key); //$NON-NLS-1$ |
cbateman | 123ece3 | 2008-03-25 19:46:06 +0000 | [diff] [blame] | 300 | } |
| 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 | { |
gkessler | cfc5308 | 2008-11-18 22:25:04 +0000 | [diff] [blame] | 312 | return JSFCorePlugin.PLUGIN_ID + "." + baseName; //$NON-NLS-1$ |
cbateman | 123ece3 | 2008-03-25 19:46:06 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** |
| 316 | * preference key. Match to DiagnosticFactory constants |
| 317 | */ |
gkessler | cfc5308 | 2008-11-18 22:25:04 +0000 | [diff] [blame] | 318 | public final static String INCOMPATIBLE_METHOD_TYPES = createQualifiedKeyName("INCOMPATIBLE_METHOD_TYPES"); //$NON-NLS-1$ |
cbateman | 123ece3 | 2008-03-25 19:46:06 +0000 | [diff] [blame] | 319 | /** |
| 320 | * preference key. Match to DiagnosticFactory constants |
| 321 | */ |
gkessler | cfc5308 | 2008-11-18 22:25:04 +0000 | [diff] [blame] | 322 | public final static String INCOMPATIBLE_TYPES = createQualifiedKeyName("INCOMPATIBLE_TYPES"); //$NON-NLS-1$ |
cbateman | 123ece3 | 2008-03-25 19:46:06 +0000 | [diff] [blame] | 323 | /** |
| 324 | * preference key. Match to DiagnosticFactory constants |
| 325 | */ |
gkessler | cfc5308 | 2008-11-18 22:25:04 +0000 | [diff] [blame] | 326 | public final static String METHOD_EXPRESSION_EXPECTED = createQualifiedKeyName("METHOD_EXPRESSION_EXPECTED"); //$NON-NLS-1$ |
cbateman | 123ece3 | 2008-03-25 19:46:06 +0000 | [diff] [blame] | 327 | /** |
| 328 | * preference key. Match to DiagnosticFactory constants |
| 329 | */ |
gkessler | cfc5308 | 2008-11-18 22:25:04 +0000 | [diff] [blame] | 330 | public final static String PROPERTY_NOT_READABLE = createQualifiedKeyName("PROPERTY_NOT_READABLE"); //$NON-NLS-1$ |
cbateman | 123ece3 | 2008-03-25 19:46:06 +0000 | [diff] [blame] | 331 | /** |
| 332 | * preference key. Match to DiagnosticFactory constants |
| 333 | */ |
gkessler | cfc5308 | 2008-11-18 22:25:04 +0000 | [diff] [blame] | 334 | public final static String PROPERTY_NOT_WRITABLE = createQualifiedKeyName("PROPERTY_NOT_WRITABLE"); //$NON-NLS-1$ |
cbateman | 123ece3 | 2008-03-25 19:46:06 +0000 | [diff] [blame] | 335 | /** |
| 336 | * preference key. Match to DiagnosticFactory constants |
| 337 | */ |
gkessler | cfc5308 | 2008-11-18 22:25:04 +0000 | [diff] [blame] | 338 | public final static String VALUE_EXPRESSION_EXPECTED = createQualifiedKeyName("VALUE_EXPRESSION_EXPECTED"); //$NON-NLS-1$ |
cbateman | 123ece3 | 2008-03-25 19:46:06 +0000 | [diff] [blame] | 339 | } |