|
10 | 10 | *******************************************************************************/
|
11 | 11 | package org.eclipse.cdt.cmake.ui.internal;
|
12 | 12 |
|
| 13 | +import java.io.BufferedReader; |
| 14 | +import java.io.File; |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.InputStreamReader; |
13 | 17 | import java.nio.file.Path;
|
14 | 18 | import java.util.ArrayList;
|
15 | 19 | import java.util.Collections;
|
|
20 | 24 | import org.eclipse.cdt.cmake.core.ICMakeToolChainFile;
|
21 | 25 | import org.eclipse.cdt.cmake.core.ICMakeToolChainManager;
|
22 | 26 | import org.eclipse.cdt.cmake.core.internal.CMakeToolChainManager;
|
| 27 | +import org.eclipse.cdt.core.CCorePlugin; |
23 | 28 | import org.eclipse.cdt.core.build.IToolChain;
|
| 29 | +import org.eclipse.cdt.core.cdtvariables.CdtVariableException; |
| 30 | +import org.eclipse.cdt.core.cdtvariables.ICdtVariable; |
| 31 | +import org.eclipse.cdt.core.cdtvariables.ICdtVariableManager; |
| 32 | +import org.eclipse.cdt.core.settings.model.ICConfigurationDescription; |
| 33 | +import org.eclipse.cdt.ui.newui.BuildVarListDialog; |
| 34 | +import org.eclipse.cdt.utils.ui.controls.FileListControl; |
24 | 35 | import org.eclipse.core.runtime.CoreException;
|
| 36 | +import org.eclipse.core.runtime.preferences.InstanceScope; |
| 37 | +import org.eclipse.jface.dialogs.Dialog; |
25 | 38 | import org.eclipse.jface.dialogs.MessageDialog;
|
26 | 39 | import org.eclipse.jface.layout.TableColumnLayout;
|
27 | 40 | import org.eclipse.jface.preference.PreferencePage;
|
28 | 41 | import org.eclipse.jface.viewers.ColumnWeightData;
|
29 | 42 | import org.eclipse.jface.window.Window;
|
30 | 43 | import org.eclipse.jface.wizard.WizardDialog;
|
31 | 44 | import org.eclipse.swt.SWT;
|
| 45 | +import org.eclipse.swt.events.ModifyEvent; |
| 46 | +import org.eclipse.swt.events.ModifyListener; |
32 | 47 | import org.eclipse.swt.events.SelectionAdapter;
|
33 | 48 | import org.eclipse.swt.events.SelectionEvent;
|
34 | 49 | import org.eclipse.swt.layout.GridData;
|
35 | 50 | import org.eclipse.swt.layout.GridLayout;
|
36 | 51 | import org.eclipse.swt.widgets.Button;
|
37 | 52 | import org.eclipse.swt.widgets.Composite;
|
38 | 53 | import org.eclipse.swt.widgets.Control;
|
| 54 | +import org.eclipse.swt.widgets.DirectoryDialog; |
39 | 55 | import org.eclipse.swt.widgets.Group;
|
| 56 | +import org.eclipse.swt.widgets.Label; |
| 57 | +import org.eclipse.swt.widgets.Shell; |
40 | 58 | import org.eclipse.swt.widgets.Table;
|
41 | 59 | import org.eclipse.swt.widgets.TableColumn;
|
42 | 60 | import org.eclipse.swt.widgets.TableItem;
|
| 61 | +import org.eclipse.swt.widgets.Text; |
43 | 62 | import org.eclipse.ui.IWorkbench;
|
44 | 63 | import org.eclipse.ui.IWorkbenchPreferencePage;
|
| 64 | +import org.osgi.service.prefs.Preferences; |
45 | 65 |
|
46 | 66 | /**
|
47 | 67 | * GUI page to configure workbench preferences for cmake.
|
48 | 68 | */
|
49 | 69 | public class CMakePreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
|
50 | 70 |
|
| 71 | + private static final String NODENAME = "cmakeEnvironment"; //$NON-NLS-1$ |
| 72 | + private static final String VALUE_DELIMITER = " || "; //$NON-NLS-1$ |
| 73 | + |
51 | 74 | private ICMakeToolChainManager manager;
|
52 | 75 | private Table filesTable;
|
53 | 76 | private Button removeButton;
|
| 77 | + private Button variablesButton; |
| 78 | + private Button testButton; |
| 79 | + private Button browseButton; |
| 80 | + private Button editButton; |
| 81 | + |
| 82 | + private Text cmakeLocationTextBox; |
| 83 | + private Text generatorLocationTextBox; |
| 84 | + |
| 85 | + private String[] generatorLocations; |
| 86 | + private String cmakeLocation; |
| 87 | + private boolean useCmakeToolLocation; |
54 | 88 |
|
55 | 89 | private Map<Path, ICMakeToolChainFile> filesToAdd = new HashMap<>();
|
56 | 90 | private Map<Path, ICMakeToolChainFile> filesToRemove = new HashMap<>();
|
57 | 91 |
|
58 | 92 | @Override
|
59 | 93 | public void init(IWorkbench workbench) {
|
60 | 94 | manager = Activator.getService(ICMakeToolChainManager.class);
|
| 95 | + updateCmakeToolGroupData(); |
61 | 96 | }
|
62 | 97 |
|
63 | 98 | @Override
|
@@ -141,11 +176,138 @@ public void widgetSelected(SelectionEvent e) {
|
141 | 176 | }
|
142 | 177 | });
|
143 | 178 |
|
| 179 | + // CMake tools section |
| 180 | + Group cmakeToolsGroup = new Group(control, SWT.NONE); |
| 181 | + cmakeToolsGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
| 182 | + cmakeToolsGroup.setText(Messages.CMakePreferencePage_CMakeTools); |
| 183 | + cmakeToolsGroup.setLayout(new GridLayout(1, false)); |
| 184 | + |
| 185 | + Composite checkBoxComp = new Composite(cmakeToolsGroup, SWT.NONE); |
| 186 | + checkBoxComp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 187 | + checkBoxComp.setLayout(new GridLayout()); |
| 188 | + |
| 189 | + Button useCMakeToolLocCheckBox = new Button(checkBoxComp, SWT.CHECK); |
| 190 | + useCMakeToolLocCheckBox.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 3, 1)); |
| 191 | + useCMakeToolLocCheckBox.setText(Messages.CMakePreferencePage_UseCMakeToolLocationsInCMakeBuilds); |
| 192 | + useCMakeToolLocCheckBox.setToolTipText(Messages.CMakePreferencePage_UseCMakeToolLocationsInCMakeBuildsTooltip); |
| 193 | + useCMakeToolLocCheckBox.setSelection(useCmakeToolLocation); |
| 194 | + useCMakeToolLocCheckBox.addListener(SWT.Selection, e -> { |
| 195 | + useCmakeToolLocation = useCMakeToolLocCheckBox.getSelection(); |
| 196 | + updateCMakeGroup(useCmakeToolLocation); |
| 197 | + }); |
| 198 | + |
| 199 | + Composite locationComp = new Composite(cmakeToolsGroup, SWT.NONE); |
| 200 | + locationComp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); |
| 201 | + locationComp.setLayout(new GridLayout(3, false)); |
| 202 | + |
| 203 | + Label cmakeLocationLabel = new Label(locationComp, SWT.NONE); |
| 204 | + cmakeLocationLabel.setText(Messages.CMakePreferencePage_CMakeLocation); |
| 205 | + cmakeLocationLabel.setToolTipText(Messages.CMakePreferencePage_CMakeLocationTooltip); |
| 206 | + |
| 207 | + cmakeLocationTextBox = new Text(locationComp, SWT.BORDER); |
| 208 | + cmakeLocationTextBox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true)); |
| 209 | + cmakeLocationTextBox.setText(cmakeLocation); |
| 210 | + cmakeLocationTextBox.addModifyListener(new ModifyListener() { |
| 211 | + @Override |
| 212 | + public void modifyText(ModifyEvent evt) { |
| 213 | + cmakeLocation = resolveVariableValue(cmakeLocationTextBox.getText()); |
| 214 | + } |
| 215 | + }); |
| 216 | + |
| 217 | + Composite cmakeLocationButtonComp = new Composite(locationComp, SWT.NONE); |
| 218 | + cmakeLocationButtonComp.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 219 | + cmakeLocationButtonComp.setLayout(new GridLayout(3, true)); |
| 220 | + |
| 221 | + variablesButton = new Button(cmakeLocationButtonComp, SWT.PUSH); |
| 222 | + variablesButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
| 223 | + variablesButton.setText(Messages.CMakePreferencePage_Variables); |
| 224 | + variablesButton.setData(cmakeLocationTextBox); |
| 225 | + variablesButton.addListener(SWT.Selection, e -> { |
| 226 | + String x = null; |
| 227 | + if (variablesButton.getData() instanceof Text t) { |
| 228 | + x = getVariableDialog(getShell(), null); |
| 229 | + if (x != null) { |
| 230 | + t.insert(x); |
| 231 | + } |
| 232 | + } |
| 233 | + }); |
| 234 | + |
| 235 | + testButton = new Button(cmakeLocationButtonComp, SWT.PUSH); |
| 236 | + testButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
| 237 | + testButton.setText(Messages.CMakePreferencePage_Test); |
| 238 | + testButton.setToolTipText(Messages.CMakePreferencePage_TestTooltip); |
| 239 | + testButton.setData(cmakeLocationTextBox); |
| 240 | + testButton.addListener(SWT.Selection, e -> { |
| 241 | + try { |
| 242 | + Process p = Runtime.getRuntime() |
| 243 | + .exec(new String[] { cmakeLocation + File.separatorChar + "cmake", "--version" }); //$NON-NLS-1$ //$NON-NLS-2$ |
| 244 | + List<String> buf = new ArrayList<>(); |
| 245 | + BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); |
| 246 | + String line; |
| 247 | + while ((line = br.readLine()) != null) { |
| 248 | + buf.add(line); |
| 249 | + } |
| 250 | + MessageDialog.openInformation(getShell(), Messages.CMakePreferencePage_TestCmakeLocation_Title, |
| 251 | + Messages.CMakePreferencePage_TestCmakeLocation_Body + String.join(System.lineSeparator(), buf)); |
| 252 | + } catch (IOException e1) { |
| 253 | + MessageDialog.openError(getShell(), Messages.CMakePreferencePage_FailToTestCmakeLocation_Title, |
| 254 | + Messages.CMakePreferencePage_FailToTestCmakeLocation_Body + e1.getMessage()); |
| 255 | + } |
| 256 | + }); |
| 257 | + |
| 258 | + browseButton = new Button(cmakeLocationButtonComp, SWT.PUSH); |
| 259 | + browseButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
| 260 | + browseButton.setText(Messages.CMakePreferencePage_Browse); |
| 261 | + browseButton.setData(cmakeLocationTextBox); |
| 262 | + browseButton.addListener(SWT.Selection, e -> { |
| 263 | + DirectoryDialog dirDialog = new DirectoryDialog(getShell()); |
| 264 | + String browsedDirectory = dirDialog.open(); |
| 265 | + if (browsedDirectory != null && browseButton.getData() instanceof Text t) { |
| 266 | + t.setText(browsedDirectory); |
| 267 | + } |
| 268 | + }); |
| 269 | + |
| 270 | + Label generatorLocationsLabel = new Label(locationComp, SWT.NONE); |
| 271 | + generatorLocationsLabel.setText(Messages.CMakePreferencePage_GeneratorLocation); |
| 272 | + generatorLocationsLabel.setToolTipText(Messages.CMakePreferencePage_GeneratorLocationTooltip); |
| 273 | + |
| 274 | + generatorLocationTextBox = new Text(locationComp, SWT.BORDER); |
| 275 | + generatorLocationTextBox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 276 | + generatorLocationTextBox.setEditable(false); |
| 277 | + generatorLocationTextBox.setText(String.join(VALUE_DELIMITER, generatorLocations)); |
| 278 | + |
| 279 | + Composite generatorLocationButtonComp = new Composite(locationComp, SWT.NONE); |
| 280 | + generatorLocationButtonComp.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); |
| 281 | + generatorLocationButtonComp.setLayout(new GridLayout(3, true)); |
| 282 | + |
| 283 | + editButton = new Button(generatorLocationButtonComp, SWT.PUSH); |
| 284 | + editButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
| 285 | + editButton.setText(Messages.CMakePreferencePage_Edit); |
| 286 | + editButton.setData(generatorLocationTextBox); |
| 287 | + editButton.addListener(SWT.Selection, e -> { |
| 288 | + EditGenerationLocationDialog dialog = new EditGenerationLocationDialog(getShell(), |
| 289 | + Messages.CMakePreferencePage_EditGeneratorLocations_Title, generatorLocations); |
| 290 | + if (dialog.open() == Window.OK && editButton.getData() instanceof Text t) { |
| 291 | + generatorLocations = dialog.getValues(); |
| 292 | + t.setText(String.join(VALUE_DELIMITER, generatorLocations)); |
| 293 | + } |
| 294 | + }); |
| 295 | + |
144 | 296 | updateTable();
|
| 297 | + updateCMakeGroup(useCmakeToolLocation); |
145 | 298 |
|
146 | 299 | return control;
|
147 | 300 | }
|
148 | 301 |
|
| 302 | + protected void updateCMakeGroup(boolean enable) { |
| 303 | + cmakeLocationTextBox.setEnabled(enable); |
| 304 | + generatorLocationTextBox.setEnabled(enable); |
| 305 | + variablesButton.setEnabled(enable); |
| 306 | + testButton.setEnabled(enable); |
| 307 | + browseButton.setEnabled(enable); |
| 308 | + editButton.setEnabled(enable); |
| 309 | + } |
| 310 | + |
149 | 311 | private void updateTable() {
|
150 | 312 | List<ICMakeToolChainFile> sorted = new ArrayList<>(getFiles().values());
|
151 | 313 | Collections.sort(sorted, (o1, o2) -> o1.getPath().toString().compareToIgnoreCase(o2.getPath().toString()));
|
@@ -205,7 +367,89 @@ public boolean performOk() {
|
205 | 367 | filesToAdd.clear();
|
206 | 368 | filesToRemove.clear();
|
207 | 369 |
|
| 370 | + // Update Preferences for cmakeSupplier |
| 371 | + getPreferences().putBoolean("useCmakeToolLocation", useCmakeToolLocation); //$NON-NLS-1$ |
| 372 | + getPreferences().put("cmakeLocation", cmakeLocation); //$NON-NLS-1$ |
| 373 | + getPreferences().put("generatorLocations", String.join(VALUE_DELIMITER, generatorLocations)); //$NON-NLS-1$ |
| 374 | + |
208 | 375 | return true;
|
209 | 376 | }
|
210 | 377 |
|
| 378 | + public void updateCmakeToolGroupData() { |
| 379 | + useCmakeToolLocation = getPreferences().getBoolean("useCmakeToolLocation", false); //$NON-NLS-1$ |
| 380 | + cmakeLocation = getPreferences().get("cmakeLocation", ""); //$NON-NLS-1$ //$NON-NLS-2$ |
| 381 | + String genLocations = getPreferences().get("generatorLocations", ""); //$NON-NLS-1$ //$NON-NLS-2$ |
| 382 | + generatorLocations = genLocations.length() > 0 ? genLocations.split(" \\|\\| ") : new String[0]; //$NON-NLS-1$ |
| 383 | + } |
| 384 | + |
| 385 | + private String resolveVariableValue(String value) { |
| 386 | + try { |
| 387 | + ICdtVariableManager vm = CCorePlugin.getDefault().getCdtVariableManager(); |
| 388 | + return vm.resolveValue(value, null, "", null); //$NON-NLS-1$ |
| 389 | + } catch (CdtVariableException e) { |
| 390 | + e.printStackTrace(); |
| 391 | + } |
| 392 | + return null; |
| 393 | + } |
| 394 | + |
| 395 | + private String getVariableDialog(Shell shell, ICConfigurationDescription cfgd) { |
| 396 | + ICdtVariableManager vm = CCorePlugin.getDefault().getCdtVariableManager(); |
| 397 | + BuildVarListDialog dialog = new BuildVarListDialog(shell, vm.getVariables(cfgd)); |
| 398 | + dialog.setTitle(Messages.VariablesDialog_Title); |
| 399 | + if (dialog.open() == Window.OK) { |
| 400 | + Object[] selected = dialog.getResult(); |
| 401 | + if (selected.length > 0) { |
| 402 | + String s = ((ICdtVariable) selected[0]).getName(); |
| 403 | + return "${" + s.trim() + "}"; //$NON-NLS-1$//$NON-NLS-2$ |
| 404 | + } |
| 405 | + } |
| 406 | + return null; |
| 407 | + } |
| 408 | + |
| 409 | + class EditGenerationLocationDialog extends Dialog { |
| 410 | + |
| 411 | + private String fTitle; |
| 412 | + private FileListControl fListEditor; |
| 413 | + private String[] fGeneratorLocations; |
| 414 | + |
| 415 | + public EditGenerationLocationDialog(Shell parentShell, String title, String[] generatorLocations) { |
| 416 | + super(parentShell); |
| 417 | + this.fGeneratorLocations = generatorLocations; |
| 418 | + } |
| 419 | + |
| 420 | + @Override |
| 421 | + protected void configureShell(Shell shell) { |
| 422 | + super.configureShell(shell); |
| 423 | + if (fTitle != null) { |
| 424 | + shell.setText(fTitle); |
| 425 | + } |
| 426 | + } |
| 427 | + |
| 428 | + @Override |
| 429 | + protected Control createDialogArea(Composite parent) { |
| 430 | + Composite comp = new Composite(parent, SWT.NULL); |
| 431 | + comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 432 | + comp.setLayout(new GridLayout()); |
| 433 | + fListEditor = new FileListControl(comp, |
| 434 | + Messages.cMakePreferencePage_EditGeneratorLocations_GeneratorLocation, 0); |
| 435 | + if (fGeneratorLocations != null) { |
| 436 | + fListEditor.setList(fGeneratorLocations); |
| 437 | + } |
| 438 | + return comp; |
| 439 | + } |
| 440 | + |
| 441 | + @Override |
| 442 | + protected void okPressed() { |
| 443 | + fGeneratorLocations = fListEditor.getItems(); |
| 444 | + super.okPressed(); |
| 445 | + } |
| 446 | + |
| 447 | + public String[] getValues() { |
| 448 | + return fGeneratorLocations; |
| 449 | + } |
| 450 | + } |
| 451 | + |
| 452 | + private Preferences getPreferences() { |
| 453 | + return InstanceScope.INSTANCE.getNode(CCorePlugin.PLUGIN_ID).node(NODENAME); |
| 454 | + } |
211 | 455 | }
|
0 commit comments