001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package com.sun.perseus.demo;
033:
034: import java.util.Random;
035:
036: import com.sun.svg.component.SVGList;
037:
038: /**
039: *
040: */
041: /**
042: * Encapsulates retrieving contact information.
043: */
044: public class ContactListSource implements SVGList.ListModel {
045: public static final int NAME = 0;
046: public static final int EMAIL = 1;
047: public static final int CELL_PHONE = 2;
048: public static final int WORK_PHONE = 3;
049: public static final int HOME_PHONE = 4;
050: public static final int ADDRESS_1 = 5;
051: public static final int ADDRESS_2 = 6;
052:
053: /**
054: * Used for random phone number generation.
055: */
056: private static final Random random = new Random();
057:
058: /**
059: * Fake list of data to put in the list.
060: */
061: private String[][] listItemsData;
062:
063: /**
064: * Cache object used to return value in getElementAt
065: */
066: private ContactDetails contactDetails = new ContactDetails();
067:
068: /**
069: * Fake list of cities.
070: */
071: private static final String[] SAMPLE_CITIES = { "Atlanta",
072: "San Francisco", "Santa Clara", "New York", "Chicago",
073: "Detroit", "Las Vegas" };
074:
075: /**
076: * Fake list of streets
077: */
078: private static final String[] SAMPLE_STREETS = { "Network Circle",
079: "El Camino Real", "Market Street", "De Anza Boulevard",
080: "Stevens Creek", "Willow Road", "San Antonio Road",
081: "Montague", "Lick Mill", "Cupertino Avenue" };
082:
083: public ContactListSource() {
084: listItemsData = new String[][] {
085: { "Alec Pietersen", "Alec.Pietersen@example.com",
086: randomPhone(), randomPhone(), randomPhone(),
087: randomAddress1(), randomAddress2() },
088: { "Alfred Anderson", "Alfred.Anderson@example.com",
089: randomPhone(), randomPhone(), randomPhone(),
090: randomAddress1(), randomAddress2() },
091: { "Allan Shaw", "Allan.Shaw@example.com",
092: randomPhone(), randomPhone(), randomPhone(),
093: randomAddress1(), randomAddress2() },
094: { "Andrew Smith", "Andrew.Smith@example.com",
095: randomPhone(), randomPhone(), randomPhone(),
096: randomAddress1(), randomAddress2() },
097: { "Archie Stoddart", "Archie.Stoddart@example.com",
098: randomPhone(), randomPhone(), randomPhone(),
099: randomAddress1(), randomAddress2() },
100: { "Arthur Warner", "Arthur.Warner@example.com",
101: randomPhone(), randomPhone(), randomPhone(),
102: randomAddress1(), randomAddress2() },
103: { "Aubrey Read", "Aubrey.Read@example.com",
104: randomPhone(), randomPhone(), randomPhone(),
105: randomAddress1(), randomAddress2() },
106: { "Betty Archdale", "Betty.Archdale@example.com",
107: randomPhone(), randomPhone(), randomPhone(),
108: randomAddress1(), randomAddress2() },
109: { "Betty Edwards", "Betty.Edwards@example.com",
110: randomPhone(), randomPhone(), randomPhone(),
111: randomAddress1(), randomAddress2() },
112: { "Charlotte Carr", "Charlotte.Carr@example.com",
113: randomPhone(), randomPhone(), randomPhone(),
114: randomAddress1(), randomAddress2() },
115: { "Claire Birch", "Claire.Birch@example.com",
116: randomPhone(), randomPhone(), randomPhone(),
117: randomAddress1(), randomAddress2() },
118: { "Darren Trescoth", "Darren.Trescoth@example.com",
119: randomPhone(), randomPhone(), randomPhone(),
120: randomAddress1(), randomAddress2() },
121: { "Jack Taylor", "Jack.Taylor@example.com",
122: randomPhone(), randomPhone(), randomPhone(),
123: randomAddress1(), randomAddress2() },
124: { "Jim Collingwood", "Jim.Collingwood@example.com",
125: randomPhone(), randomPhone(), randomPhone(),
126: randomAddress1(), randomAddress2() },
127: { "Kevin Stewart", "Kevin.Stewart@example.com",
128: randomPhone(), randomPhone(), randomPhone(),
129: randomAddress1(), randomAddress2() },
130: { "Marcus Strauss", "Marcus.Strauss@example.com",
131: randomPhone(), randomPhone(), randomPhone(),
132: randomAddress1(), randomAddress2() },
133: { "Mary Snowball", "Mary.Snowball@example.com",
134: randomPhone(), randomPhone(), randomPhone(),
135: randomAddress1(), randomAddress2() },
136: { "Mike Gough", "Mike.Gough@example.com",
137: randomPhone(), randomPhone(), randomPhone(),
138: randomAddress1(), randomAddress2() },
139: { "Paul Hendrick", "Paul.Hendrick@example.com",
140: randomPhone(), randomPhone(), randomPhone(),
141: randomAddress1(), randomAddress2() },
142: { "Plum MacLaren", "Plum.MacLaren@example.com",
143: randomPhone(), randomPhone(), randomPhone(),
144: randomAddress1(), randomAddress2() },
145: { "Robin Flintoff", "Robin.Flintoff@example.com",
146: randomPhone(), randomPhone(), randomPhone(),
147: randomAddress1(), randomAddress2() },
148: { "Rosalie Johnson", "Rosalie.Johnson@example.com",
149: randomPhone(), randomPhone(), randomPhone(),
150: randomAddress1(), randomAddress2() },
151: { "Walter Steel", "Walter.Steel@example.com",
152: randomPhone(), randomPhone(), randomPhone(),
153: randomAddress1(), randomAddress2() }, };
154: }
155:
156: /**
157: * @param c the first character for the searched contact.
158: * @return the index of the first contact entry with the given character
159: */
160: public int firstIndexFor(final char c) {
161: for (int i = 0; i < listItemsData.length; i++) {
162: if (Character.toLowerCase(listItemsData[i][0].charAt(0)) == c) {
163: return i;
164: }
165: }
166: return -1;
167: }
168:
169: /**
170: * @return a random street address.
171: */
172: private static final String randomAddress1() {
173: return "" + randomDigit() + randomDigit() + randomDigit() + " "
174: + randomStreetName();
175: }
176:
177: /**
178: * @return a random street name.
179: */
180: private static final String randomStreetName() {
181: int i = (int) (random.nextFloat() * SAMPLE_STREETS.length);
182: return SAMPLE_STREETS[i];
183: }
184:
185: /**
186: * @return a random city zip code and country.
187: */
188: private static final String randomAddress2() {
189: return "" + randomDigit() + randomDigit() + randomDigit()
190: + randomDigit() + randomDigit() + " " + randomCity()
191: + ", " + "USA";
192: }
193:
194: /**
195: * @return a random city name
196: */
197: private static final String randomCity() {
198: int i = (int) (random.nextFloat() * SAMPLE_CITIES.length);
199: return SAMPLE_CITIES[i];
200: }
201:
202: /**
203: * @return a random phone number.
204: */
205: private static final String randomPhone() {
206: return "" + randomDigit() + randomDigit() + randomDigit() + " "
207: + randomDigit() + randomDigit() + randomDigit() + " "
208: + randomDigit() + randomDigit() + " " + randomDigit()
209: + randomDigit();
210: }
211:
212: /**
213: * @return a random digit
214: */
215: private static final int randomDigit() {
216: return (int) (9f * random.nextFloat());
217: }
218:
219: /**
220: * @return the number of contact entries.
221: */
222: public final int getSize() {
223: return listItemsData.length;
224: }
225:
226: /**
227: * @param ci the requested contact index.
228: * @return an object holding the contact details data.
229: */
230: public Object getElementAt(int ci) {
231: contactDetails.name = listItemsData[ci][NAME];
232: contactDetails.email = listItemsData[ci][EMAIL];
233: contactDetails.cellPhone = listItemsData[ci][CELL_PHONE];
234: contactDetails.workPhone = listItemsData[ci][WORK_PHONE];
235: contactDetails.homePhone = listItemsData[ci][HOME_PHONE];
236: contactDetails.address1 = listItemsData[ci][ADDRESS_1];
237: contactDetails.address2 = listItemsData[ci][ADDRESS_2];
238:
239: return contactDetails;
240: }
241: }
|