KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > javaee > blueprints > autocomplete > ApplicationBean


1 /* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
2    You may not modify, use, reproduce, or distribute this software except
3    in compliance with the terms of the License at:
4        http://developer.sun.com/berkeley_license.html
5  */

6 package com.sun.javaee.blueprints.autocomplete;
7
8 import java.io.BufferedInputStream JavaDoc;
9 import java.io.InputStreamReader JavaDoc;
10 import java.io.BufferedReader JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.util.ArrayList JavaDoc;
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 /**
22  * This application bean is a managed JSF bean in application scope.
23  * When created it will read in a list of US cities and their zip codes.
24  * It makes this list available to others who ask for it - such as the
25  * AJAX autocompletion servlet.
26  *
27  * @author Tor Norbye
28  * @author Mark Basler
29  */

30 public class ApplicationBean {
31     static final boolean bDebug=false;
32     private String JavaDoc[] cities;
33     private String JavaDoc[] zips;
34     private String JavaDoc[] states =
35         new String JavaDoc[] {
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     /** Creates a new instance of ApplicationBean */
43     public ApplicationBean() {
44         initializeCities();
45     }
46
47     private void initializeCities() {
48         InputStream JavaDoc is = null;
49         BufferedReader JavaDoc reader=null;
50         ArrayList JavaDoc alZip=new ArrayList JavaDoc(), alCities=new ArrayList JavaDoc();
51
52         try {
53             
54             // read in record
55
reader=new BufferedReader JavaDoc(new InputStreamReader JavaDoc(ApplicationBean.class.getResourceAsStream(
56                         "/com/sun/javaee/blueprints/autocomplete/cities.data")));
57             
58             cities = new String JavaDoc[] { "INITIALIZATION ERROR" };
59             zips = new String JavaDoc[] { "INITIALIZATION ERROR" };
60
61             String JavaDoc sxLine=null, sxZip=null;
62             
63             // read in variable amount of data
64
// rewrote method, because older version was erroring out
65
while((sxLine=reader.readLine()) != null) {
66                 // loop through are read in zip, cities and states
67
// is in the format "00000 City, State"
68
if(bDebug) System.out.println("RAW DATA =" + sxLine);
69                 if(sxLine.length() > 7) {
70                     sxZip=sxLine.substring(0,5);
71                     // get zip
72
alZip.add(sxZip);
73
74                     // get city & state
75
alCities.add(sxLine.substring(6) + " " + sxZip);
76                 }
77             }
78             cities=(String JavaDoc[])alCities.toArray(new String JavaDoc[alCities.size()]);
79             zips=(String JavaDoc[])alZip.toArray(new String JavaDoc[alZip.size()]);
80             
81    
82         } catch (IOException JavaDoc 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 JavaDoc ioe) {
90                     ioe.printStackTrace();
91                 }
92             }
93         }
94     }
95
96     /** Return a sorted array of City, State entries. */
97     public String JavaDoc[] getCities() {
98         // TODO Create full list
99
return cities;
100     }
101
102     /** Return zip codes corresponding to the City, State array. */
103     public String JavaDoc[] getZips() {
104         return zips;
105     }
106
107     public void completeCity(FacesContext context, String JavaDoc 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 JavaDoc prefix, CompletionResult result) {
113         if(bDebug) System.out.println("Completing state - " + prefix);
114         AutoCompleteUtilities.addMatchingItems(states, prefix, result);
115     }
116 }
117
Popular Tags