|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2024-2025 Espressif Systems (Shanghai) PTE LTD. All rights reserved. |
| 3 | + * Use is subject to license terms. |
| 4 | + *******************************************************************************/ |
| 5 | +package com.espressif.idf.ui.dialogs; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | +import java.util.Optional; |
| 9 | +import java.util.stream.Stream; |
| 10 | + |
| 11 | +import org.eclipse.core.runtime.CoreException; |
| 12 | +import org.eclipse.jface.dialogs.IDialogConstants; |
| 13 | +import org.eclipse.jface.dialogs.IMessageProvider; |
| 14 | +import org.eclipse.jface.dialogs.TitleAreaDialog; |
| 15 | +import org.eclipse.launchbar.core.ILaunchBarManager; |
| 16 | +import org.eclipse.launchbar.core.ILaunchDescriptor; |
| 17 | +import org.eclipse.swt.SWT; |
| 18 | +import org.eclipse.swt.layout.GridData; |
| 19 | +import org.eclipse.swt.layout.GridLayout; |
| 20 | +import org.eclipse.swt.widgets.Combo; |
| 21 | +import org.eclipse.swt.widgets.Composite; |
| 22 | +import org.eclipse.swt.widgets.Control; |
| 23 | +import org.eclipse.swt.widgets.Label; |
| 24 | +import org.eclipse.swt.widgets.Shell; |
| 25 | + |
| 26 | +import com.espressif.idf.core.logging.Logger; |
| 27 | +import com.espressif.idf.ui.UIPlugin; |
| 28 | + |
| 29 | +public class SelectLaunchConfigDialog extends TitleAreaDialog |
| 30 | +{ |
| 31 | + private Combo descriptorsCombo; |
| 32 | + private final List<String> suitableConfiguratios; |
| 33 | + |
| 34 | + public SelectLaunchConfigDialog(Shell parentShell, List<String> suitableConfiguratios) |
| 35 | + { |
| 36 | + super(parentShell); |
| 37 | + this.suitableConfiguratios = suitableConfiguratios; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + protected void configureShell(Shell newShell) |
| 42 | + { |
| 43 | + super.configureShell(newShell); |
| 44 | + newShell.setText(Messages.SelectLaunchConfigDialog_Title); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void create() |
| 49 | + { |
| 50 | + super.create(); |
| 51 | + setTitle(Messages.SelectLaunchConfigDialog_Title); |
| 52 | + setMessage(Messages.SelectLaunchConfigDialog_Text, IMessageProvider.INFORMATION); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected void createButtonsForButtonBar(Composite parent) |
| 57 | + { |
| 58 | + // create OK and Cancel buttons by default |
| 59 | + createButton(parent, IDialogConstants.OK_ID, "Launch", true); //$NON-NLS-1$ |
| 60 | + createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected Control createDialogArea(Composite parent) |
| 65 | + { |
| 66 | + Composite area = (Composite) super.createDialogArea(parent); |
| 67 | + Composite container = new Composite(area, SWT.NONE); |
| 68 | + container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 69 | + GridLayout layout = new GridLayout(2, false); |
| 70 | + container.setLayout(layout); |
| 71 | + |
| 72 | + Label descriptorsLabel = new Label(container, SWT.NONE); |
| 73 | + descriptorsLabel.setText(Messages.SelectLaunchConfigDialog_LableText); |
| 74 | + |
| 75 | + GridData comboLayoutData = new GridData(); |
| 76 | + comboLayoutData.grabExcessHorizontalSpace = true; |
| 77 | + comboLayoutData.horizontalAlignment = GridData.FILL; |
| 78 | + comboLayoutData.horizontalSpan = 1; |
| 79 | + |
| 80 | + descriptorsCombo = new Combo(container, SWT.READ_ONLY); |
| 81 | + descriptorsCombo.setItems(suitableConfiguratios.toArray(new String[0])); |
| 82 | + descriptorsCombo.select(0); |
| 83 | + descriptorsCombo.setLayoutData(comboLayoutData); |
| 84 | + return super.createDialogArea(parent); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected void okPressed() |
| 89 | + { |
| 90 | + ILaunchBarManager launchBarManager = UIPlugin.getService(ILaunchBarManager.class); |
| 91 | + try |
| 92 | + { |
| 93 | + ILaunchDescriptor[] descriptors = launchBarManager.getLaunchDescriptors(); |
| 94 | + Optional<ILaunchDescriptor> optDisc = Stream.of(descriptors) |
| 95 | + .filter(disc -> disc.getName().contentEquals(descriptorsCombo.getText())).findFirst(); |
| 96 | + if (optDisc.isPresent()) |
| 97 | + { |
| 98 | + launchBarManager.setActiveLaunchDescriptor(optDisc.get()); |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | + catch (CoreException e) |
| 103 | + { |
| 104 | + Logger.log(e); |
| 105 | + } |
| 106 | + |
| 107 | + super.okPressed(); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments