Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eb97e629af93d9fdc14a8a07fc95b77f3d057321 (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
/*******************************************************************************
 * Copyright (c) 2004, 2006 TimeSys 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:
 *     TimeSys Corporation - Initial implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.preferences;

import java.util.ArrayList;

import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class CFileTypeDialog extends Dialog {
	
	public CFileTypeDialog(Shell parentShell) {
		super(parentShell);
	}

	private Text		fTextPattern;
	private Combo		fComboType;

	private String		fPattern;
	private IContentType fType;
	
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(PreferencesMessages.CFileTypeDialog_title); 
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);

		getOkayButton().setEnabled(getPatternFromControl().length() > 0);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);
		((GridLayout) composite.getLayout()).numColumns = 2;

		// Pattern row
		
		Label pattern = new Label(composite, SWT.NONE);
				
		pattern.setText(PreferencesMessages.CFileTypeDialog_patternLabel); 

		fTextPattern = new Text(composite, SWT.BORDER | SWT.SINGLE);
		
		fTextPattern.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		if (null != fPattern) {
			fTextPattern.setText(fPattern);
		}

		fTextPattern.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				getOkayButton().setEnabled(getPatternFromControl().length() > 0);
			}
		});
		
		// Type row
		
		Label type = new Label(composite, SWT.NONE);
		
		type.setText(PreferencesMessages.CFileTypeDialog_typeLabel); 
		
		fComboType = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY | SWT.SINGLE);

		fComboType.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		populateTypesCombo();
		
		return composite;
	}

	public void setPattern(String pattern) {
		fPattern = pattern;
	}

	public String getPattern() {
		return fPattern;
	}
	
	public IContentType getContentType() {
		return fType;
	}

	private void populateTypesCombo() {
		IContentTypeManager manager = Platform.getContentTypeManager();
		String[]	ids = CoreModel.getRegistedContentTypeIds();
		ArrayList list = new ArrayList(ids.length);
		for (int i = 0; i < ids.length; i++) {
			IContentType ctype = manager.getContentType(ids[i]);
			if (ctype != null) {
				list.add(ctype);
			}
		}

		IContentType[] ctypes = new IContentType[list.size()];
		list.toArray(ctypes);
		int	index = -1;

		for (int i = 0; i < ctypes.length; i++) {
			fComboType.add(ctypes[i].getName());
		}
		
		fComboType.setData(ctypes);

		if (null != fType) {
			index = fComboType.indexOf(fType.getName());
		}

		fComboType.select((index < 0) ? 0 : index);
	}

	Button getOkayButton() {
		return getButton(IDialogConstants.OK_ID);
	}
	
	String getPatternFromControl() {
		return fTextPattern.getText().trim();
	}
	
	private IContentType getTypeFromControl() {
		IContentType type	= null;
		int		index	= fComboType.getSelectionIndex();
		
		if (-1 != index) {
			String			name	= fComboType.getItem(index);
			IContentType[]	types	= (IContentType[]) fComboType.getData();
			for (int i = 0; i < types.length; i++) {
				if (name.equals(types[i].getName())) {
					type = types[i];
				}
			}
		}
		return type;
	}
	
	@Override
	protected void okPressed() {
		fPattern 	=	getPatternFromControl();
		fType		=	getTypeFromControl();
		
		super.okPressed();
	}

	
}

Back to the top