|
| 1 | +// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package software.aws.toolkits.jetbrains.services.caws |
| 4 | + |
| 5 | +import com.intellij.openapi.project.Project |
| 6 | +import com.intellij.openapi.util.Disposer |
| 7 | +import com.intellij.openapi.wm.StatusBar |
| 8 | +import com.intellij.openapi.wm.StatusBarWidget |
| 9 | +import com.intellij.openapi.wm.StatusBarWidgetFactory |
| 10 | +import com.intellij.util.Consumer |
| 11 | +import java.awt.Component |
| 12 | +import java.awt.event.MouseEvent |
| 13 | + |
| 14 | +private const val WIDGET_ID = "CawsSpaceProjectInfo" |
| 15 | + |
| 16 | +class CawsStatusBarInstaller : StatusBarWidgetFactory { |
| 17 | + private val spaceName: String? = System.getenv(CawsConstants.CAWS_ENV_ORG_NAME_VAR) |
| 18 | + private val projectName: String? = System.getenv(CawsConstants.CAWS_ENV_PROJECT_NAME_VAR) |
| 19 | + |
| 20 | + override fun getId(): String = WIDGET_ID |
| 21 | + |
| 22 | + override fun getDisplayName(): String = "$spaceName/$projectName" |
| 23 | + |
| 24 | + override fun isAvailable(project: Project): Boolean = spaceName != null && projectName != null |
| 25 | + |
| 26 | + override fun createWidget(project: Project): StatusBarWidget = CawsSpaceProjectInfo(spaceName, projectName) |
| 27 | + |
| 28 | + override fun disposeWidget(widget: StatusBarWidget) { |
| 29 | + Disposer.dispose(widget) |
| 30 | + } |
| 31 | + |
| 32 | + override fun canBeEnabledOn(statusBar: StatusBar): Boolean = true |
| 33 | +} |
| 34 | + |
| 35 | +private class CawsSpaceProjectInfo(val spaceName: String?, val projectName: String?) : |
| 36 | + StatusBarWidget, |
| 37 | + StatusBarWidget.TextPresentation { |
| 38 | + |
| 39 | + override fun ID(): String = WIDGET_ID |
| 40 | + |
| 41 | + override fun getPresentation(): StatusBarWidget.WidgetPresentation = this |
| 42 | + |
| 43 | + override fun getTooltipText(): String = "$spaceName/$projectName" |
| 44 | + override fun getClickConsumer(): Consumer<MouseEvent>? = null |
| 45 | + |
| 46 | + override fun getText(): String = "$spaceName/$projectName" |
| 47 | + |
| 48 | + override fun getAlignment(): Float = Component.CENTER_ALIGNMENT |
| 49 | + |
| 50 | + override fun dispose() {} |
| 51 | + |
| 52 | + override fun install(statusBar: StatusBar) {} |
| 53 | +} |
0 commit comments