Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap_mobile_shp_driver / src-file / org / gvsig / data / datastores / vectorial / file / shp_util / ResourceReader.java @ 22037

History | View | Annotate | Download (8.38 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *   +34 963862235
28
 *   gvsig@gva.es
29
 *   http://www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 *
43
 *    or
44
 *
45
 *   Instituto de Rob?tica
46
 *   Apartado de correos 2085
47
 *   46071 Valencia
48
 *   (Spain)
49
 *   
50
 *   +34 963 543 577
51
 *   jjordan@robotica.uv.es
52
 *   http://robotica.uv.es
53
 *   
54
 */
55

    
56
package org.gvsig.data.datastores.vectorial.file.shp_util;
57

    
58
import java.awt.Container;
59
import java.awt.Image;
60
import java.awt.MediaTracker;
61
import java.awt.Toolkit;
62
import java.io.File;
63

    
64
import org.apache.log4j.Logger;
65

    
66
/**
67
 * Utility class to read resource files.
68
 * 
69
 * @see java.awt.MediaTracker
70
 * 
71
 * @author jldominguez
72
 *
73
 */
74
public class ResourceReader {
75
        
76
        private static final String WORKSPACE = "D:/PROYECTOS/Filtros2/libFMap_data_shape";
77
        
78
        private static Logger logger = Logger.getLogger(ResourceReader.class);
79
        private static String resourceDir = "";
80
        
81
        private static int IMAGE_LOAD_TIMEOUT = 1200;
82
        
83
        /**
84
         * Icon shown while the app is processing.
85
         */
86
        public static Image PROCESSING_ICON;
87
        /**
88
         * Array of GPS icons (16 positions)
89
         */
90
        public static Image[] GPS_CURSOR_ICON;
91
        /**
92
         * number of positions of the GPS cursor
93
         */
94
        public static int GPS_CURSOR_ICON_COUNT = 16;
95
        
96
        public static int PROCESS_ICON_X = 90;
97
        public static int PROCESS_ICON_Y = 105; //110;
98
        public static int PROCESS_ICON_SIZE = 60;
99
        
100
        /**
101
         * Used to read images
102
         */
103
        public static MediaTracker mt = new MediaTracker(new Container());
104
        public static int imageCount = 0;
105

    
106
        static {
107
                
108
                String base = System.getProperty("java.class.path");
109
                
110
                int ind = base.indexOf(File.pathSeparator);
111
                if (ind != -1) base = base.substring(0, ind);
112
                ind = base.lastIndexOf(File.separator);
113
                base = base.substring(0, ind);
114
                //logger.debug("Base directory: " + base);
115
                ind = base.lastIndexOf(File.separator);
116
                resourceDir = base.substring(0, ind) + File.separator + "resources";
117
                PROCESSING_ICON = getResourceImage("img", "processing.png");
118
                
119
                GPS_CURSOR_ICON = new Image[GPS_CURSOR_ICON_COUNT];
120
                for (int i=0; i<GPS_CURSOR_ICON_COUNT; i++) {
121
                        try {
122
                                GPS_CURSOR_ICON[i] = getResourceImage("img", "gps_cursor_" + i + ".png");
123
                        } catch (Exception ex) {
124
                                logger.error("Unable to read GPS icon # " + i + " : " + ex.getMessage());
125
                        }
126
                }
127
        }
128
        
129
        /**
130
         * Gets the GPS icon index  for a given angle
131
         * @param angle heading 
132
         * @param in_radians whether it is radians or degrees
133
         * @return the GPS icon index
134
         */
135
        public static int getGpsIconIndexForAngle(double angle, boolean in_radians) {
136
                
137
                double a_in_degrees = (in_radians) ? (angle * 180.0 / Math.PI) : angle;
138
                
139
                double step = 360.0 / GPS_CURSOR_ICON_COUNT;
140
                
141
                if (Math.abs(a_in_degrees) > 1000) {
142
                        logger.error("Absurd angle: " + angle + "; in_radians = " + in_radians);
143
                        logger.error("Returned North icon (0)");
144
                        return 0;
145
                }
146
                
147
                // normalize
148
                while (step >= 360) step = step - 360.0;
149
                while (step < 0) step = step + 360.0;
150

    
151
                int n = Math.round((float) Math.floor(0.5 + a_in_degrees / step));
152
                
153
                if ((n < 0) || (n >= GPS_CURSOR_ICON_COUNT)) {
154
                        logger.error("Bad n = " + n + "; GPS_CURSOR_ICON_COUNT = " + GPS_CURSOR_ICON_COUNT);
155
                        logger.error("Forced n = n MOD GPS_CURSOR_ICON_COUNT");
156
                        n = n % GPS_CURSOR_ICON_COUNT;
157
                }
158

    
159
                logger.debug("-------------- GPS icon index for icon: -----------------");
160
                logger.debug("angle = " + angle + "; radians = " + in_radians);
161
                logger.debug("returned: " + n);
162
                return n;
163
        }
164
        
165
        /**
166
         * Gets an image from the resources folder 
167
         * @param dir the resources subfolder
168
         * @param file_name the file name
169
         * @return the image from the resources folder
170
         */
171
        public static Image getResourceImage(String dir, String file_name) {
172
                String imgPath = resourceDir + File.separator + dir + File.separator + file_name;
173
                Image img = Toolkit.getDefaultToolkit().createImage(imgPath);
174
                mt.addImage(img,imageCount);
175
                try{
176
                        mt.waitForID(imageCount, IMAGE_LOAD_TIMEOUT);
177
                        mt.removeImage(img);
178
                        return img;
179
                } catch (InterruptedException e) {
180
                        logger.error("While downloading: " + e.getMessage());
181
                        return null;
182
                }
183
                
184
        }
185
        
186
        /**
187
         * Gets an image from an array of bytes for a well known format (png, jpg, gif)
188
         * @param data the byte array 
189
         * @return the image
190
         */
191
        public static Image getImage(byte[] data) {
192
                Image img = Toolkit.getDefaultToolkit().createImage(data);
193
                mt.addImage(img,imageCount);
194
                try {
195
                        mt.waitForID(imageCount, IMAGE_LOAD_TIMEOUT);
196
                        mt.removeImage(img);
197
                        return img;
198
                } catch (InterruptedException e) {
199
                        logger.error("While downloading: " + e.getMessage());
200
                        return null;
201
                }
202
        }
203

    
204
        /**
205
         * Gets scaled version of image with Media Tracker.
206
         * 
207
         * @param img source image
208
         * @param newwidth new width
209
         * @param newheight new height
210
         * @return re sampled image
211
         */
212
        public static Image getScaled(Image img, int newwidth, int newheight) {
213
                
214
                Image newimage = img.getScaledInstance(newwidth, newheight, Image.SCALE_FAST);
215
                mt.addImage(newimage, imageCount);
216
                try {
217
                        mt.waitForID(imageCount, IMAGE_LOAD_TIMEOUT);
218
                        mt.removeImage(newimage);
219
                        return newimage;
220
                } catch (InterruptedException e) {
221
                        logger.error("While resampling: " + e.getMessage());
222
                        return null;
223
                }
224
        }
225
        
226
        /**
227
         * Gets an image from a file in a well known format (png, jpg, gif)
228
         * @param data the image file 
229
         * @return the image object
230
         */
231
        public static Image getImage(File data) {
232
                try {
233
                        logger.debug("Getting image with Media Tracker: " + data.getAbsolutePath());
234
                        logger.debug("Getting image with Media Tracker, imagecount = " + imageCount);
235
                        Image img = Toolkit.getDefaultToolkit().createImage(data.getAbsolutePath());
236
                        mt.addImage(img, imageCount);
237
                        mt.waitForID(imageCount, IMAGE_LOAD_TIMEOUT);
238
                        mt.removeImage(img);
239

    
240
                        return img;
241
                } catch (Throwable th) {
242
                        logger.error("While getting downloaded image: " + th.getMessage());
243
                        return null;
244
                }
245
        }
246

    
247
        /**
248
         * gets a file from the working directory
249
         * @param dir subfolder
250
         * @param file_name file name
251
         * @return the file object
252
         */
253
        public static File getWorkingDirFile(String dir, String file_name) {
254
                File resp = new File(
255
                                WORKSPACE
256
                                + File.separator
257
                                + dir
258
                                + File.separator
259
                                + file_name);
260
                return resp;
261
        }
262
        
263
        /**
264
         * Gets the working subfolder
265
         * @param dir subfolder name
266
         * @return the working subfolder
267
         */
268
        public static File getWorkingDir(String dir) {
269
                File resp = new File(
270
                                WORKSPACE
271
                                + File.separator
272
                                + dir);
273
                return resp;
274
        }
275
        
276
        /**
277
         * Gets a file from the resource folder 
278
         * @param dir
279
         * @param file_name
280
         * @return the file object
281
         */
282
        public static File getResourceFile(String dir, String file_name) {
283
                String filePath = resourceDir + File.separator + dir + File.separator + file_name;
284
                File resp = new File(filePath);
285
                return resp;
286
        }
287
        
288
        /**
289
         * Gets a file from the pda sd card folder 
290
         * @param dir
291
         * @param file_name
292
         * @return the file object
293
         */
294
        public static File getPdaTestFile(String sdfolder, String dir, String file_name) {
295
                String filePath = File.separator + sdfolder + File.separator + dir + File.separator + file_name;
296
                File resp = new File(filePath);
297
                return resp;
298
        }
299
        
300
        
301
        /**
302
         * gets a subfolder from the resources folder
303
         * @param dir the subfolder name
304
         * @return the file object
305
         */
306
        public static File getResourceFolder(String dir) {
307
                String filePath = resourceDir + File.separator + dir;
308
                File resp = new File(filePath);
309
                return resp;
310
        }
311
}