Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 61760ada7e6f75e358585526c14feab7fdd023cf (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
/*******************************************************************************
 * Copyright (c) 2007, 2008 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.HashMap;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.cdt.core.model.LanguageManager;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Table;

public abstract class LanguageMappingWidget {

	protected static final int MINIMUM_COLUMN_WIDTH = 150;
	protected Composite fContents;
	protected boolean fIsReadOnly;

	protected Table fTable;
	protected HashMap<String, String> fContentTypeNamesToIDsMap;
	protected Set<IContentType> fAffectedContentTypes;
	protected Font fOverriddenFont;
	protected LanguageMappingWidget fChild;
	protected IAdaptable fElement;

	protected Set<String> fOverriddenContentTypes;

	private boolean fIsChanged;

	public LanguageMappingWidget() {
		fOverriddenFont = JFaceResources.getFontRegistry().getItalic(JFaceResources.DIALOG_FONT);
		fOverriddenContentTypes = Collections.emptySet();

		// keep a mapping of all registered content types and their names
		fContentTypeNamesToIDsMap = new HashMap<>();
		String[] contentTypesIDs = LanguageManager.getInstance().getRegisteredContentTypeIds();

		IContentTypeManager contentTypeManager = Platform.getContentTypeManager();

		for (int i = 0; i < contentTypesIDs.length; i++) {

			String name = contentTypeManager.getContentType(contentTypesIDs[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, contentTypesIDs[i]);
		}

		fAffectedContentTypes = new HashSet<>();
	}

	public IAdaptable getElement() {
		return fElement;
	}

	public void setElement(IAdaptable element) {
		fElement = element;
	}

	public void setOverriddenContentTypes(Set<String> contentTypes) {
		fOverriddenContentTypes = contentTypes;
	}

	public IContentType[] getAffectedContentTypes() {
		return fAffectedContentTypes.toArray(new IContentType[fAffectedContentTypes.size()]);
	}

	public void setReadOnly(boolean isReadOnly) {
		fIsReadOnly = isReadOnly;
	}

	public void setChild(LanguageMappingWidget child) {
		fChild = child;
	}

	public boolean isChanged() {
		return fIsChanged;
	}

	public void setChanged(boolean changed) {
		fIsChanged = changed;
	}

	protected void createHeader(Composite parent, String description) {
		Link link = new Link(fContents, SWT.NONE);
		link.setText(description);

		link.addListener(SWT.Selection, new LanguageMappingLinkListener(fContents.getShell(), getElement()) {
			@Override
			protected void refresh() {
				refreshMappings();
			}
		});

		GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
		gridData.widthHint = MINIMUM_COLUMN_WIDTH * 2;
		gridData.horizontalSpan = 2;
		link.setLayoutData(gridData);
	}

	public abstract Composite createContents(Composite parent, String description);

	public abstract void refreshMappings();
}

Back to the top