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