Merge branch 'eugenp:master' into BAEL-6415
This commit is contained in:
commit
e0f77b0c25
|
@ -0,0 +1,15 @@
|
|||
package reminderapplication;
|
||||
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.Insets;
|
||||
|
||||
public class ConstraintsBuilder {
|
||||
|
||||
static GridBagConstraints constraint(int x, int y) {
|
||||
final GridBagConstraints gridBagConstraints = new GridBagConstraints();
|
||||
gridBagConstraints.gridx = x;
|
||||
gridBagConstraints.gridy = y;
|
||||
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
|
||||
return gridBagConstraints;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,194 @@
|
|||
package reminderapplication;
|
||||
|
||||
import static reminderapplication.ConstraintsBuilder.*;
|
||||
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.HeadlessException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.swing.DefaultComboBoxModel;
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
public class EditReminderFrame extends JFrame {
|
||||
|
||||
private static Timer TIMER = new Timer();
|
||||
|
||||
private final TimeReminderApplication reminderApplication;
|
||||
private final JLabel reminderTextLabel;
|
||||
private final JLabel repeatPeriodLabel;
|
||||
private final JLabel setDelayLabel;
|
||||
private final JComboBox<Integer> delay;
|
||||
private final JComboBox<Integer> period;
|
||||
private final JButton cancelButton;
|
||||
private final JButton okButton;
|
||||
private final JTextField textField;
|
||||
private final JLabel delaysLabel;
|
||||
private final JLabel periodLabel;
|
||||
|
||||
private final int reminderIndex;
|
||||
|
||||
public EditReminderFrame(TimeReminderApplication reminderApp, String reminderText, int delayInSeconds, int periodInSeconds, int index) throws HeadlessException {
|
||||
this.reminderApplication = reminderApp;
|
||||
reminderIndex = index;
|
||||
textField = createTextField(reminderText);
|
||||
delay = createDelayComboBox(delayInSeconds);
|
||||
period = createPeriodComboBox(periodInSeconds);
|
||||
cancelButton = createCancelButton();
|
||||
okButton = createOkButton();
|
||||
reminderTextLabel = createReminderTextLabel();
|
||||
repeatPeriodLabel = createRepeatPeriodLabel();
|
||||
setDelayLabel = createSetDelayLabel();
|
||||
delaysLabel = createDelaysLabel();
|
||||
periodLabel = createPeriodLabel();
|
||||
configureVisualRepresentation();
|
||||
configureActions();
|
||||
}
|
||||
|
||||
private void configureActions() {
|
||||
updateReminder();
|
||||
}
|
||||
|
||||
private void configureVisualRepresentation() {
|
||||
configureFrame();
|
||||
setLocationRelativeTo(null);
|
||||
setLayout(new GridBagLayout());
|
||||
add(reminderTextLabel, constraint(0,0));
|
||||
add(repeatPeriodLabel, constraint(1,0));
|
||||
add(setDelayLabel, constraint(2,0));
|
||||
add(textField, constraint(0, 1));
|
||||
add(delay, constraint(1, 1));
|
||||
add(period, constraint(2, 1));
|
||||
add(delaysLabel, constraint(1,3));
|
||||
add(periodLabel, constraint(2,3));
|
||||
add(okButton, constraint(1, 4));
|
||||
add(cancelButton, constraint(2, 4));
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
private void configureFrame() {
|
||||
setTitle("Set Reminder");
|
||||
setName("Set Reminder");
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
}
|
||||
|
||||
private static JLabel createSetDelayLabel() {
|
||||
return createLabel("Set Delay", "Set Delay Label");
|
||||
}
|
||||
|
||||
private static JLabel createRepeatPeriodLabel() {
|
||||
return createLabel("Set Period", "Set Repeat Period Label");
|
||||
}
|
||||
|
||||
private static JLabel createReminderTextLabel() {
|
||||
return createLabel("Reminder Text", "Reminder Text Label");
|
||||
}
|
||||
|
||||
private JLabel createPeriodLabel() {
|
||||
return createLabel("0", "Period label");
|
||||
}
|
||||
|
||||
private JLabel createDelaysLabel() {
|
||||
return createLabel("30", "Delays Label");
|
||||
}
|
||||
|
||||
private JComboBox<Integer> createPeriodComboBox(final int periodInSeconds) {
|
||||
final JComboBox<Integer> comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{0, 5, 10, 20}));
|
||||
comboBox.setSelectedItem(periodInSeconds);
|
||||
comboBox.setName("set Period");
|
||||
comboBox.addActionListener(e -> periodLabel.setText(comboBox.getSelectedItem().toString()));
|
||||
return comboBox;
|
||||
}
|
||||
|
||||
private JComboBox<Integer> createDelayComboBox(final int delayInSeconds) {
|
||||
final JComboBox<Integer> comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{30, 25, 15, 5}));
|
||||
comboBox.setSelectedItem(delayInSeconds);
|
||||
comboBox.setName("set Delay");
|
||||
comboBox.addActionListener(e -> delaysLabel.setText(comboBox.getSelectedItem().toString()));
|
||||
return comboBox;
|
||||
}
|
||||
|
||||
private JTextField createTextField(final String reminderText) {
|
||||
final JTextField textField = new JTextField(20);
|
||||
textField.setName("Field");
|
||||
textField.setText(reminderText);
|
||||
return textField;
|
||||
}
|
||||
|
||||
private JButton createOkButton() {
|
||||
final JButton button = new JButton("ok");
|
||||
button.setName("OK");
|
||||
return button;
|
||||
}
|
||||
|
||||
private void updateReminder() {
|
||||
okButton.addActionListener(e -> this.dispose());
|
||||
okButton.addActionListener(e -> {
|
||||
final int periodInSeconds = getTimeInSeconds(period);
|
||||
final int delayInSeconds = getTimeInSeconds(delay);
|
||||
final Reminder reminder = new Reminder(textField.getText(), delayInSeconds, periodInSeconds);
|
||||
((DefaultListModel<Reminder>) reminderApplication.getReminders()).set(reminderIndex, reminder);
|
||||
});
|
||||
okButton.addActionListener(e -> scheduleReminder(textField, delay, period));
|
||||
}
|
||||
|
||||
private void scheduleReminder(final JTextField textField, final JComboBox<Integer> delay, final JComboBox<Integer> period) {
|
||||
final int periodInSeconds = getTimeInSeconds(period);
|
||||
if (periodInSeconds == 0)
|
||||
scheduleNonRepeatedReminder(textField, delay);
|
||||
else
|
||||
scheduleRepeatedReminder(textField, delay, period);
|
||||
}
|
||||
|
||||
private void scheduleRepeatedReminder(final JTextField textField, final JComboBox<Integer> delay, final JComboBox<Integer> period) {
|
||||
final int delayInSeconds = getTimeInSeconds(delay);
|
||||
final int periodInSeconds = getTimeInSeconds(period);
|
||||
final TimerTask timerTask = getTimerTask(textField.getText(), delayInSeconds, periodInSeconds);
|
||||
TIMER.schedule(timerTask, TimeUnit.SECONDS.toMillis(delayInSeconds), TimeUnit.SECONDS.toMillis(periodInSeconds));
|
||||
}
|
||||
|
||||
private void scheduleNonRepeatedReminder(final JTextField textField, final JComboBox<Integer> delay) {
|
||||
final int delayInSeconds = getTimeInSeconds(delay);
|
||||
final int periodInSeconds = 0;
|
||||
final TimerTask timerTask = getTimerTask(textField.getText(), delayInSeconds, periodInSeconds);
|
||||
TIMER.schedule(timerTask, TimeUnit.SECONDS.toMillis(delayInSeconds));
|
||||
|
||||
}
|
||||
|
||||
private int getTimeInSeconds(final JComboBox<Integer> comboBox) {
|
||||
if (comboBox != null && comboBox.getSelectedItem() != null)
|
||||
return ((Integer) comboBox.getSelectedItem());
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
private TimerTask getTimerTask(final String reminderText, final Integer delayInSeconds, final Integer periodInSeconds) {
|
||||
return new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
new ReminderPopupFrame(reminderApplication, reminderText, delayInSeconds, periodInSeconds);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private JButton createCancelButton() {
|
||||
final JButton button = new JButton("cancel");
|
||||
button.setName("Cancel");
|
||||
button.addActionListener(e -> this.dispose());
|
||||
return button;
|
||||
}
|
||||
|
||||
private static JLabel createLabel(final String text, final String name) {
|
||||
JLabel label = new JLabel(text);
|
||||
label.setName(name);
|
||||
return label;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package reminderapplication;
|
||||
|
||||
public class Reminder {
|
||||
|
||||
private static String REMINDER_FORMAT = "Reminder Text: %s; Delay: %d; Period: %d;";
|
||||
|
||||
private final String name;
|
||||
private final int delay;
|
||||
private final int period;
|
||||
|
||||
public Reminder(final String name, final int delay, final int period) {
|
||||
this.name = name;
|
||||
this.delay = delay;
|
||||
this.period = period;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getDelay() {
|
||||
return delay;
|
||||
}
|
||||
|
||||
public int getPeriod() {
|
||||
return period;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return REMINDER_FORMAT.formatted(name, delay, period);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,186 @@
|
|||
package reminderapplication;
|
||||
|
||||
import static reminderapplication.ConstraintsBuilder.*;
|
||||
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.HeadlessException;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.swing.DefaultComboBoxModel;
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
public class ReminderFrame extends JFrame {
|
||||
|
||||
private static Timer TIMER = new Timer();
|
||||
private final TimeReminderApplication reminderApplication;
|
||||
private final JLabel reminderTextLabel;
|
||||
private final JLabel repeatPeriodLabel;
|
||||
private final JLabel setDelayLabel;
|
||||
private final JComboBox<Integer> delay;
|
||||
private final JComboBox<Integer> period;
|
||||
private final JButton cancelButton;
|
||||
private final JButton okButton;
|
||||
private final JTextField textField;
|
||||
private final JLabel delaysLabel;
|
||||
private final JLabel periodLabel;
|
||||
|
||||
public ReminderFrame(TimeReminderApplication reminderApp) throws HeadlessException {
|
||||
this.reminderApplication = reminderApp;
|
||||
textField = createTextField();
|
||||
delay = createDelayComboBox();
|
||||
period = createPeriodComboBox();
|
||||
cancelButton = createCancelButton();
|
||||
okButton = createOkButton();
|
||||
reminderTextLabel = createReminderTextLabel();
|
||||
repeatPeriodLabel = createRepeatPeriodLabel();
|
||||
setDelayLabel = createSetDelayLabel();
|
||||
delaysLabel = createDelaysLabel();
|
||||
periodLabel = createPeriodLabel();
|
||||
configureVisualRepresentation();
|
||||
configureActions();
|
||||
}
|
||||
|
||||
private void configureActions() {
|
||||
createNewReminder();
|
||||
}
|
||||
|
||||
private void configureVisualRepresentation() {
|
||||
configureFrame();
|
||||
setLocationRelativeTo(null);
|
||||
setLayout(new GridBagLayout());
|
||||
add(reminderTextLabel, constraint(0,0));
|
||||
add(repeatPeriodLabel, constraint(1,0));
|
||||
add(setDelayLabel, constraint(2,0));
|
||||
add(textField, constraint(0, 1));
|
||||
add(delay, constraint(1, 1));
|
||||
add(period, constraint(2, 1));
|
||||
add(delaysLabel, constraint(1,3));
|
||||
add(periodLabel, constraint(2,3));
|
||||
add(okButton, constraint(1, 4));
|
||||
add(cancelButton, constraint(2, 4));
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
private void configureFrame() {
|
||||
setTitle("Set Reminder");
|
||||
setName("Set Reminder");
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
}
|
||||
|
||||
private static JLabel createSetDelayLabel() {
|
||||
return createLabel("Set Delay", "Set Delay Label");
|
||||
}
|
||||
|
||||
private static JLabel createRepeatPeriodLabel() {
|
||||
return createLabel("Set Period", "Set Repeat Period Label");
|
||||
}
|
||||
|
||||
private static JLabel createReminderTextLabel() {
|
||||
return createLabel("Reminder Text", "Reminder Text Label");
|
||||
}
|
||||
|
||||
private JLabel createPeriodLabel() {
|
||||
return createLabel("0", "Period label");
|
||||
}
|
||||
|
||||
private JLabel createDelaysLabel() {
|
||||
return createLabel("30", "Delays Label");
|
||||
}
|
||||
|
||||
private JComboBox<Integer> createPeriodComboBox() {
|
||||
final JComboBox<Integer> comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{0, 5, 10, 20}));
|
||||
comboBox.setName("set Period");
|
||||
comboBox.addActionListener(e -> periodLabel.setText(comboBox.getSelectedItem().toString()));
|
||||
return comboBox;
|
||||
}
|
||||
|
||||
private JComboBox<Integer> createDelayComboBox() {
|
||||
final JComboBox<Integer> comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{30, 25, 15, 5}));
|
||||
comboBox.setName("set Delay");
|
||||
comboBox.addActionListener(e -> delaysLabel.setText(comboBox.getSelectedItem().toString()));
|
||||
return comboBox;
|
||||
}
|
||||
|
||||
private JTextField createTextField() {
|
||||
final JTextField textField = new JTextField(20);
|
||||
textField.setName("Field");
|
||||
return textField;
|
||||
}
|
||||
|
||||
private JButton createOkButton() {
|
||||
final JButton button = new JButton("ok");
|
||||
button.setName("OK");
|
||||
return button;
|
||||
}
|
||||
|
||||
private void createNewReminder() {
|
||||
|
||||
okButton.addActionListener(e -> this.dispose());
|
||||
okButton.addActionListener(e -> {
|
||||
final int periodInSeconds = getTimeInSeconds(period);
|
||||
final int delayInSeconds = getTimeInSeconds(delay);
|
||||
final Reminder reminder = new Reminder(textField.getText(), delayInSeconds, periodInSeconds);
|
||||
((DefaultListModel<Reminder>) reminderApplication.getReminders()).addElement(reminder);
|
||||
});
|
||||
okButton.addActionListener(e -> scheduleReminder(textField, delay, period));
|
||||
}
|
||||
|
||||
private void scheduleReminder(final JTextField textField, final JComboBox<Integer> delay, final JComboBox<Integer> period) {
|
||||
final int periodInSeconds = getTimeInSeconds(period);
|
||||
if (periodInSeconds == 0)
|
||||
scheduleNonRepeatedReminder(textField, delay);
|
||||
else
|
||||
scheduleRepeatedReminder(textField, delay, period);
|
||||
}
|
||||
|
||||
private void scheduleRepeatedReminder(final JTextField textField, final JComboBox<Integer> delay, final JComboBox<Integer> period) {
|
||||
final int delayInSeconds = getTimeInSeconds(delay) + 200;
|
||||
final int periodInSeconds = getTimeInSeconds(period);
|
||||
final TimerTask timerTask = getTimerTask(textField.getText(), delayInSeconds, periodInSeconds);
|
||||
TIMER.schedule(timerTask, TimeUnit.SECONDS.toMillis(delayInSeconds), TimeUnit.SECONDS.toMillis(periodInSeconds));
|
||||
}
|
||||
|
||||
private void scheduleNonRepeatedReminder(final JTextField textField, final JComboBox<Integer> delay) {
|
||||
final int delayInSeconds = getTimeInSeconds(delay);
|
||||
final int periodInSeconds = 0;
|
||||
final TimerTask timerTask = getTimerTask(textField.getText(), delayInSeconds, periodInSeconds);
|
||||
TIMER.schedule(timerTask, TimeUnit.SECONDS.toMillis(delayInSeconds));
|
||||
|
||||
}
|
||||
|
||||
private int getTimeInSeconds(final JComboBox<Integer> comboBox) {
|
||||
if (comboBox != null && comboBox.getSelectedItem() != null)
|
||||
return ((Integer) comboBox.getSelectedItem());
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
private TimerTask getTimerTask(final String reminderText, final Integer delayInSeconds, final Integer periodInSeconds) {
|
||||
return new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
new ReminderPopupFrame(reminderApplication, reminderText, delayInSeconds, periodInSeconds);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private JButton createCancelButton() {
|
||||
final JButton button = new JButton("cancel");
|
||||
button.setName("Cancel");
|
||||
button.addActionListener(e -> this.dispose());
|
||||
return button;
|
||||
}
|
||||
|
||||
private static JLabel createLabel(final String text, final String name) {
|
||||
JLabel label = new JLabel(text);
|
||||
label.setName(name);
|
||||
return label;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,151 @@
|
|||
package reminderapplication;
|
||||
|
||||
import static reminderapplication.ConstraintsBuilder.*;
|
||||
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.HeadlessException;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.swing.DefaultComboBoxModel;
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
public class ReminderPopupFrame extends JFrame {
|
||||
|
||||
private static final Timer TIMER = new Timer();
|
||||
private final int AUTOMATIC_CLOSE_TIME_IN_SECONDS = 10;
|
||||
private final TimeReminderApplication reminderApplication;
|
||||
private final JLabel reminderTextLabel;
|
||||
private final JLabel repeatPeriodLabel;
|
||||
private final JLabel setDelayLabel;
|
||||
private final JComboBox<Integer> delay;
|
||||
private final JComboBox<Integer> period;
|
||||
private final JButton cancelButton;
|
||||
private final JButton okButton;
|
||||
private final JTextField textField;
|
||||
private final JLabel delaysLabel;
|
||||
private final JLabel periodLabel;
|
||||
|
||||
public ReminderPopupFrame(TimeReminderApplication reminderApp, final String text, final Integer delayInSeconds, final Integer periodInSeconds) throws HeadlessException {
|
||||
this.reminderApplication = reminderApp;
|
||||
textField = createTextField(text);
|
||||
delay = createDelayComboBox(delayInSeconds);
|
||||
period = createPeriodComboBox(periodInSeconds);
|
||||
cancelButton = createCancelButton();
|
||||
okButton = createDisabledOkButton();
|
||||
reminderTextLabel = createReminderTextLabel();
|
||||
repeatPeriodLabel = createRepeatPeriodLabel();
|
||||
setDelayLabel = createSetDelayLabel();
|
||||
delaysLabel = createDelaysLabel();
|
||||
periodLabel = createPeriodLabel();
|
||||
configureVisualRepresentation();
|
||||
configureActions();
|
||||
}
|
||||
|
||||
private void configureActions() {
|
||||
scheduleClosing();
|
||||
}
|
||||
|
||||
private void scheduleClosing() {
|
||||
final TimerTask timerTask = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
ReminderPopupFrame.this.dispose();
|
||||
}
|
||||
};
|
||||
TIMER.schedule(timerTask, TimeUnit.SECONDS.toMillis(AUTOMATIC_CLOSE_TIME_IN_SECONDS));
|
||||
}
|
||||
|
||||
private void configureVisualRepresentation() {
|
||||
configureFrame();
|
||||
setLocationRelativeTo(null);
|
||||
setLayout(new GridBagLayout());
|
||||
add(reminderTextLabel, constraint(0,0));
|
||||
add(repeatPeriodLabel, constraint(1,0));
|
||||
add(setDelayLabel, constraint(2,0));
|
||||
add(textField, constraint(0, 1));
|
||||
add(delay, constraint(1, 1));
|
||||
add(period, constraint(2, 1));
|
||||
add(delaysLabel, constraint(1,3));
|
||||
add(periodLabel, constraint(2,3));
|
||||
add(okButton, constraint(1, 4));
|
||||
add(cancelButton, constraint(2, 4));
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
private void configureFrame() {
|
||||
setTitle("Set Reminder");
|
||||
setName("Set Reminder");
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
}
|
||||
|
||||
private static JLabel createSetDelayLabel() {
|
||||
return createLabel("Set Delay", "Set Delay Label");
|
||||
}
|
||||
|
||||
private static JLabel createRepeatPeriodLabel() {
|
||||
return createLabel("Set Period", "Set Repeat Period Label");
|
||||
}
|
||||
|
||||
private static JLabel createReminderTextLabel() {
|
||||
return createLabel("Reminder Text", "Reminder Text Label");
|
||||
}
|
||||
|
||||
private JLabel createPeriodLabel() {
|
||||
return createLabel("0", "Period label");
|
||||
}
|
||||
|
||||
private JLabel createDelaysLabel() {
|
||||
return createLabel("30", "Delays Label");
|
||||
}
|
||||
|
||||
private JComboBox<Integer> createPeriodComboBox(final Integer periodInSeconds) {
|
||||
final JComboBox<Integer> comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{0, 5, 10, 20}));
|
||||
comboBox.setName("set Period");
|
||||
comboBox.setSelectedItem(periodInSeconds);
|
||||
comboBox.addActionListener(e -> periodLabel.setText(comboBox.getSelectedItem().toString()));
|
||||
return comboBox;
|
||||
}
|
||||
|
||||
private JComboBox<Integer> createDelayComboBox(Integer delay) {
|
||||
final JComboBox<Integer> comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{30, 25, 15, 5}));
|
||||
comboBox.setSelectedItem(delay);
|
||||
comboBox.setName("set Delay");
|
||||
comboBox.addActionListener(e -> delaysLabel.setText(comboBox.getSelectedItem().toString()));
|
||||
return comboBox;
|
||||
}
|
||||
|
||||
private JTextField createTextField(final String text) {
|
||||
final JTextField textField = new JTextField(20);
|
||||
textField.setName("Field");
|
||||
textField.setText(text);
|
||||
return textField;
|
||||
}
|
||||
|
||||
private JButton createDisabledOkButton() {
|
||||
final JButton button = new JButton("ok");
|
||||
button.setName("OK");
|
||||
button.setEnabled(false);
|
||||
return button;
|
||||
}
|
||||
|
||||
private JButton createCancelButton() {
|
||||
final JButton button = new JButton("cancel");
|
||||
button.setName("Cancel");
|
||||
button.addActionListener(e -> this.dispose());
|
||||
return button;
|
||||
}
|
||||
|
||||
private static JLabel createLabel(final String text, final String name) {
|
||||
JLabel label = new JLabel(text);
|
||||
label.setName(name);
|
||||
return label;
|
||||
}
|
||||
|
||||
}
|
|
@ -28,6 +28,9 @@ import static org.junit.Assert.assertFalse;
|
|||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
|
||||
@Disabled //fixing in https://team.baeldung.com/browse/JAVA-23897
|
||||
public class CertificatesUnitTest {
|
||||
|
||||
private static final String GODADDY_CA_ALIAS = "godaddyrootg2ca [jdk]";
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
# Introduction to Avaje Inject
|
||||
|
||||
This module contains articles about Avaje
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [Introduction to Avaje Inject](https://www.baeldung.com/avaje-inject/intro)
|
|
@ -0,0 +1,37 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>inject-intro</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>avaje-inject-intro</name>
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<avaje.inject.version>9.5</avaje.inject.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.avaje</groupId>
|
||||
<artifactId>avaje-inject</artifactId>
|
||||
<version>${avaje.inject.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.avaje</groupId>
|
||||
<artifactId>avaje-inject-test</artifactId>
|
||||
<version>${avaje.inject.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Annotation processors -->
|
||||
<dependency>
|
||||
<groupId>io.avaje</groupId>
|
||||
<artifactId>avaje-inject-generator</artifactId>
|
||||
<version>${avaje.inject.version}</version>
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.avaje.intro;
|
||||
|
||||
import io.avaje.inject.Bean;
|
||||
import io.avaje.inject.Factory;
|
||||
|
||||
@Factory
|
||||
public class ArmsFactory {
|
||||
|
||||
@Bean
|
||||
public Sword provideSword() {
|
||||
return new Sword();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Shield provideShield() {
|
||||
return new Shield(25);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.avaje.intro;
|
||||
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class Knight {
|
||||
|
||||
private Sword sword;
|
||||
|
||||
private Shield shield;
|
||||
|
||||
@Inject
|
||||
public Knight(Sword sword, Shield shield) {
|
||||
this.sword = sword;
|
||||
this.shield = shield;
|
||||
}
|
||||
|
||||
public Sword sword() {
|
||||
return sword;
|
||||
}
|
||||
|
||||
public void sword(Sword engine) {
|
||||
this.sword = engine;
|
||||
}
|
||||
|
||||
public Shield shield() {
|
||||
return shield;
|
||||
}
|
||||
|
||||
public void shield(Shield brand) {
|
||||
this.shield = brand;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.avaje.intro;
|
||||
|
||||
import io.avaje.inject.BeanScope;
|
||||
import io.avaje.inject.PostConstruct;
|
||||
import io.avaje.inject.PreDestroy;
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class Ninja {
|
||||
|
||||
private Sword sword;
|
||||
|
||||
@PostConstruct
|
||||
void equip(BeanScope scope) {
|
||||
sword = scope.get(Sword.class);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
void dequip() {
|
||||
sword = null;
|
||||
}
|
||||
|
||||
public Sword sword() {
|
||||
return sword;
|
||||
}
|
||||
|
||||
public void sword(Sword engine) {
|
||||
this.sword = engine;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.avaje.intro;
|
||||
|
||||
public class Shield {
|
||||
|
||||
private int defense;
|
||||
|
||||
public Shield(int defense) {
|
||||
this.defense = defense;
|
||||
}
|
||||
|
||||
public int defense() {
|
||||
return defense;
|
||||
}
|
||||
|
||||
public void defense(int defense) {
|
||||
this.defense = defense;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.avaje.intro;
|
||||
|
||||
public class Sword {
|
||||
|
||||
public void attack() {
|
||||
System.out.println("swing");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.avaje.intro;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import io.avaje.inject.BeanScope;
|
||||
|
||||
class AvajeUnitTest {
|
||||
|
||||
@Test
|
||||
void givenBeanScope_whenScopeGetsKnight_thenKnightShouldHaveDependencies() {
|
||||
|
||||
final var scope = BeanScope.builder().build();
|
||||
final var knight = scope.get(Knight.class);
|
||||
|
||||
assertNotNull(knight);
|
||||
assertNotNull(knight.sword());
|
||||
assertNotNull(knight.shield());
|
||||
assertEquals(25, knight.shield().defense());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.avaje.intro;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import io.avaje.inject.test.InjectTest;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
@InjectTest
|
||||
class ExampleInjectTest {
|
||||
|
||||
@Mock Shield shield;
|
||||
|
||||
@Inject Knight knight;
|
||||
|
||||
@Test
|
||||
void givenMockedShield_whenGetShield_thenShieldShouldHaveMockedValue() {
|
||||
|
||||
Mockito.when(shield.defense()).thenReturn(0);
|
||||
assertNotNull(knight);
|
||||
assertNotNull(knight.sword());
|
||||
assertNotNull(knight.shield());
|
||||
assertEquals(0, knight.shield().defense());
|
||||
}
|
||||
}
|
|
@ -230,7 +230,7 @@
|
|||
<maven-resources-plugin.version>3.3.0</maven-resources-plugin.version>
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
|
||||
<spring-boot.version>3.0.0</spring-boot.version>
|
||||
<spring-boot.version>3.1.2</spring-boot.version>
|
||||
<junit-jupiter.version>5.8.2</junit-jupiter.version>
|
||||
<native-build-tools-plugin.version>0.9.17</native-build-tools-plugin.version>
|
||||
<java.version>17</java.version>
|
||||
|
|
|
@ -27,14 +27,15 @@ public class QueryService {
|
|||
return entityManager.createQuery(query).getResultList();
|
||||
}
|
||||
|
||||
public BookRecord findBookById(Long id) {
|
||||
public BookRecord findBookByTitle(String title) {
|
||||
TypedQuery<BookRecord> query = entityManager
|
||||
.createQuery("SELECT new com.baeldung.recordswithjpa.records.BookRecord(b.id, b.title, b.author, b.isbn) " +
|
||||
"FROM Book b WHERE b.id = :id", BookRecord.class);
|
||||
query.setParameter("id", id);
|
||||
"FROM Book b WHERE b.title = :title", BookRecord.class);
|
||||
query.setParameter("title", title);
|
||||
return query.getSingleResult();
|
||||
}
|
||||
|
||||
|
||||
public List<BookRecord> findAllBooksUsingMapping() {
|
||||
Query query = entityManager.createNativeQuery("SELECT * FROM book", "BookRecordMapping");
|
||||
return query.getResultList();
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.recordswithjpa.embeddable;
|
||||
|
||||
import jakarta.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public record Author(
|
||||
String firstName,
|
||||
String lastName
|
||||
) {}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.recordswithjpa.embeddable;
|
||||
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.metamodel.spi.EmbeddableInstantiator;
|
||||
import org.hibernate.metamodel.spi.ValueAccess;
|
||||
|
||||
public class AuthorInstallator implements EmbeddableInstantiator {
|
||||
|
||||
public boolean isInstance(Object object, SessionFactoryImplementor sessionFactory) {
|
||||
return object instanceof Author;
|
||||
}
|
||||
|
||||
public boolean isSameClass(Object object, SessionFactoryImplementor sessionFactory) {
|
||||
return object.getClass().equals(Author.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object instantiate(final ValueAccess valueAccess, final SessionFactoryImplementor sessionFactoryImplementor) {
|
||||
final String firstName = valueAccess.getValue(0, String.class);
|
||||
final String secondName = valueAccess.getValue(1, String.class);
|
||||
return new Author(firstName, secondName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package com.baeldung.recordswithjpa.embeddable;
|
||||
|
||||
import jakarta.persistence.Embedded;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import org.hibernate.annotations.EmbeddableInstantiator;
|
||||
|
||||
@Entity
|
||||
@Table(name = "embeadable_author_book")
|
||||
public class EmbeddableBook {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String title;
|
||||
@Embedded
|
||||
@EmbeddableInstantiator(AuthorInstallator.class)
|
||||
private Author author;
|
||||
private String isbn;
|
||||
|
||||
public EmbeddableBook() {
|
||||
}
|
||||
|
||||
public EmbeddableBook(Long id, String title, Author author, String isbn) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.isbn = isbn;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Author getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(Author author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
public void setIsbn(String isbn) {
|
||||
this.isbn = isbn;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.recordswithjpa.repository;
|
||||
|
||||
import com.baeldung.recordswithjpa.embeddable.EmbeddableBook;
|
||||
import com.baeldung.recordswithjpa.embeddable.Author;
|
||||
import java.util.List;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
public interface EmbeddableBookRepository extends CrudRepository<EmbeddableBook, Long> {
|
||||
@Query("SELECT b FROM EmbeddableBook b WHERE b.author = :author")
|
||||
List<EmbeddableBook> findBookByAuthor(@Param("author") Author author);
|
||||
|
||||
}
|
|
@ -1,13 +1,8 @@
|
|||
package com.baeldung.recordswithjpa;
|
||||
|
||||
import com.baeldung.recordswithjpa.entity.Book;
|
||||
import com.baeldung.recordswithjpa.records.BookRecord;
|
||||
import com.baeldung.recordswithjpa.repository.BookRepository;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -27,8 +22,8 @@ public class QueryServiceIntegrationTest extends RecordsAsJpaIntegrationTest {
|
|||
|
||||
@Test
|
||||
void findBookById() {
|
||||
BookRecord bookById = queryService.findBookById(1L);
|
||||
assertEquals("The Lord of the Rings", bookById.title());
|
||||
BookRecord bookByTitle = queryService.findBookByTitle("The Lord of the Rings");
|
||||
assertNotNull(bookByTitle);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.recordswithjpa;
|
||||
|
||||
import com.baeldung.recordswithjpa.embeddable.Author;
|
||||
import com.baeldung.recordswithjpa.embeddable.EmbeddableBook;
|
||||
import com.baeldung.recordswithjpa.repository.EmbeddableBookRepository;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
public class RecordsAsJpaEmbeddableIntegrationTest {
|
||||
@Autowired
|
||||
protected EmbeddableBookRepository bookRepository;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
|
||||
Author author = new Author("J.R.R.", "Tolkien");
|
||||
EmbeddableBook book1 = new EmbeddableBook(null, "The Lord of the Rings", author, "978-0544003415");
|
||||
EmbeddableBook book2 = new EmbeddableBook(null, "The Hobbit", author, "978-0547928227");
|
||||
|
||||
bookRepository.save(book1);
|
||||
bookRepository.save(book2);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
bookRepository.deleteAll();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.recordswithjpa.repository;
|
||||
|
||||
import com.baeldung.recordswithjpa.RecordsAsJpaEmbeddableIntegrationTest;
|
||||
import com.baeldung.recordswithjpa.embeddable.Author;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EmbeddableBookRepositoryIntegrationTest extends RecordsAsJpaEmbeddableIntegrationTest {
|
||||
|
||||
@Test
|
||||
void findBookByAuthor() {
|
||||
assertEquals(2, bookRepository.findBookByAuthor(new Author("J.R.R.", "Tolkien")).size());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue