MyIF interface development example - m

By Nicholas Duchon - March 16, 2012, Musings

UML from jGrasp:

Display:

Code:

// File: MyIFm.java
// Author: Nicholas Duchon
// Date: Mar 16, 2012
// Part m of the interface demonstrations, after k
// 
// NOW:
//   > use enum-based single comparator in MyRecord
//   > interface MyIF defined to limit changes when going to next version
//   > MyButton class added to simplify button setup
//   > switch's used to handle events rather than new classes

   import javax.swing.JFrame;
   import javax.swing.JApplet;
   import javax.swing.JTextField;
   import javax.swing.JButton;
   import javax.swing.JPanel;
   import javax.swing.JTextArea;
   import javax.swing.JScrollPane;
   import java.awt.event.ActionListener;
   import java.awt.event.ActionEvent;
   import java.awt.BorderLayout;
   import java.awt.GridLayout;
   import java.awt.Font;
   import java.util.Random;

   import java.util.ArrayList;
   import java.util.Collections;
   import java.util.Comparator;
  
   enum Order {ONE, TWO, THREE, FOUR, DISPLAY, NEW, SUFFLE};

   interface MyIF {
      void handleOrder (Order x);
      Order getOrder ();
   }; // end interface definition

   public class MyIFm extends JApplet implements MyIF {
      static final long serialVersionUID = -3804044462946113366L;
  
      static Random rn = new Random();
      static int startNumber = 20;
      static int maxNumber = 1000;
      static String colLabel = String.format ("%10s%10s%10s%10s\n", "One", "Two", "Three", "Four");
  
      ArrayList <MyRecord> arr = new ArrayList <MyRecord> ();
      public Order order = Order.ONE;
  
   // input and output text areas
      private JTextField asf    = new JTextField("" + startNumber);
      private JTextArea  ioArea = new JTextArea (10,10);
  
      public void init () {
         fillArray(startNumber);
     
         setLayout (new BorderLayout ());
     
      // add listeners to fields and buttons
      // listeners are named inner classes, see below
         asf.addActionListener (
               new ActionListener () {
                  public void actionPerformed (ActionEvent e) {
                     fillArray (e.getActionCommand());
                  }
               }); // end anonymous inner class TextField action listener
     
      // TextArea always get auto scrolls
         ioArea.setEditable (true);
         ioArea.setFont (new Font ("Monospaced", Font.PLAIN, 12));
         JScrollPane spTextIO = new JScrollPane (ioArea);
     
      // create and add buttons to control panel
         JPanel textIObp = new JPanel (new GridLayout (2, 4)); // button panel
         textIObp.add(asf);
         textIObp.add(new MyButton ("Display Array", this, Order.DISPLAY));
         textIObp.add(new MyButton ("New Array"    , this, Order.NEW    ));
         textIObp.add(new MyButton ("Shuffle Array", this, Order.SUFFLE ));
         textIObp.add(new MyButton ("Sort on one"  , this, Order.ONE    ));
         textIObp.add(new MyButton ("Sort on two"  , this, Order.TWO    ));
         textIObp.add(new MyButton ("Sort on Three", this, Order.THREE  ));
         textIObp.add(new MyButton ("Sort on Four" , this, Order.FOUR   ));
     
         add (spTextIO, BorderLayout.CENTER);
         add (textIObp, BorderLayout.SOUTH);
     
         display();
      } // end method init
  
      MyRecord getNewRandomRecord (int index) {
         return
            new MyRecord(
            index,
            rn.nextInt(maxNumber),
            rn.nextInt(maxNumber),
            rn.nextInt(maxNumber),
            this
            ); // end creating new random instance of MyRecord
      } // end getNewRandomMember
  
      void fillArray (int size) {
         arr.clear();
         for (int i = 0; i < size; i++)
            arr.add ( getNewRandomRecord(i) );
      } //end method fillArray
  
      void fillArray (String st) {
         try {
            int size = Integer.parseInt(st.trim());
            fillArray (size);
            display();
         }
            catch (NumberFormatException ex) {
               ioArea.setText ("Bad number format");
            } // end try/catch
      } //end method fillArray
  
      public void display () {
         ioArea.setText (colLabel);
         for (int i = 0; i < arr.size(); i++)
            ioArea.append(arr.get(i) + "\n");
         ioArea.append ("done!");
         ioArea.setCaretPosition (0);
      } // end method display
     
      public void handleOrder (Order pOrd) {
         order = pOrd;
         switch (order) {
            case SUFFLE:  Collections.shuffle(arr);
               break;
            case NEW:     fillArray(asf.getText());
            case ONE:
            case TWO:
            case THREE:
            case FOUR:  Collections.sort (arr);
            case DISPLAY:
         } // end switch on order
         display ();
      } // end handleOrder
     
      public Order getOrder () {
         return order;
      } // end method getOrder
  
      public static void main (String args[]) {
         JFrame window = new JFrame ();
         window.setSize (500,500);
         window.setLocationRelativeTo (null);
         MyIFm m = new MyIFm ();
         window.add (m);
         m.init ();
         m.start ();
         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         window.setVisible (true);
      } // end main
  
   } // end class MyIFm

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   class MyRecord implements Comparable <MyRecord> {
  
      private int one, two, three, four;
      MyIF parent = null;
  
      public MyRecord (int p1, int p2, int p3, int p4, MyIF pp) {
         one    = p1;
         two    = p2;
         three  = p3;
         four   = p4;
         parent = pp;
      } // end 4 int parameter constructor
  
      public int compareTo (MyRecord mr) {
      // following only OK if no arithmetic overflows happen
         switch (parent.getOrder()) {
            case ONE:  
               return one   - mr.one;
            case TWO:  
               return two   - mr.two;
            case THREE:
               return three - mr.three;
            case FOUR: 
               return four  - mr.four;
            default:   
               return one   - mr.one;
         } // end switch
      } // end method compareTo required by Comparable interface
  
      public String toString () {
         return String.format ("%10d%10d%10d%10d", one, two, three, four);
      } // end method toString
  
   } // end class MyRecord
  
   class MyButton extends JButton implements ActionListener {
      MyIF parent = null;
      Order ord = Order.ONE;
   
      public MyButton (String ps, MyIF pp, Order po) {
         setText (ps);
         parent = pp;
         ord = po;
         addActionListener (this);
      } // end MyButton constructor
   
      public void actionPerformed (ActionEvent e) {
         parent.handleOrder (ord);
      } // end method actionPerformed
   } // end class MyButton