View Javadoc

1   /*
2    * Copyright (C) 2010 Unreal Visualizer Authors
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package nl.tudelft.goal.ut2004.visualizer.gui.widgets;
18  
19  import java.util.Collection;
20  
21  import javax.swing.JPanel;
22  import javax.swing.SwingUtilities;
23  
24  import nl.tudelft.goal.ut2004.visualizer.controller.ServerController;
25  import nl.tudelft.goal.ut2004.visualizer.data.GameData;
26  import nl.tudelft.goal.ut2004.visualizer.util.CollectionEventAdaptor;
27  import nl.tudelft.goal.ut2004.visualizer.util.SelectableWayPoint;
28  
29  import cz.cuni.amis.pogamut.ut2004.communication.worldview.map.Waypoint;
30  import cz.cuni.amis.utils.collections.ObservableCollection;
31  
32  /**
33   * Smart Navpoint selection box. Can only be used when there is a connection.
34   * 
35   * @author M.P. Korstanje
36   * 
37   */
38  public class WaypointBox extends JPanel {
39  
40  	private final SuggestionField suggestionField;
41  	private final Listener listener;
42  	private final ObservableCollection<Waypoint> waypoints;
43  	private final SuggestionModel suggestionModel;
44  
45  	public WaypointBox() {
46  
47  		this.listener = new Listener();
48  		this.suggestionModel = new SuggestionModel();
49  		this.suggestionField = new SuggestionField(suggestionModel);
50  		this.add(suggestionField);
51  
52  		ServerController controller = ServerController.getInstance();
53  		GameData data = controller.getGameData();
54  		waypoints = data.getWaypoints();
55  		waypoints.addCollectionListener(listener);
56  		addAll(waypoints);
57  
58  	}
59  
60  	/**
61  	 * Returns the way point selected by this combo box.
62  	 * 
63  	 * @return the selected waypoint or null if none.
64  	 */
65  	public Waypoint getSelected() {
66  		SelectableWayPoint sw = (SelectableWayPoint) suggestionModel
67  				.getSelectedItem();
68  		if (sw != null) {
69  			return sw.getWaypoint();
70  		}
71  
72  		return null;
73  	}
74  
75  	public void setSelected(Object item) {
76  		if (item instanceof Waypoint) {
77  			item = new SelectableWayPoint((Waypoint) item);
78  		}
79  		suggestionField.setSelectedItem(item);
80  	}
81  
82  	/**
83  	 * Adds all ways points in a representable form to the list model.
84  	 * 
85  	 * @param waypoints
86  	 */
87  	private void addAll(Collection<Waypoint> waypoints) {
88  		synchronized (waypoints) {
89  			for (Waypoint w : waypoints) {
90  				suggestionModel.addElement(new SelectableWayPoint(w));
91  			}
92  		}
93  	}
94  
95  	private void removeAll(Collection<Waypoint> waypoints) {
96  		synchronized (waypoints) {
97  			for (Waypoint w : waypoints) {
98  				suggestionModel.removeElement(new SelectableWayPoint(w));
99  			}
100 		}
101 	}
102 
103 	/**
104 	 * Called when this panel will no longer be used. Useful for clean up.
105 	 * 
106 	 * TODO: Currently only exists to accommodate the {@link WaypointBox} which
107 	 * has no meaningfull way to detect when it is diposed and should remove
108 	 * it's listeners. The {@link ObservableCollection} should use weak
109 	 * references.
110 	 * 
111 	 */
112 	public void dispose() {
113 		waypoints.removeCollectionListener(listener);
114 	}
115 
116 	private class Listener extends CollectionEventAdaptor<Waypoint> {
117 
118 		@Override
119 		public void postRemoveEvent(final Collection<Waypoint> alreadyRemoved,
120 				Collection<Waypoint> whereWereRemoved) {
121 			SwingUtilities.invokeLater(new Runnable() {
122 
123 				@Override
124 				public void run() {
125 					removeAll(alreadyRemoved);
126 				}
127 
128 			});
129 		}
130 
131 		@Override
132 		public void postAddEvent(final Collection<Waypoint> alreadyAdded,
133 				Collection<Waypoint> whereWereAdded) {
134 			SwingUtilities.invokeLater(new Runnable() {
135 
136 				@Override
137 				public void run() {
138 					addAll(alreadyAdded);
139 				}
140 			});
141 		}
142 
143 	}
144 }