I am currently learning Java programing by building real life business application for repair shops. I am posting this code so I can get critical review, and change my application architecture if necessary. I would like your opinion on my approach to MVC pattern.
This is a GUI swing class for one of the dialogs.
public class ClientRegistrationWindow
{ public JDialog window;
public JPanel contentPane = new JPanel();
public JLabel lablelIDValue = new JLabel();
public JLabel labelFirstName = new JLabel();
public JTextField textFieldFirstName = new JTextField();
public JLabel labelLastName = new JLabel();
public JTextField textFieldLastName = new JTextField();
public JLabel labelPrimePhoneNum = new JLabel();
public JTextField textFieldPrimePhoneNum = new JTextField();
public JTextField textFieldAlternativePhoneNum = new JTextField();
public JTextField textFieldEmail = new JTextField();
public JTextField textFieldAddress = new JTextField();
public JLabel labelMarketing = new JLabel();
public JComboBox<Property> comboBoxMarketing = new JComboBox<Property>();
public JButton buttonAdd = new JButton();
public JButton buttonCancel = new JButton();
/**
* Creates JDialog "Customer Registration Form".
*/
public ClientRegistrationWindow(Window owner)
{
createWindow(owner);
createIDView();
createFirstNameView();
createLastNameView();
createPrimePhoneNumView();
createAlternativePhoneNumView();
createEmailView();
createAddressView();
createMarketingView();
createButtonsView();
}
private void createWindow(Window owner)
{
window = new JDialog(owner);
window.setTitle(ClientGUITextUtils.TITLE);
window.setResizable(false);
window.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
window.setBounds(100, 100, 460, 440);
window.setContentPane(contentPane);
window.setVisible(true);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
}
private void createIDView()
{
JLabel labelID = new JLabel();
labelID.setText(ClientGUITextUtils.CLIENT_ID_LABEL);
labelID.setBounds(35, 25, 110, 25);
contentPane.add(labelID);
lablelIDValue.setText("X-XXXXX");
lablelIDValue.setHorizontalAlignment(SwingConstants.CENTER);
lablelIDValue.setFont(new Font("Tahoma", Font.BOLD, 15));
lablelIDValue.setBounds(150, 25, 160, 25);
contentPane.add(lablelIDValue);
}
private void createFirstNameView()
{
labelFirstName.setText(ClientGUITextUtils.FIRST_NAME_LABEL);
labelFirstName.setBounds(35, 90, 185, 15);
contentPane.add(labelFirstName);
textFieldFirstName.setColumns(10);
textFieldFirstName.setBounds(35, 105, 185, 25);
contentPane.add(textFieldFirstName);
}
private void createLastNameView()
{
labelLastName.setText(ClientGUITextUtils.LAST_NAME_LABEL);
labelLastName.setBounds(235, 90, 185, 15);
contentPane.add(labelLastName);
textFieldLastName.setColumns(10);
textFieldLastName.setBounds(235, 105, 185, 25);
contentPane.add(textFieldLastName);
}
private void createPrimePhoneNumView()
{
labelPrimePhoneNum.setText(ClientGUITextUtils.PRIME_PHONE_NUMBER_LABEL);
labelPrimePhoneNum.setBounds(35, 140, 185, 15);
contentPane.add(labelPrimePhoneNum);
textFieldPrimePhoneNum.setColumns(10);
textFieldPrimePhoneNum.setBounds(35, 155, 185, 25);
contentPane.add(textFieldPrimePhoneNum);
}
private void createAlternativePhoneNumView()
{
JLabel labelAlternativePhoneNum =new JLabel(ClientGUITextUtils.SECOND_NUMBER_LABEL);
labelAlternativePhoneNum.setBounds(235, 140, 185, 15);
contentPane.add(labelAlternativePhoneNum);
textFieldAlternativePhoneNum.setColumns(10);
textFieldAlternativePhoneNum.setBounds(235, 155, 185, 25);
contentPane.add(textFieldAlternativePhoneNum);
}
private void createEmailView()
{
JLabel labelEmail = new JLabel(ClientGUITextUtils.EMAIL_LABEL);
labelEmail.setBounds(35, 190, 385, 15);
contentPane.add(labelEmail);
textFieldEmail.setColumns(10);
textFieldEmail.setBounds(35, 205, 385, 25);
contentPane.add(textFieldEmail);
}
private void createAddressView()
{
JLabel labelAddress = new JLabel(ClientGUITextUtils.ADDRESS_LABEL);
labelAddress.setBounds(35, 240, 385, 15);
contentPane.add(labelAddress);
textFieldAddress.setColumns(10);
textFieldAddress.setBounds(35, 255, 385, 25);
contentPane.add(textFieldAddress);
}
private void createMarketingView()
{
labelMarketing.setText(ClientGUITextUtils.MARKETING_LABEL);
labelMarketing.setBounds(235, 290, 185, 15);
contentPane.add(labelMarketing);
comboBoxMarketing.setSize(185, 25);
comboBoxMarketing.setLocation(235, 305);
contentPane.add(comboBoxMarketing);
}
private void createButtonsView()
{
buttonAdd.setText(ClientGUITextUtils.ADD_CLIENT_BUTTON);
buttonAdd.setFont(new Font("Dialog", Font.PLAIN, 15));
buttonAdd.setBounds(35, 365, 110, 25);
contentPane.add(buttonAdd);
buttonCancel.setText(ClientGUITextUtils.CANCEL_BUTTON);
buttonCancel.setFont(new Font("Dialog", Font.PLAIN, 15));
buttonCancel.setBounds(310, 365, 110, 25);
contentPane.add(buttonCancel);
}
}
This is the controller class for that window.
public class ClientRegistrationController implements InputDialogController
{
private int clientID;
private ClientRegistrationWindow clientRegistration;
public ClientRegistrationController(Window owner)
{
clientID = IDGeneratorUtils.getNewClientID();
clientRegistration = new ClientRegistrationWindow(owner);
clientRegistration.lablelIDValue.setText(String.valueOf(clientID));
clientRegistration.buttonAdd.addActionListener(new AddDataElement(this));
clientRegistration.buttonCancel.addActionListener(new CloseWindow(this));
}
private Client scanWindowInput()
{
Client newClient = new Client();
newClient.setID(clientID);
newClient.setFirstName(clientRegistration.textFieldFirstName.getText());
newClient.setLastName(clientRegistration.textFieldLastName.getText());
newClient.setPrimePhoneNumber(clientRegistration.textFieldPrimePhoneNum.getText());
newClient.setAlternativePhoneNumber(clientRegistration.textFieldAlternativePhoneNum.getText());
newClient.setEmail(clientRegistration.textFieldEmail.getText());
newClient.setAddress(clientRegistration.textFieldAddress.getText());
newClient.setMarketing((Property) clientRegistration.comboBoxMarketing.getSelectedItem());
return newClient;
}
@Override
public void trySavingDataElement()
{
if(ClientValidatorUtils.isValid(clientRegistration))
{
DataStructure.clientsDataTable.save(scanWindowInput());
IDGeneratorUtils.incrementClientCounter();
closeWindow();
}
else
{
new ShowClientInputErrors(clientRegistration);
}
}
@Override
public void closeWindow()
{
clientRegistration.window.dispose();
}
}
This is Client registration input validation class.
public class ClientValidatorUtils
{
public static boolean isValid(ClientRegistrationWindow clientRegistration)
{
return isFirstNameEntered(clientRegistration)
&& isLastNameEntered(clientRegistration)
&& isPhoneNumberEntered(clientRegistration)
&& isPhoneNumberUnique(clientRegistration)
&& isMarketingNotSelected(clientRegistration);
}
public static boolean isFirstNameEntered(ClientRegistrationWindow clientRegistration)
{
return !("".equals(clientRegistration.textFieldFirstName.getText()));
}
public static boolean isLastNameEntered(ClientRegistrationWindow clientRegistration)
{
return !("".equals(clientRegistration.textFieldLastName.getText()));
}
public static boolean isPhoneNumberEntered(ClientRegistrationWindow clientRegistration)
{
return !("".equals(clientRegistration.textFieldPrimePhoneNum.getText()));
}
public static boolean isPhoneNumberUnique(ClientRegistrationWindow clientRegistration)
{
return !(DataStructure.clientsDataTable
.uniqueStringCollision(clientRegistration.textFieldPrimePhoneNum.getText()));
}
public static boolean isMarketingNotSelected(ClientRegistrationWindow clientRegistration)
{
return clientRegistration.comboBoxMarketing.getSelectedItem() != null;
}
}
This is the input error handler.
public class ShowClientInputErrors
{
private ClientRegistrationWindow clientRegistration;
public ShowClientInputErrors(ClientRegistrationWindow clientRegistration)
{
this.clientRegistration = clientRegistration;
showPhomeNumberNotUniqueError();
showPhomeNumberNotEnterdError();
showFirstNameNotEnteredError();
showLastNameNotEnteredError();
showMarketingNotSelectedError();
}
private void showPhomeNumberNotUniqueError()
{
if(ClientValidatorUtils.isPhoneNumberUnique(clientRegistration))
{
displayDefaultPhoneNumber();
}
else
{
displayPhoneNumberDuplicateError();
}
}
private void showPhomeNumberNotEnterdError()
{
if(ClientValidatorUtils.isPhoneNumberEntered(clientRegistration))
{
displayDefaultPhoneNumber();
}
else
{
displayPhoneNumberNotEnteredError();
}
}
private void displayDefaultPhoneNumber()
{
clientRegistration.labelPrimePhoneNum
.setText(ClientGUITextUtils.PRIME_PHONE_NUMBER_LABEL);
clientRegistration.textFieldPrimePhoneNum.setBackground(Color.WHITE);
}
private void displayPhoneNumberNotEnteredError()
{
clientRegistration.labelPrimePhoneNum
.setText(ClientGUITextUtils.PHONE_NUMBER_NOT_ENTERED_ERROR_MESSAGE);
clientRegistration.textFieldPrimePhoneNum.setBackground(Color.YELLOW);
}
private void displayPhoneNumberDuplicateError()
{
clientRegistration.labelPrimePhoneNum
.setText(ClientGUITextUtils.PHONE_NUMBER_NOT_UNIQUE_ERROR_MESSAGE);
clientRegistration.textFieldPrimePhoneNum.setBackground(Color.YELLOW);
}
private void showFirstNameNotEnteredError()
{
if(ClientValidatorUtils.isFirstNameEntered(clientRegistration))
{
clientRegistration.labelFirstName
.setText(ClientGUITextUtils.FIRST_NAME_LABEL);
clientRegistration.textFieldFirstName.setBackground(Color.WHITE);
}
else
{
clientRegistration.labelFirstName
.setText(ClientGUITextUtils.FIRST_NAME_NOT_ENTERED_ERROR_MESSAGE);
clientRegistration.textFieldFirstName.setBackground(Color.YELLOW);
}
}
private void showLastNameNotEnteredError()
{
if(ClientValidatorUtils.isLastNameEntered(clientRegistration))
{
clientRegistration.labelLastName
.setText(ClientGUITextUtils.LAST_NAME_LABEL);
clientRegistration.textFieldLastName.setBackground(Color.WHITE);
}
else
{
clientRegistration.labelLastName
.setText(ClientGUITextUtils.LAST_NAME_NOT_ENTERED_ERROR_MESSAGE);
clientRegistration.textFieldLastName.setBackground(Color.YELLOW);
}
}
private void showMarketingNotSelectedError()
{
if(ClientValidatorUtils.isMarketingNotSelected(clientRegistration))
{
clientRegistration.labelMarketing
.setText(ClientGUITextUtils.MARKETING_LABEL);
clientRegistration.comboBoxMarketing.setBackground(Color.WHITE);
}
else
{
clientRegistration.labelMarketing
.setText(ClientGUITextUtils.MARKETING_NOT_SELECTED_ERROR_MESSAGE);
clientRegistration.comboBoxMarketing.setBackground(Color.YELLOW);
}
}
}