|
| 1 | +/* |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.microsoft.azure.toolkit.intellij.webapp.runner.webappconfig.slimui; |
| 7 | + |
| 8 | +import com.intellij.icons.AllIcons; |
| 9 | +import com.intellij.openapi.project.Project; |
| 10 | +import com.intellij.ui.SimpleListCellRenderer; |
| 11 | +import com.intellij.ui.components.fields.ExtendableTextComponent; |
| 12 | +import com.microsoft.azure.toolkit.ide.appservice.model.AppServiceConfig; |
| 13 | +import com.microsoft.azure.toolkit.intellij.common.AzureComboBox; |
| 14 | +import com.microsoft.azure.toolkit.lib.appservice.model.JavaVersion; |
| 15 | +import com.microsoft.azure.toolkit.lib.appservice.model.Runtime; |
| 16 | +import com.microsoft.azure.toolkit.lib.appservice.service.IAppService; |
| 17 | +import com.microsoft.azure.toolkit.lib.common.model.ResourceGroup; |
| 18 | +import com.microsoft.azure.toolkit.lib.webapp.WebAppService; |
| 19 | +import com.microsoft.azuretools.azurecommons.helpers.NotNull; |
| 20 | +import com.microsoft.azuretools.azurecommons.helpers.Nullable; |
| 21 | +import org.apache.commons.lang3.StringUtils; |
| 22 | +import rx.Subscription; |
| 23 | + |
| 24 | +import javax.swing.*; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Objects; |
| 27 | +import java.util.Optional; |
| 28 | + |
| 29 | +public abstract class AppServiceComboBox<T extends AppServiceConfig> extends AzureComboBox<T> { |
| 30 | + |
| 31 | + protected Project project; |
| 32 | + protected Subscription subscription; |
| 33 | + |
| 34 | + protected T configModel; |
| 35 | + |
| 36 | + public AppServiceComboBox(final Project project) { |
| 37 | + super(false); |
| 38 | + this.project = project; |
| 39 | + this.setRenderer(new AppComboBoxRender()); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void setValue(T value) { |
| 44 | + if (isDraftResource(value)) { |
| 45 | + configModel = value; |
| 46 | + } |
| 47 | + super.setValue(value); |
| 48 | + } |
| 49 | + |
| 50 | + @NotNull |
| 51 | + @Override |
| 52 | + protected List<? extends T> loadItems() throws Exception { |
| 53 | + final List<T> items = loadAppServiceModels(); |
| 54 | + if (configModel != null && StringUtils.isEmpty(configModel.getResourceId())) { |
| 55 | + final boolean exist = items.stream().anyMatch(item -> AppServiceConfig.isSameApp(item, configModel)); |
| 56 | + if (!exist) { |
| 57 | + items.add(configModel); |
| 58 | + } |
| 59 | + } |
| 60 | + return items; |
| 61 | + } |
| 62 | + |
| 63 | + protected abstract List<T> loadAppServiceModels() throws Exception; |
| 64 | + |
| 65 | + @Nullable |
| 66 | + @Override |
| 67 | + protected ExtendableTextComponent.Extension getExtension() { |
| 68 | + return ExtendableTextComponent.Extension.create(AllIcons.General.Add, "Create", this::createResource); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected String getItemText(final Object item) { |
| 73 | + if (item instanceof AppServiceConfig) { |
| 74 | + final AppServiceConfig selectedItem = (AppServiceConfig) item; |
| 75 | + return isDraftResource(selectedItem) ? String.format("(New) %s", selectedItem.getName()) : selectedItem.getName(); |
| 76 | + } else { |
| 77 | + return Objects.toString(item, StringUtils.EMPTY); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + protected boolean isJavaAppService(IAppService<?> appService) { |
| 82 | + try { |
| 83 | + return Optional.ofNullable(appService.getRuntime()).map(Runtime::getJavaVersion) |
| 84 | + .map(javaVersion -> !Objects.equals(javaVersion, JavaVersion.OFF)) |
| 85 | + .orElse(false); |
| 86 | + } catch (final RuntimeException e) { |
| 87 | + // app service may have been removed while parsing, return false in this case |
| 88 | + return false; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + protected abstract void createResource(); |
| 93 | + |
| 94 | + public static class AppComboBoxRender extends SimpleListCellRenderer { |
| 95 | + |
| 96 | + @Override |
| 97 | + public void customize(JList list, Object value, int index, boolean b, boolean b1) { |
| 98 | + if (value instanceof AppServiceConfig) { |
| 99 | + final AppServiceConfig app = (AppServiceConfig) value; |
| 100 | + if (index >= 0) { |
| 101 | + setText(getAppServiceLabel(app)); |
| 102 | + } else { |
| 103 | + setText(app.getName()); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private String getAppServiceLabel(AppServiceConfig appServiceModel) { |
| 109 | + final String appServiceName = isDraftResource(appServiceModel) ? |
| 110 | + String.format("(New) %s", appServiceModel.getName()) : appServiceModel.getName(); |
| 111 | + final String runtime = WebAppService.getInstance().getRuntimeDisplayName(appServiceModel.getRuntime()); |
| 112 | + final String resourceGroup = Optional.ofNullable(appServiceModel.getResourceGroup()).map(ResourceGroup::getName).orElse(StringUtils.EMPTY); |
| 113 | + return String.format("<html><div>%s</div></div><small>Runtime: %s | Resource Group: %s</small></html>", |
| 114 | + appServiceName, runtime, resourceGroup); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private static boolean isDraftResource(final AppServiceConfig config) { |
| 119 | + return StringUtils.isEmpty(config.getResourceId()); |
| 120 | + } |
| 121 | +} |
0 commit comments