25
25
import org .jboss .tools .intellij .openshift .ui .project .CreateNewProjectDialog ;
26
26
import org .jboss .tools .intellij .openshift .utils .odo .Odo ;
27
27
import org .jetbrains .annotations .NotNull ;
28
+ import org .slf4j .Logger ;
29
+ import org .slf4j .LoggerFactory ;
28
30
29
31
import java .awt .Point ;
30
32
import java .io .IOException ;
33
35
import java .util .concurrent .CompletionException ;
34
36
35
37
import static org .jboss .tools .intellij .openshift .actions .ActionUtils .runWithProgress ;
38
+ import static org .jboss .tools .intellij .openshift .actions .NodeUtils .getRoot ;
36
39
import static org .jboss .tools .intellij .openshift .telemetry .TelemetryService .TelemetryResult ;
37
40
38
41
public class CreateProjectAction extends LoggedInClusterAction {
39
42
43
+ private static final Logger LOGGER = LoggerFactory .getLogger (CreateProjectAction .class );
44
+
45
+ @ Override
46
+ public void update (AnActionEvent e ) {
47
+ super .update (e );
48
+ if (e .getPresentation ().isVisible ()) {
49
+ Odo odo = getOdo (e );
50
+ if (odo == null ) {
51
+ return ;
52
+ }
53
+ // overrides label given in plugin.xml
54
+ e .getPresentation ().setText ("New " + getKind (odo ));
55
+ }
56
+ }
57
+
40
58
@ Override
41
- public String getTelemetryActionName () { return "create project" ; }
59
+ public String getTelemetryActionName () {
60
+ return "create project" ;
61
+ }
42
62
43
63
public static void execute (ApplicationsRootNode rootNode ) {
44
64
Odo odo = rootNode .getOdo ().getNow (null );
@@ -55,14 +75,15 @@ public void actionPerformedOnSelectedObject(AnActionEvent anActionEvent, Object
55
75
doActionPerformed (location , odo , getEventProject (anActionEvent ));
56
76
}
57
77
58
- private void doActionPerformed (final Point location , final Odo odo , Project project ) {
78
+ private void doActionPerformed (final Point location , @ NotNull final Odo odo , Project project ) {
79
+ String kind = getKind (odo );
59
80
runWithProgress ((ProgressIndicator progress ) ->
60
81
CompletableFuture
61
82
.supplyAsync (() -> {
62
83
try {
63
84
return odo .getNamespaces ();
64
85
} catch (IOException e ) {
65
- NotificationUtils .notifyError ("Create New Project" , "Could not get projects : " + e .getMessage ());
86
+ NotificationUtils .notifyError ("Create New " + kind , "Could not get " + kind . toLowerCase () + "s : " + e .getMessage ());
66
87
sendTelemetryError (e .getMessage ());
67
88
throw new CompletionException (e );
68
89
}
@@ -71,7 +92,7 @@ private void doActionPerformed(final Point location, final Odo odo, Project proj
71
92
if (error != null ) {
72
93
return null ;
73
94
}
74
- CreateNewProjectDialog dialog = openCreateProjectDialog (allProjects , location , project );
95
+ CreateNewProjectDialog dialog = openCreateProjectDialog (allProjects , kind , location , project );
75
96
if (dialog .isOK ()) {
76
97
return dialog .getNewProject ();
77
98
} else {
@@ -87,28 +108,54 @@ private void doActionPerformed(final Point location, final Odo odo, Project proj
87
108
}
88
109
createProject (newProject , odo );
89
110
}, SwingUtils .EXECUTOR_BACKGROUND ),
90
- "Create Active Project ..." ,
91
- project );
111
+ "Create Active " + kind + " ..." ,
112
+ project );
92
113
}
93
114
94
- private void createProject (String newProject , Odo odo ) {
95
- Notification notification = NotificationUtils .notifyInformation ("Create Project" , "Creating project " + newProject );
115
+ private void createProject (String newProject , @ NotNull Odo odo ) {
116
+ String kind = getKind (odo );
117
+ Notification notification = NotificationUtils .notifyInformation ("Create " + kind , "Creating " + kind .toLowerCase () + " newProject" );
96
118
try {
97
- odo .createProject (newProject );
98
- notification .expire ();
99
- NotificationUtils .notifyInformation ("Create Project" , "Project " + newProject + " successfully created" );
100
- sendTelemetryResults (TelemetryResult .SUCCESS );
101
- } catch (IOException | CompletionException e ) {
102
- notification .expire ();
103
- sendTelemetryError (e );
104
- UIHelper .executeInUI (() -> Messages .showErrorDialog ("Error: " + e .getLocalizedMessage (), "Create Project" ));
105
- }
119
+ odo .createProject (newProject );
120
+ notification .expire ();
121
+ NotificationUtils .notifyInformation ("Create " + kind , kind + newProject + " successfully created" );
122
+ sendTelemetryResults (TelemetryResult .SUCCESS );
123
+ } catch (IOException | CompletionException e ) {
124
+ notification .expire ();
125
+ sendTelemetryError (e );
126
+ UIHelper .executeInUI (() -> Messages .showErrorDialog ("Error: " + e .getLocalizedMessage (), "Create " + kind ));
127
+ }
106
128
}
107
129
108
- private CreateNewProjectDialog openCreateProjectDialog (List <String > allProjects , Point location , Project project ) {
109
- CreateNewProjectDialog dialog = new CreateNewProjectDialog (project , allProjects , location );
130
+ protected CreateNewProjectDialog openCreateProjectDialog (List <String > allProjects , String kind , Point location , Project project ) {
131
+ CreateNewProjectDialog dialog = new CreateNewProjectDialog (project , allProjects , kind , location );
110
132
dialog .show ();
111
133
return dialog ;
112
134
}
113
135
136
+ @ Override
137
+ public boolean isVisible (Object selected ) {
138
+ return isRoot (selected )
139
+ && isLoggedIn (selected );
140
+ }
141
+
142
+ private boolean isLoggedIn (Object node ) {
143
+ ApplicationsRootNode root = getRoot (node );
144
+ if (root == null ) {
145
+ return false ;
146
+ }
147
+ return root .isLogged ();
148
+ }
149
+
150
+ private boolean isRoot (Object node ) {
151
+ return node instanceof ApplicationsRootNode ;
152
+ }
153
+
154
+ private String getKind (Odo odo ) {
155
+ if (odo .isOpenShift ()) {
156
+ return "Project" ;
157
+ } else {
158
+ return "Namespace" ;
159
+ }
160
+ }
114
161
}
0 commit comments