Skip to main content
summaryrefslogtreecommitdiffstats
blob: 30579306fca36420610c814e58e3de1dcaa6319b (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
/*******************************************************************************
 * Copyright (c) 2005, 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.core.internal.validation;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jpt.core.IResourcePart;
import org.eclipse.jpt.core.JptCorePlugin;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
import org.osgi.service.prefs.BackingStoreException;

//TODO:  Probably want to merge the behavior in this class into JptCorePlugin
public class JpaValidationPreferences {

	/*
	 * prefix for all preference strings.  This is only used internally.
	 * Clients of get*LevelProblemPrefernce() and set*LevelProblemPreference 
	 * should not include the prefix.
	 */
	private static final String PROBLEM_PREFIX = "problem."; //$NON-NLS-1$

	public static final String ERROR = "error"; //$NON-NLS-1$
	public static final String WARNING = "warning"; //$NON-NLS-1$
	public static final String INFO = "info"; //$NON-NLS-1$
	public static final String IGNORE = "ignore"; //$NON-NLS-1$

	static final int NO_SEVERITY_PREFERENCE = -1;

	public static final String WORKSPACE_PREFERENCES_OVERRIDEN = "workspace_preferences_overriden"; //$NON-NLS-1$

	/**
	 * Returns only the severity level of a given problem preference.  This does not
	 * include information on whether the problem is ignored.  See isProblemIgnored.
	 * @return an IMessage severity level
	 */
	public static int getProblemSeverityPreference(Object targetObject, String messageId) {
		IProject project = getProject(targetObject);
		String problemPreference = getPreference(project, messageId);
		
		if (problemPreference == null){
			return NO_SEVERITY_PREFERENCE;
		} else if (problemPreference.equals(ERROR)){
			return IMessage.HIGH_SEVERITY;
		} else if (problemPreference.equals(WARNING)){
			return IMessage.NORMAL_SEVERITY;
		} else if (problemPreference.equals(INFO)){
			return IMessage.LOW_SEVERITY;
		}
		return NO_SEVERITY_PREFERENCE;
	}

	private static IProject getProject(Object targetObject) {
		IAdaptable target = (IAdaptable)targetObject;
		IResource resource = ((IResourcePart) target.getAdapter(IResourcePart.class)).getResource();
		IProject project = resource.getProject();
		return project;
	}

	/**
	 * Returns whether or not this problem should be ignored based on project or
	 * workspace preferences
	 */
	public static boolean isProblemIgnored(IProject project, String messageId){
		String problemPreference = getPreference(project, messageId);
		if (problemPreference != null && problemPreference.equals(IGNORE)){
			return true;
		}
		return false;
	}

	private static String getPreference(IProject project, String messageId) {
		String problemPreference = null;
		problemPreference = getProjectLevelProblemPreference(project, messageId);
		//if severity is still null, check the workspace preferences
		if (problemPreference == null) {
			problemPreference = getWorkspaceLevelProblemPreference(messageId);
		}
		return problemPreference;
	}

	/**
	 * Returns the String value of the problem preference from the project preferences
	 */
	public static String getProjectLevelProblemPreference(IProject project, String messageId){
		return getPreference(JptCorePlugin.getProjectPreferences(project), messageId);
	}

	public static void setProjectLevelProblemPreference(IProject project, String messageId, String problemPreference) {
		IEclipsePreferences projectPreferences = JptCorePlugin.getProjectPreferences(project);
		setPreference(projectPreferences, messageId, problemPreference);
		flush(projectPreferences);
	}

	/**
	 * Returns the String value of the problem preference from the workspace preferences
	 */
	public static String getWorkspaceLevelProblemPreference(String messageId){
		return getPreference(JptCorePlugin.getWorkspacePreferences(), messageId);
	}

	public static void setWorkspaceLevelProblemPreference(String messageId, String problemPreference) {
		IEclipsePreferences workspacePreferences = JptCorePlugin.getWorkspacePreferences();
		setPreference(workspacePreferences, messageId, problemPreference);
		flush(workspacePreferences);
	}

	private static String getPreference(IEclipsePreferences preferences, String messageId) {
		return preferences.get(appendProblemPrefix(messageId), null);
	}

	private static void setPreference(IEclipsePreferences preferences, String messageId, String problemPreference) {
		if (problemPreference == null){
			preferences.remove(appendProblemPrefix(messageId));
		}
		else {
			preferences.put(appendProblemPrefix(messageId), problemPreference);
		}
	}

	private static String appendProblemPrefix(String messageId) {
		return PROBLEM_PREFIX + messageId;
	}

	public static void flush(IEclipsePreferences prefs) {
		try {
			prefs.flush();
		} catch(BackingStoreException ex) {
			JptCorePlugin.log(ex);
		}
	}
}

Back to the top