Skip to content

Commit

Permalink
add custom option pane ui
Browse files Browse the repository at this point in the history
  • Loading branch information
akmsw committed Jun 24, 2024
1 parent f601c38 commit 5e0c7de
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/armameeldoparti/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import armameeldoparti.utils.common.CommonFields;
import armameeldoparti.utils.common.CommonFunctions;
import armameeldoparti.utils.common.Constants;
import armameeldoparti.utils.common.custom.graphical.CustomOptionPaneUI;
import armameeldoparti.views.AnchoragesView;
import armameeldoparti.views.HelpView;
import armameeldoparti.views.MainMenuView;
Expand Down Expand Up @@ -172,7 +173,13 @@ private static void setUpGeneralGraphicalProperties() {
UIManager.put("ComboBox.foreground", Color.WHITE);
UIManager.put("ComboBox.selectionBackground", Constants.COLOR_GREEN_MEDIUM);
UIManager.put("ComboBox.selectionForeground", Color.WHITE);
UIManager.put("OptionPaneUI", CustomOptionPaneUI.class
.getName());
UIManager.put("OptionPane.background", Constants.COLOR_GREEN_LIGHT);
UIManager.put("OptionPane.cancelButtonText", Constants.TEXT_BUTTON_DIALOG_CANCEL);
UIManager.put("OptionPane.noButtonText", Constants.TEXT_BUTTON_DIALOG_NO);
UIManager.put("OptionPane.okButtonText", Constants.TEXT_BUTTON_DIALOG_OK);
UIManager.put("OptionPane.yesButtonText", Constants.TEXT_BUTTON_DIALOG_YES);
UIManager.put("Panel.background", Constants.COLOR_GREEN_LIGHT);
UIManager.put("RadioButton.background", Constants.COLOR_GREEN_LIGHT);
UIManager.put("RadioButton.focus", Constants.COLOR_GREEN_LIGHT);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/armameeldoparti/utils/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public final class Constants {
public static final int MIN_PLAYERS_PER_ANCHORAGE = 2;
public static final int MIX_BY_SKILL_POINTS = 1;
public static final int MIX_RANDOM = 0;
public static final int ROUNDED_BORDER_ARC_BUTTON_DIALOG = 15;
public static final int ROUNDED_BORDER_ARC_COMBOBOX = 10;
public static final int ROUNDED_BORDER_ARC_GENERAL = 30;
public static final int ROUNDED_BORDER_ARC_SCROLLBAR = 20;
Expand Down Expand Up @@ -171,6 +172,10 @@ public final class Constants {
public static final String REGEX_NAMES_VALIDATION = "[a-z A-ZÁÉÍÓÚáéíóúñÑ]+";
public static final String REGEX_PDA_DATA_RETRIEVE = "[CLMFG].+>.+";
public static final String REGEX_PLAYERS_COUNT = "(?!(?<=" + PLAYERS_PER_TEAM + ")\\d).";
public static final String TEXT_BUTTON_DIALOG_CANCEL = "Cancelar";
public static final String TEXT_BUTTON_DIALOG_NO = "No";
public static final String TEXT_BUTTON_DIALOG_OK = "Aceptar";
public static final String TEXT_BUTTON_DIALOG_YES = "Sí";
public static final String URL_CONTACT = "https://github.com/" + PROGRAM_AUTHOR_GITHUB_USERNAME;
public static final String URL_ISSUES = URL_CONTACT + "/" + PROGRAM_TITLE.replace(" ", "-") + "/issues";

Expand Down
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") };
};
}
}

0 comments on commit 5e0c7de

Please sign in to comment.