Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3a7330065f08a460e96d3d4188e8889eb8785932 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*******************************************************************************
 * Copyright (c) 2009 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.preferences;

import java.text.MessageFormat;

import org.eclipse.cdt.codan.core.CodanCorePlugin;
import org.eclipse.cdt.codan.core.CodanRuntime;
import org.eclipse.cdt.codan.core.model.ICheckersRegistry;
import org.eclipse.cdt.codan.core.model.IProblem;
import org.eclipse.cdt.codan.core.model.IProblemProfile;
import org.eclipse.cdt.codan.core.param.IProblemPreference;
import org.eclipse.cdt.codan.internal.ui.CodanUIActivator;
import org.eclipse.cdt.codan.internal.ui.CodanUIMessages;
import org.eclipse.cdt.codan.internal.ui.dialogs.CustomizeProblemDialog;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.PixelConverter;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.preferences.ScopedPreferenceStore;

/**
 * This class represents a preference page that is contributed to the
 * Preferences dialog. By subclassing <samp>FieldEditorPreferencePage</samp>, we
 * can use the field support built into JFace that allows us to create a page
 * that is small and knows how to save, restore and apply itself.
 * <p>
 * This page is used to modify preferences only. They are stored in the
 * preference store that belongs to the main plug-in class. That way,
 * preferences can be accessed directly via the preference store.
 */
