Skip to content

Commit 4e063c1

Browse files
authored
Merge pull request #6031 from microsoft/run-configuration/panel
[Task #1890182] Extract web app deployment run configuration panel in IntelliJ
2 parents 5302d03 + 3474843 commit 4e063c1

File tree

7 files changed

+711
-0
lines changed

7 files changed

+711
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.openapi.project.Project;
9+
import com.microsoft.azure.toolkit.ide.appservice.webapp.model.WebAppConfig;
10+
import com.microsoft.azure.toolkit.intellij.webapp.WebAppCreationDialog;
11+
import com.microsoft.azure.toolkit.lib.Azure;
12+
import com.microsoft.azure.toolkit.lib.appservice.AzureWebApp;
13+
import com.microsoft.azure.toolkit.lib.appservice.service.IWebApp;
14+
import com.microsoft.tooling.msservices.components.DefaultLoader;
15+
16+
import java.util.List;
17+
import java.util.stream.Collectors;
18+
19+
public class WebAppComboBox extends AppServiceComboBox<WebAppConfig> {
20+
public WebAppComboBox(Project project) {
21+
super(project);
22+
}
23+
24+
@Override
25+
protected List<WebAppConfig> loadAppServiceModels() {
26+
final List<IWebApp> webApps = Azure.az(AzureWebApp.class).list();
27+
return webApps.stream().parallel()
28+
.filter(this::isJavaAppService)
29+
.sorted((a, b) -> a.name().compareToIgnoreCase(b.name()))
30+
.map(WebAppConfig::fromRemote)
31+
.collect(Collectors.toList());
32+
}
33+
34+
@Override
35+
protected void createResource() {
36+
// todo: hide deployment part in creation dialog
37+
WebAppCreationDialog webAppCreationDialog = new WebAppCreationDialog(project);
38+
webAppCreationDialog.setDeploymentVisible(false);
39+
webAppCreationDialog.setOkActionListener(webAppConfig -> {
40+
WebAppComboBox.this.addItem(webAppConfig);
41+
WebAppComboBox.this.setSelectedItem(webAppConfig);
42+
DefaultLoader.getIdeHelper().invokeLater(webAppCreationDialog::close);
43+
});
44+
webAppCreationDialog.show();
45+
}
46+
}

0 commit comments

Comments
 (0)