summaryrefslogtreecommitdiffstats
path: root/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/widgets/FileScopeComposite.java
blob: 1a8536c82048d70b0caf9f7b8466508a71783d94 (plain)
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
/*******************************************************************************
 * Copyright (c) 2009, 2010 Alena Laskavaia 
 * 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:
 *    Alena Laskavaia  - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.internal.ui.widgets;

import org.eclipse.cdt.codan.core.model.IProblem;
import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
import org.eclipse.cdt.codan.core.param.FileScopeProblemPreference;
import org.eclipse.cdt.codan.core.param.IProblemPreference;
import org.eclipse.cdt.codan.core.param.MapProblemPreference;
import org.eclipse.cdt.codan.internal.ui.CodanUIMessages;
import org.eclipse.cdt.codan.internal.ui.preferences.FileScopePreferencePage;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

/**
 * Composite to show problem scope
 * 
 */
public class FileScopeComposite extends Composite {
	private FileScopePreferencePage page;
	private IProblem problem;
	private PreferenceStore prefStore;
	private FileScopeProblemPreference scope;

	/**
	 * @param parent
	 * @param problem
	 * @param resource
	 * @param style
	 */
	public FileScopeComposite(Composite parent, final IProblem problem,
			IResource resource) {
		super(parent, SWT.NONE);
		if (problem == null)
			throw new NullPointerException();
		this.setLayout(new GridLayout(2, false));
		this.problem = problem;
		this.prefStore = new PreferenceStore();
		IProblemPreference info = problem.getPreference();
		FileScopeProblemPreference scopeIn = null;
		if (info == null
				|| (!(info instanceof MapProblemPreference))
				|| ((scopeIn = (FileScopeProblemPreference) ((MapProblemPreference) info)
						.getChildDescriptor(FileScopeProblemPreference.KEY)) == null)) {
			Label label = new Label(this, 0);
			label.setText(CodanUIMessages.ParametersComposite_None);
			return;
		}
		scope = (FileScopeProblemPreference) scopeIn.clone();
		scope.setResource(resource);
		initPrefStore();
		page = new FileScopePreferencePage(scope);
		page.setPreferenceStore(prefStore);
		page.noDefaultAndApplyButton();
		page.createControl(parent);
		page.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
	}

	public void save(IProblemWorkingCopy problem) {
		if (page != null)
			page.performOk();
		savePrefStore();
	}

	private void savePrefStore() {
		if (scope == null)
			return;
		String key = scope.getQualifiedKey();
		((MapProblemPreference) problem.getPreference()).setChildValue(
				FileScopeProblemPreference.KEY, scope);
		prefStore.setValue(key, scope.exportValue());
	}

	private void initPrefStore() {
		if (scope == null)
			return;
		String key = scope.getQualifiedKey();
		prefStore.setValue(key, scope.exportValue());
	}

	/**
	 * @return the problem
	 */
	public IProblem getProblem() {
		return problem;
	}
}