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.awt.GridLayout;
20  import java.util.Collection;
21  
22  import javax.swing.JPanel;
23  import javax.swing.SwingUtilities;
24  
25  import nl.tudelft.goal.ut2004.visualizer.controller.ServerController;
26  import nl.tudelft.goal.ut2004.visualizer.data.GameData;
27  import nl.tudelft.goal.ut2004.visualizer.util.CollectionEventAdaptor;
28  import nl.tudelft.goal.ut2004.visualizer.util.SelectableMapList;
29  
30  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.MapList;
31  import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
32  import cz.cuni.amis.utils.collections.ObservableCollection;
33  import cz.cuni.amis.utils.flag.FlagListener;
34  
35  /**
36   * Smart Map selection box.
37   * 
38   * @author M.P. Korstanje
39   * 
40   */
41  
42  public class MapBox extends JPanel {
43  
44  	private final SuggestionField suggestionField;
45  	private final Listener listener;
46  	private final ObservableCollection<MapList> maps;
47  	private final SuggestionModel suggestionModel;
48  
49  	public MapBox() {
50  		this.setLayout(new GridLayout(1, 1));
51  		this.listener = new Listener();
52  		this.suggestionModel = new SuggestionModel();
53  		this.suggestionField = new SuggestionField(suggestionModel);
54  		this.add(suggestionField);
55  
56  		ServerController controller = ServerController.getInstance();
57  		GameData data = controller.getGameData();
58  		maps = data.getAvailableMaps();
59  		maps.addCollectionListener(listener);
60  		addAll(maps);
61  
62  		controller.getServerDefinition().getServerFlag()
63  				.addListener(new FlagListener<IUT2004Server>() {
64  
65  					@Override
66  					public void flagChanged(IUT2004Server changedValue) {
67  						if (changedValue != null) {
68  							suggestionField.setSelectedItem(changedValue
69  									.getMapName());
70  						}
71  					}
72  				});
73  
74  		IUT2004Server server = controller.getServer();
75  		if (server != null) {
76  			suggestionField.setSelectedItem(server.getMapName());
77  		}
78  	}
79  
80  	/**
81  	 * Returns the way point selected by this combo box.
82  	 * 
83  	 * @return the selected waypoint or null if none.
84  	 */
85  	public MapList getSelected() {
86  		SelectableMapList sw = (SelectableMapList) suggestionModel
87  				.getSelectedItem();
88  		if (sw != null) {
89  			return sw.getMapList();
90  		}
91  
92  		return null;
93  	}
94  
95  	public void setSelected(MapList item) {
96  		suggestionField.setSelectedItem(item);
97  	}
98  
99  	/**
100 	 * Adds all ways points in a representable form to the list model.
101 	 * 
102 	 * @param maps
103 	 */
104 	private void addAll(Collection<MapList> maps) {
105 		synchronized (maps) {
106 			for (MapList w : maps) {
107 				suggestionModel.addElement(new SelectableMapList(w));
108 			}
109 		}
110 	}
111 
112 	/**
113 	 * Removes all ways points in a representable form to the list model.
114 	 * 
115 	 * @param maps
116 	 */
117 	private void removeAll(Collection<MapList> maps) {
118 		synchronized (maps) {
119 			for (MapList w : maps) {
120 				suggestionModel.removeElement(new SelectableMapList(w));
121 			}
122 		}
123 	}
124 
125 	/**
126 	 * Called when this panel will no longer be used. Useful for clean up.
127 	 * 
128 	 * TODO: Currently only exists to accommodate the {@link WaypointBox} which
129 	 * has no meaningfull way to detect when it is diposed and should remove
130 	 * it's listeners. The {@link ObservableCollection} should use weak
131 	 * references.
132 	 * 
133 	 */
134 	public void dispose() {
135 		maps.removeCollectionListener(listener);
136 	}
137 
138 	private class Listener extends CollectionEventAdaptor<MapList> {
139 
140 		@Override
141 		public void postRemoveEvent(final Collection<MapList> alreadyRemoved,
142 				Collection<MapList> whereWereRemoved) {
143 			SwingUtilities.invokeLater(new Runnable() {
144 
145 				@Override
146 				public void run() {
147 					removeAll(alreadyRemoved);
148 
149 				}
150 			});
151 		}
152 
153 		@Override
154 		public void postAddEvent(final Collection<MapList> alreadyAdded,
155 				Collection<MapList> whereWereAdded) {
156 			SwingUtilities.invokeLater(new Runnable() {
157 
158 				@Override
159 				public void run() {
160 					addAll(alreadyAdded);
161 
162 				}
163 			});
164 		}
165 
166 	}
167 
168 }