1
\$\begingroup\$

I am trying to come up with a UIState system that works well with Java UI. Before I show it I know I could add enums to help distinguish the states but I've left that out for simplicity. Here is what I have so far. What updates would you suggest?

public class UIStateFrame extends JFrame{

class actionListener implements ActionListener
{
    public void actionPerformed(ActionEvent e) {
        System.out.println("click: "+e.getActionCommand());
        UIStateFrame.instance.setCurrent(current.getNext(e.getActionCommand()));
    }   
}

public static UIStateFrame instance;
public static UIState current;
public void setCurrent(UIState newState)
{
    System.out.println("New State: "+newState);
    current = newState;
    this.getContentPane().removeAll();
    add(current.display);
    this.validate();
    this.repaint();
}
public static void main(String[] args) 
{
    new UIStateFrame();

    //This would be custom UIPanels created elsewhere, here is just a demonstration
    JPanel red = new JPanel();
    red.setBackground(Color.red);
    JButton toBlue = new JButton("toBlue");
    toBlue.setActionCommand("Blue");
    toBlue.addActionListener(instance.new actionListener());
    red.add(toBlue);
    UIState redState = new UIState(red);

    JPanel Blue = new JPanel();
    Blue.setBackground(Color.blue);
    JButton button2 = new JButton("toRed");
    button2.setActionCommand("Red");
    button2.addActionListener(instance.new actionListener());
    Blue.add(button2);
    JButton button = new JButton("toGreen");
    button.setActionCommand("Green");
    button.addActionListener(instance.new actionListener());
    Blue.add(button);       
    UIState blueState = new UIState(Blue);

    JPanel Green = new JPanel();
    Green.setBackground(Color.green);
    JButton toRed = new JButton("toRed");
    toRed.setActionCommand("Red");
    toRed.addActionListener(instance.new actionListener());
    Green.add(toRed);
    UIState greenState = new UIState(Green);

    //transitions
    redState.addNext("Blue", blueState);
    blueState.addNext("Red", redState);
    blueState.addNext("Green", greenState);
    greenState.addNext("Red", redState);

    UIStateFrame.instance.setCurrent(redState);
}
public UIStateFrame()
{
    instance = this;
    this.setSize(500,500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    this.setVisible(true);      
}
}
/*
 * Dynamic Panel control to manage states within the system
 * note: going back is also considered going next
 */
class UIState
{
    public static List<UIState> allStates = new ArrayList<>();
    JPanel display;
    Map<String,UIState> next = new TreeMap<>();
    public UIState(JPanel pan)
    {
        display = pan;
        allStates.add(this);
    }
    public void addNext(String key, UIState state)
    {
        next.put(key, state);
    }   
    public UIState getNext(String key)
    {
        return next.get(key);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.