Skip to content

Commit b339d23

Browse files
committed
using proper chainging load ns -> display dialog (#637)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent 991e10f commit b339d23

File tree

3 files changed

+66
-32
lines changed

3 files changed

+66
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 Red Hat, Inc.
3+
* Distributed under license by Red Hat, Inc. All rights reserved.
4+
* This program is made available under the terms of the
5+
* Eclipse Public License v2.0 which accompanies this distribution,
6+
* and is available at http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
******************************************************************************/
11+
package com.redhat.devtools.intellij.kubernetes
12+
13+
import com.intellij.openapi.application.ApplicationManager
14+
import java.util.concurrent.Executor
15+
16+
object CompletableFutureUtils {
17+
val UI_EXECUTOR = Executor { runnable: Runnable ->
18+
ApplicationManager.getApplication().invokeLater { runnable.run() }
19+
}
20+
21+
val PLATFORM_EXECUTOR = Executor { runnable: Runnable ->
22+
ApplicationManager.getApplication().executeOnPooledThread { runnable.run() }
23+
}
24+
}

Diff for: src/main/kotlin/com/redhat/devtools/intellij/kubernetes/actions/SetCurrentNamespaceAction.kt

+23-16
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
package com.redhat.devtools.intellij.kubernetes.actions
1212

1313
import com.intellij.openapi.actionSystem.AnActionEvent
14-
import com.intellij.openapi.application.runInEdt
1514
import com.intellij.openapi.diagnostic.logger
1615
import com.intellij.openapi.project.Project
1716
import com.redhat.devtools.intellij.common.actions.StructureTreeAction
17+
import com.redhat.devtools.intellij.kubernetes.CompletableFutureUtils.PLATFORM_EXECUTOR
18+
import com.redhat.devtools.intellij.kubernetes.CompletableFutureUtils.UI_EXECUTOR
1819
import com.redhat.devtools.intellij.kubernetes.dialogs.ResourceNameDialog
1920
import com.redhat.devtools.intellij.kubernetes.model.IResourceModel
2021
import com.redhat.devtools.intellij.kubernetes.model.Notification
@@ -24,9 +25,10 @@ import com.redhat.devtools.intellij.kubernetes.model.util.ResourceException
2425
import com.redhat.devtools.intellij.kubernetes.model.util.toMessage
2526
import com.redhat.devtools.intellij.kubernetes.telemetry.TelemetryService
2627
import com.redhat.devtools.intellij.kubernetes.tree.KubernetesStructure.NamespacesFolder
28+
import io.fabric8.kubernetes.api.model.Namespace
2729
import java.awt.Point
2830
import java.awt.event.MouseEvent
29-
import java.util.concurrent.CompletableFuture.runAsync
31+
import java.util.concurrent.CompletableFuture
3032
import javax.swing.tree.TreePath
3133

3234
class SetCurrentNamespaceAction : StructureTreeAction(false) {
@@ -39,23 +41,28 @@ class SetCurrentNamespaceAction : StructureTreeAction(false) {
3941
}
4042

4143
private fun openNameDialog(project: Project, model: IResourceModel, location: Point?) {
42-
runAsync {
43-
val projects = loadNamespaces(model)
44-
runInEdt {
44+
CompletableFuture.supplyAsync(
45+
{ loadNamespaces(model) },
46+
PLATFORM_EXECUTOR
47+
).thenApplyAsync (
48+
{ projects ->
4549
val dialog = ResourceNameDialog(project, "Namespace", projects, onOk(model), location)
46-
dialog.showAndGet()
47-
}
48-
}
50+
dialog.show()
51+
},
52+
UI_EXECUTOR
53+
)
4954
}
5055

51-
private fun loadNamespaces(model: IResourceModel) = try {
52-
model.getCurrentContext()?.getAllResources(NamespacesOperator.KIND, ResourcesIn.NO_NAMESPACE)
53-
} catch (e: ResourceException) {
54-
logger<SetCurrentNamespaceAction>().warn(
55-
"Could not get all namespaces.", e
56-
)
57-
null
58-
} ?: emptyList()
56+
private fun loadNamespaces(model: IResourceModel): Collection<Namespace> {
57+
return try {
58+
model.getCurrentContext()?.getAllResources(NamespacesOperator.KIND, ResourcesIn.NO_NAMESPACE)
59+
} catch (e: ResourceException) {
60+
logger<SetCurrentNamespaceAction>().warn(
61+
"Could not get all namespaces.", e
62+
)
63+
null
64+
} ?: emptyList()
65+
}
5966

6067
private fun onOk(model: IResourceModel): (projectName: String) -> Unit {
6168
return { name ->

Diff for: src/main/kotlin/com/redhat/devtools/intellij/kubernetes/actions/SetCurrentProjectAction.kt

+19-16
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
package com.redhat.devtools.intellij.kubernetes.actions
1212

1313
import com.intellij.openapi.actionSystem.AnActionEvent
14-
import com.intellij.openapi.application.runInEdt
1514
import com.intellij.openapi.diagnostic.logger
1615
import com.intellij.openapi.project.Project
1716
import com.redhat.devtools.intellij.common.actions.StructureTreeAction
17+
import com.redhat.devtools.intellij.kubernetes.CompletableFutureUtils
1818
import com.redhat.devtools.intellij.kubernetes.dialogs.ResourceNameDialog
1919
import com.redhat.devtools.intellij.kubernetes.model.IResourceModel
2020
import com.redhat.devtools.intellij.kubernetes.model.Notification
@@ -26,8 +26,8 @@ import com.redhat.devtools.intellij.kubernetes.telemetry.TelemetryService
2626
import com.redhat.devtools.intellij.kubernetes.tree.OpenShiftStructure.ProjectsFolder
2727
import java.awt.Point
2828
import java.awt.event.MouseEvent
29+
import java.util.concurrent.CompletableFuture
2930
import javax.swing.tree.TreePath
30-
import org.jetbrains.concurrency.runAsync
3131

3232
class SetCurrentProjectAction : StructureTreeAction(false) {
3333

@@ -39,23 +39,26 @@ class SetCurrentProjectAction : StructureTreeAction(false) {
3939
}
4040

4141
private fun openNameDialog(project: Project, model: IResourceModel, location: Point?) {
42-
runAsync {
43-
val projects = loadProjects(model)
44-
runInEdt {
42+
CompletableFuture.supplyAsync(
43+
{ loadProjects(model) },
44+
CompletableFutureUtils.PLATFORM_EXECUTOR
45+
).thenApplyAsync(
46+
{ projects ->
4547
val dialog = ResourceNameDialog(project, "Project", projects, onOk(model), location)
46-
dialog.showAndGet()
47-
}
48-
}
48+
dialog.show()
49+
},
50+
CompletableFutureUtils.UI_EXECUTOR
51+
)
4952
}
5053

51-
private fun loadProjects(model: IResourceModel) = try {
52-
model.getCurrentContext()?.getAllResources(ProjectsOperator.KIND, ResourcesIn.NO_NAMESPACE)
53-
} catch (e: ResourceException) {
54-
logger<SetCurrentProjectAction>().warn(
55-
"Could not get all projects.", e
56-
)
57-
null
58-
} ?: emptyList()
54+
private fun loadProjects(model: IResourceModel): Collection<io.fabric8.openshift.api.model.Project> {
55+
return try {
56+
model.getCurrentContext()?.getAllResources(ProjectsOperator.KIND, ResourcesIn.NO_NAMESPACE)
57+
} catch (e: ResourceException) {
58+
logger<SetCurrentProjectAction>().warn("Could not get all projects.", e)
59+
emptyList()
60+
} ?: emptyList()
61+
}
5962

6063
private fun onOk(model: IResourceModel): (projectName: String) -> Unit {
6164
return { name ->

0 commit comments

Comments
 (0)