Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9935492a6765639d995afe2cbcf5db0e543448e7 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation 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:
 * IBM Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.language;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.swt.SWT;
import org.eclipse.swt.accessibility.AccessibleAdapter;
import org.eclipse.swt.accessibility.AccessibleEvent;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.TableEditor;
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.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.dialogs.PropertyPage;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.language.ProjectLanguageConfiguration;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.LanguageManager;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.ui.CUIPlugin;

import org.eclipse.cdt.internal.core.CContentTypes;
import org.eclipse.cdt.internal.core.Util;
import org.eclipse.cdt.internal.core.language.LanguageMapping;
import org.eclipse.cdt.internal.core.language.LanguageMappingResolver;

import org.eclipse.cdt.internal.ui.preferences.PreferencesMessages;
import org.eclipse.cdt.internal.ui.util.Messages;

public class FileLanguageMappingPropertyPage extends PropertyPage {

	private static final int MINIMUM_COLUMN_WIDTH = 150;
	private static final int LANGUAGE_COLUMN = 1;
	private static final int CONFIGURATION_COLUMN = 0;
	
	private static final int LANGUAGE_ID = 0;
	private static final int LANGUAGE_NAME = 1;
	
	private static final String ALL_CONFIGURATIONS = ""; //$NON-NLS-1$
	
	private IContentType fContentType;
	private Composite fContents;
	private Table fTable;
	private ILanguage[] fLanguages;
	private Map<String, ILanguage> fLanguageIds;
	private boolean fHasChanges;
	
	public FileLanguageMappingPropertyPage() {
		super();
		fLanguages = LanguageManager.getInstance().getRegisteredLanguages();
		fLanguageIds = LanguageVerifier.computeAvailableLanguages();
	}
	
