Skip to main content
summaryrefslogtreecommitdiffstats
blob: 149dee9935159bd46c385d6228fc031828a00f6c (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*******************************************************************************
 * Copyright (c) 2010 Alena Laskavaia and others.
 * 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.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.cdt.codan.core.param.FileScopeProblemPreference;
import org.eclipse.cdt.codan.internal.ui.CodanUIActivator;
import org.eclipse.cdt.codan.internal.ui.CodanUIMessages;
import org.eclipse.cdt.codan.internal.ui.dialogs.ExclusionInclusionEntryDialog;
import org.eclipse.cdt.codan.internal.ui.widgets.BasicElementLabels;
import org.eclipse.cdt.internal.ui.wizards.dialogfields.DialogField;
import org.eclipse.cdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
import org.eclipse.cdt.internal.ui.wizards.dialogfields.IListAdapter;
import org.eclipse.cdt.internal.ui.wizards.dialogfields.LayoutUtil;
import org.eclipse.cdt.internal.ui.wizards.dialogfields.ListDialogField;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

public class FileScopePreferencePage extends PreferencePage {
	private ListDialogField fInclusionPatternList;
	private ListDialogField fExclusionPatternList;
	private FileScopeProblemPreference fCurrElement;
	private IProject fCurrProject;
	private IContainer fCurrSourceFolder;
	private static final int IDX_ADD = 0;
	private static final int IDX_ADD_MULTIPLE = 1;
	private static final int IDX_EDIT = 2;
	private static final int IDX_REMOVE = 4;

	public FileScopePreferencePage(FileScopeProblemPreference entryToEdit) {
		setTitle(CodanUIMessages.ExclusionInclusionDialog_title);
		setDescription(CodanUIMessages.ExclusionInclusionDialog_description2);
		fCurrElement = entryToEdit;
		fCurrProject = entryToEdit.getProject();
		IWorkspaceRoot root = fCurrProject != null ? fCurrProject
				.getWorkspace().getRoot() : ResourcesPlugin.getWorkspace()
				.getRoot();
		IResource res = root.findMember(entryToEdit.getPath());
		if (res instanceof IContainer) {
			fCurrSourceFolder = (IContainer) res;
		}
		if (res == null)
			fCurrSourceFolder = root;
		String excLabel = CodanUIMessages.ExclusionInclusionDialog_exclusion_pattern_label;
		ImageDescriptor excDescriptor = null; //JavaPluginImages.DESC_OBJS_EXCLUSION_FILTER_ATTRIB;
		String[] excButtonLabels = new String[] {
				CodanUIMessages.ExclusionInclusionDialog_exclusion_pattern_add,
				CodanUIMessages.ExclusionInclusionDialog_exclusion_pattern_add_multiple,
				CodanUIMessages.ExclusionInclusionDialog_exclusion_pattern_edit,
				null,
				CodanUIMessages.ExclusionInclusionDialog_exclusion_pattern_remove };
		String incLabel = CodanUIMessages.ExclusionInclusionDialog_inclusion_pattern_label;
		ImageDescriptor incDescriptor = null; //JavaPluginImages.DESC_OBJS_INCLUSION_FILTER_ATTRIB;
		String[] incButtonLabels = new String[] {
				CodanUIMessages.ExclusionInclusionDialog_inclusion_pattern_add,
				CodanUIMessages.ExclusionInclusionDialog_inclusion_pattern_add_multiple,
				CodanUIMessages.ExclusionInclusionDialog_inclusion_pattern_edit,
				null,
				CodanUIMessages.ExclusionInclusionDialog_inclusion_pattern_remove };
		fExclusionPatternList = createListContents(entryToEdit,
				FileScopeProblemPreference.EXCLUSION, excLabel, null,
				excButtonLabels);
		fInclusionPatternList = createListContents(entryToEdit,
				FileScopeProblemPreference.INCLUSION, incLabel, null,
				incButtonLabels);
	}

	@Override
	protected Control createContents(Composite parent) {
		Composite inner = new Composite(parent, SWT.NONE);
		inner.setFont(parent.getFont());
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		layout.numColumns = 2;
		inner.setLayout(layout);
		inner.setLayoutData(new GridData(GridData.FILL_BOTH));
		fInclusionPatternList.doFillIntoGrid(inner, 3);
		LayoutUtil.setHorizontalSpan(
				fInclusionPatternList.getLabelControl(null), 2);
		LayoutUtil.setHorizontalGrabbing(fInclusionPatternList
				.getListControl(null));
		fExclusionPatternList.doFillIntoGrid(inner, 3);
		LayoutUtil.setHorizontalSpan(
				fExclusionPatternList.getLabelControl(null), 2);
		LayoutUtil.setHorizontalGrabbing(fExclusionPatternList
				.getListControl(null));
		setControl(inner);
		Dialog.applyDialogFont(inner);
		return inner;
	}

	private static class ExclusionInclusionLabelProvider extends LabelProvider {
		private Image fElementImage;

		public ExclusionInclusionLabelProvider(String descriptorPath) {
			if (descriptorPath != null) {
				ImageDescriptor d = CodanUIActivator
						.getImageDescriptor(descriptorPath);
			}
			fElementImage = null; // XXX
		}

		@Override
		public Image getImage(Object element) {
			return fElementImage;
		}

		@Override
		public String getText(Object element) {
			return BasicElementLabels.getFilePattern((String) element);
		}
	}

	private ListDialogField createListContents(
			FileScopeProblemPreference entryToEdit, String key, String label,
			String descriptor, String[] buttonLabels) {
		ExclusionPatternAdapter adapter = new ExclusionPatternAdapter();
		ListDialogField patternList = new ListDialogField(adapter,
				buttonLabels, new ExclusionInclusionLabelProvider(descriptor));
		patternList.setDialogFieldListener(adapter);
		patternList.setLabelText(label);
		patternList.enableButton(IDX_EDIT, false);
		IPath[] pattern = entryToEdit.getAttribute(key);
		ArrayList elements = new ArrayList(pattern.length);
		for (int i = 0; i < pattern.length; i++) {
			String patternName = pattern[i].toString();
			if (patternName.length() > 0)
				elements.add(patternName);
		}
		patternList.setElements(elements);
		patternList.selectFirstElement();
		patternList.enableButton(IDX_ADD_MULTIPLE, fCurrSourceFolder != null);
		patternList.setViewerComparator(new ViewerComparator());
		return patternList;
	}

	protected void doCustomButtonPressed(ListDialogField field, int index) {
		if (index == IDX_ADD) {
			addEntry(field);
		} else if (index == IDX_EDIT) {
			editEntry(field);
		} else if (index == IDX_ADD_MULTIPLE) {
			addMultipleEntries(field);
		} else if (index == IDX_REMOVE) {
			field.removeElements(field.getSelectedElements());
		}
		updateStatus();
	}

	private void updateStatus() {
		fCurrElement.setAttribute(FileScopeProblemPreference.INCLUSION,
				getInclusionPattern());
		fCurrElement.setAttribute(FileScopeProblemPreference.EXCLUSION,
				getExclusionPattern());
	}

	protected void doDoubleClicked(ListDialogField field) {
		editEntry(field);
		updateStatus();
	}

	protected void doSelectionChanged(ListDialogField field) {
		List selected = field.getSelectedElements();
		field.enableButton(IDX_EDIT, canEdit(selected));
	}

	private boolean canEdit(List selected) {
		return selected.size() == 1;
	}

	private void editEntry(ListDialogField field) {
		List selElements = field.getSelectedElements();
		if (selElements.size() != 1) {
			return;
		}
		List existing = field.getElements();
		String entry = (String) selElements.get(0);
		ExclusionInclusionEntryDialog dialog = new ExclusionInclusionEntryDialog(
				getShell(), isExclusion(field), entry, existing, fCurrElement);
		if (dialog.open() == Window.OK) {
			field.replaceElement(entry, dialog.getExclusionPattern());
		}
	}

	private boolean isExclusion(ListDialogField field) {
		return field == fExclusionPatternList;
	}

	private void addEntry(ListDialogField field) {
		List existing = field.getElements();
		ExclusionInclusionEntryDialog dialog = new ExclusionInclusionEntryDialog(
				getShell(), isExclusion(field), null, existing, fCurrElement);
		if (dialog.open() == Window.OK) {
			field.addElement(dialog.getExclusionPattern());
		}
	}

	// -------- ExclusionPatternAdapter --------
	private class ExclusionPatternAdapter implements IListAdapter,
			IDialogFieldListener {
		/**
		 * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#customButtonPressed(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField,
		 *      int)
		 */
		public void customButtonPressed(ListDialogField field, int index) {
			doCustomButtonPressed(field, index);
		}

		/**
		 * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#selectionChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField)
		 */
		public void selectionChanged(ListDialogField field) {
			doSelectionChanged(field);
		}

		/**
		 * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#doubleClicked(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField)
		 */
		public void doubleClicked(ListDialogField field) {
			doDoubleClicked(field);
		}

		/**
		 * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener#dialogFieldChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField)
		 */
		public void dialogFieldChanged(DialogField field) {
		}
	}

	protected void doStatusLineUpdate() {
	}

	protected void checkIfPatternValid() {
	}

	private IPath[] getPattern(ListDialogField field) {
		Object[] arr = field.getElements().toArray();
		Arrays.sort(arr);
		IPath[] res = new IPath[arr.length];
		for (int i = 0; i < res.length; i++) {
			res[i] = new Path((String) arr[i]);
		}
		return res;
	}

	public IPath[] getExclusionPattern() {
		return getPattern(fExclusionPatternList);
	}

	public IPath[] getInclusionPattern() {
		return getPattern(fInclusionPatternList);
	}

	/*
	 * @see org.eclipse.jface.window.Window#configureShell(Shell)
	 */
	protected void configureShell(Shell newShell) {
	}

	private void addMultipleEntries(ListDialogField field) {
		String title, message;
		if (isExclusion(field)) {
			title = CodanUIMessages.ExclusionInclusionDialog_ChooseExclusionPattern_title;
			message = CodanUIMessages.ExclusionInclusionDialog_ChooseExclusionPattern_description;
		} else {
			title = CodanUIMessages.ExclusionInclusionDialog_ChooseInclusionPattern_title;
			message = CodanUIMessages.ExclusionInclusionDialog_ChooseInclusionPattern_description;
		}
		IPath[] res = ExclusionInclusionEntryDialog.chooseExclusionPattern(
				getShell(), fCurrSourceFolder, title, message, null, true);
		if (res != null) {
			for (int i = 0; i < res.length; i++) {
				field.addElement(res[i].toString());
			}
		}
	}

	@Override
	public void noDefaultAndApplyButton() {
		super.noDefaultAndApplyButton();
	}
}

Back to the top