-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: remove AskWithApplyAllDialog form file
- Loading branch information
Showing
8 changed files
with
182 additions
and
166 deletions.
There are no files selected for viewing
83 changes: 0 additions & 83 deletions
83
idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/AskWithApplyAllDialog.form
This file was deleted.
Oops, something went wrong.
173 changes: 122 additions & 51 deletions
173
idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/AskWithApplyAllDialog.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 |
---|---|---|
@@ -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() | ||
} |
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
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.