-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
132 additions
and
0 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
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
120 changes: 120 additions & 0 deletions
120
src/main/java/armameeldoparti/utils/common/custom/graphical/CustomOptionPaneUI.java
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,120 @@ | ||
package armameeldoparti.utils.common.custom.graphical; | ||
|
||
import armameeldoparti.utils.common.Constants; | ||
import java.awt.Container; | ||
import java.util.Arrays; | ||
import javax.swing.JComponent; | ||
import javax.swing.JOptionPane; | ||
import javax.swing.JPanel; | ||
import javax.swing.SwingUtilities; | ||
import javax.swing.UIManager; | ||
import javax.swing.plaf.ComponentUI; | ||
import javax.swing.plaf.basic.BasicOptionPaneUI; | ||
import net.miginfocom.swing.MigLayout; | ||
|
||
/** | ||
* A custom option pane UI that fits the overall program aesthetics. | ||
* | ||
* @author Bonino, Francisco Ignacio. | ||
* | ||
* @version 0.0.1 | ||
* | ||
* @since 3.0 | ||
*/ | ||
public class CustomOptionPaneUI extends BasicOptionPaneUI { | ||
|
||
// ---------- Constructor -------------------------------------------------------------------------------------------------------------------------- | ||
|
||
/** | ||
* Creates the custom option pane UI according to the overall program aesthetics. | ||
*/ | ||
public CustomOptionPaneUI() { | ||
super(); | ||
} | ||
|
||
// ---------- Public methods ----------------------------------------------------------------------------------------------------------------------- | ||
|
||
/** | ||
* The "java:S1172" warning is suppressed since the argument is intentionally unused. | ||
* | ||
* @param component Intentionally unused argument. | ||
* | ||
* @return A new custom option pane UI. | ||
*/ | ||
@SuppressWarnings("java:S1172") | ||
public static ComponentUI createUI(JComponent component) { | ||
return new CustomOptionPaneUI(); | ||
} | ||
|
||
// ---------- Protected methods -------------------------------------------------------------------------------------------------------------------- | ||
|
||
/** | ||
* Creates a button for each string in {@code buttons} and adds then to {@code container}. If {@code buttons} does not contain any string-instance | ||
* object, an array is retrieved using the message type for the dialog. This array contains the strings | ||
* that will be used in the buttons to be placed in the dialog. | ||
* | ||
* <p>The "java:S1190" and "java:S117" warnings are suppressed since JDK22 allows the use of unnamed variables. | ||
* | ||
* @param container A container for the buttons. | ||
* @param buttons An array with the strings for each button of the dialog. | ||
* @param initialIndex An initial index used for validation. | ||
* | ||
* @see #getButtonsForMessageType(int) | ||
*/ | ||
@Override | ||
@SuppressWarnings({"java:S1190", "java:S117"}) | ||
protected void addButtonComponents(Container container, Object[] buttons, int initialIndex) { | ||
if (buttons == null) { | ||
return; | ||
} | ||
|
||
if (buttons.length <= 0) { | ||
return; | ||
} | ||
|
||
if (Arrays.asList(buttons) | ||
.stream() | ||
.noneMatch(String.class::isInstance)) { | ||
buttons = getButtonsForMessageType(optionPane.getMessageType()); | ||
} | ||
|
||
final int buttonsNumber = buttons.length; | ||
|
||
JPanel buttonPanel = new JPanel(); | ||
|
||
buttonPanel.setLayout(new MigLayout()); | ||
buttonPanel.setOpaque(false); | ||
|
||
for (Object buttonText : buttons) { | ||
CustomButton customButton = new CustomButton((String) buttonText, Constants.ROUNDED_BORDER_ARC_BUTTON_DIALOG); | ||
|
||
customButton.addActionListener(_ -> { | ||
if (initialIndex >= 0 && initialIndex < buttonsNumber) { | ||
((JOptionPane) SwingUtilities.getAncestorOfClass(JOptionPane.class, container)) | ||
.setValue(buttonText); | ||
} | ||
}); | ||
|
||
buttonPanel.add(customButton); | ||
} | ||
|
||
container.add(buttonPanel, Constants.MIG_LAYOUT_SOUTH); | ||
} | ||
|
||
// ---------- Private methods ---------------------------------------------------------------------------------------------------------------------- | ||
|
||
/** | ||
* @param messageType The type of the message to be shown in the dialog. | ||
* | ||
* @return An array with the strings for each button to be placed in the dialog based on the message type. | ||
*/ | ||
private Object[] getButtonsForMessageType(int messageType) { | ||
return switch (messageType) { | ||
case JOptionPane.WARNING_MESSAGE -> new Object[] { UIManager.getString("OptionPane.okButtonText"), | ||
UIManager.getString("OptionPane.cancelButtonText") }; | ||
case JOptionPane.QUESTION_MESSAGE -> new Object[] { UIManager.getString("OptionPane.yesButtonText"), | ||
UIManager.getString("OptionPane.noButtonText") }; | ||
default -> new Object[] { UIManager.getString("OptionPane.okButtonText") }; | ||
}; | ||
} | ||
} |