55
66package com .microsoft .azure .toolkit .intellij .common ;
77
8+ import com .intellij .openapi .Disposable ;
9+ import com .intellij .openapi .ui .ComponentValidator ;
10+ import com .intellij .openapi .ui .ValidationInfo ;
811import com .microsoft .azure .toolkit .lib .common .form .AzureFormInput ;
912import com .microsoft .azure .toolkit .lib .common .form .AzureValidationInfo ;
13+ import com .microsoft .azure .toolkit .lib .common .task .AzureTaskManager ;
1014
1115import javax .accessibility .AccessibleRelation ;
1216import javax .annotation .Nonnull ;
17+ import javax .annotation .Nullable ;
1318import javax .swing .*;
19+ import java .util .Objects ;
1420import java .util .Optional ;
1521
16- public interface AzureFormInputComponent <T > extends AzureFormInput <T > {
22+ import static com .microsoft .azure .toolkit .lib .common .form .AzureValidationInfo .Type .PENDING ;
23+ import static com .microsoft .azure .toolkit .lib .common .form .AzureValidationInfo .Type .SUCCESS ;
24+ import static com .microsoft .azure .toolkit .lib .common .form .AzureValidationInfo .Type .WARNING ;
25+
26+ public interface AzureFormInputComponent <T > extends AzureFormInput <T >, Disposable {
1727 default JComponent getInputComponent () {
1828 return (JComponent ) this ;
1929 }
2030
21- /**
22- * NOTE: don't override
23- */
2431 @ Nonnull
2532 @ Override
2633 default AzureValidationInfo validateInternal (T value ) {
@@ -30,11 +37,47 @@ default AzureValidationInfo validateInternal(T value) {
3037 return AzureFormInput .super .validateInternal (value );
3138 }
3239
40+ @ Override
41+ default void setValidationInfo (@ Nullable AzureValidationInfo vi ) {
42+ AzureFormInput .super .setValidationInfo (vi );
43+ final ValidationInfo info = Objects .nonNull (vi ) && (vi .getType () == PENDING || vi .getType () == SUCCESS ) ? null : toIntellijValidationInfo (vi );
44+ final String state = Objects .isNull (info ) ? null : info .warning ? "warning" : "error" ;
45+ final JComponent input = this .getInputComponent ();
46+ // see com.intellij.openapi.ui.ComponentValidator.updateInfo
47+ input .putClientProperty ("JComponent.outline" , state );
48+ input .revalidate ();
49+ input .repaint ();
50+ // see com.intellij.openapi.ui.DialogWrapper.setErrorInfoAll
51+ final ComponentValidator v = ComponentValidator .getInstance (input ).orElseGet (() -> (new ComponentValidator (this )).installOn (input ));
52+ if (v != null ) {
53+ AzureTaskManager .getInstance ().runLater (() -> v .updateInfo (info ));
54+ }
55+ }
56+
3357 @ Override
3458 default String getLabel () {
3559 final JLabel label = (JLabel ) this .getInputComponent ().getClientProperty (AccessibleRelation .LABELED_BY );
3660 return Optional .ofNullable (label ).map (JLabel ::getText )
3761 .map (t -> t .endsWith (":" ) ? t .substring (0 , t .length () - 1 ) : t )
3862 .orElse (this .getClass ().getSimpleName ());
3963 }
64+
65+ @ Nullable
66+ static ValidationInfo toIntellijValidationInfo (@ Nullable final AzureValidationInfo info ) {
67+ if (Objects .isNull (info )) {
68+ return null ;
69+ }
70+ final AzureFormInput <?> input = info .getInput ();
71+ final JComponent component = input instanceof AzureFormInputComponent ? ((AzureFormInputComponent <?>) input ).getInputComponent () : null ;
72+ final ValidationInfo v = new ValidationInfo (Optional .ofNullable (info .getMessage ()).orElse ("Unknown error" ), component );
73+ if (info .getType () == WARNING ) {
74+ v .asWarning ();
75+ }
76+ return v ;
77+ }
78+
79+ @ Override
80+ default void dispose () {
81+ this .clearAll ();
82+ }
4083}
0 commit comments