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
+ if (!odo .isOpenShift ()) {
54
+ e .getPresentation ().setText ("New " + getKind (odo ));
55
+ }
56
+ }
57
+ }
58
+
40
59
@ Override
41
60
public String getTelemetryActionName () { return "create project" ; }
42
61
@@ -55,14 +74,15 @@ public void actionPerformedOnSelectedObject(AnActionEvent anActionEvent, Object
55
74
doActionPerformed (location , odo , getEventProject (anActionEvent ));
56
75
}
57
76
58
- private void doActionPerformed (final Point location , final Odo odo , Project project ) {
77
+ private void doActionPerformed (final Point location , @ NotNull final Odo odo , Project project ) {
78
+ String kind = getKind (odo );
59
79
runWithProgress ((ProgressIndicator progress ) ->
60
80
CompletableFuture
61
81
.supplyAsync (() -> {
62
82
try {
63
83
return odo .getNamespaces ();
64
84
} catch (IOException e ) {
65
- NotificationUtils .notifyError ("Create New Project" , "Could not get projects : " + e .getMessage ());
85
+ NotificationUtils .notifyError ("Create New " + kind , "Could not get " + kind . toLowerCase () + "s : " + e .getMessage ());
66
86
sendTelemetryError (e .getMessage ());
67
87
throw new CompletionException (e );
68
88
}
@@ -71,7 +91,7 @@ private void doActionPerformed(final Point location, final Odo odo, Project proj
71
91
if (error != null ) {
72
92
return null ;
73
93
}
74
- CreateNewProjectDialog dialog = openCreateProjectDialog (allProjects , location , project );
94
+ CreateNewProjectDialog dialog = openCreateProjectDialog (allProjects , kind , location , project );
75
95
if (dialog .isOK ()) {
76
96
return dialog .getNewProject ();
77
97
} else {
@@ -87,28 +107,54 @@ private void doActionPerformed(final Point location, final Odo odo, Project proj
87
107
}
88
108
createProject (newProject , odo );
89
109
}, SwingUtils .EXECUTOR_BACKGROUND ),
90
- "Create Active Project ..." ,
91
- project );
110
+ "Create Active " + kind + " ..." ,
111
+ project );
92
112
}
93
113
94
- private void createProject (String newProject , Odo odo ) {
95
- Notification notification = NotificationUtils .notifyInformation ("Create Project" , "Creating project " + newProject );
114
+ private void createProject (String newProject , @ NotNull Odo odo ) {
115
+ String kind = getKind (odo );
116
+ Notification notification = NotificationUtils .notifyInformation ("Create " + kind , "Creating " + kind .toLowerCase () + " newProject" );
96
117
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
- }
118
+ odo .createProject (newProject );
119
+ notification .expire ();
120
+ NotificationUtils .notifyInformation ("Create " + kind , kind + newProject + " successfully created" );
121
+ sendTelemetryResults (TelemetryResult .SUCCESS );
122
+ } catch (IOException | CompletionException e ) {
123
+ notification .expire ();
124
+ sendTelemetryError (e );
125
+ UIHelper .executeInUI (() -> Messages .showErrorDialog ("Error: " + e .getLocalizedMessage (), "Create " + kind ));
126
+ }
106
127
}
107
128
108
- private CreateNewProjectDialog openCreateProjectDialog (List <String > allProjects , Point location , Project project ) {
109
- CreateNewProjectDialog dialog = new CreateNewProjectDialog (project , allProjects , location );
129
+ protected CreateNewProjectDialog openCreateProjectDialog (List <String > allProjects , String kind , Point location , Project project ) {
130
+ CreateNewProjectDialog dialog = new CreateNewProjectDialog (project , allProjects , kind , location );
110
131
dialog .show ();
111
132
return dialog ;
112
133
}
113
134
135
+ @ Override
136
+ public boolean isVisible (Object selected ) {
137
+ return isRoot (selected )
138
+ && isLoggedIn (selected );
139
+ }
140
+
141
+ private boolean isLoggedIn (Object node ) {
142
+ ApplicationsRootNode root = getRoot (node );
143
+ if (root == null ) {
144
+ return false ;
145
+ }
146
+ return root .isLogged ();
147
+ }
148
+
149
+ private boolean isRoot (Object node ) {
150
+ return node instanceof ApplicationsRootNode ;
151
+ }
152
+
153
+ private String getKind (Odo odo ) {
154
+ if (odo .isOpenShift ()) {
155
+ return "Project" ;
156
+ } else {
157
+ return "Namespace" ;
158
+ }
159
+ }
114
160
}
0 commit comments