	@Override
	protected Control createContents(Composite parent) {
		IFile file = getFile();
		IProject project = file.getProject();
		fContentType = CContentTypes.getContentType(project, file.getLocation().toString());
		
		fContents = new Composite(parent, SWT.NONE);
		fContents.setLayout(new GridLayout(2, false));

		Label contentTypeLabel = new Label(fContents, SWT.NONE);
		contentTypeLabel.setText(PreferencesMessages.FileLanguagesPropertyPage_contentTypeLabel);
		contentTypeLabel.setLayoutData(new GridData(SWT.TRAIL, SWT.CENTER, false, false));
		
		Label contentTypeDescriptionLabel = new Label(fContents, SWT.NONE);
		contentTypeDescriptionLabel.setText(fContentType.getName());
		contentTypeDescriptionLabel.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
		
		try {
			createMappingTable(fContents, file);
		} catch (CoreException e) {
			CUIPlugin.log(e);
		}
		
		Link link = new Link(fContents, SWT.NONE);
		link.setText(PreferencesMessages.FileLanguagesPropertyPage_description);
		link.addListener(SWT.Selection, new LanguageMappingLinkListener(parent.getShell(), project) {
			@Override
			protected void refresh() {
				try {
					refreshMappings();
				} catch (CoreException e) {
					CUIPlugin.log(e);
				}
			}
		});
		
		link.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false, 2, 1));
		
		fContents.pack();
		return fContents;
	}

	private void createMappingTable(Composite contents, final IFile file) throws CoreException {
		Composite tableParent = new Composite(contents, SWT.NONE);
		tableParent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));

		fTable = new Table(tableParent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.FULL_SELECTION);
		fTable.setHeaderVisible(true);
		fTable.setLinesVisible(true);
		fTable.getAccessible().addAccessibleListener(new AccessibleAdapter() {
			@Override
			public void getName(AccessibleEvent e) {
				e.result = PreferencesMessages.FileLanguagesPropertyPage_mappingTableTitle;
			}
		});
		fTable.setToolTipText(PreferencesMessages.FileLanguagesPropertyPage_mappingTableTitle);

		TableColumn contentTypeColumn = new TableColumn(fTable, SWT.LEAD);
		contentTypeColumn.setText(PreferencesMessages.FileLanguagesPropertyPage_configurationColumn);

		TableColumn languageColumn = new TableColumn(fTable, SWT.LEAD);
		languageColumn.setText(PreferencesMessages.ProjectLanguagesPropertyPage_languageColumn);

		TableColumnLayout layout = new TableColumnLayout();
		layout.setColumnData(contentTypeColumn, new ColumnWeightData(1, MINIMUM_COLUMN_WIDTH, true));
		layout.setColumnData(languageColumn, new ColumnWeightData(1, MINIMUM_COLUMN_WIDTH, true));
		tableParent.setLayout(layout);
		
		final TableEditor editor = new TableEditor(fTable);
		editor.grabHorizontal = true;
		editor.grabVertical = true;
		editor.setColumn(LANGUAGE_COLUMN);
		
		final IProject project = file.getProject();

		fTable.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				Control oldEditor = editor.getEditor();
				if (oldEditor != null) {
					oldEditor.dispose();
				}
				
				TableItem item = (TableItem) event.item;
				if (item == null) {
					return;
				}

				LanguageTableData data = (LanguageTableData) item.getData();
				CCombo newEditor = new CCombo(fTable, SWT.READ_ONLY);
				populateLanguages(project, file, data.configuration, data.languageId, newEditor);
				
				newEditor.addListener(SWT.Selection, new Listener() {
					public void handleEvent(Event event) {
						CCombo combo = (CCombo) editor.getEditor();
						int index = combo.getSelectionIndex();
						if (index != -1) {
							TableItem item = editor.getItem();
							item.setText(LANGUAGE_COLUMN, combo.getText());
							
							String selectedLanguage = ((String[]) combo.getData())[index];
							LanguageTableData data = (LanguageTableData) item.getData();
							data.languageId = selectedLanguage;
							fHasChanges = true;
							
							try {
								refreshMappings();
							} catch (CoreException e) {
								CUIPlugin.log(e);
							}
						}
					}
				});
				
				newEditor.setFocus();
				editor.setEditor(newEditor, item, LANGUAGE_COLUMN);
			}
		});
		
		populateLanguageTable(fTable);
		refreshMappings();
	}

	private void populateLanguageTable(Table table) throws CoreException {
		IFile file = getFile();
		IProject project = file.getProject();
		ICProjectDescription description = CoreModel.getDefault().getProjectDescription(project);
		ICConfigurationDescription[] configurations = description.getConfigurations();
		
		TableItem defaultItem = new TableItem(table, SWT.NONE);
		defaultItem.setText(CONFIGURATION_COLUMN, PreferencesMessages.FileLanguagesPropertyPage_defaultMapping);
		
		ProjectLanguageConfiguration config = LanguageManager.getInstance().getLanguageConfiguration(project);
		
		Set<String> missingLanguages = LanguageVerifier.removeMissingLanguages(config, description, fLanguageIds);
		if (missingLanguages.size() > 0) {
			MessageBox messageBox = new MessageBox(getShell(), SWT.ICON_WARNING | SWT.OK);
			messageBox.setText(PreferencesMessages.LanguageMappings_missingLanguageTitle);
			String affectedLanguages = LanguageVerifier.computeAffectedLanguages(missingLanguages);
			messageBox.setMessage(Messages.format(PreferencesMessages.FileLanguagesPropertyPage_missingLanguage, affectedLanguages));
			messageBox.open();
		}
		
		String defaultLanguageId = config.getLanguageForFile(null, file);
		LanguageTableData defaultData = new LanguageTableData(null, defaultLanguageId );
		defaultItem.setData(defaultData);
		
		for (int i = 0; i < configurations.length; i++) {
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(CONFIGURATION_COLUMN, configurations[i].getName());
			String languageId = config.getLanguageForFile(configurations[i], file);
			
			if (languageId != null) {
				ILanguage language = fLanguageIds.get(languageId);
				String languageName =  language.getName();
				item.setText(LANGUAGE_COLUMN, languageName);
			}
			
			LanguageTableData data = new LanguageTableData(configurations[i], languageId);
			item.setData(data);
		}
	}

	private void populateLanguages(IProject project, IFile file, ICConfigurationDescription configuration, String selectedLanguage, CCombo combo) {
		try {
			String[][] languageInfo = getLanguages(project, file, configuration);
			combo.setItems(languageInfo[LANGUAGE_NAME]);
			combo.setData(languageInfo[LANGUAGE_ID]);
			
			findSelection(configuration, selectedLanguage, combo);
			fContents.layout();
		} catch (CoreException e) {
			CCorePlugin.log(e);
		}
	}
	
	private void refreshMappings() throws CoreException {
		IFile file = getFile();
		IProject project = file.getProject();
		
		LanguageManager manager = LanguageManager.getInstance();
		TableItem[] items = fTable.getItems();
		for (int i = 0; i < items.length; i++) {
			TableItem item = items[i];
			LanguageTableData data = (LanguageTableData) item.getData();
			if (data.languageId == null) {
				LanguageMapping mapping = computeInheritedMapping(project, file, data.configuration);
				item.setText(LANGUAGE_COLUMN, computeInheritedFrom(data.configuration, mapping));
			} else {
				ILanguage language = manager.getLanguage(data.languageId);
				item.setText(LANGUAGE_COLUMN, language.getName());
			}
		}
	}

	private void findSelection(ICConfigurationDescription configuration, String languageId, CCombo combo) throws CoreException {
//		if (languageId == null) {
//			TableItem[] items = fTable.getItems();
//			for (int i = 0; i < items.length; i++) {
//				LanguageTableData data = (LanguageTableData) items[i].getData();
//				if (configuration == null && data.configuration == null) {
//					languageId = data.languageId;
//					break;
//				} else if (configuration != null && data.configuration != null) {
//					languageId = data.languageId;
//					break;
//				}
//			}
//		}
		
		if (languageId == null) {
			// No mapping was defined so we'll choose the default.
			combo.select(0);
			return;
		}
		
		LanguageManager manager = LanguageManager.getInstance();
		ILanguage language = manager.getLanguage(languageId);
		String name = language.getName();
		
		for (int i = 1; i < combo.getItemCount(); i++) {
			if (name.equals(combo.getItem(i))) {
				combo.select(i);
				return;
			}
		}
		
		// Couldn't find the mapping so we'll choose the default.
		combo.select(0);
	}

	@Override
	public boolean performOk() {
		try {
			if (!fHasChanges) {
				return true;
			}
			
			IFile file = getFile();
			IProject project = file.getProject();
			LanguageManager manager = LanguageManager.getInstance();
			ProjectLanguageConfiguration config = manager.getLanguageConfiguration(project);
			
			Map<String, String> mappings = new TreeMap<String, String>();
			TableItem[] items = fTable.getItems();
			for (int i = 0; i < items.length; i++) {
				TableItem item = items[i];
				LanguageTableData data = (LanguageTableData) item.getData();
				if (data.languageId == null) {
					continue;
				}
				String configurationId;
				if (data.configuration == null) {
					configurationId = ALL_CONFIGURATIONS;
				} else {
					configurationId = data.configuration.getId();
				}
				mappings.put(configurationId, data.languageId);
			}
			config.setFileMappings(file, mappings);
			manager.storeLanguageMappingConfiguration(file);
			fHasChanges = false;
			return true;
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return false;
		}
	}
	
	private IFile getFile() {
		return (IFile) getElement().getAdapter(IFile.class);
	}

	@Override
	protected void performDefaults() {
		super.performDefaults();
	}
	
	private String computeInheritedFrom(ICConfigurationDescription configuration, LanguageMapping mapping) throws CoreException {
		String inheritedFrom;
		ILanguage language;
		
		LanguageTableData data = (LanguageTableData) fTable.getItem(0).getData();
		if (configuration != null && data.languageId != null) {
			inheritedFrom = PreferencesMessages.FileLanguagesPropertyPage_inheritedFromFile;
			language = LanguageManager.getInstance().getLanguage(data.languageId);
		} else {
			language = mapping.language;
			switch (mapping.inheritedFrom) {
			case LanguageMappingResolver.DEFAULT_MAPPING:
				inheritedFrom = PreferencesMessages.FileLanguagesPropertyPage_inheritedFromSystem;
				break;
			case LanguageMappingResolver.PROJECT_MAPPING:
				inheritedFrom = PreferencesMessages.FileLanguagesPropertyPage_inheritedFromProject;
				break;
			case LanguageMappingResolver.WORKSPACE_MAPPING:
				inheritedFrom = PreferencesMessages.FileLanguagesPropertyPage_inheritedFromWorkspace;
				break;
			default:
				throw new CoreException(Util.createStatus(new IllegalArgumentException()));
			}
		}
		return Messages.format(inheritedFrom, language.getName());
	}
	
	private LanguageMapping computeInheritedMapping(IProject project, IFile file, ICConfigurationDescription configuration) throws CoreException {
		LanguageMapping mappings[] = LanguageMappingResolver.computeLanguage(project, file.getProjectRelativePath().toPortableString(), configuration, fContentType.getId(), true);
		LanguageMapping inheritedMapping = mappings[0];
		
		// Skip over the file mapping because we want to know what mapping the file
		// mapping overrides.
		if (inheritedMapping.inheritedFrom == LanguageMappingResolver.FILE_MAPPING ) {
			inheritedMapping = mappings[1];
		}
		
		return inheritedMapping;
	}
	
	private String[][] getLanguages(IProject project, IFile file, ICConfigurationDescription configuration) throws CoreException {
		String[][] descriptions = new String[2][fLanguages.length + 1];
		
		LanguageMapping inheritedMapping = computeInheritedMapping(project, file, configuration);
		
		int index = 0;
		descriptions[LANGUAGE_ID][index] = null;
		descriptions[LANGUAGE_NAME][index] = computeInheritedFrom(configuration, inheritedMapping);

		index++;
		for (int i = 0; i < fLanguages.length; i++) {
			descriptions[LANGUAGE_ID][index] = fLanguages[i].getId();
			descriptions[LANGUAGE_NAME][index] = fLanguages[i].getName();
			index++;
		}
		return descriptions;
	}
	
	private static class LanguageTableData {
		ICConfigurationDescription configuration;
		String languageId;
		
		LanguageTableData(ICConfigurationDescription configuration, String languageId) {
			this.configuration = configuration;
			this.languageId = languageId;
		}
	}
}

Back to the top