forked from flutter/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlutterRetargetAppAction.java
More file actions
86 lines (74 loc) · 2.86 KB
/
FlutterRetargetAppAction.java
File metadata and controls
86 lines (74 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright 2016 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.actions;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.actionSystem.ex.ActionUtil;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.jetbrains.lang.dart.analytics.Analytics;
import com.jetbrains.lang.dart.analytics.AnalyticsData;
import io.flutter.utils.FlutterModuleUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* A "retargeting" action that redirects to another action that is set up elsewhere with
* context required to execute.
*/
public abstract class FlutterRetargetAppAction extends DumbAwareAction {
@NotNull
private final String myActionId;
@NotNull
private final List<String> myPlaces = new ArrayList<>();
FlutterRetargetAppAction(@NotNull String actionId,
@Nullable String text,
@Nullable String description,
@SuppressWarnings("SameParameterValue") @NotNull String @NotNull ... places) {
super(text, description, null);
myActionId = actionId;
myPlaces.addAll(Arrays.asList(places));
}
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
final AnAction action = getAction(e.getProject());
if (action != null) {
ActionUtil.performAction(action, e);
Analytics.report(AnalyticsData.forAction(this, e));
}
}
@Override
public void update(@NotNull AnActionEvent e) {
final Presentation presentation = e.getPresentation();
final Project project = e.getProject();
if (project == null || !FlutterModuleUtils.hasFlutterModule(project) || !myPlaces.contains(e.getPlace())) {
presentation.setVisible(false);
return;
}
presentation.setVisible(true);
presentation.setEnabled(false);
// Retargeted actions defer to their targets for presentation updates.
final AnAction action = getAction(project);
if (action != null) {
final Presentation template = action.getTemplatePresentation();
final String text = template.getTextWithMnemonic();
if (text != null) {
presentation.setText(text, true);
}
ActionUtil.updateAction(action, e);
}
}
private @Nullable AnAction getAction(@Nullable Project project) {
return project == null ? null : ProjectActions.getAction(project, myActionId);
}
}