home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / unuy2wen / cybcerone / utils / entrykeypad.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  4.0 KB  |  171 lines

  1. // EntryKeypad.java
  2. // 28.02.96
  3. //
  4. // A keypad with a display
  5.  
  6. package cybcerone.utils;
  7.  
  8. import java.awt.Event;
  9. import java.awt.Graphics;
  10. import java.awt.Rectangle;
  11. import java.awt.Color;
  12.  
  13. /**
  14.  * This is a Keypad with a box for input.  Can be used to enter
  15.  * letters and numbers.
  16.  */
  17. public class EntryKeypad extends Keypad {
  18.   private Rectangle entryBox = scale (54, 212, 182, 29);
  19.  
  20.   private static final String trailer = 
  21.     " .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .";
  22.  
  23.   /** This erases the current entry. */
  24.   protected static final Character eraseChar = new Character ('|');
  25.  
  26.   /** This is the backspace character. */
  27.   protected static final Character backspaceChar = new Character ((char)8);
  28.   
  29.   private StringBuffer entry;
  30.   
  31.  
  32.   /**
  33.    * One EntryKeypad to go.
  34.    */
  35.   public EntryKeypad (String id, String statusText, Appletlike app) {
  36.     super (id, statusText, app);
  37.     setFont (Fonts.bigFont);
  38.     reset ();
  39.   }
  40.  
  41.   protected void setEntryBox (Rectangle box) {
  42.     entryBox = scale (box);
  43.   }
  44.   
  45.   /**
  46.    * Add a logical button to this keypad.  Note that the value must be
  47.    * of type Character (no way to enforce this since it'll use the parent
  48.    * method if you pass a different type).
  49.    */
  50.   protected void addButton (Character value, int x, int y,
  51.                 int width, int height) {
  52.     super.addButton (value, x, y, width, height);
  53.   }
  54.  
  55.   /**
  56.    * Draw the background, the text in the entry box, and then the selected
  57.    * button.
  58.    */
  59.   public void paint (Graphics g) {
  60.     super.paint (g);
  61.     paintEntryBox (g);
  62.   }
  63.  
  64.   protected void paintEntryBox (Graphics g) {
  65.     Graphics box = g.create (entryBox.x, entryBox.y, 
  66.                  entryBox.width, entryBox.height);
  67.     box.setColor (Color.white);
  68.     box.fillRect (0, 0, entryBox.width, entryBox.height);
  69.     box.setColor (Color.black);
  70.     box.drawString (entry + trailer, scale (4), scale (23));
  71.   }
  72.  
  73.   protected void paintActive (Graphics g) {
  74.     KeypadButton activeButton = buttons.getHighlighted ();
  75.  
  76.     if (activeBg != null && activeButton != null) {
  77.       Rectangle placement = activeButton.getPlacement ();
  78.       Graphics active = g.create ();
  79.       active.clipRect (placement.x, placement.y, 
  80.                placement.width,placement.height);
  81.       active.drawImage (activeBg, 0, 0, this);
  82.     }
  83.   }
  84.  
  85.   /** Clear the entry. */
  86.   public void reset () {
  87.     entry = new StringBuffer ();
  88.     update ();
  89.   }
  90.  
  91.   /** Add this letter to the entry. */
  92.   private void addLetter (Character letter) {
  93.     entry.append (letter);
  94.   }
  95.  
  96.   /** Delete the last letter of the entry. */
  97.   private void deleteLetter () {
  98.     int length = entry.length ();
  99.  
  100.     if (length > 0) {
  101.       entry.setLength (length - 1);
  102.     }
  103.   }
  104.  
  105.   /**
  106.    * This button was clicked.  Get it's value.  If it's the erase
  107.    * character, reset.  If it's the backspace character, delete
  108.    * a letter.  Otherwise, add the letter to the entry.
  109.    */
  110.   public void update (KeypadButton theButton) {
  111.     Character theValue;
  112.  
  113.     if (theButton != null) {
  114.       theValue = (Character)theButton.getValue ();
  115.      
  116.       buttons.setHighlighted (theButton);
  117.  
  118.       if (theValue.equals (eraseChar))
  119.     reset ();
  120.       else if (theValue.equals (backspaceChar))
  121.     deleteLetter ();
  122.       else 
  123.     addLetter (theValue);
  124.  
  125.       update ();
  126.     }
  127.   }
  128.  
  129.   /** update the parent */
  130.   private void update () {
  131.     repaint ();
  132.     app.update (entry.toString ());
  133.   }
  134.  
  135.   /**
  136.    * The user clicked somewhere, take appropriate action.
  137.    */
  138.   public boolean mouseDown (Event evt, int x, int y) {
  139.     update (whichButton (x, y));
  140.     return true;
  141.   }
  142.  
  143.   /**
  144.    * Unpress the button.
  145.    */
  146.   public boolean mouseUp (Event evt, int x, int y) {
  147.     buttons.clearHighlighted ();
  148.     repaint ();
  149.     return true;
  150.   }
  151.  
  152.   /**
  153.    * The user pressed a key.  Make it look like he pressed the button on
  154.    * the keypad.
  155.    */
  156.   public boolean keyDown (Event evt, int key) {
  157.     update (whichButton (new Character (Character.toUpperCase ((char)key))));
  158.     return true;
  159.   }
  160.  
  161.   /**
  162.    * The key's up, so unpress it.
  163.    */
  164.   public boolean keyUp (Event evt, int key) {
  165.     buttons.clearHighlighted ();
  166.     repaint ();
  167.     return true;
  168.   }
  169.  
  170. }
  171.