Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c7dd3bdf009f246658c85ec114054f95bc4f9804 (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
/*******************************************************************************
 * Copyright (c) 2020 Martin Weber.
 *
 * 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
 *******************************************************************************/
package org.eclipse.cdt.cmake.is.core.ui.internal;

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.eclipse.cdt.cmake.is.core.IParserPreferences;
import org.eclipse.cdt.cmake.is.core.IParserPreferencesAccess;
import org.eclipse.cdt.cmake.is.core.IParserPreferencesMetadata;
import org.eclipse.core.runtime.preferences.PreferenceMetadata;
import org.eclipse.e4.core.contexts.EclipseContextFactory;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.osgi.framework.FrameworkUtil;

/**
 * Preference page for indexer support.
 */
public class IndexerSupportPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {

	private Text pattern;
	private Button btnVersionsEnabled;
	private Button btnWithConsole;
	private final IParserPreferencesAccess prefsAccess;

	public IndexerSupportPreferencePage() {
		prefsAccess = EclipseContextFactory.getServiceContext(FrameworkUtil.getBundle(getClass()).getBundleContext())
				.get(IParserPreferencesAccess.class);
		setDescription(Messages.IndexerSupportPreferencePage_description);
	}

	/**
	 * Creates the field editors.
	 */
	@Override
	protected Control createContents(Composite parent) {
		final IParserPreferencesMetadata prefsMeta = prefsAccess.metadata();
		final IParserPreferences prefs = prefsAccess.getWorkspacePreferences();

		final Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));
		GridDataFactory.swtDefaults().applyTo(composite);

		final Group gr = createGroup(composite, SWT.FILL, 1,
				Messages.IndexerSupportPreferencePage_label_version_suffix_group, 2);
		btnVersionsEnabled = createCheckbox(gr, SWT.BEGINNING, 2, prefsMeta.tryVersionSuffix());
		btnVersionsEnabled.setSelection(prefs.getTryVersionSuffix());
		{
			Label label = new Label(gr, SWT.NONE);
			label.setText(Messages.IndexerSupportPreferencePage_label_suffix_pattern);
			GridDataFactory.defaultsFor(label).applyTo(label);
		}

		pattern = new Text(gr, SWT.SINGLE | SWT.BORDER);
		GridDataFactory.defaultsFor(pattern).applyTo(pattern);
		pattern.setToolTipText(prefsMeta.versionSuffixPattern().description());
		pattern.setEnabled(btnVersionsEnabled.getSelection());
		pattern.setText(prefs.getVersionSuffixPattern());
		pattern.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				if (btnVersionsEnabled.getSelection()) {
					final String text = pattern.getText();
					try {
						Pattern.compile(text);
						setErrorMessage(null);
					} catch (PatternSyntaxException ex) {
						String msg = String.format(Messages.IndexerSupportPreferencePage_errmsg_suffix_regex,
								ex.getDescription(), ex.getPattern(), ex.getIndex());
						setErrorMessage(msg);
					}
				}
			}
		});

		// to adjust sensitivity...
		btnVersionsEnabled.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				boolean selected = ((Button) event.widget).getSelection();
				pattern.setEnabled(selected);
				if (!selected) {
					setErrorMessage(null);
				}
			}
		});

		btnWithConsole = createCheckbox(composite, SWT.BEGINNING, 1, prefsMeta.allocateConsole());
		btnWithConsole.setSelection(prefs.getAllocateConsole());

		return composite;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	@Override
	public void init(IWorkbench workbench) {
	}

	@Override
	protected void performDefaults() {
		final IParserPreferencesMetadata prefsMeta = prefsAccess.metadata();
		btnVersionsEnabled.setSelection(prefsMeta.tryVersionSuffix().defaultValue());
		pattern.setText(prefsMeta.versionSuffixPattern().defaultValue());
		btnWithConsole.setSelection(prefsMeta.allocateConsole().defaultValue());
		setErrorMessage(null);
		super.performDefaults();
	}

	@Override
	public boolean performOk() {
		final IParserPreferences prefs = prefsAccess.getWorkspacePreferences();
		prefs.setTryVersionSuffix(btnVersionsEnabled.getSelection());
		prefs.setVersionSuffixPattern(pattern.getText());
		prefs.setAllocateConsole(btnWithConsole.getSelection());
		return true;
	}

	/**
	 * Creates a check-box button.
	 *
	 * @param parent
	 * @param horizontalAlignment how control will be positioned horizontally within
	 *                            a cell of the parent's grid layout, one of:
	 *                            SWT.BEGINNING (or SWT.LEFT), SWT.CENTER, SWT.END
	 *                            (or SWT.RIGHT), or SWT.FILL
	 * @param horizontalSpan      number of column cells in the parent's grid layout
	 *                            that the control will take up.
	 * @param text                text to display on the check-box
	 */
	private static Button createCheckbox(Composite parent, int horizontalAlignment, int horizontalSpan,
			PreferenceMetadata<Boolean> option) {
		Button b = new Button(parent, SWT.CHECK);
		b.setText(option.name());
		b.setToolTipText(option.description());
		GridDataFactory.defaultsFor(b).align(horizontalAlignment, SWT.CENTER).span(horizontalSpan, 1).grab(true, false)
				.applyTo(b);
		return b;
	}

	/**
	 * Creates a group with a grid layout.
	 *
	 * @param parent
	 * @param horizontalAlignment how control will be positioned horizontally within
	 *                            a cell of the parent's grid layout, one of:
	 *                            SWT.BEGINNING (or SWT.LEFT), SWT.CENTER, SWT.END
	 *                            (or SWT.RIGHT), or SWT.FILL
	 * @param horizontalSpan      number of column cells in the parent's grid layout
	 *                            that the control will take up.
	 * @param text                title text to display on the group
	 * @param numColumns          the number of columns in the grid inside the group
	 */
	private static Group createGroup(Composite parent, int horizontalAlignment, int horizontalSpan, String text,
			int numColumns) {
		Group gr = new Group(parent, SWT.NONE);
		gr.setLayout(new GridLayout(numColumns, false));
		gr.setText(text);
		GridDataFactory.defaultsFor(gr).align(horizontalAlignment, SWT.CENTER).span(horizontalSpan, 1).grab(true, false)
				.applyTo(gr);
		return gr;
	}
}

Back to the top