summaryrefslogtreecommitdiff
path: root/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/AbstractDialog.java
blob: a4d8e1f5b29216082394a87f5a4b22fdf5816179 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package cuchaz.enigma.gui.dialog;

import java.awt.*;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import cuchaz.enigma.utils.I18n;
import cuchaz.enigma.utils.Pair;
import cuchaz.enigma.utils.validation.ValidationContext;

public abstract class AbstractDialog extends JDialog {

	protected final ValidationContext vc = new ValidationContext();

	private boolean actionConfirm = false;

	public AbstractDialog(Frame owner, String title, String confirmAction, String cancelAction) {
		super(owner, I18n.translate(title), true);

		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		Container inputContainer = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		List<Pair<String, Component>> components = createComponents();

		for (int i = 0; i < components.size(); i += 1) {
			Pair<String, Component> entry = components.get(i);
			JLabel label = new JLabel(I18n.translate(entry.a));
			Component component = entry.b;

			c.gridy = i;
			c.insets = new Insets(4, 4, 4, 4);

			c.gridx = 0;
			c.weightx = 0.0;
			c.anchor = GridBagConstraints.LINE_END;
			c.fill = GridBagConstraints.NONE;
			inputContainer.add(label, c);

			c.gridx = 1;
			c.weightx = 1.0;
			c.anchor = GridBagConstraints.LINE_START;
			c.fill = GridBagConstraints.HORIZONTAL;
			inputContainer.add(component, c);
		}
		contentPane.add(inputContainer, BorderLayout.CENTER);
		Container buttonContainer = new JPanel(new GridBagLayout());
		c = new GridBagConstraints();
		c.weightx = 1.0;
		c.insets = new Insets(4, 4, 4, 4);
		c.anchor = GridBagConstraints.LINE_END;
		JButton connectButton = new JButton(I18n.translate(confirmAction));
		connectButton.addActionListener(event -> confirm());
		buttonContainer.add(connectButton, c);
		c.weightx = 0.0;
		c.anchor = GridBagConstraints.CENTER;
		JButton abortButton = new JButton(I18n.translate(cancelAction));
		abortButton.addActionListener(event -> cancel());
		buttonContainer.add(abortButton, c);
		contentPane.add(buttonContainer, BorderLayout.SOUTH);

		pack();
		setLocationRelativeTo(owner);
	}

	protected abstract List<Pair<String, Component>> createComponents();

	protected void confirm() {
		vc.reset();
		validateInputs();
		if (vc.canProceed()) {
			actionConfirm = true;
			setVisible(false);
		}
	}

	protected void cancel() {
		actionConfirm = false;
		setVisible(false);
	}

	public boolean isActionConfirm() {
		return actionConfirm;
	}

	public void validateInputs() {
	}

}