View Javadoc

1   package nl.tudelft.goal.ut2004.visualizer.gui.widgets;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Collections;
6   import java.util.LinkedList;
7   import java.util.List;
8   import java.util.TreeMap;
9   
10  import javax.swing.ComboBoxModel;
11  import javax.swing.event.ListDataEvent;
12  import javax.swing.event.ListDataListener;
13  
14  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.MapList;
15  
16  public final class SuggestionModel implements ComboBoxModel {
17  
18  	private final TreeMap<String, Object> map;
19  	private final List<String> sortedKeys;
20  
21  	public SuggestionModel() {
22  		this.map = new TreeMap<String, Object>();
23  		this.sortedKeys = new ArrayList<String>();
24  		this.listeners = new LinkedList<ListDataListener>();
25  	}
26  
27  	public void addElement(Object element) {
28  		map.put(element.toString(), element);
29  		sortedKeys.add(element.toString());
30  		// TODO: Optimize.
31  		Collections.sort(sortedKeys);
32  		modelChanged();
33  	}
34  
35  	private void modelChanged() {
36  		setSelectedItem(selected);
37  		notifyListeners();
38  	}
39  
40  	public void removeElement(Object element) {
41  		map.remove(element.toString());
42  		sortedKeys.remove(element.toString());
43  		Collections.sort(sortedKeys);
44  		modelChanged();
45  	}
46  
47  	private void notifyListeners() {
48  		for (ListDataListener l : listeners) {
49  			l.contentsChanged(new ListDataEvent(this,
50  					ListDataEvent.CONTENTS_CHANGED, 0, getSize()));
51  		}
52  	}
53  
54  	public List<String> sugestFor(String query) {
55  
56  		String lowerCaseQuery = query.toLowerCase();
57  
58  		// Find new suggestions
59  		List<String> suggestion = new LinkedList<String>();
60  		for (String key : map.keySet()) {
61  			if (key.toLowerCase().contains(lowerCaseQuery)) {
62  				System.out.println("Found " + key);
63  				suggestion.add(key);
64  			}
65  		}
66  
67  		sortedKeys.clear();
68  		sortedKeys.addAll(suggestion);
69  
70  		// Don't notify listeners.
71  		// This will unset the selected text.
72  		// notifyListeners();
73  
74  		return suggestion;
75  	}
76  
77  	@Override
78  	public Object getElementAt(int index) {
79  		return map.get(sortedKeys.get(index));
80  	}
81  
82  	@Override
83  	public int getSize() {
84  		return sortedKeys.size();
85  	}
86  
87  	private final List<ListDataListener> listeners;
88  
89  	@Override
90  	public void removeListDataListener(ListDataListener l) {
91  		listeners.remove(l);
92  	}
93  
94  	@Override
95  	public void addListDataListener(ListDataListener l) {
96  		listeners.add(l);
97  	}
98  
99  	private Object selected;
100 
101 	@Override
102 	public Object getSelectedItem() {
103 		return selected;
104 	}
105 
106 	@Override
107 	public void setSelectedItem(Object anItem) {
108 		if (anItem != null) {
109 			selected = map.get(anItem.toString());
110 		} else {
111 			selected = anItem;
112 		}
113 	}
114 
115 	public Object getItem(String key) {
116 		return map.get(key);
117 	}
118 
119 }