Skip to content

Commit

Permalink
chore: remove AskWithApplyAllDialog form file
Browse files Browse the repository at this point in the history
  • Loading branch information
tangcent committed Jan 3, 2025
1 parent 3b4ad22 commit 190db1a
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 166 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,92 +1,163 @@
package com.itangcent.idea.plugin.dialog

import com.intellij.openapi.ui.Messages
import com.intellij.util.ui.JBUI
import com.itangcent.common.logger.Log
import com.itangcent.idea.utils.SwingUtils
import java.awt.EventQueue
import java.awt.GridBagConstraints
import java.awt.GridBagLayout
import java.awt.Window
import java.awt.event.KeyEvent
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.*

class AskWithApplyAllDialog(owner: Window? = null) : JDialog(owner) {
private var contentPane: JPanel? = null
private var buttonOK: JButton? = null
private var buttonCancel: JButton? = null
private var buttonNO: JButton? = null
private var applyToAllCheckBox: JCheckBox? = null
private var messageLabel: JLabel? = null
private var callBack: ((Int, Boolean) -> Unit)? = null
/**
* Represents the labels for the three buttons in a confirmation dialog
*/
data class ConfirmationDialogLabels(
val okText: String = "OK",
val noText: String = "No",
val cancelText: String = "Cancel"
)

fun updateMessage(
message: String,
) {
EventQueue.invokeLater {
messageLabel!!.text = message
}
class AskWithApplyAllDialog(owner: Window? = null) : JDialog(owner) {
private val contentPane = JPanel(GridBagLayout()).apply {
border = BorderFactory.createEmptyBorder(10, 10, 10, 10)
}

/**
* @param buttonNames [YES,NO,CANCEL]
*/
fun updateButtons(buttonNames: Array<String>) {
EventQueue.invokeLater {
try {
buttonOK!!.text = buttonNames[0]
buttonNO!!.text = buttonNames[1]
buttonCancel!!.text = buttonNames[2]
} catch (e: Exception) {
LOG.error("failed set button name: $buttonNames")
}
}
private val messageLabel = JLabel().apply {
border = BorderFactory.createEmptyBorder(0, 0, 10, 0)
}

fun setCallBack(
callBack: (Int, Boolean) -> Unit,
) {
this.callBack = callBack
private val applyToAllCheckBox = JCheckBox("Apply to all").apply {
border = BorderFactory.createEmptyBorder(10, 0, 10, 0)
}

private fun onOK() {
dispose()
callBack?.invoke(com.intellij.openapi.ui.Messages.OK, applyToAllCheckBox!!.isSelected)
private val buttonPanel = JPanel().apply {
layout = BoxLayout(this, BoxLayout.X_AXIS)
border = BorderFactory.createEmptyBorder(10, 0, 0, 0)
}

private fun onNO() {
dispose()
callBack?.invoke(com.intellij.openapi.ui.Messages.NO, applyToAllCheckBox!!.isSelected)
}
private val defaultLabels = ConfirmationDialogLabels()
private val buttonOK = JButton(defaultLabels.okText)
private val buttonNO = JButton(defaultLabels.noText)
private val buttonCancel = JButton(defaultLabels.cancelText)

private fun onCancel() {
dispose()
callBack?.invoke(com.intellij.openapi.ui.Messages.CANCEL, applyToAllCheckBox!!.isSelected)
}
private var callBack: ((Int, Boolean) -> Unit)? = null

init {
setContentPane(contentPane)
title = "Confirm"
isModal = true
getRootPane().defaultButton = buttonOK
initComponents()
initLayout()
initListeners()
SwingUtils.centerWindow(this)
}

buttonOK!!.addActionListener { onOK() }
buttonNO!!.addActionListener { onNO() }
buttonCancel!!.addActionListener { onCancel() }

// call onCancel() when cross is clicked
private fun initComponents() {
defaultCloseOperation = DO_NOTHING_ON_CLOSE
rootPane.defaultButton = buttonOK

buttonPanel.add(Box.createHorizontalGlue())
buttonPanel.add(buttonOK)
buttonPanel.add(Box.createHorizontalStrut(5))
buttonPanel.add(buttonNO)
buttonPanel.add(Box.createHorizontalStrut(5))
buttonPanel.add(buttonCancel)
}

private fun initLayout() {
setContentPane(contentPane)

contentPane.add(messageLabel, GridBagConstraints().apply {
gridx = 0
gridy = 0
weightx = 1.0
fill = GridBagConstraints.HORIZONTAL
anchor = GridBagConstraints.WEST
insets = JBUI.insets(0)
})

contentPane.add(applyToAllCheckBox, GridBagConstraints().apply {
gridx = 0
gridy = 1
weightx = 1.0
fill = GridBagConstraints.HORIZONTAL
anchor = GridBagConstraints.WEST
insets = JBUI.insets(0)
})

contentPane.add(buttonPanel, GridBagConstraints().apply {
gridx = 0
gridy = 2
weightx = 1.0
fill = GridBagConstraints.HORIZONTAL
anchor = GridBagConstraints.EAST
insets = JBUI.insets(0)
})

pack()
}

private fun initListeners() {
buttonOK.addActionListener { onOK() }
buttonNO.addActionListener { onNO() }
buttonCancel.addActionListener { onCancel() }

addWindowListener(object : WindowAdapter() {
override fun windowClosing(e: WindowEvent) {
onCancel()
}
})

// call onCancel() on ESCAPE
contentPane!!.registerKeyboardAction(
contentPane.registerKeyboardAction(
{ onCancel() },
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
)
}

fun updateMessage(message: String) {
EventQueue.invokeLater {
messageLabel.text = message
pack()
}
}

/**
* Updates the text of all three buttons.
*
* @param labels The labels for all three buttons
*/
fun updateButtonLabels(labels: ConfirmationDialogLabels) {
EventQueue.invokeLater {
buttonOK.text = labels.okText
buttonNO.text = labels.noText
buttonCancel.text = labels.cancelText
pack()
}
}

fun setCallBack(callBack: (Int, Boolean) -> Unit) {
this.callBack = callBack
}

private fun onOK() {
dispose()
callBack?.invoke(Messages.OK, applyToAllCheckBox.isSelected)
}

private fun onNO() {
dispose()
callBack?.invoke(Messages.NO, applyToAllCheckBox.isSelected)
}

private fun onCancel() {
dispose()
callBack?.invoke(Messages.CANCEL, applyToAllCheckBox.isSelected)
}

companion object : Log()
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class SuvApiExportDialog : ContextDialog() {

contentPane = suvApiExportPanel
getRootPane().defaultButton = buttonOK
SwingUtils.centerWindow(this)

buttonOK.addActionListener { onOK() }

Expand Down Expand Up @@ -139,6 +138,8 @@ class SuvApiExportDialog : ContextDialog() {
selectAllCheckBox.isSelected = apiList.model.size == apiList.selectionModel.selectedItemsCount
}
}

SwingUtils.centerWindow(this)
}

private fun onSelectedAll() = this.trigger.withTrigger("onSelectAll") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.intellij.openapi.ui.Messages
import com.intellij.openapi.ui.Messages.YesNoResult
import com.itangcent.idea.plugin.dialog.AskWithApplyAllDialog
import com.itangcent.idea.plugin.dialog.ChooseWithTipDialog
import com.itangcent.idea.plugin.dialog.ConfirmationDialogLabels
import com.itangcent.idea.utils.SwingUtils
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.util.UIUtils
Expand Down Expand Up @@ -142,15 +143,15 @@ class DefaultMessagesHelper : MessagesHelper {

override fun showAskWithApplyAllDialog(
message: String?,
buttonNames: Array<String>?,
buttonLabels: ConfirmationDialogLabels,
callBack: (Int, Boolean) -> Unit,
) {
actionContext.runInSwingUI {
val chooseWithTipDialog = AskWithApplyAllDialog(SwingUtils.preferableWindow())
buttonNames?.let { chooseWithTipDialog.updateButtons(buttonNames) }
chooseWithTipDialog.updateMessage(message ?: "Yes or No?")
UIUtils.show(chooseWithTipDialog)
chooseWithTipDialog.setCallBack(callBack)
val dialog = AskWithApplyAllDialog(SwingUtils.preferableWindow())
dialog.updateButtonLabels(buttonLabels)
dialog.updateMessage(message ?: "Yes or No?")
UIUtils.show(dialog)
dialog.setCallBack(callBack)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.itangcent.idea.swing
import com.google.inject.ImplementedBy
import com.intellij.openapi.ui.Messages
import com.itangcent.common.concurrent.ValueHolder
import com.itangcent.idea.plugin.dialog.ConfirmationDialogLabels
import com.itangcent.intellij.context.ActionContext
import org.jetbrains.annotations.Nls
import javax.swing.Icon
Expand Down Expand Up @@ -61,13 +62,16 @@ interface MessagesHelper {
)

/**
* @param message tip at the top
* @param buttonNames [YES,NO,CANCEL]
* @param callBack callback when button be clicked
* Shows a dialog with a message and three buttons, plus an "Apply to all" checkbox.
*
* @param message The message to display at the top of the dialog
* @param buttonLabels The labels for the three buttons (OK/YES, NO, CANCEL)
* @param callBack Callback invoked when a button is clicked, with the button type ([Messages.OK], [Messages.NO], [Messages.CANCEL])
* and the state of the "Apply to all" checkbox
*/
fun showAskWithApplyAllDialog(
message: String?,
buttonNames: Array<String>?,
buttonLabels: ConfirmationDialogLabels = ConfirmationDialogLabels(),
callBack: (Int, Boolean) -> Unit,
)
}
Expand All @@ -88,7 +92,7 @@ fun <T> MessagesHelper.showChooseWithTipDialog(

fun MessagesHelper.showAskWithApplyAllDialog(
message: String?,
buttonNames: Array<String>?,
buttonLabels: ConfirmationDialogLabels = ConfirmationDialogLabels(),
key: String,
callBack: (Int) -> Unit,
) {
Expand All @@ -99,7 +103,7 @@ fun MessagesHelper.showAskWithApplyAllDialog(
return
}

this.showAskWithApplyAllDialog(message, buttonNames) { ret, applyAll ->
this.showAskWithApplyAllDialog(message, buttonLabels) { ret, applyAll ->
if (applyAll) {
actionContext?.cache(key, ret)
}
Expand Down
Loading

0 comments on commit 190db1a

Please sign in to comment.