home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 25 / IOPROG_25.ISO / SOFT / JavaS / javastar-eval.exe / data1.cab / Program_Files / examples / namedb2 / RoloApi.java < prev    next >
Encoding:
Java Source  |  1999-02-11  |  4.3 KB  |  181 lines

  1. /* ****************************************************************
  2. ** @(#)RoloApi.java    1.2 97/05/27
  3. **
  4. ** Copyright 1997 Sun Microsystems, Inc. All Rights Reserved
  5. **
  6. ** ****************************************************************
  7. */
  8.  
  9. import java.io.*;
  10. import java.util.Vector;
  11. import java.util.StringTokenizer;
  12.  
  13. public class RoloApi {
  14.   
  15.   protected SortedList sl;
  16.   public RoloEntry recurrent;
  17.   public int current;
  18.   public File filename;
  19.   public boolean saved;
  20.  
  21.   private Vector search;
  22.   
  23.   public RoloApi() {
  24.     sl = new SortedList();
  25.     filename=null;
  26.     saved=false;
  27.     current= -1;
  28.     recurrent=null;
  29.   }
  30.   
  31.   public void open(File file) {
  32.     sl = new SortedList();
  33.     sl.load(file, RoloEntry.roloobj);
  34.     filename=file;
  35.     saved=true;
  36.     current=0;
  37.     updateCurrent();
  38.   }
  39.  
  40.   public void updateCurrent() {
  41.     recurrent=getEntry(current);
  42.   }
  43.  
  44.   public void save() {
  45.     sl.save(filename);
  46.   }
  47.   
  48.   public RoloEntry getEntry(int key) {
  49.     RoloEntry rolo;
  50.     if (key== -1 || key>=getSize())
  51.       return new RoloEntry();
  52.     rolo = (RoloEntry)sl.getItem(key);
  53.     return rolo;
  54.   }
  55.  
  56.   public int addEntry( String name, String address1, String address2,
  57.             String phone, String email, String other) {
  58.     recurrent=new RoloEntry( name, address1, address2, phone, email, other);
  59.     return putEntry( recurrent );
  60.   }
  61.  
  62.   public int putEntry(RoloEntry rolo) {
  63.     RoloEntry r=(RoloEntry)rolo.clone();
  64.     return sl.putItem(r.name, new ListItem(r.name,r));
  65.   }
  66.  
  67.   public void showEntry(int i) {
  68.     current=i;
  69.     updateCurrent();
  70.   }
  71.  
  72.   public void removeEntry(int key) {
  73.       sl.removeItem(key);
  74.   }
  75.   
  76.   public int getSize() {
  77.     return sl.size();
  78.   }
  79.   
  80.   public String[] getAllKeys() {
  81.     String[] keys;
  82.     keys = sl.getKeys();
  83.     return keys;
  84.   }
  85.  
  86.   private void addparam(int sel,String contains) {
  87.     int i;
  88.     String s;
  89.     StringTokenizer st=new StringTokenizer( contains );
  90.     while (st.hasMoreTokens()) {
  91.       s=st.nextToken().toLowerCase();
  92.       if (sel==0) {
  93.     for(i=0;i<6;i++)
  94.       ((Vector)search.elementAt(i)).addElement(s);
  95.       } else {
  96.     ((Vector)search.elementAt(sel-1)).addElement(s);
  97.       }
  98.     }
  99.   }
  100.  
  101.   public Vector searchIt(int cat, String searchStr) {
  102.     int ientry,isearch;
  103.     String match;
  104.     boolean found;
  105.     Vector foundindex=new Vector();
  106.  
  107.     search=new Vector(6);
  108.     for(int i=0;i<7;i++) {
  109.       search.addElement(new Vector());
  110.     }
  111.  
  112.     addparam(cat,searchStr);
  113.     RoloEntry entry;
  114.     for (ientry=0;ientry<getSize();ientry++) {
  115.       found=false;
  116.       entry=getEntry( ientry );
  117.  
  118.       for (isearch=0;isearch<((Vector)search.elementAt(0)).size();isearch++) {
  119.     match=(String)((Vector)search.elementAt(0)).elementAt(isearch);
  120.     if (entry.name.toLowerCase().indexOf(match)!=(-1)) {
  121.       found=true;
  122.       break;
  123.     }
  124.       }
  125.  
  126.       if (found==false) {
  127.     for (isearch=0;isearch<((Vector)search.elementAt(1)).size();isearch++) {
  128.       match=(String)((Vector)search.elementAt(1)).elementAt(isearch);
  129.       if (entry.address1.toLowerCase().indexOf(match)!=(-1)) {
  130.         found=true;
  131.         break;
  132.       }
  133.     }
  134.       }
  135.  
  136.       if (found==false) {
  137.     for (isearch=0;isearch<((Vector)search.elementAt(2)).size();isearch++) {
  138.       match=(String)((Vector)search.elementAt(2)).elementAt(isearch);
  139.       if (entry.address2.toLowerCase().indexOf(match)!=(-1)) {
  140.         found=true;
  141.         break;
  142.       }
  143.     }
  144.       }
  145.       if (found==false) {
  146.     for (isearch=0;isearch<((Vector)search.elementAt(3)).size();isearch++) {
  147.       match=(String)((Vector)search.elementAt(3)).elementAt(isearch);
  148.       if (entry.phone.toLowerCase().indexOf(match)!=(-1)) {
  149.         found=true;
  150.         break;
  151.       }
  152.     }
  153.       }
  154.  
  155.       if (found==false) {
  156.     for (isearch=0;isearch<((Vector)search.elementAt(4)).size();isearch++) {
  157.       match=(String)((Vector)search.elementAt(4)).elementAt(isearch);
  158.       if (entry.email.toLowerCase().indexOf(match)!=(-1)) {
  159.         found=true;
  160.         break;
  161.       }
  162.     }
  163.       }
  164.       if (found==false) {
  165.     for (isearch=0;isearch<((Vector)search.elementAt(5)).size();isearch++) {
  166.       match=(String)((Vector)search.elementAt(5)).elementAt(isearch);
  167.       if (entry.other.toLowerCase().indexOf(match)!=(-1)) {
  168.         found=true;
  169.         break;
  170.       }
  171.     }
  172.       }
  173.       if (found==true) {
  174.     foundindex.addElement(new Integer(ientry));
  175.       }
  176.     }
  177.     return foundindex;
  178.  }
  179.     
  180. }
  181.