public class CodanPreferencePage extends FieldEditorOverlayPage implements
		IWorkbenchPreferencePage {
	private IProblemProfile profile;
	private ISelectionChangedListener problemSelectionListener;
	private IProblem selectedProblem;
	private Group info;
	private Label infoDesc;
	private Label infoMessage;
	private Label infoParams;
	private Button infoButton;
	private ProblemsTreeEditor checkedTreeEditor;

	public CodanPreferencePage() {
		super(GRID);
		setPreferenceStore(new ScopedPreferenceStore(new InstanceScope(),
				CodanCorePlugin.PLUGIN_ID));
		// setDescription("Code Analysis Preference Page");
		problemSelectionListener = new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				if (info != null) {
					if (event.getSelection() instanceof ITreeSelection) {
						ITreeSelection s = (ITreeSelection) event
								.getSelection();
						if (s.getFirstElement() instanceof IProblem)
							setSelectedProblem((IProblem) s.getFirstElement());
						else
							setSelectedProblem(null);
					}
				}
			}
		};
	}

	@Override
	protected String getPageId() {
		return "org.eclipse.cdt.codan.internal.ui.preferences.CodanPreferencePage"; //$NON-NLS-1$
	}

	/**
	 * Creates the field editors. Field editors are abstractions of the common
	 * GUI blocks needed to manipulate various types of preferences. Each field
	 * editor knows how to save and restore itself.
	 */
	@Override
	public void createFieldEditors() {
		profile = isPropertyPage() ? getRegistry()
				.getResourceProfileWorkingCopy((IResource) getElement())
				: getRegistry().getWorkspaceProfile();
		checkedTreeEditor = new ProblemsTreeEditor(getFieldEditorParent(),
				profile);
		addField(checkedTreeEditor);
		checkedTreeEditor.getTreeViewer().addSelectionChangedListener(
				problemSelectionListener);
		checkedTreeEditor.getTreeViewer().addDoubleClickListener(
				new IDoubleClickListener() {
					public void doubleClick(DoubleClickEvent event) {
						openCustomizeDialog();
					}
				});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.cdt.codan.internal.ui.preferences.FieldEditorOverlayPage#
	 * createContents(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createContents(Composite parent) {
		Composite comp = (Composite) super.createContents(parent);
		createInfoControl(comp);
		return comp;
	}

	/**
	 * @param comp
	 */
	private void createInfoControl(Composite comp) {
		info = new Group(comp, SWT.NONE);
		info.setLayoutData(new GridData(GridData.FILL_BOTH));
		info.setLayout(new GridLayout(3, false));
		info.setText(CodanUIMessages.CodanPreferencePage_Info);
		GridDataFactory gdLab = GridDataFactory.swtDefaults()
				.align(SWT.BEGINNING, SWT.BEGINNING).grab(false, false);
		GridDataFactory gdFact = GridDataFactory.swtDefaults()
				.align(SWT.BEGINNING, SWT.BEGINNING).grab(true, true);
		// message
		Label labelMessage = new Label(info, SWT.NONE);
		labelMessage.setText(CodanUIMessages.CodanPreferencePage_MessageLabel);
		labelMessage.setLayoutData(gdLab.create());
		infoMessage = new Label(info, SWT.WRAP);
		infoMessage.setLayoutData(gdFact.copy().span(2, 1).create());
		// description
		Label labelDesc = new Label(info, SWT.NONE);
		labelDesc.setText(CodanUIMessages.CodanPreferencePage_Description);
		labelDesc.setLayoutData(gdLab.create());
		infoDesc = new Label(info, SWT.WRAP);
		PixelConverter pixelConverter = new PixelConverter(comp);
		infoDesc.setLayoutData(gdFact
				.copy()
				.span(2, 1)
				.hint(pixelConverter.convertWidthInCharsToPixels(60),
						pixelConverter.convertHeightInCharsToPixels(3))
				.create());
		// params
		Label labelParams = new Label(info, SWT.NONE);
		labelParams.setText(CodanUIMessages.CodanPreferencePage_Parameters);
		labelParams.setLayoutData(gdLab.create());
		infoParams = new Label(info, SWT.NONE);
		infoParams.setLayoutData(gdFact.create());
		infoButton = new Button(info, SWT.PUSH);
		infoButton.setLayoutData(GridDataFactory.swtDefaults()
				.align(SWT.END, SWT.BEGINNING).create());
		infoButton.setText(CodanUIMessages.CodanPreferencePage_Customize);
		infoButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				openCustomizeDialog();
			}
		});
		restoreWidgetValues();
	}

	/**
	 * @param selection
	 */
	protected void setSelectedProblem(IProblem problem) {
		this.selectedProblem = problem;
		updateProblemInfo();
	}

	/**
	 * @return
	 */
	protected ICheckersRegistry getRegistry() {
		return CodanRuntime.getInstance().getChechersRegistry();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.preference.PreferencePage#performApply()
	 */
	@Override
	public boolean performOk() {
		saveWidgetValues();
		// if (isPropertyPage())
		getRegistry().updateProfile((IResource) getElement(), null);
		return super.performOk();
	}

	/**
	 * 
	 */
	private void saveWidgetValues() {
		CodanUIActivator
				.getDefault()
				.getDialogSettings()
				.put(getWidgetId(),
						selectedProblem == null ? "" : selectedProblem.getId()); //$NON-NLS-1$
	}

	private void restoreWidgetValues() {
		String id = CodanUIActivator.getDefault().getDialogSettings()
				.get(getWidgetId());
		if (id != null && id.length() > 0) {
			checkedTreeEditor.getTreeViewer().setSelection(
					new StructuredSelection(profile.findProblem(id)), true);
		} else {
			setSelectedProblem(null);
		}
	}

	/**
	 * @return
	 */
	protected String getWidgetId() {
		return getPageId() + ".selection"; //$NON-NLS-1$
	}

	/**
	 * 
	 */
	private void updateProblemInfo() {
		if (selectedProblem == null) {
			infoMessage.setText(""); //$NON-NLS-1$
			infoDesc.setText(""); //$NON-NLS-1$
			infoParams.setText(""); //$NON-NLS-1$
			infoButton.setEnabled(false);
		} else {
			IProblemPreference pref = selectedProblem.getPreference();
			String description = selectedProblem.getDescription();
			if (description == null)
				description = CodanUIMessages.CodanPreferencePage_NoInfo;
			String messagePattern = selectedProblem.getMessagePattern();
			String message = CodanUIMessages.CodanPreferencePage_NoInfo;
			if (messagePattern != null) {
				message = MessageFormat.format(messagePattern, "X", "Y", "Z"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 
			}
			infoMessage.setText(message);
			infoDesc.setText(description);
			infoParams
					.setText(pref == null ? CodanUIMessages.CodanPreferencePage_NoInfo
							: CodanUIMessages.CodanPreferencePage_HasPreferences);
			infoButton.setEnabled(true);
		}
		info.layout(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	public void init(IWorkbench workbench) {
	}

	/**
	 * 
	 */
	protected void openCustomizeDialog() {
		CustomizeProblemDialog d = new CustomizeProblemDialog(getShell(),
				selectedProblem);
		d.open();
	}
}

Back to the top