Skip to content

Commit

Permalink
Fixes #104
Browse files Browse the repository at this point in the history
  • Loading branch information
Milchreis committed Oct 6, 2023
1 parent ac4cef2 commit fac45bf
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.milchreis.uibooster.model;

public interface ButtonClickListener {

void onClick(FormElement element, Form form);

}
74 changes: 74 additions & 0 deletions src/main/java/de/milchreis/uibooster/model/FormBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,44 @@ public FormBuilder addLabel(String label) {
* @param onClick expects an implementation of Runnable, which is executed when the button is clicked
*/
public FormBuilder addButton(String buttonLabel, Runnable onClick) {
return addButton(null, buttonLabel, (element, form) -> onClick.run());
}

/**
* Adds a button to the form.
*
* @param buttonLabel expects the text which is shown on the button
* @param onClick expects an implementation of Runnable, which is executed when the button is clicked
*/
public FormBuilder addButton(String buttonLabel, ButtonClickListener onClick) {
return addButton(null, buttonLabel, onClick);
}

/**
* Adds a button to the form.
*
* @param buttonLabel expects the text which is shown on the button
* @param onClick expects an implementation of Runnable, which is executed when the button is clicked
* @param backgroundColor expects a color for the tint of this button background
*/
public FormBuilder addButton(String buttonLabel, ButtonClickListener onClick, Color backgroundColor) {
addElement(new ButtonFormElement(buttonLabel, buttonLabel, onClick, backgroundColor, null));
return this;
}

/**
* Adds a button to the form.
*
* @param buttonLabel expects the text which is shown on the button
* @param onClick expects an implementation of Runnable, which is executed when the button is clicked
* @param backgroundColor expects a color for the tint of this button background
* @param textColor expects a color for the text in this button
*/
public FormBuilder addButton(String buttonLabel, ButtonClickListener onClick, Color backgroundColor, Color textColor) {
addElement(new ButtonFormElement(buttonLabel, buttonLabel, onClick, backgroundColor, textColor));
return this;
}

/**
* Adds a button to the form.
*
Expand All @@ -222,10 +257,49 @@ public FormBuilder addButton(String buttonLabel, Runnable onClick) {
* @param onClick expects an implementation of Runnable, which is executed when the button is clicked
*/
public FormBuilder addButton(String label, String buttonLabel, Runnable onClick) {
addElement(new ButtonFormElement(label, buttonLabel, (element, form) -> onClick.run()));
return this;
}

/**
* Adds a button to the form.
*
* @param label expects the label for this input element
* @param buttonLabel expects the text which is shown on the button
* @param onClick expects an implementation of Runnable, which is executed when the button is clicked
*/
public FormBuilder addButton(String label, String buttonLabel, ButtonClickListener onClick) {
addElement(new ButtonFormElement(label, buttonLabel, onClick));
return this;
}

/**
* Adds a button to the form.
*
* @param label expects the label for this input element
* @param buttonLabel expects the text which is shown on the button
* @param onClick expects an implementation of Runnable, which is executed when the button is clicked
* @param backgroundColor expects a color for the tint of this button background
* @param textColor expects a color for the text in this button
*/
public FormBuilder addButton(String label, String buttonLabel, ButtonClickListener onClick, Color backgroundColor, Color textColor) {
addElement(new ButtonFormElement(label, buttonLabel, onClick, backgroundColor, textColor));
return this;
}

/**
* Adds a button to the form.
*
* @param label expects the label for this input element
* @param buttonLabel expects the text which is shown on the button
* @param onClick expects an implementation of Runnable, which is executed when the button is clicked
* @param backgroundColor expects a color for the tint of this button background
*/
public FormBuilder addButton(String label, String buttonLabel, ButtonClickListener onClick, Color backgroundColor) {
addElement(new ButtonFormElement(label, buttonLabel, onClick, backgroundColor, null));
return this;
}

/**
* Adds a progress bar to the form.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
package de.milchreis.uibooster.model.formelements;

import de.milchreis.uibooster.model.ButtonClickListener;
import de.milchreis.uibooster.model.FormElement;
import de.milchreis.uibooster.model.FormElementChangeListener;

import javax.swing.*;
import java.awt.*;

public class ButtonFormElement extends FormElement {

private String buttonLabel;
private final Runnable onClick;
private final ButtonClickListener onClick;
private Color backgroundColor;
private Color textColor;
private JButton button;

public ButtonFormElement(String label, String buttonLabel, Runnable onClick) {
super(label);
public ButtonFormElement(String label, String buttonLabel, ButtonClickListener onClick) {
this(label, buttonLabel, onClick, null, null);
}

public ButtonFormElement(String label, String buttonLabel, ButtonClickListener onClick, Color backgroundColor, Color textColor) {
super(label);
this.buttonLabel = buttonLabel;
this.onClick = onClick;

this.backgroundColor = backgroundColor;
this.textColor = textColor;
}

@Override
public JComponent createComponent(FormElementChangeListener onChange) {
button = new JButton(buttonLabel);

setBackgroundColor(backgroundColor);
setTextColor(textColor);

button.addActionListener(l -> {
onClick.run();
onClick.onClick(this, form);

if (onChange != null)
onChange.onChange(this, buttonLabel, form);
Expand All @@ -46,4 +60,22 @@ public void setValue(Object value) {
buttonLabel = value.toString();
button.setText(buttonLabel);
}

public Color getBackgroundColor() {
return backgroundColor;
}

public Color getTextColor() {
return textColor;
}

public void setBackgroundColor(Color backgroundColor) {
if (backgroundColor != null) this.backgroundColor = backgroundColor;
button.setBackground(backgroundColor);
}

public void setTextColor(Color textColor) {
if (textColor != null) this.textColor = textColor;
button.setForeground(textColor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public void test_form_dialog() {
.addText("First name of your best friend").setID("first")
.addText("Last name of your best friend").setID("last")
.addDatePicker("Birthday of your best friend").setID("birthday")
.addButton("Submit", () -> {
}).setID("submit")
.addButton("Submit", () -> {}).setID("submit")
.addTable("Your friends", Arrays.asList("first name", "last name", "birthday"), new String[][]{}, false).setID("table")
.setChangeListener((element, value, form) -> {

Expand All @@ -36,4 +35,4 @@ public void test_form_dialog() {
.show();

}
}
}
47 changes: 31 additions & 16 deletions src/test/java/de/milchreis/uibooster/FormBuilderTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package de.milchreis.uibooster;

import static java.lang.Thread.sleep;

import de.milchreis.uibooster.model.Form;
import de.milchreis.uibooster.model.FormElement;
import de.milchreis.uibooster.model.ListElement;
import java.awt.Color;
import java.awt.Font;
import de.milchreis.uibooster.model.formelements.ButtonFormElement;
import org.junit.jupiter.api.Test;

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import org.junit.jupiter.api.Test;
import static java.lang.Thread.sleep;

class FormBuilderTest {

Expand All @@ -22,7 +21,7 @@ class FormBuilderTest {
public void test_form_close_itself() {

booster.createForm("Title")
.addButton("Close window", () -> {
.addButton("Close window", (button, form) -> {
System.out.println("Close this window");
}
).setID("close")
Expand Down Expand Up @@ -50,7 +49,7 @@ public void test_form_dialog() {
"Adventure", "Crime", "Mystery", "Sci-fi", "Horror"))
.addLabel("Choose an action")
.addButton("half full", () -> booster.showInfoDialog("Optimist"))
.addButton("half empty", () -> booster.showInfoDialog("Pessimist"))
.addButton("half empty", (button, form1) -> booster.showInfoDialog("Pessimist"))
.addSlider("How many liters did you drink today?", 0, 5, 1, 5, 1)
.addCheckbox("Are you fine?", "yes")
.addColorPicker("Favorite color?", new Color(212, 32, 39))
Expand Down Expand Up @@ -201,8 +200,7 @@ public void test_form_disable_elements() throws InterruptedException {
public void test_disable_button_dialog() throws InterruptedException {
Form form = booster
.createForm("Test")
.addButton("Prev", () -> {
}).setDisabled().setID("test")
.addButton("Prev", (button, form1) -> {}).setDisabled().setID("test")
.run();

sleep(1000);
Expand All @@ -214,12 +212,9 @@ public void test_disable_button_in_row_dialog() throws InterruptedException {
Form form = booster
.createForm("Test")
.startRow()
.addButton("On", () -> {
}).setID("on1")
.addButton("Off", () -> {
}).setDisabled().setID("off1")
.addButton("On", () -> {
}).setID("on2")
.addButton("On", (button, form1) -> {}).setID("on1")
.addButton("Off", (button, form1) -> {}).setDisabled().setID("off1")
.addButton("On", (button, form1) -> {}).setID("on2")
.endRow()
.show();

Expand Down Expand Up @@ -328,4 +323,24 @@ void test_form_with_init_listener() {
.show();
}

@Test
void test_custom_color_button() {
booster.createForm("small form")
.addButton("Default button", "click me", (e, f) -> {})
.addButton("Colored button here", "click me", (e, f) -> {}, new Color(183, 128, 0), Color.WHITE)
.addButton( "click me", (e, f) -> {}, new Color(71, 118, 65), Color.WHITE)
.show();
}

@Test
void test_change_custom_color_button() {
booster.createForm("small form")
.addButton("darker", (e, f) -> {
final ButtonFormElement button = e.toButton();
button.setBackgroundColor(button.getBackgroundColor().darker());
},
new Color(71, 118, 65), Color.WHITE)
.show();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ public void test_form_dialog_with_rows_and_disable() {
.addButton(" ", "half full", () -> booster.showInfoDialog("Optimist")).setID("btn1")
.addButton(" ", "half empty", () -> booster.showInfoDialog("Pessimist")).setDisabled()
.endRow()
.addButton("Favorite font?", null).setDisabled()
.addButton("Favorite font?", (Runnable) null).setDisabled()
.startRow("Select one")
.addText("?").setID("?")
.addButton("Favorite font 1?", null).setDisabled()
.addButton("Favorite font 1?", (Runnable) null).setDisabled()
.endRow()
.addButton("Favorite font 2?", null)
.addButton("Favorite font 3?", null).setDisabled()
.addButton("Favorite font 2?", (Runnable) null)
.addButton("Favorite font 3?", (Runnable) null).setDisabled()
.setChangeListener((element, value, filledForm) -> System.out.println(
"Component " + element.getLabel() +
" at position " + element.getIndex() +
Expand Down

0 comments on commit fac45bf

Please sign in to comment.