UNB/ CS/ David Bremner/ teaching/ java/ RPNCalc5.java
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import java.awt.Dimension;
public class RPNCalc5 extends DBFrame implements KeyPadClient, 
                                                 HistoryPaneClient{

    private JLabel resultLabel=new JLabel("results");

    private PromptField input;
    private RPNStack stack=new RPNStack();
    
    private HistoryPane results;

    private final String labels[][]=
            { {"7","8","9"}, 
              {"4","5","6"},
              {"1","2","3"},
              {"0",".", "Enter"},
              {"+","-","*"},
              {"/","clear","quit" }};

    public void mouseClickCallback(String text){
        input.setText(text);
    }

    public void keypressCallback(String text){
        if (text.equals("clear")){
            input.setText("");
            return;
        }
        if (text.equals("quit")){
            System.exit(0);
        }
            
        if ('0' <= text.charAt(0) && 
            text.charAt(0) <= '9' || text.charAt(0)=='.'){           
            input.setText(input.getText() + text);
        } else{
            process(text);
        }
    }

    private void process(String text){
        String result,operand;
            
        operand=input.getText();
        if (!operand.equals(""))
            stack.push(operand);

        result=stack.process(text);
        results.addElement(result);
        input.setText("");
    }
    public RPNCalc5(){
        BoxLayout calcLayout=new BoxLayout(getContentPane(),BoxLayout.X_AXIS);
        getContentPane().setLayout(calcLayout);

        
        results=new HistoryPane("123456789.999999");
        results.addClient(this);

        JPanel rightGroup=new JPanel(new BorderLayout());
        rightGroup.add(resultLabel,"North");
        rightGroup.add(results,"Center");

        JPanel leftGroup=new JPanel();
        leftGroup.setLayout(new BoxLayout(leftGroup, BoxLayout.Y_AXIS));

        input=new PromptField("input",20);
        leftGroup.add(input);

        leftGroup.add(new KeyPad(labels,this));

        getContentPane().add(leftGroup);
        getContentPane().add(rightGroup);

        setTitle("RPN Calculator");
        setVisible(true);

        pack();
    }
    public static void main(String [] args){
        RPNCalc5 r=new RPNCalc5();
    }
}
//