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