-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegisterNewUserPanel.java
117 lines (103 loc) · 3.07 KB
/
RegisterNewUserPanel.java
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.barattoManager.ui.panels.optionPane;
import com.barattoManager.config.AppConfigurator;
import com.barattoManager.exception.AlreadyExistException;
import com.barattoManager.exception.EmptyStringException;
import com.barattoManager.user.UserManager;
import javax.swing.*;
import java.awt.*;
/**
* Class used to create a JPanel used to register a new user
*/
public class RegisterNewUserPanel {
private static final String NEW_USER_CORRECTLY_CREATED = "Nuovo %s creato correttamente.\n\nUsername: %s\nPassword: %s";
/**
* User registration panel title
*/
private static final String TITLE_REGISTRATION = "Registrazione";
/**
* Register a new viewer label
*/
private static final String LABEL_REGISTER_NEW_VIEWER = "Registrazione di un nuovo fruitore";
/**
* Register a new configurator label
*/
private static final String LABEL_REGISTER_NEW_CONFIGURATOR = "Registrazione di un nuovo configuratore";
/**
* User registered message title
*/
private static final String TITLE_REGISTERED = "Registrazione effetuata";
/**
* Choose a username label
*/
private static final String LABEL_CHOOSE_USERNAME= "Scegli un username:";
/**
* Error
*/
private static final String ERROR = "Errore";
/**
* Username field
*/
private final JTextField usernameField = new JTextField(13);
/**
* mainPanel
*/
private final JPanel mainPanel = new JPanel();
/**
* parentPanel
*/
private final JPanel parentPanel;
/**
* true if is a configurator
*/
private final boolean isConfigurator;
/**
* The {@link RegisterNewUserPanel} constructor
* @param parentPanel
* @param isConfigurator
*/
public RegisterNewUserPanel(JPanel parentPanel, boolean isConfigurator) {
this.parentPanel = parentPanel;
this.isConfigurator = isConfigurator;
var panel = new JPanel();
panel.setLayout(new GridLayout(0, 1));
panel.add(new JLabel(isConfigurator ? LABEL_REGISTER_NEW_CONFIGURATOR : LABEL_REGISTER_NEW_VIEWER));
var namePanel = new JPanel();
namePanel.add(new JLabel(LABEL_CHOOSE_USERNAME));
namePanel.add(usernameField);
panel.add(namePanel);
mainPanel.setVisible(true);
mainPanel.add(panel);
}
/**
* Method used to create a new user
*/
public void createNewUser(){
int result = JOptionPane.showOptionDialog(
parentPanel,
mainPanel,
TITLE_REGISTRATION,
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
null,
null
);
if (result == JOptionPane.OK_OPTION) {
try {
UserManager.getInstance().addNewUser(
usernameField.getText(),
AppConfigurator.getInstance().getPasswordSetting("default_pwd"),
false
);
JOptionPane.showMessageDialog(
parentPanel,
NEW_USER_CORRECTLY_CREATED.formatted(isConfigurator? "configuratore" : "fruitore" ,usernameField.getText(), AppConfigurator.getInstance().getPasswordSetting("default_pwd")),
TITLE_REGISTERED,
JOptionPane.INFORMATION_MESSAGE
);
} catch (AlreadyExistException | EmptyStringException ex) {
JOptionPane.showMessageDialog(parentPanel, ex.getMessage(), ERROR, JOptionPane.ERROR_MESSAGE);
}
}
}
}