home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161a.iso / handson / files / copyjava.exe / com / sun / java / swing / plaf / basic / StringSpinner.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  2.1 KB  |  106 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import java.awt.Component;
  4. import java.awt.event.FocusEvent;
  5. import java.awt.event.KeyEvent;
  6.  
  7. public class StringSpinner extends Spinner {
  8.    private String[] names;
  9.    private String typed;
  10.  
  11.    public StringSpinner(int var1, String var2, String[] var3) {
  12.       super(var1, var2);
  13.       this.names = var3;
  14.       ((Spinner)this).setMinimum(0);
  15.       ((Spinner)this).setMaximum(var3.length - 1);
  16.       ((Spinner)this).setWrap(true);
  17.       ((Spinner)this).updateUI();
  18.    }
  19.  
  20.    public String[] getNameArray() {
  21.       return this.names;
  22.    }
  23.  
  24.    public String getValueName() {
  25.       return this.names[super.value];
  26.    }
  27.  
  28.    public String getTypedString() {
  29.       return this.typed;
  30.    }
  31.  
  32.    private void resynctyped() {
  33.       if (this.typed != null) {
  34.          int var1 = this.typed.length();
  35.  
  36.          for(int var2 = 0; var2 < this.names.length; ++var2) {
  37.             if (this.typed.regionMatches(true, 0, this.names[var2], 0, var1)) {
  38.                ((Spinner)this).setValue(var2);
  39.                ((Component)this).repaint();
  40.                return;
  41.             }
  42.          }
  43.  
  44.          if (var1 > 1) {
  45.             this.typed = this.typed.substring(0, var1 - 1);
  46.          } else {
  47.             this.typed = null;
  48.          }
  49.  
  50.          this.resynctyped();
  51.       }
  52.  
  53.    }
  54.  
  55.    public void keyPressed(KeyEvent var1) {
  56.       if (var1.isActionKey()) {
  57.          switch (var1.getKeyCode()) {
  58.             case 38:
  59.                ((Spinner)this).setValue(super.value + 1);
  60.                return;
  61.             case 39:
  62.             default:
  63.                break;
  64.             case 40:
  65.                ((Spinner)this).setValue(super.value - 1);
  66.                return;
  67.          }
  68.       } else {
  69.          char var2 = var1.getKeyChar();
  70.          if (var2 > ' ' && var2 < 127) {
  71.             this.typed = this.typed != null ? this.typed + (char)var2 : String.valueOf((char)var2);
  72.             this.resynctyped();
  73.             return;
  74.          }
  75.  
  76.          switch (var2) {
  77.             case '\b':
  78.             case '\u007f':
  79.                if (this.typed != null) {
  80.                   if (this.typed.length() <= 0) {
  81.                      this.typed = null;
  82.                   } else {
  83.                      this.typed = this.typed.substring(0, this.typed.length() - 1);
  84.                   }
  85.                }
  86.  
  87.                this.resynctyped();
  88.                return;
  89.          }
  90.       }
  91.  
  92.    }
  93.  
  94.    public void focusGained(FocusEvent var1) {
  95.       this.typed = null;
  96.       ((Component)this).repaint();
  97.       super.focusGained(var1);
  98.    }
  99.  
  100.    public void focusLost(FocusEvent var1) {
  101.       this.typed = null;
  102.       ((Component)this).repaint();
  103.       super.focusLost(var1);
  104.    }
  105. }
  106.