77
88import com .microsoft .azure .toolkit .lib .common .bundle .AzureString ;
99import com .microsoft .azure .toolkit .lib .common .exception .AzureToolkitRuntimeException ;
10- import com .microsoft .azure .toolkit .lib .common .model .AbstractAzResource ;
11- import com .microsoft .azure .toolkit .lib .common .model .AbstractAzResourceModule ;
1210import com .microsoft .azure .toolkit .lib .common .model .AzResource ;
1311import com .microsoft .azure .toolkit .lib .common .model .AzResourceModule ;
1412import com .microsoft .azure .toolkit .lib .common .model .Emulatable ;
@@ -50,11 +48,13 @@ public class Action<D> extends OperationBase implements Cloneable {
5048 public static final Action .Id <Object > OPEN_AZURE_SETTINGS = Action .Id .of ("user/common.open_azure_settings" );
5149 public static final Action .Id <Object > DISABLE_AUTH_CACHE = Action .Id .of ("user/account.disable_auth_cache" );
5250
51+ public static final String COMMON = "common" ;
52+
5353 @ Nonnull
5454 private final Id <D > id ;
5555 @ Nonnull
5656 private Predicate <D > enableWhen = o -> true ;
57- private Predicate <Object > visibleWhen = o -> true ;
57+ private BiPredicate <Object , String > visibleWhen = ( o , place ) -> true ;
5858 private Function <D , String > iconProvider ;
5959 private Function <D , String > labelProvider ;
6060 private Function <D , AzureString > titleProvider ;
@@ -64,6 +64,7 @@ public class Action<D> extends OperationBase implements Cloneable {
6464 private List <Function <D , String >> titleParamProviders = new ArrayList <>();
6565
6666 private D source ;
67+ private String place ;
6768 @ Setter
6869
6970 private Predicate <D > authRequiredProvider = Action ::isAuthRequiredForAzureResource ;
@@ -87,11 +88,15 @@ public String getId() {
8788 return this .id .id ;
8889 }
8990
90- @ Nonnull
9191 public IView .Label getView (D s ) {
92+ return getView (s , COMMON );
93+ }
94+
95+ @ Nonnull
96+ public IView .Label getView (D s , final String place ) {
9297 final D source = Optional .ofNullable (this .source ).orElse (s );
9398 try {
94- final boolean visible = this .visibleWhen .test (source );
99+ final boolean visible = this .visibleWhen .test (source , place );
95100 if (visible ) {
96101 final String label = this .labelProvider .apply (source );
97102 final String icon = Optional .ofNullable (this .iconProvider ).map (p -> p .apply (source )).orElse (null );
@@ -110,7 +115,7 @@ public IView.Label getView(D s) {
110115 @ SuppressWarnings ("unchecked" )
111116 public BiConsumer <D , Object > getHandler (D s , Object e ) {
112117 final D source = Optional .ofNullable (this .source ).orElse (s );
113- if (!this .visibleWhen .test (source ) && !this .enableWhen .test (source )) {
118+ if (!this .visibleWhen .test (source , COMMON ) && !this .enableWhen .test (source )) {
114119 return null ;
115120 }
116121 for (int i = this .handlers .size () - 1 ; i >= 0 ; i --) {
@@ -203,6 +208,11 @@ public Action<D> enableWhen(@Nonnull Predicate<D> enableWhen) {
203208 }
204209
205210 public Action <D > visibleWhen (@ Nonnull Predicate <Object > visibleWhen ) {
211+ this .visibleWhen = (object , ignore ) -> visibleWhen .test (object );
212+ return this ;
213+ }
214+
215+ public Action <D > visibleWhen (@ Nonnull BiPredicate <Object , String > visibleWhen ) {
206216 this .visibleWhen = visibleWhen ;
207217 return this ;
208218 }
0 commit comments