|
| 1 | +// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package software.aws.toolkits.jetbrains.services.lambda.execution.local |
| 4 | + |
| 5 | +import com.intellij.openapi.application.runInEdt |
| 6 | +import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory |
| 7 | +import com.intellij.openapi.project.Project |
| 8 | +import com.intellij.openapi.ui.ComboBox |
| 9 | +import com.intellij.openapi.ui.TextComponentAccessor |
| 10 | +import com.intellij.openapi.ui.TextFieldWithBrowseButton |
| 11 | +import com.intellij.ui.IdeBorderFactory |
| 12 | +import com.intellij.ui.SortedComboBoxModel |
| 13 | +import com.intellij.util.ui.JBUI |
| 14 | +import com.intellij.util.ui.UIUtil |
| 15 | +import org.jetbrains.yaml.YAMLFileType |
| 16 | +import software.amazon.awssdk.services.lambda.model.Runtime |
| 17 | +import software.aws.toolkits.core.utils.tryOrNull |
| 18 | +import software.aws.toolkits.jetbrains.services.cloudformation.Function |
| 19 | +import software.aws.toolkits.jetbrains.services.lambda.LambdaWidgets.lambdaMemory |
| 20 | +import software.aws.toolkits.jetbrains.services.lambda.LambdaWidgets.lambdaTimeout |
| 21 | +import software.aws.toolkits.jetbrains.services.lambda.execution.LambdaInputPanel |
| 22 | +import software.aws.toolkits.jetbrains.services.lambda.sam.SamTemplateUtils.findFunctionsFromTemplate |
| 23 | +import software.aws.toolkits.jetbrains.services.lambda.validOrNull |
| 24 | +import software.aws.toolkits.jetbrains.ui.EnvironmentVariablesTextField |
| 25 | +import software.aws.toolkits.jetbrains.ui.HandlerPanel |
| 26 | +import software.aws.toolkits.jetbrains.ui.ProjectFileBrowseListener |
| 27 | +import software.aws.toolkits.jetbrains.ui.SliderPanel |
| 28 | +import software.aws.toolkits.jetbrains.utils.ui.addQuickSelect |
| 29 | +import software.aws.toolkits.jetbrains.utils.ui.find |
| 30 | +import software.aws.toolkits.resources.message |
| 31 | +import java.io.File |
| 32 | +import java.util.Comparator |
| 33 | +import javax.swing.DefaultComboBoxModel |
| 34 | +import javax.swing.JCheckBox |
| 35 | +import javax.swing.JComboBox |
| 36 | +import javax.swing.JPanel |
| 37 | + |
| 38 | +class LocalLambdaRunSettingsEditorPanel(private val project: Project) { |
| 39 | + lateinit var panel: JPanel |
| 40 | + lateinit var handlerPanel: HandlerPanel |
| 41 | + lateinit var environmentVariables: EnvironmentVariablesTextField |
| 42 | + private lateinit var runtimeModel: SortedComboBoxModel<Runtime> |
| 43 | + lateinit var runtime: JComboBox<Runtime> |
| 44 | + lateinit var lambdaInput: LambdaInputPanel |
| 45 | + lateinit var useTemplate: JCheckBox |
| 46 | + lateinit var function: JComboBox<Function> |
| 47 | + private lateinit var functionModels: DefaultComboBoxModel<Function> |
| 48 | + lateinit var templateFile: TextFieldWithBrowseButton |
| 49 | + lateinit var lambdaInputPanel: JPanel |
| 50 | + lateinit var timeoutSlider: SliderPanel |
| 51 | + lateinit var memorySlider: SliderPanel |
| 52 | + lateinit var invalidator: JCheckBox |
| 53 | + |
| 54 | + var lastSelectedRuntime: Runtime? = null |
| 55 | + |
| 56 | + private fun createUIComponents() { |
| 57 | + handlerPanel = HandlerPanel(project) |
| 58 | + lambdaInput = LambdaInputPanel(project) |
| 59 | + functionModels = DefaultComboBoxModel() |
| 60 | + function = ComboBox(functionModels) |
| 61 | + function.addActionListener { updateComponents() } |
| 62 | + runtimeModel = SortedComboBoxModel(compareBy(Comparator.naturalOrder()) { it: Runtime -> it.toString() }) |
| 63 | + runtime = ComboBox(runtimeModel) |
| 64 | + environmentVariables = EnvironmentVariablesTextField() |
| 65 | + timeoutSlider = lambdaTimeout() |
| 66 | + memorySlider = lambdaMemory() |
| 67 | + } |
| 68 | + |
| 69 | + init { |
| 70 | + lambdaInputPanel.border = IdeBorderFactory.createTitledBorder(message("lambda.input.label"), false, JBUI.emptyInsets()) |
| 71 | + useTemplate.addActionListener { updateComponents() } |
| 72 | + templateFile.textField.addQuickSelect(useTemplate, Runnable { updateComponents() }) |
| 73 | + templateFile.addActionListener( |
| 74 | + ProjectFileBrowseListener( |
| 75 | + project, |
| 76 | + templateFile, |
| 77 | + FileChooserDescriptorFactory.createSingleFileDescriptor(YAMLFileType.YML), |
| 78 | + TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT |
| 79 | + ) { |
| 80 | + setTemplateFile(it.canonicalPath) |
| 81 | + } |
| 82 | + ) |
| 83 | + runtime.addActionListener { |
| 84 | + val index = runtime.selectedIndex |
| 85 | + if (index < 0) { |
| 86 | + lastSelectedRuntime = null |
| 87 | + return@addActionListener |
| 88 | + } |
| 89 | + val selectedRuntime = runtime.getItemAt(index) |
| 90 | + if (selectedRuntime == lastSelectedRuntime) return@addActionListener |
| 91 | + lastSelectedRuntime = selectedRuntime |
| 92 | + handlerPanel.setRuntime(selectedRuntime) |
| 93 | + } |
| 94 | + updateComponents() |
| 95 | + } |
| 96 | + |
| 97 | + private fun updateComponents() { |
| 98 | + val handler = handlerPanel.handler |
| 99 | + handlerPanel.isEnabled = !useTemplate.isSelected |
| 100 | + runtime.isEnabled = !useTemplate.isSelected |
| 101 | + templateFile.isEnabled = useTemplate.isSelected |
| 102 | + timeoutSlider.setEnabled(!useTemplate.isSelected) |
| 103 | + memorySlider.setEnabled(!useTemplate.isSelected) |
| 104 | + if (useTemplate.isSelected) { |
| 105 | + handler.background = UIUtil.getComboBoxDisabledBackground() |
| 106 | + handler.foreground = UIUtil.getComboBoxDisabledForeground() |
| 107 | + if (functionModels.selectedItem is Function) { |
| 108 | + val selected = functionModels.selectedItem as Function |
| 109 | + handler.text = selected.handler() |
| 110 | + val memorySize = selected.memorySize() |
| 111 | + val timeout = selected.timeout() |
| 112 | + if (memorySize != null) { |
| 113 | + memorySlider.value = memorySize |
| 114 | + } |
| 115 | + if (timeout != null) { |
| 116 | + timeoutSlider.value = timeout |
| 117 | + } |
| 118 | + val runtime = Runtime.fromValue(tryOrNull { selected.runtime() }) |
| 119 | + runtimeModel.selectedItem = runtime.validOrNull |
| 120 | + function.isEnabled = true |
| 121 | + } |
| 122 | + } else { |
| 123 | + handler.background = UIUtil.getTextFieldBackground() |
| 124 | + handler.foreground = UIUtil.getTextFieldForeground() |
| 125 | + function.setEnabled(false) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + fun setTemplateFile(file: String?) { |
| 130 | + if (file == null) { |
| 131 | + templateFile.text = "" |
| 132 | + updateFunctionModel(emptyList()) |
| 133 | + } else { |
| 134 | + templateFile.text = file |
| 135 | + val functions = findFunctionsFromTemplate(project, File(file)) |
| 136 | + updateFunctionModel(functions) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private fun updateFunctionModel(functions: List<Function>) { |
| 141 | + functionModels.removeAllElements() |
| 142 | + function.isEnabled = functions.isNotEmpty() |
| 143 | + functionModels.addAll(functions) |
| 144 | + if (functions.size == 1) { |
| 145 | + functionModels.setSelectedItem(functions[0]) |
| 146 | + } else { |
| 147 | + function.setSelectedIndex(-1) |
| 148 | + } |
| 149 | + updateComponents() |
| 150 | + } |
| 151 | + |
| 152 | + fun selectFunction(logicalFunctionName: String?) { |
| 153 | + logicalFunctionName ?: return |
| 154 | + val function = functionModels.find { it.logicalName == logicalFunctionName } ?: return |
| 155 | + functionModels.selectedItem = function |
| 156 | + updateComponents() |
| 157 | + } |
| 158 | + |
| 159 | + fun setRuntimes(runtimes: List<Runtime>?) { |
| 160 | + runtimeModel.setAll(runtimes) |
| 161 | + } |
| 162 | + |
| 163 | + fun invalidateConfiguration() { |
| 164 | + runInEdt { invalidator.isSelected = !invalidator.isSelected } |
| 165 | + } |
| 166 | +} |
0 commit comments