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 Dec 31, 2024
1 parent 6c46326 commit ffc8365
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.intellij.openapi.ui.Messages
import com.itangcent.common.concurrent.ValueHolder
import com.itangcent.common.utils.toBool
import com.itangcent.idea.plugin.condition.ConditionOnSetting
import com.itangcent.idea.plugin.dialog.ConfirmationDialogLabels
import com.itangcent.idea.swing.MessagesHelper
import com.itangcent.intellij.config.ConfigReader
import com.itangcent.intellij.context.ActionContext
Expand Down Expand Up @@ -71,7 +72,8 @@ class AlwaysAskYapiSaveInterceptor : YapiSaveInterceptor {
val context = ActionContext.getContext() ?: return true
context.instance(MessagesHelper::class).showAskWithApplyAllDialog(
"The api [${apiInfo["title"]}] already existed in the project.\n" +
"Do you want update it?", arrayOf("Update", "Skip", "Cancel")
"Do you want update it?",
ConfirmationDialogLabels(okText = "Update", noText = "Skip", cancelText = "Cancel")
) { ret, applyAll ->
if (ret == Messages.CANCEL) {
context.stop()
Expand Down

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)
}
}
}
Loading

0 comments on commit ffc8365

Please sign in to comment.