Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: db02beeae878f81aa0fb3bfd0c4d774cd2fda56d (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
/*******************************************************************************
 * 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.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.internal.ui.dialogs.IStatusChangeListener;
import org.eclipse.cdt.internal.ui.refactoring.includes.IncludeGroupStyle;
import org.eclipse.cdt.internal.ui.refactoring.includes.IncludeGroupStyle.IncludeKind;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.core.resources.IProject;
import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;

/**
 * The preference block for configuring style of include statements.
 */
public class IncludeStyleBlock extends TabConfigurationBlock {
	static final Key KEY_STYLE_RELATED = getCDTUIKey(PreferenceConstants.INCLUDE_STYLE_RELATED);
	static final Key KEY_STYLE_PARTNER = getCDTUIKey(PreferenceConstants.INCLUDE_STYLE_PARTNER);
	static final Key KEY_STYLE_SAME_FOLDER = getCDTUIKey(PreferenceConstants.INCLUDE_STYLE_SAME_FOLDER);
	static final Key KEY_STYLE_SUBFOLDER = getCDTUIKey(PreferenceConstants.INCLUDE_STYLE_SUBFOLDER);
	static final Key KEY_STYLE_SYSTEM = getCDTUIKey(PreferenceConstants.INCLUDE_STYLE_SYSTEM);
	static final Key KEY_STYLE_SYSTEM_WITH_EXTENSION = getCDTUIKey(
			PreferenceConstants.INCLUDE_STYLE_SYSTEM_WITH_EXTENSION);
	static final Key KEY_STYLE_SYSTEM_WITHOUT_EXTENSION = getCDTUIKey(
			PreferenceConstants.INCLUDE_STYLE_SYSTEM_WITHOUT_EXTENSION);
	static final Key KEY_STYLE_OTHER = getCDTUIKey(PreferenceConstants.INCLUDE_STYLE_OTHER);
	static final Key KEY_STYLE_SAME_PROJECT = getCDTUIKey(PreferenceConstants.INCLUDE_STYLE_SAME_PROJECT);
	static final Key KEY_STYLE_OTHER_PROJECT = getCDTUIKey(PreferenceConstants.INCLUDE_STYLE_OTHER_PROJECT);
	static final Key KEY_STYLE_EXTERNAL = getCDTUIKey(PreferenceConstants.INCLUDE_STYLE_EXTERNAL);
	static final Key KEY_STYLE_MATCHING_PATTERN = getCDTUIKey(PreferenceConstants.INCLUDE_STYLE_MATCHING_PATTERN);

	static final Map<IncludeKind, Key> KEY_MAP = createKeyMap();
	static final Key[] STYLE_KEYS = KEY_MAP.values().toArray(new Key[KEY_MAP.size()]);

	private static final String[] TAB_LABELS = { PreferencesMessages.IncludeStyleBlock_categories_tab,
			PreferencesMessages.IncludeStyleBlock_order_tab, };

	private final List<IncludeGroupStyle> styles;

	public IncludeStyleBlock(IStatusChangeListener context, IProject project, IWorkbenchPreferenceContainer container) {
		this(context, project, container, new ArrayList<IncludeGroupStyle>());
	}

	private IncludeStyleBlock(IStatusChangeListener context, IProject project, IWorkbenchPreferenceContainer container,
			List<IncludeGroupStyle> styles) {
		super(context, project, createTabs(context, project, container, styles), TAB_LABELS, container);
		this.styles = styles;
		settingsUpdated();
	}

	private static OptionsConfigurationBlock[] createTabs(IStatusChangeListener context, IProject project,
			IWorkbenchPreferenceContainer container, List<IncludeGroupStyle> styles) {
		IncludeCategoriesBlock includeCategoriesBlock = new IncludeCategoriesBlock(context, project, container, styles);
		IncludeOrderBlock includeOrderBlock = new IncludeOrderBlock(context, project, container, styles);
		return new OptionsConfigurationBlock[] { includeCategoriesBlock, includeOrderBlock };
	}

	private static Map<IncludeKind, Key> createKeyMap() {
		Map<IncludeKind, Key> map = new HashMap<IncludeKind, Key>();
		map.put(IncludeKind.RELATED, KEY_STYLE_RELATED);
		map.put(IncludeKind.PARTNER, KEY_STYLE_PARTNER);
		map.put(IncludeKind.IN_SAME_FOLDER, KEY_STYLE_SAME_FOLDER);
		map.put(IncludeKind.IN_SUBFOLDER, KEY_STYLE_SUBFOLDER);
		map.put(IncludeKind.SYSTEM, KEY_STYLE_SYSTEM);
		map.put(IncludeKind.SYSTEM_WITH_EXTENSION, KEY_STYLE_SYSTEM_WITH_EXTENSION);
		map.put(IncludeKind.SYSTEM_WITHOUT_EXTENSION, KEY_STYLE_SYSTEM_WITHOUT_EXTENSION);
		map.put(IncludeKind.OTHER, KEY_STYLE_OTHER);
		map.put(IncludeKind.IN_SAME_PROJECT, KEY_STYLE_SAME_PROJECT);
		map.put(IncludeKind.IN_OTHER_PROJECT, KEY_STYLE_OTHER_PROJECT);
		map.put(IncludeKind.EXTERNAL, KEY_STYLE_EXTERNAL);
		return Collections.unmodifiableMap(map);
	}

	@Override
	protected boolean processChanges(IWorkbenchPreferenceContainer container) {
		boolean result = super.processChanges(container);
		for (IncludeGroupStyle style : styles) {
			IncludeKind includeKind = style.getIncludeKind();
			Key key = KEY_MAP.get(includeKind);
			if (includeKind != IncludeKind.MATCHING_PATTERN) {
				setValue(key, style.toXmlString());
			} else {
				// TODO(sprigogin): Support custom include categories.
			}
		}
		return result;
	}

	@Override
	protected void settingsUpdated() {
		if (styles != null) {
			styles.clear();
			for (Map.Entry<IncludeKind, Key> entry : KEY_MAP.entrySet()) {
				IncludeKind includeKind = entry.getKey();
				IncludeGroupStyle style = null;
				String str = getValue(entry.getValue());
				if (str != null)
					style = IncludeGroupStyle.fromXmlString(str, includeKind);
				if (style == null)
					style = new IncludeGroupStyle(includeKind);
				styles.add(style);
			}
		}
		// TODO Propagate styles to tabs.
		super.settingsUpdated();
	}
}

Back to the top