Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d05d7790ba2a6b5d453472db4575474d63cdb630 (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
/*******************************************************************************
 *  Copyright (c) 2007, 2012 IBM Corporation 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:
 *   IBM Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.language;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.cdt.core.model.LanguageManager;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.internal.ui.preferences.PreferencesMessages;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class ProjectContentTypeMappingDialog extends ContentTypeMappingDialog {

	private Combo fConfiguration;
	private ICConfigurationDescription[] fConfigurations;
	private String[] fContentTypesIDs;
	private Set<String> fFilteredContentTypes;

	public ProjectContentTypeMappingDialog(Shell parentShell, ICConfigurationDescription[] configurations) {
		super(parentShell);
		fConfigurations = configurations;

		fContentTypesIDs = LanguageManager.getInstance().getRegisteredContentTypeIds();
		IContentTypeManager contentTypeManager = Platform.getContentTypeManager();

		for (int i = 0; i < fContentTypesIDs.length; i++) {
			String name = contentTypeManager.getContentType(fContentTypesIDs[i]).getName();

			// keep track of what ID this name corresponds to so that when
			// we setup the mapping
			// later based upon user selection, we'll know what ID to use
			fContentTypeNamesToIDsMap.put(name, fContentTypesIDs[i]);
		}
	}

	private void configureConfigurations(Combo combo) {
		combo.add(PreferencesMessages.ContentTypeMappingsDialog_allConfigurations);
		for (int i = 0; i < fConfigurations.length; i++) {
			combo.add(fConfigurations[i].getName());
		}
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite area = new Composite(parent, SWT.NONE);
		area.setLayout(new GridLayout(2, false));

		Label configurationLabel = new Label(area, SWT.TRAIL);
		configurationLabel.setText(PreferencesMessages.ContentTypeMappingsDialog_configuration);
		fConfiguration = new Combo(area, SWT.DROP_DOWN | SWT.READ_ONLY);
		fConfiguration.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		configureConfigurations(fConfiguration);
		fConfiguration.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				int index = fConfiguration.getSelectionIndex();
				if (index <= 0) {
					fSelectedConfigurationName = null;
					fSelectedConfigurationID = null;
					configureContentTypes(fContentType, null);
					getButton(IDialogConstants.OK_ID).setEnabled(false);
					return;
				}

				// Shift index by one because of "All configurations" entry.
				int configurationIndex = index - 1;
				ICConfigurationDescription configuration = fConfigurations[configurationIndex];

				fSelectedConfigurationName = configuration.getName();
				fSelectedConfigurationID = configuration.getId();
				configureContentTypes(fContentType, configuration);

				getButton(IDialogConstants.OK_ID).setEnabled(isValidSelection());
			}
		});
		fConfiguration.select(0);

		Label contentTypeLabel = new Label(area, SWT.TRAIL);
		contentTypeLabel.setText(PreferencesMessages.ContentTypeMappingsDialog_contentType);

		fContentType = new Combo(area, SWT.DROP_DOWN | SWT.READ_ONLY);
		fContentType.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		fContentType.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				fSelectedContentTypeName = fContentType.getText();
				fSelectedContentTypeID = fContentTypeNamesToIDsMap.get(fSelectedContentTypeName);
				getButton(IDialogConstants.OK_ID).setEnabled(isValidSelection());
			}
		});
		configureContentTypes(fContentType, null);

		Label languageLabel = new Label(area, SWT.TRAIL);
		languageLabel.setText(PreferencesMessages.ContentTypeMappingsDialog_language);

		fLanguage = new Combo(area, SWT.DROP_DOWN | SWT.READ_ONLY);
		fLanguage.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		fLanguage.setItems(getLanguages());
		fLanguage.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				fSelectedLanguageName = fLanguage.getText();
				fSelectedLanguageID = fLanguageNamesToIDsMap.get(fSelectedLanguageName);
				getButton(IDialogConstants.OK_ID).setEnabled(isValidSelection());
			}
		});

		return area;
	}

	private void configureContentTypes(Combo combo, ICConfigurationDescription configuration) {
		combo.removeAll();
		IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
		List<String> names = new LinkedList<String>();

		for (int i = 0; i < fContentTypesIDs.length; i++) {
			String contentTypeId = fContentTypesIDs[i];
			String name = contentTypeManager.getContentType(contentTypeId).getName();

			if (configuration != null) {
				String key = ProjectLanguageMappingWidget.createFilterKey(configuration.getId(), contentTypeId);
				if (!fFilteredContentTypes.contains(key)) {
					names.add(name);
				}
			} else {
				names.add(name);
			}
		}

		Collections.sort(names);
		for (String name : names) {
			combo.add(name);
		}
	}

	@Override
	protected boolean isValidSelection() {
		return fContentType.getSelectionIndex() != -1 && fLanguage.getSelectionIndex() != -1
				&& fConfiguration.getSelectionIndex() != -1;
	}

	public void setContentTypeFilter(Set<String> contentTypeFilter) {
		fFilteredContentTypes = contentTypeFilter;
	}

	public String getConfigurationID() {
		return fSelectedConfigurationID;
	}
}

Back to the top