1 6 package com.sun.javaee.blueprints.autocomplete; 7 8 import java.io.BufferedInputStream ; 9 import java.io.InputStreamReader ; 10 import java.io.BufferedReader ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.util.ArrayList ; 14 15 import javax.faces.context.FacesContext; 16 17 import com.sun.j2ee.blueprints.ui.autocomplete.AutoCompleteUtilities; 18 import com.sun.j2ee.blueprints.ui.autocomplete.CompletionResult; 19 20 21 30 public class ApplicationBean { 31 static final boolean bDebug=false; 32 private String [] cities; 33 private String [] zips; 34 private String [] states = 35 new String [] { 36 "AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", "IA", "ID", "IL", 37 "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", 38 "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", 39 "VA", "VT", "WA", "WI", "WV", "WY" 40 }; 41 42 43 public ApplicationBean() { 44 initializeCities(); 45 } 46 47 private void initializeCities() { 48 InputStream is = null; 49 BufferedReader reader=null; 50 ArrayList alZip=new ArrayList (), alCities=new ArrayList (); 51 52 try { 53 54 reader=new BufferedReader (new InputStreamReader (ApplicationBean.class.getResourceAsStream( 56 "/com/sun/javaee/blueprints/autocomplete/cities.data"))); 57 58 cities = new String [] { "INITIALIZATION ERROR" }; 59 zips = new String [] { "INITIALIZATION ERROR" }; 60 61 String sxLine=null, sxZip=null; 62 63 while((sxLine=reader.readLine()) != null) { 66 if(bDebug) System.out.println("RAW DATA =" + sxLine); 69 if(sxLine.length() > 7) { 70 sxZip=sxLine.substring(0,5); 71 alZip.add(sxZip); 73 74 alCities.add(sxLine.substring(6) + " " + sxZip); 76 } 77 } 78 cities=(String [])alCities.toArray(new String [alCities.size()]); 79 zips=(String [])alZip.toArray(new String [alZip.size()]); 80 81 82 } catch (IOException ioe) { 83 System.out.println("Failed to initialize city datastructures: " + ioe); 84 ioe.printStackTrace(); 85 } finally { 86 if (is != null) { 87 try { 88 is.close(); 89 } catch (IOException ioe) { 90 ioe.printStackTrace(); 91 } 92 } 93 } 94 } 95 96 97 public String [] getCities() { 98 return cities; 100 } 101 102 103 public String [] getZips() { 104 return zips; 105 } 106 107 public void completeCity(FacesContext context, String prefix, CompletionResult result) { 108 if(bDebug) System.out.println("Completing City - " + prefix); 109 AutoCompleteUtilities.addMatchingItems(cities, prefix, result); 110 } 111 112 public void completeState(FacesContext context, String prefix, CompletionResult result) { 113 if(bDebug) System.out.println("Completing state - " + prefix); 114 AutoCompleteUtilities.addMatchingItems(states, prefix, result); 115 } 116 } 117 | Popular Tags |