Question

So I'm making a custom menu and this is the sub-menu item class which handles invoking the correct method. Currently I am using a switch statement for the method name, but is it the best way to do it?

I would love a new set of eyes, I have been looking at this way to long. Thanks!

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;

import com.as.devcenter.Util;

public class MenuSubItem extends JLabel implements MouseListener {

    private static final long serialVersionUID = 1L;

    private String action = "";

    public MenuSubItem(String label, String action, Class<?> className) {
    addMouseListener(this);

    this.action = action;

    setText(label);
    setName(action);
    setSize(Util.widthOfText(getText(), Util.getDefaultFont()) + 4, 22);
    }

    /*
     * Start Mouse Events
     */

    @Override
    public void mouseClicked(MouseEvent e) {
    Util.log("MenuSubItem clicked");
    if (action.equalsIgnoreCase("")) {
        Util.log("No action was provided for MenuSubItem: " + getText());
    } else {
        try {
        Method method = Actions.class.getDeclaredMethod(action, className);

        switch (method.getName()) {
        case "newProject":
            NewProjectFrame npf = new NewProjectFrame();
            method.invoke(npf, npf);
            break;
        case "openConsole":
            Console console = new Console();
            method.invoke(console, console);
            break;
        }
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
        if (e1 instanceof NoSuchMethodException) {
            Util.log("Could not identify action: " + action);
        } else {
            e1.printStackTrace();
        }
        }

    }
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
}

Actions class (just have 2 methods atm but will have more)

public class Actions {

    public static void newProject(NewProjectFrame npf){
        npf.setup();
        Util.getProgram().items[0].clear();
    }

    public static void openConsole(Console console){
    console.setup();
    Util.getProgram().items[0].clear();
    }
}
Was it helpful?

Solution

Use the Command pattern:

In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.

From http://en.wikipedia.org/wiki/Command_pattern

This example is very similar to your problem:

http://alvinalexander.com/java/java-command-design-pattern-in-java-examples

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top