View Javadoc

1   package org.molwind.view;
2   
3   /*
4    * This file is part of Molwind.
5    *
6    * Molwind is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Molwind is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Molwind. If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  import java.io.IOException;
21  import java.io.OutputStream;
22  import java.util.Hashtable;
23  
24  import org.molwind.io.MolwindLocator;
25  import org.molwind.model.LayeredPosition;
26  import org.molwind.model.MolwindWorld;
27  import org.molwind.util.MolwindLogger;
28  
29  /**
30   * Tiles generates and manages the image tiles served to the client.
31   *
32   * @author <a href="mailto:oliver.karch@molwind.org">Oliver Karch</a>
33   * @version 1.0
34   */
35  public class Tiles {
36  
37      private String worldName;
38      private MolwindWorld world;
39      private Hashtable tileStore;
40  
41      private static Hashtable worldStore;
42  
43  
44      private Tiles(final String newWorldName, final MolwindWorld newWorld) {
45          tileStore = new Hashtable();
46          this.worldName = newWorldName;
47          this.world = newWorld;
48      }
49  
50  
51      /**
52       * Returns an instance to the tiles storage which has been associated to the
53       * given world.
54       *
55       * @param worldName
56       *      the name of the world
57       * @return
58       *      a tiles manager
59       * @throws java.io.IOException
60       *      is thrown upon i/o error
61       */
62      public static synchronized Tiles getInstance(final String worldName)
63      throws IOException {
64          if (worldStore == null) {
65              worldStore = new Hashtable();
66          }
67  
68          Tiles tiles = (Tiles) worldStore.get(worldName);
69  
70          if (tiles == null) {
71              MolwindWorld world = MolwindLocator.getLocator()
72                      .locateWorld(worldName);
73  
74              if (world != null) {
75                  tiles = new Tiles(worldName, world);
76                  worldStore.put(worldName, tiles);
77                  MolwindLogger.debug("New tiles store created for: "
78                          + worldName);
79              }
80          }
81  
82          return tiles;
83      }
84  
85      /**
86       * Get the WorldName value.
87       *
88       * @return
89       *      the WorldName value
90       */
91      public String getWorldName() {
92          return worldName;
93      }
94  
95      /**
96       * Set the WorldName value.
97       *
98       * @param newWorldName
99       *      the new WorldName value
100      */
101     public void setWorldName(final String newWorldName) {
102         this.worldName = newWorldName;
103     }
104 
105     /**
106      * Get the World value.
107      *
108      * @return
109      *      the World value
110      */
111     public MolwindWorld getWorld() {
112         return world;
113     }
114 
115     /**
116      * Set the World value.
117      *
118      * @param newWorld
119      *      the new World value
120      */
121     public void setWorld(final MolwindWorld newWorld) {
122         this.world = newWorld;
123     }
124 
125     /**
126      * Returns a tile from the tile cache for the given position.
127      *
128      * @param position
129      *      the layered position (X,Y,L)
130      * @return
131      *      if present in the cache the tile is returned, otherwise null
132      */
133     public Tile getTile(final LayeredPosition position) {
134         return (Tile) tileStore.get(position);
135     }
136 
137     /**
138      * Adds a tile to the tile cache.
139      *
140      * @param position
141      *      the layered position (X,Y,L)
142      * @param tile
143      *      the tile to be stored
144      */
145     public void addTile(final LayeredPosition position, final Tile tile) {
146         tileStore.put(position, tile);
147     }
148 
149     /**
150      * Writes the image of a tile for the given position to an output stream.
151      *
152      * @param position
153      *      the position (X,Y,L)
154      * @param output
155      *      the output stream
156      * @throws java.io.IOException
157      *      is thrown upon i/o error
158      */
159     public void writeTile(final LayeredPosition position,
160             final OutputStream output)
161     throws IOException {
162         Tile tile = getTile(position);
163 
164         if (tile == null) {
165             MolwindLogger.debug("Generating tile " + position);
166 	    // TileGenerator generator = (TileGenerator) world.getTileGenerator();
167 	     TileGenerator generator = world.getTileGenerator();
168             try {
169                 tile = generator.generateTile(position);
170             } catch (TileException te) {
171                 throw new IOException(te.getMessage());
172             }
173 
174             addTile(position, tile);
175             MolwindLogger.debug("Tile generated " + tile);
176         }
177 
178         tile.writeTo(output);
179     }
180 
181 }