Skip to content

Commit 9357f3e

Browse files
committed
Merge branch 'develop' into qianjin-sqlserver
2 parents 4169e7c + 983e76c commit 9357f3e

27 files changed

+1174
-1641
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-lib/src/main/java/com/microsoft/azure/toolkit/intellij/common/AzureComboBox.java

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@
3636
import java.io.InterruptedIOException;
3737
import java.util.ArrayList;
3838
import java.util.Collections;
39+
import java.util.EventListener;
3940
import java.util.List;
4041
import java.util.Objects;
4142
import java.util.Optional;
4243
import java.util.function.BiPredicate;
4344
import java.util.function.Function;
4445
import java.util.function.Predicate;
46+
import java.util.function.Supplier;
4547

4648
public abstract class AzureComboBox<T> extends ComboBox<T> implements AzureFormInputComponent<T> {
4749
public static final String EMPTY_ITEM = StringUtils.EMPTY;
@@ -54,8 +56,13 @@ public abstract class AzureComboBox<T> extends ComboBox<T> implements AzureFormI
5456
private boolean required;
5557
private Object value;
5658
private boolean valueNotSet = true;
57-
private boolean valueFixed;
5859
private String label;
60+
@Nullable
61+
@Setter
62+
protected Boolean valueFixed;
63+
@Getter
64+
@Setter
65+
private Supplier<? extends List<? extends T>> itemsLoader;
5966

6067
public AzureComboBox() {
6168
this(true);
@@ -70,6 +77,15 @@ public AzureComboBox(boolean refresh) {
7077
}
7178
}
7279

80+
public AzureComboBox(@Nonnull Supplier<? extends List<? extends T>> itemsLoader) {
81+
this(itemsLoader, true);
82+
}
83+
84+
public AzureComboBox(@Nonnull Supplier<? extends List<? extends T>> itemsLoader, boolean refresh) {
85+
this(refresh);
86+
this.itemsLoader = itemsLoader;
87+
}
88+
7389
@Override
7490
public JComponent getInputComponent() {
7591
return this;
@@ -80,7 +96,7 @@ protected void init() {
8096
this.inputEditor = new AzureComboBoxEditor();
8197
this.setEditable(true);
8298
this.setEditor(this.inputEditor);
83-
this.setRenderer(new SimpleListCellRenderer<T>() {
99+
this.setRenderer(new SimpleListCellRenderer<>() {
84100
@Override
85101
public void customize(@Nonnull final JList<? extends T> l, final T t, final int i, final boolean b,
86102
final boolean b1) {
@@ -91,10 +107,20 @@ public void customize(@Nonnull final JList<? extends T> l, final T t, final int
91107
if (isFilterable()) {
92108
this.addPopupMenuListener(new AzureComboBoxPopupMenuListener());
93109
}
110+
final TailingDebouncer valueDebouncer = new TailingDebouncer(() -> {
111+
@SuppressWarnings("unchecked")
112+
final ValueListener<T>[] listeners = this.listenerList.getListeners(ValueListener.class);
113+
for (final ValueListener<T> listener : listeners) {
114+
listener.onValueChanged(this.getValue());
115+
}
116+
}, DEBOUNCE_DELAY);
94117
this.addItemListener((e) -> {
95118
if (e.getStateChange() == ItemEvent.SELECTED) {
96119
this.refreshValue();
97120
}
121+
if (e.getStateChange() == ItemEvent.SELECTED || e.getStateChange() == ItemEvent.DESELECTED) {
122+
valueDebouncer.debounce();
123+
}
98124
});
99125
}
100126

@@ -105,24 +131,28 @@ public T getValue() {
105131

106132
@Override
107133
public void setValue(final T val) {
108-
this.setValue(val, false);
134+
this.setValue(val, null);
109135
}
110136

111-
public void setValue(final T val, final boolean fixed) {
112-
this.valueFixed = fixed;
113-
this.setEditable(!this.valueFixed);
137+
public void setValue(final T val, final Boolean fixed) {
138+
Optional.ofNullable(fixed).ifPresent(f -> {
139+
this.valueFixed = fixed;
140+
this.setEditable(!f);
141+
});
114142
this.valueNotSet = false;
115143
this.value = val;
116144
this.refreshValue();
117145
}
118146

119147
public void setValue(final ItemReference<T> val) {
120-
this.setValue(val, false);
148+
this.setValue(val, null);
121149
}
122150

123-
public void setValue(final ItemReference<T> val, final boolean fixed) {
124-
this.valueFixed = fixed;
125-
this.setEditable(!this.valueFixed);
151+
public void setValue(final ItemReference<T> val, final Boolean fixed) {
152+
Optional.ofNullable(fixed).ifPresent(f -> {
153+
this.valueFixed = fixed;
154+
this.setEditable(!f);
155+
});
126156
this.valueNotSet = false;
127157
this.value = val;
128158
this.refreshValue();
@@ -166,7 +196,7 @@ public void refreshItems() {
166196
private void doRefreshItems() {
167197
try {
168198
this.setLoading(true);
169-
final List<? extends T> items = this.loadItems();
199+
final List<? extends T> items = this.loadItemsInner();
170200
this.setLoading(false);
171201
AzureTaskManager.getInstance().runLater(() -> this.setItems(items), AzureTask.Modality.ANY);
172202
} catch (final Exception e) {
@@ -203,7 +233,7 @@ protected void setLoading(final boolean loading) {
203233
this.setEnabled(false);
204234
this.setEditor(this.loadingSpinner);
205235
} else {
206-
this.setEnabled(!valueFixed);
236+
this.setEnabled(!Objects.equals(valueFixed, true));
207237
this.setEditor(this.inputEditor);
208238
}
209239
}, AzureTask.Modality.ANY);
@@ -227,11 +257,21 @@ protected ExtendableTextComponent.Extension getExtension() {
227257
}
228258

229259
protected Observable<? extends List<? extends T>> loadItemsAsync() {
230-
return Observable.fromCallable(this::loadItems).subscribeOn(Schedulers.io());
260+
return Observable.fromCallable(this::loadItemsInner).subscribeOn(Schedulers.io());
261+
}
262+
263+
protected final List<? extends T> loadItemsInner() throws Exception {
264+
if (Objects.nonNull(this.itemsLoader)) {
265+
return this.itemsLoader.get();
266+
} else {
267+
return this.loadItems();
268+
}
231269
}
232270

233271
@Nonnull
234-
protected abstract List<? extends T> loadItems() throws Exception;
272+
protected List<? extends T> loadItems() throws Exception {
273+
return Collections.emptyList();
274+
}
235275

236276
@Nullable
237277
protected T getDefaultValue() {
@@ -394,4 +434,17 @@ public boolean is(Object obj) {
394434
return this.predicate.test((T) obj);
395435
}
396436
}
437+
438+
public void addValueListener(ValueListener<T> listener) {
439+
this.listenerList.add(ValueListener.class, listener);
440+
}
441+
442+
public void removeValueListener(ValueListener<T> listener) {
443+
this.listenerList.remove(ValueListener.class, listener);
444+
}
445+
446+
@FunctionalInterface
447+
public interface ValueListener<T> extends EventListener {
448+
void onValueChanged(@Nullable T value);
449+
}
397450
}

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-lib/src/main/java/com/microsoft/azure/toolkit/intellij/common/AzureComboBoxSimple.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package com.microsoft.azure.toolkit.intellij.common;
77

8-
import lombok.RequiredArgsConstructor;
8+
import lombok.Getter;
99
import lombok.Setter;
1010

1111
import javax.annotation.Nonnull;
@@ -14,13 +14,19 @@
1414
import java.util.Objects;
1515
import java.util.function.Supplier;
1616

17-
@RequiredArgsConstructor
1817
@Setter
18+
@Getter
1919
public class AzureComboBoxSimple<T> extends AzureComboBox<T> {
2020

21-
private final Supplier<? extends List<? extends T>> supplier;
21+
private Supplier<? extends List<? extends T>> supplier;
2222
private Validator validator;
2323

24+
public AzureComboBoxSimple(@Nonnull final Supplier<? extends List<? extends T>> supplier) {
25+
super(false);
26+
this.supplier = supplier;
27+
this.refreshItems();
28+
}
29+
2430
@Nonnull
2531
protected List<? extends T> loadItems() throws Exception {
2632
if (Objects.nonNull(this.supplier)) {

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-lib/src/main/java/com/microsoft/azure/toolkit/intellij/common/AzureFormPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ default void setVisible(boolean visible) {
1919

2020
@Override
2121
default T getData() {
22-
throw new AzureToolkitRuntimeException("method not implemeted");
22+
throw new AzureToolkitRuntimeException("method not implemented");
2323
}
2424

2525
@Override

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-lib/src/main/java/com/microsoft/azure/toolkit/intellij/common/IntellijAzureMessager.java

Lines changed: 0 additions & 74 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.common.messager;
7+
8+
import com.intellij.notification.Notification;
9+
import com.intellij.notification.NotificationType;
10+
import com.intellij.notification.Notifications;
11+
import com.intellij.openapi.ui.MessageDialogBuilder;
12+
import com.microsoft.azure.toolkit.intellij.common.handler.IntelliJAzureExceptionHandler;
13+
import com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException;
14+
import com.microsoft.azure.toolkit.lib.common.messager.IAzureMessager;
15+
import org.apache.commons.lang3.ArrayUtils;
16+
17+
import javax.annotation.Nonnull;
18+
19+
public class IntellijAzureMessager implements IAzureMessager {
20+
private static final String NOTIFICATION_GROUP_ID = "Azure Plugin";
21+
private static final String DEFAULT_MESSAGE_TITLE = "Azure";
22+
23+
private void showNotification(@Nonnull String title, @Nonnull String message, NotificationType type) {
24+
Notifications.Bus.notify(new Notification(NOTIFICATION_GROUP_ID, title, message, type));
25+
}
26+
27+
private String getTitle(String... title) {
28+
if (ArrayUtils.isEmpty(title)) {
29+
return DEFAULT_MESSAGE_TITLE;
30+
}
31+
return title[0];
32+
}
33+
34+
public boolean confirm(@Nonnull String message, String title) {
35+
return MessageDialogBuilder.yesNo(getTitle(title), message).guessWindowAndAsk();
36+
}
37+
38+
public void alert(@Nonnull String message, String title) {
39+
MessageDialogBuilder.okCancel(getTitle(title), message).guessWindowAndAsk();
40+
}
41+
42+
public void success(@Nonnull String message, String title) {
43+
this.showNotification(getTitle(title), message, NotificationType.INFORMATION);
44+
}
45+
46+
public void info(@Nonnull String message, String title) {
47+
this.showNotification(getTitle(title), message, NotificationType.INFORMATION);
48+
}
49+
50+
public void warning(@Nonnull String message, String title) {
51+
this.showNotification(getTitle(title), message, NotificationType.WARNING);
52+
}
53+
54+
public void error(@Nonnull String message, String title) {
55+
this.showNotification(getTitle(title), message, NotificationType.ERROR);
56+
}
57+
58+
public void error(@Nonnull Throwable throwable, String title) {
59+
IntelliJAzureExceptionHandler.getInstance().handleException(throwable);
60+
}
61+
62+
public void error(@Nonnull Throwable throwable, @Nonnull String message, String title) {
63+
final AzureToolkitRuntimeException wrapped = new AzureToolkitRuntimeException(message, throwable);
64+
IntelliJAzureExceptionHandler.getInstance().handleException(wrapped);
65+
}
66+
67+
public String value(String val) {
68+
return val;
69+
}
70+
}

0 commit comments

Comments
 (0)