-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'zowe-release/v1.2.1-223' into zowe-release/v1.2.1-231
Signed-off-by: Uladzislau <[email protected]>
- Loading branch information
Showing
7 changed files
with
343 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/main/kotlin/eu/ibagroup/formainframe/utils/ui/WindowsLikeMessageDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* This program and the accompanying materials are made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Copyright IBA Group 2020 | ||
*/ | ||
|
||
package org.zowe.explorer.utils.ui | ||
|
||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.ui.messages.MessageDialog | ||
import com.intellij.ui.components.panels.NonOpaquePanel | ||
import com.intellij.util.ui.UIUtil | ||
import java.awt.Component | ||
import java.awt.event.ActionEvent | ||
import javax.swing.* | ||
|
||
/** | ||
* Windows like message dialog class which does the same stuff as @see Messages.showDialog() but | ||
* uses Windows based representation of the dialog buttons | ||
*/ | ||
class WindowsLikeMessageDialog( | ||
project: Project?, | ||
parentComponent: Component?, | ||
message: String, | ||
title: String, | ||
options: Array<String>, | ||
defaultOptionIndex: Int, | ||
focusedOptionIndex: Int, | ||
icon: Icon?, | ||
doNotAskOption: com.intellij.openapi.ui.DoNotAskOption?, | ||
canBeParent: Boolean, | ||
helpId: String? | ||
) : MessageDialog( | ||
project, | ||
parentComponent, | ||
message, | ||
title, | ||
options, | ||
defaultOptionIndex, | ||
focusedOptionIndex, | ||
icon, | ||
doNotAskOption, | ||
canBeParent, | ||
helpId) { | ||
|
||
companion object { | ||
/** | ||
* Static function is used to show Windows based message dialog | ||
*/ | ||
fun showWindowsLikeMessageDialog(project: Project?, | ||
parentComponent: Component? = null, | ||
message: String, | ||
title: String, | ||
options: Array<String>, | ||
defaultOptionIndex: Int, | ||
focusedOptionIndex: Int, | ||
icon: Icon? = null, | ||
doNotAskOption: com.intellij.openapi.ui.DoNotAskOption? = null, | ||
canBeParent: Boolean = false, | ||
helpId: String? = null): Int { | ||
val windowsLikeMessageDialog = WindowsLikeMessageDialog(project, parentComponent, message, title, options, defaultOptionIndex, focusedOptionIndex, icon, doNotAskOption, canBeParent, helpId) | ||
windowsLikeMessageDialog.show() | ||
return windowsLikeMessageDialog.exitCode | ||
} | ||
} | ||
|
||
override fun createActions(): Array<Action> { | ||
return mutableListOf<Action>().toTypedArray() | ||
} | ||
|
||
override fun createLeftSideActions(): Array<Action> { | ||
val actions = mutableListOf<Action>() | ||
for (i in myOptions.indices) { | ||
val option = myOptions[i] | ||
val action = object: AbstractAction(UIUtil.replaceMnemonicAmpersand(option)) { | ||
override fun actionPerformed(e: ActionEvent?) { | ||
close(i, true) | ||
} | ||
} | ||
|
||
if (i == myDefaultOptionIndex) { | ||
action.putValue(DEFAULT_ACTION, true); | ||
} | ||
|
||
if (i == myFocusedOptionIndex) { | ||
action.putValue(FOCUSED_ACTION, true); | ||
} | ||
|
||
UIUtil.assignMnemonic(option, action); | ||
actions.add(action); | ||
} | ||
|
||
if (helpId != null) { | ||
actions.add(helpAction); | ||
} | ||
return actions.toTypedArray(); | ||
} | ||
|
||
override fun createButtonsPanel(buttons: MutableList<out JButton>): JPanel { | ||
return createLayoutButtonsPanel(buttons) | ||
} | ||
|
||
/** | ||
* Function is used to create the bottom JComponent of the message dialog containing N buttons followed one by one | ||
* using vertical direction | ||
*/ | ||
private fun createLayoutButtonsPanel(buttons: MutableList<out JButton>): JPanel { | ||
val buttonsPanel: JPanel = NonOpaquePanel() | ||
val jPanels = mutableListOf<JPanel>() | ||
buttonsPanel.layout = BoxLayout(buttonsPanel, BoxLayout.Y_AXIS) | ||
|
||
for (i in buttons.indices) { | ||
val button: JButton = buttons[i] | ||
val jPanel: JPanel = NonOpaquePanel() | ||
jPanel.add(button) | ||
jPanels.add(jPanel) | ||
} | ||
|
||
jPanels.forEach { buttonsPanel.add(it) } | ||
return buttonsPanel | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
src/test/kotlin/eu/ibagroup/formainframe/utils/ui/WindowsLikeMessageDialogTestSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* This program and the accompanying materials are made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Copyright IBA Group 2020 | ||
*/ | ||
|
||
package org.zowe.explorer.utils.ui | ||
|
||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.openapi.project.ProjectManager | ||
import com.intellij.ui.UiInterceptors | ||
import org.zowe.explorer.testutils.WithApplicationShouldSpec | ||
import io.kotest.assertions.assertSoftly | ||
import io.kotest.matchers.shouldBe | ||
import io.mockk.* | ||
import java.awt.EventQueue | ||
import javax.swing.Action | ||
import javax.swing.JButton | ||
import javax.swing.JPanel | ||
|
||
class WindowsLikeMessageDialogTestSpec : WithApplicationShouldSpec({ | ||
|
||
afterSpec { | ||
clearAllMocks() | ||
} | ||
|
||
context("Windows dialog common spec") { | ||
val application = ApplicationManager.getApplication() | ||
val project = ProjectManager.getInstance().defaultProject | ||
mockkStatic(EventQueue::class) | ||
mockkObject(application) | ||
every { EventQueue.isDispatchThread() } returns true | ||
every { application.isDispatchThread } returns true | ||
|
||
val actions = arrayOf( | ||
"Skip the conflicting file(s)", | ||
"Replace the file(s) in the destination", | ||
"Decide for each file" | ||
) | ||
|
||
val customDialog = WindowsLikeMessageDialog( | ||
project, | ||
null, | ||
"The destination already has file(s) with\nthe same name.\n" + | ||
"Please, select an action.", | ||
"Name conflicts in 1 file(s)", | ||
actions, | ||
0, | ||
0, | ||
null, | ||
null, | ||
false, | ||
"helpId" | ||
) | ||
|
||
val classUnderTest = spyk(customDialog, "testDialog") | ||
|
||
should("create right side empty actions of the dialog") { | ||
val expected = mutableListOf<Action>().toTypedArray() | ||
val methodToTest = classUnderTest::class.java.declaredMethods.single { it.name == "createActions" } | ||
val result = methodToTest.invoke(classUnderTest) | ||
|
||
assertSoftly { | ||
result as Array<*> shouldBe expected | ||
} | ||
} | ||
|
||
should("create left side actions of the dialog") { | ||
val methodToTest = classUnderTest::class.java.declaredMethods.single { it.name == "createLeftSideActions" } | ||
val result = methodToTest.invoke(classUnderTest) as Array<*> | ||
val actionToPerform = (result[0] as Action) | ||
|
||
// Call default action to be able to cover lambda expression | ||
actionToPerform.actionPerformed(null) | ||
|
||
assertSoftly { | ||
// Plus help action, because helpId is not null | ||
result.size shouldBe 4 | ||
} | ||
} | ||
|
||
should("create buttons panel of the dialog with 3 buttons") { | ||
val methodToTest = classUnderTest::class.java.declaredMethods.single { it.name == "createButtonsPanel" } | ||
val arguments = mutableListOf(JButton(actions[0]), JButton(actions[1]), JButton(actions[2])) | ||
val result = methodToTest.invoke(classUnderTest, arguments) as JPanel | ||
|
||
assertSoftly { | ||
((result.getComponent(0) as JPanel).getComponent(0) as JButton).text shouldBe "Skip the conflicting file(s)" | ||
((result.getComponent(1) as JPanel).getComponent(0) as JButton).text shouldBe "Replace the file(s) in the destination" | ||
((result.getComponent(2) as JPanel).getComponent(0) as JButton).text shouldBe "Decide for each file" | ||
} | ||
} | ||
unmockkAll() | ||
} | ||
|
||
context("test showWindowsLikeMessageDialog static function") { | ||
val project = ProjectManager.getInstance().defaultProject | ||
mockkStatic(EventQueue::class) | ||
every { EventQueue.isDispatchThread() } returns true | ||
|
||
val actions = arrayOf( | ||
"Skip the conflicting file(s)", | ||
"Replace the file(s) in the destination", | ||
"Decide for each file" | ||
) | ||
|
||
should("call showWindowsLikeMessageDialog") { | ||
mockkStatic(UiInterceptors::class) | ||
every { UiInterceptors.tryIntercept(any()) } returns true | ||
val exitCode = WindowsLikeMessageDialog.showWindowsLikeMessageDialog( | ||
project, | ||
null, | ||
"The destination already has file(s) with\nthe same name.\n" + | ||
"Please, select an action.", | ||
"Name conflicts in 1 file(s)", | ||
actions, | ||
0, | ||
0, | ||
null, | ||
null, | ||
false, | ||
null | ||
) | ||
assertSoftly { | ||
exitCode shouldBe 1 | ||
} | ||
} | ||
unmockkAll() | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.