jeudi 9 février 2023

How to implement Mediator Design Pattern in Netbeans?

I am trying to implement something similar to this example on refactoring guru: Mediator in Java in my project.

I have created additional package with simple login form frame and some of the classes to test how it might work. Here is some code for more context...

Component interface:

package mediator;

public interface Component {

    void setMediator(Mediator mediator);

    String getName();
}

Mediator interface:

package mediator;

public interface Mediator {

    void login();

    void registerComponent(Component component);
}

Login button:

package mediator;

import java.awt.event.ActionEvent;
import javax.swing.JButton;

public class LoginButton extends JButton implements Component {

    private Mediator mediator;

    public LoginButton() {
        super("Log in");
    }

    @Override
    protected void fireActionPerformed(ActionEvent actionEvent) {
        mediator.login();
    }

    @Override
    public void setMediator(Mediator mediator) {
        this.mediator = mediator;
    }

    @Override
    public String getName() {
        return "LoginButton";
    }
}

Username field:

package mediator;

import java.awt.event.KeyEvent;
import javax.swing.JTextField;

public class UsernameField extends JTextField implements Component {

    private Mediator mediator;

    @Override
    public void setMediator(Mediator mediator) {
        this.mediator = mediator;
    }

    @Override
    protected void processComponentKeyEvent(KeyEvent keyEvent) {
        mediator.login();
    }

    @Override
    public String getName() {
        return "UsernameField";
    }
}

Passoword field:

package mediator;

import java.awt.event.KeyEvent;
import javax.swing.JTextField;

public class PasswordField extends JTextField implements Component {

    private Mediator mediator;

    @Override
    public void setMediator(Mediator mediator) {
        this.mediator = mediator;
    }

    @Override
    protected void processComponentKeyEvent(KeyEvent keyEvent) {
        mediator.login();
    }

    @Override
    public String getName() {
        return "PasswordField";
    }
}

Mediator:

package mediator;

public class Dialog implements Mediator {

    private UsernameField username;
    private PasswordField password;
    private LoginButton login;

    @Override
    public void login() {
        if (username.getText().trim().isEmpty()) {
            System.out.println("Username is empty!");
            return;
        }
        if (password.getText().trim().isEmpty()) {
            System.out.println("Password is empty!");
            return;
        }
        System.out.println("All good!");
    }

    @Override
    public void registerComponent(Component component) {
        component.setMediator(this);
        switch (component.getName()) {
            case "LoginButton":
                login = (LoginButton) component;
                break;
            case "UsernameField":
                username = (UsernameField) component;
                break;
            case "PasswordField":
                password = (PasswordField) component;
                break;
        }
    }
}

Frame:

package mediator;

public class LoginFrame extends javax.swing.JFrame {

    Mediator mediator;

    /**
     * Creates new form LoginFrame
     */
    public LoginFrame() {
        initComponents();
        mediator = new Dialog();
        mediator.registerComponent(new LoginButton());
        mediator.registerComponent(new UsernameField());
        mediator.registerComponent(new PasswordField());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")

    private void login_Button_MouseClicked(java.awt.event.MouseEvent evt) {

        mediator.login();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoginFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton login_Button_;
    private javax.swing.JTextField password_Field_;
    private javax.swing.JLabel password_Label_;
    private javax.swing.JTextField username_Field_;
    private javax.swing.JLabel username_Label_;
    // End of variables declaration
}

I want to be able to validate and dynamically change components on jframe when action is called from mediator.

The problem is i dont know how to do it because of netbeans built in gui builder and action listeners. I have similar jframes in my project where i used gui builder and i would like to keep it that way.

Do i send for example the whole jbutton i created in jframe as a parameter and then cast it to a class? What is the general best practice to follow when doing something similar? I am generally confused :).

Does enyone know what is the best way to do this?

When i run the code above and click login button a couple of times with the text displayed i get this output: Output picture! I have also tried to pass login_Button_ variable to a LoginButton class to type cast it but i was not able to do it.

I know i missed something crucial but i just don't know what.

Any help will be appreciated!

Aucun commentaire:

Enregistrer un commentaire