Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: efe8fbce5321af485bc2dd9909a629419388a17c (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
/*******************************************************************************
 * Copyright (c) 2013 Google, Inc and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * 	   Sergey Prigogin (Google) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.preferences;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.layout.PixelConverter;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.KeyEvent;
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.Label;
import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;

import org.eclipse.cdt.internal.ui.dialogs.IStatusChangeListener;
import org.eclipse.cdt.internal.ui.dialogs.StatusInfo;
import org.eclipse.cdt.internal.ui.refactoring.includes.IncludeGroupStyle;
import org.eclipse.cdt.internal.ui.refactoring.includes.IncludeGroupStyle.IncludeKind;
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.ITreeListAdapter;
import org.eclipse.cdt.internal.ui.wizards.dialogfields.TreeListDialogField;

/**
 * The preference block for configuring styles of different categories of include statements.
 */
public class IncludeCategoriesBlock extends OptionsConfigurationBlock {
	private final List<IncludeGroupStyle> styles;
	private final Map<IncludeKind, Category> categories = new HashMap<IncludeKind, Category>();
	private TreeListDialogField<Category> categoryTree;
	private PixelConverter pixelConverter;
	private StackLayout editorAreaStack;
	private Category selectedCategory;

	public IncludeCategoriesBlock(IStatusChangeListener context, IProject project,
			IWorkbenchPreferenceContainer container, List<IncludeGroupStyle> styles) {
		super(context, project, new Key[0], container);
		this.styles = styles;
		createCategories();
	}

	private void createCategories() {
		createCategory(IncludeKind.RELATED);
		createCategory(IncludeKind.PARTNER);
		createCategory(IncludeKind.IN_SAME_FOLDER);
		createCategory(IncludeKind.IN_SUBFOLDER);
		createCategory(IncludeKind.SYSTEM);
		createCategory(IncludeKind.SYSTEM_WITH_EXTENSION);
		createCategory(IncludeKind.SYSTEM_WITHOUT_EXTENSION);
		createCategory(IncludeKind.OTHER);
		createCategory(IncludeKind.IN_SAME_PROJECT);
		createCategory(IncludeKind.IN_OTHER_PROJECT);
		createCategory(IncludeKind.EXTERNAL);
	}

	private Category createCategory(IncludeKind includeKind) {
		Category parentCategory = categories.get(includeKind.parent);
		Category category = new Category(includeKind, parentCategory);
		categories.put(category.getIncludeKind(), category);
		return category;
	}

	public void postSetSelection(Object element) {
		categoryTree.postSetSelection(new StructuredSelection(element));
	}

	@Override
	protected Control createContents(Composite parent) {
		pixelConverter = new PixelConverter(parent);

		setShell(parent.getShell());

		Composite composite = new Composite(parent, SWT.NONE);
		composite.setFont(parent.getFont());

		GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		composite.setLayout(layout);

		IncludeStyleAdapter adapter = new IncludeStyleAdapter();
		categoryTree = new TreeListDialogField<Category>(adapter, null, new IncludeStyleLabelProvider());
		categoryTree.setDialogFieldListener(adapter);
		categoryTree.setLabelText(PreferencesMessages.IncludeCategoriesBlock_header_categories);
		categoryTree.setViewerComparator(adapter);

		for (Category category : categories.values()) {
			if (category.parent == null)
				categoryTree.addElement(category);
		}

		Label label = categoryTree.getLabelControl(composite);
		label.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, false, false));

		Control tree = categoryTree.getTreeControl(composite);
		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
		gd.widthHint = pixelConverter.convertWidthInCharsToPixels(50);
		gd.heightHint = pixelConverter.convertHeightInCharsToPixels(2);
		tree.setLayoutData(gd);

		createCategoryEditors(composite);

		categoryTree.setTreeExpansionLevel(2);
		categoryTree.selectFirstElement();

		updateControls();
		return composite;
	}

	private void createCategoryEditors(Composite parent) {
		Composite editorArea = new Composite(parent, SWT.NONE);
		editorArea.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false));
		editorArea.setFont(parent.getFont());
		editorAreaStack = new StackLayout();
		editorArea.setLayout(editorAreaStack);
		Map<IncludeKind, IncludeGroupStyle> stylesByKind = new HashMap<IncludeKind, IncludeGroupStyle>();
		for (IncludeGroupStyle style : styles) {
			if (style.getIncludeKind() != IncludeKind.MATCHING_PATTERN)
				stylesByKind.put(style.getIncludeKind(), style);
		}

		for (Category category : categories.values()) {
			IncludeGroupStyleBlock block = new IncludeGroupStyleBlock(fContext, fProject, fContainer,
					category.getDescription());
			IncludeGroupStyle style = stylesByKind.get(category.getIncludeKind());
			block.setStyle(style);
			Control composite = block.createContents(editorArea);
			category.setEditor(block, composite);
		}
	}

	@Override
	protected void updateControls() {
		super.updateControls();
		// Refresh
		categoryTree.refresh();
		updateConfigurationBlock(categoryTree.getSelectedElements());
		for (Category category : categories.values()) {
			category.getEditor().updateControls();
		}
	}

	private void updateConfigurationBlock(List<Object> selection) {
		if (selection.size() == 0)
			return;
		selectedCategory = (Category) selection.get(0);
		editorAreaStack.topControl = selectedCategory.getEditorArea();
		editorAreaStack.topControl.getParent().layout();
	}

	@Override
	protected void validateSettings(Key changedKey, String oldValue, String newValue) {
		fContext.statusChanged(new StatusInfo());
	}

	/**
	 * Represents a category of settings.
	 */
	private final static class Category {
		public final Category parent;
		public final int index; // Index in the siblings list
		private final List<Category> children;
		private final IncludeKind includeKind;
		private Control editorArea;
		private IncludeGroupStyleBlock editor;

		Category(IncludeKind includeKind, Category parent) {
			this.includeKind = includeKind;
			this.parent = parent;
			children = new ArrayList<Category>();
			index = parent != null ? parent.addChild(this) : 0;
		}

		private int addChild(Category category) {
			children.add(category);
			return children.size() - 1;
		}

		Category[] getChildren() {
			return children.toArray(new Category[children.size()]);
		}

		boolean hasChildren() {
			return !children.isEmpty();
		}

		@Override
		public String toString() {
			return includeKind.name;
		}

		IncludeKind getIncludeKind() {
			return includeKind;
		}

		IncludeGroupStyleBlock getEditor() {
			return editor;
		}

		Control getEditorArea() {
			return editorArea;
		}

		void setEditor(IncludeGroupStyleBlock editor, Control editorArea) {
			this.editor = editor;
			this.editorArea = editorArea;
		}

		String getName() {
			return includeKind.name;
		}

		String getDescription() {
			return includeKind.description;
		}
	}

	private class IncludeStyleAdapter extends ViewerComparator
			implements ITreeListAdapter<Category>, IDialogFieldListener {
		@Override
		public void selectionChanged(TreeListDialogField<Category> field) {
			updateConfigurationBlock(field.getSelectedElements());
		}

		@Override
		public void customButtonPressed(TreeListDialogField<Category> field, int index) {
		}

		@Override
		public void doubleClicked(TreeListDialogField<Category> field) {
		}

		@Override
		public Category[] getChildren(TreeListDialogField<Category> field, Object element) {
			return ((Category) element).getChildren();
		}

		@Override
		public Category getParent(TreeListDialogField<Category> field, Object element) {
			return ((Category) element).parent;
		}

		@Override
		public boolean hasChildren(TreeListDialogField<Category> field, Object element) {
			return ((Category) element).hasChildren();
		}

		@Override
		public void dialogFieldChanged(DialogField field) {
		}

		@Override
		public void keyPressed(TreeListDialogField<Category> field, KeyEvent event) {
		}

		@Override
		public int category(Object element) {
			return ((Category) element).index;
		}
	}

	private static class IncludeStyleLabelProvider extends LabelProvider {
		@Override
		public Image getImage(Object element) {
			return null;
		}

		@Override
		public String getText(Object element) {
			return ((Category) element).getName();
		}
	}
}

Back to the top