sourcforge > sotacs
 

Rich Formatted Selection Box

There are two Java classes and a ressource file used in the examples of the AjaxTextField, implementing a simple phonebook.

TelephoneBook.java

public class TelephoneBook {
   
	public enum TelephoneEntryField {NAME, FIRSTNAME, PHONE;}
	
	private ArrayList<TelephoneBookEntry> entries = new ArrayList<TelephoneBookEntry>();
	
    private TelephoneBook() throws IOException{
    	InputStream is = getClass().getClassLoader().
			getResourceAsStream("examples/textfield/phonebook/phonebook.txt");
    	BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        while((line = br.readLine()) != null) entries.add(new TelephoneBookEntry(line));
    }

	public List<TelephoneBookEntry> getEntries(TelephoneEntryField field, String prefix) {
		ArrayList<TelephoneBookEntry> out = new ArrayList<TelephoneBookEntry>();
		prefix = prefix.toLowerCase();
    	for (TelephoneBookEntry entry : entries) {
			switch (field) {
			case FIRSTNAME:
				 if(entry.getFirstName().toLowerCase().startsWith(prefix)) out.add(entry);break;
			case NAME:
				 if(entry.getName().toLowerCase().startsWith(prefix)) out.add(entry);break;
			case PHONE:
				 if(entry.getTelephone().toLowerCase().startsWith(prefix)) out.add(entry);break;
			default:
				break;
			}
		}
    	return out;
	}


	//------------------- singleton ------------------------------------------- 
	
	private static TelephoneBook instance = null;

    
	public static TelephoneBook getInstance() {
		if(instance == null){
			try {
				instance = new TelephoneBook();
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
		return instance;
	}
	
	public static void main(String[] args) {
		List<TelephoneBookEntry> results = 
			TelephoneBook.getInstance().getEntries(TelephoneEntryField.NAME, "H");
		for (TelephoneBookEntry entry : results) {
			System.out.println(entry.toString());
		}
	}
}
		

TelephoneBookEntry.java

public class TelephoneBookEntry {
	private String name;
	private String firstName;
	private String telephone;
	private int age;
   
	
	public TelephoneBookEntry(String line) {
		String[] fields = line.split("\\s*,\\s*");
		firstName = fields[0].trim();
		name = fields[1].trim();
		telephone = fields[2].trim();
		age = Integer.parseInt(fields[3].trim());
	}

	public int getAge() {
		return age;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getName() {
		return name;
	}

	public String getTelephone() {
		return telephone;
	}

	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("TelephoneBookEntry[");
		buffer.append("name = ").append(name);
		buffer.append(", firstName = ").append(firstName);
		buffer.append(", telephone = ").append(telephone);
		buffer.append(", age = ").append(age);
		buffer.append("]");
		return buffer.toString();
	}
}

phonebook.txt

Barbara, Potter, 56542, 34
Arthur, Cameron, 34321, 58
Jonathan, Taylor, 23452, 24
Susan, Smith, 90876, 23
Jane, Goodman, 54328, 36
Jason, Hunter, 76532, 45
Bred, Gyusty, 40178, 48
Ronald, Reagan, 70982, 98
Jimmy, Carter, 32903, 87
Richard, Nixon, 83401, 76
Gerald, Ford, 35016, 65
Margareth, Thatcher, 69286, 78
John, Major, 14387, 54
Gerhard, Schröder, 78735, 68
Chris, Patter, 54542, 34
Arturo, Bameron, 24321, 68
Jon, Good, 2674, 64
Steve, Jobbs, 78763, 53
Tommy, Lee, 46432, 36
Ernest, Hemingway, 66532, 45
Joanne, Rowling, 187405, 48
Peter, Gadwick, 80982, 98
Hover, Bens, 12903, 87
Rich, Abend, 93401, 76
Steven, Spielberg, 29016, 65
Chris, Lloyd, 39823, 78
Dean, Martin, 84387, 54
Fred, Walton, 08735, 68
Gustavo, Jimenez, 26542, 34
Herman, Lopez, 84321, 58
Ian, Rumsy, 43452, 24
Klaus, Kelly, 50876, 23
Lindsay, York, 74328, 36
Marvin, Venders, 66532, 45
Norman, Adonan, 10178, 48
Osvald, Oviedo, 40982, 98
Paul, Hindemith, 22903, 87
Rupert, Nelson, 83401, 76
Sven, Gregster, 88016, 65
Tony, Conguelo, 87286, 78
Udnan, Kashimi, 44387, 54
Vern, Snider, 77735, 68
Walter, Briggs, 99942, 34
Xaver, Unsinn, 53421, 68
Yem, Sok, 12674, 64
Zacharias, Zax, 48763, 53
Abel, Davids, 32432, 36
Bert, Lancaster, 66532, 45
Clive, Ogler, 17405, 48
Dave, Hagman, 75982, 98
Ed, Sagan, 43903, 87
Francis, Speals, 35401, 76
Greg, Nelson, 38016, 65
Herb, Hancock, 49823, 78
Iren, Carry, 13387, 54
Manfred, Vandenburg, 11735, 68