From 6558a2a38938f70d9da726a5452821f1738e894c Mon Sep 17 00:00:00 2001 From: qianjinshen Date: Wed, 24 Feb 2021 22:36:49 +0800 Subject: [PATCH 1/4] added link dialog related ui codes. --- .../intellij/common/ModuleComboBox.java | 39 +++++++++ .../intellij/link/mysql/DatabaseComboBox.java | 68 ++++++++++++++++ .../intellij/link/mysql/ServerComboBox.java | 79 +++++++++++++++++++ .../intellij/link/mysql/UsernameComboBox.java | 51 ++++++++++++ 4 files changed, 237 insertions(+) create mode 100644 PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/common/ModuleComboBox.java create mode 100644 PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/DatabaseComboBox.java create mode 100644 PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/ServerComboBox.java create mode 100644 PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/UsernameComboBox.java diff --git a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/common/ModuleComboBox.java b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/common/ModuleComboBox.java new file mode 100644 index 0000000000..0c4978e1a4 --- /dev/null +++ b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/common/ModuleComboBox.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + */ + +package com.microsoft.azure.toolkit.intellij.common; + +import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.ModuleManager; +import com.intellij.openapi.project.Project; +import org.apache.commons.lang3.StringUtils; + +import java.util.Arrays; +import java.util.List; + +public class ModuleComboBox extends AzureComboBox { + + private final Project project; + + public ModuleComboBox(Project project) { + super(true); + this.project = project; + } + + @Override + protected List loadItems() throws Exception { + return Arrays.asList(ModuleManager.getInstance(project).getModules()); + } + + @Override + protected String getItemText(Object item) { + if (item instanceof Module) { + return ((Module) item).getName(); + } else { + return StringUtils.EMPTY; + } + } + +} diff --git a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/DatabaseComboBox.java b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/DatabaseComboBox.java new file mode 100644 index 0000000000..68836a0296 --- /dev/null +++ b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/DatabaseComboBox.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + */ + +package com.microsoft.azure.toolkit.intellij.link.mysql; + +import com.microsoft.azure.management.mysql.v2020_01_01.Server; +import com.microsoft.azure.management.mysql.v2020_01_01.implementation.DatabaseInner; +import com.microsoft.azure.management.mysql.v2020_01_01.implementation.MySQLManager; +import com.microsoft.azure.management.resources.Subscription; +import com.microsoft.azure.toolkit.intellij.common.AzureComboBox; +import com.microsoft.azuretools.authmanage.AuthMethodManager; +import com.microsoft.azuretools.sdkmanage.AzureManager; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class DatabaseComboBox extends AzureComboBox { + + @Setter + private Subscription subscription; + private Server server; + + public DatabaseComboBox() { + super(false); + } + + public void setServer(Server server) { + if (Objects.equals(server, this.server)) { + return; + } + this.server = server; + if (server == null) { + this.clear(); + return; + } + this.refreshItems(); + } + + @Override + protected String getItemText(Object item) { + if (item instanceof DatabaseInner) { + return ((DatabaseInner) item).name(); + } + return super.getItemText(item); + } + + @Override + protected List loadItems() throws Exception { + if (subscription == null) { + return new ArrayList<>(); + } + AzureManager manager = AuthMethodManager.getInstance().getAzureManager(); + if (manager == null) { + return new ArrayList<>(); + } + final MySQLManager mySQLManager = manager.getMySQLManager(subscription.subscriptionId()); + return mySQLManager.databases().inner().listByServer(server.resourceGroupName(), server.name()); + } + + @Override + public boolean isRequired() { + return true; + } +} diff --git a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/ServerComboBox.java b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/ServerComboBox.java new file mode 100644 index 0000000000..ffd0363a44 --- /dev/null +++ b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/ServerComboBox.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + */ + +package com.microsoft.azure.toolkit.intellij.link.mysql; + +import com.microsoft.azure.management.mysql.v2020_01_01.Server; +import com.microsoft.azure.management.mysql.v2020_01_01.implementation.MySQLManager; +import com.microsoft.azure.management.resources.Subscription; +import com.microsoft.azure.toolkit.intellij.common.AzureComboBox; +import com.microsoft.azuretools.authmanage.AuthMethodManager; +import com.microsoft.azuretools.sdkmanage.AzureManager; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class ServerComboBox extends AzureComboBox { + + private Subscription subscription; + + public ServerComboBox() { + super(true); + } + + public void setSubscription(Subscription subscription) { + if (Objects.equals(subscription, this.subscription)) { + return; + } + this.subscription = subscription; + if (subscription == null) { + this.clear(); + return; + } + this.refreshItems(); + } + + @Override + protected String getItemText(Object item) { + if (item instanceof Server) { + return ((Server) item).name(); + } + return super.getItemText(item); + } + + @Override + protected List loadItems() throws Exception { + if (subscription == null) { + return new ArrayList<>(); + } + AzureManager manager = AuthMethodManager.getInstance().getAzureManager(); + if (manager == null) { + return new ArrayList<>(); + } + final MySQLManager mySQLManager = manager.getMySQLManager(subscription.subscriptionId()); + return mySQLManager.servers().list(); + } + + @Override + public boolean isRequired() { + return true; + } + + @Override + protected boolean itemContains(List items, Server value) { + if (CollectionUtils.isEmpty(items) || value == null) { + return false; + } + for (Server server : items) { + if (StringUtils.equals(server.fullyQualifiedDomainName(), value.fullyQualifiedDomainName())) { + return true; + } + } + return false; + } +} diff --git a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/UsernameComboBox.java b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/UsernameComboBox.java new file mode 100644 index 0000000000..29447f92f4 --- /dev/null +++ b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/UsernameComboBox.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + */ + +package com.microsoft.azure.toolkit.intellij.link.mysql; + +import com.microsoft.azure.management.mysql.v2020_01_01.Server; +import com.microsoft.azure.toolkit.intellij.common.AzureComboBox; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class UsernameComboBox extends AzureComboBox { + + private static final String SEPARATOR = "@"; + + private Server server; + + public UsernameComboBox() { + super(false); + } + + public void setServer(Server server) { + if (Objects.equals(server, this.server)) { + return; + } + this.server = server; + if (server == null) { + this.clear(); + return; + } + this.refreshItems(); + } + + @Override + protected List loadItems() { + if (server == null) { + return new ArrayList<>(); + } + List usernames = new ArrayList<>(); + usernames.add(server.administratorLogin() + SEPARATOR + server.name()); + return usernames; + } + + @Override + public boolean isRequired() { + return true; + } +} From 0b6f86a965c9c2ca7a27b76bd9970d689d6be8bc Mon Sep 17 00:00:00 2001 From: qianjinshen Date: Thu, 25 Feb 2021 10:58:11 +0800 Subject: [PATCH 2/4] add itemContains method to change contains logic. --- .../azure/toolkit/intellij/common/AzureComboBox.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/common/AzureComboBox.java b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/common/AzureComboBox.java index 7c8b3d8f34..bde30cc88e 100644 --- a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/common/AzureComboBox.java +++ b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/common/AzureComboBox.java @@ -115,7 +115,7 @@ private void refreshValue() { final List items = this.getItems(); if (this.valueNotSet && this.value == null && !items.isEmpty()) { super.setSelectedItem(items.get(0)); - } else if (items.contains(this.value)) { + } else if (this.itemContains(items, value)) { super.setSelectedItem(this.value); } else if (value instanceof Draft) { // todo: unify model for custom created resource @@ -126,6 +126,13 @@ private void refreshValue() { } } + protected boolean itemContains(List items, T value) { + if (CollectionUtils.isEmpty(items) || value == null) { + return false; + } + return items.contains(value); + } + @Override public void setSelectedItem(final Object value) { this.setValue((T) value); From e72701fb953d37cff6d9817b1715a8919e8f547a Mon Sep 17 00:00:00 2001 From: qianjinshen Date: Mon, 1 Mar 2021 16:18:31 +0800 Subject: [PATCH 3/4] code refactor. --- .../toolkit/intellij/common/AzureComboBox.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/common/AzureComboBox.java b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/common/AzureComboBox.java index 2b98a6943f..5afe0949ee 100644 --- a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/common/AzureComboBox.java +++ b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/common/AzureComboBox.java @@ -54,6 +54,8 @@ public abstract class AzureComboBox extends ComboBox implements AzureFormI private boolean required; private Object value; private boolean valueNotSet = true; + @Setter + private boolean forceDisable; private String label; public AzureComboBox() { @@ -125,7 +127,7 @@ private void refreshValue() { } if (this.valueNotSet && this.value == null && !items.isEmpty()) { super.setSelectedItem(items.get(0)); - } else if (this.itemContains(items, value)) { + } else if (items.contains(this.value)) { super.setSelectedItem(this.value); } else if (value instanceof Draft) { // todo: unify model for custom created resource @@ -136,13 +138,6 @@ private void refreshValue() { } } - protected boolean itemContains(List items, T value) { - if (CollectionUtils.isEmpty(items) || value == null) { - return false; - } - return items.contains(value); - } - @Override public void setSelectedItem(final Object value) { this.setValue((T) value); @@ -197,7 +192,7 @@ protected void setLoading(final boolean loading) { this.setEnabled(false); this.setEditor(this.loadingSpinner); } else { - this.setEnabled(true); + this.setEnabled(forceDisable ? false : true); this.setEditor(this.inputEditor); } }); From 5bfd471a7c0953cbc5ef31020402f606485e2cfd Mon Sep 17 00:00:00 2001 From: qianjinshen Date: Mon, 1 Mar 2021 17:16:55 +0800 Subject: [PATCH 4/4] remove unused code. --- .../toolkit/intellij/link/mysql/ServerComboBox.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/ServerComboBox.java b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/ServerComboBox.java index ffd0363a44..e0c5560f3f 100644 --- a/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/ServerComboBox.java +++ b/PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/ServerComboBox.java @@ -64,16 +64,4 @@ public boolean isRequired() { return true; } - @Override - protected boolean itemContains(List items, Server value) { - if (CollectionUtils.isEmpty(items) || value == null) { - return false; - } - for (Server server : items) { - if (StringUtils.equals(server.fullyQualifiedDomainName(), value.fullyQualifiedDomainName())) { - return true; - } - } - return false; - } }