Revision 7513 branches/v10/frameworks/_fwAndami/src/com/iver/andami/Utilities.java
Utilities.java | ||
---|---|---|
83 | 83 |
/** DOCUMENT ME! */ |
84 | 84 |
private static Logger logger = Logger.getLogger(Utilities.class.getName()); |
85 | 85 |
private static final String tempDirectoryPath = System.getProperty("java.io.tmpdir")+"/tmp-andami"; |
86 |
|
|
87 | 86 |
|
87 |
|
|
88 | 88 |
/** |
89 | 89 |
* Crea un icono a partir del path de una imagen |
90 | 90 |
* |
... | ... | |
116 | 116 |
public static void cleanComponent(Component baseComponent) { |
117 | 117 |
try { |
118 | 118 |
cleanComponent(baseComponent, 0); |
119 |
} catch (Exception ignore) { // give some exception handling...
|
|
119 |
} catch (Exception ignore) { // give some exception handling... |
|
120 | 120 |
} |
121 | 121 |
} |
122 | 122 |
|
123 | 123 |
/* * The "depth" parameter was being used for text output debugging. * But isn't essential now. I'll keep it anyways, as it avoids * calling the garbage collector every recursion. */ |
124 | 124 |
protected static void cleanComponent(Component baseComponent, int depth) { |
125 |
if (baseComponent == null) // recursion terminating clause
|
|
125 |
if (baseComponent == null) // recursion terminating clause |
|
126 | 126 |
{ |
127 | 127 |
return; |
128 | 128 |
} |
129 |
|
|
129 |
|
|
130 | 130 |
if (baseComponent instanceof IWindow){ |
131 | 131 |
return; |
132 | 132 |
} |
... | ... | |
135 | 135 |
Component[] childComponents; |
136 | 136 |
int numChildren; // clean up component containers |
137 | 137 |
|
138 |
if (baseComponent instanceof Container) { // now clean up container instance variables
|
|
138 |
if (baseComponent instanceof Container) { // now clean up container instance variables |
|
139 | 139 |
|
140 |
if (baseComponent instanceof RootPaneContainer) { // Swing specialised container
|
|
140 |
if (baseComponent instanceof RootPaneContainer) { // Swing specialised container |
|
141 | 141 |
cont = (Container) baseComponent; |
142 | 142 |
numChildren = cont.getComponentCount(); |
143 | 143 |
childComponents = cont.getComponents(); |
144 | 144 |
|
145 |
for (int i = 0; i < numChildren; i++) { // remove each component from the current container
|
|
145 |
for (int i = 0; i < numChildren; i++) { // remove each component from the current container |
|
146 | 146 |
|
147 |
// each child component may be a container itself
|
|
147 |
// each child component may be a container itself |
|
148 | 148 |
cleanComponent(childComponents[i], depth + 1); |
149 | 149 |
((RootPaneContainer) cont).getContentPane().remove(childComponents[i]); |
150 | 150 |
} |
151 | 151 |
|
152 | 152 |
((RootPaneContainer) cont).getContentPane().setLayout(null); |
153 |
} else { // General Swing, and AWT, Containers
|
|
153 |
} else { // General Swing, and AWT, Containers |
|
154 | 154 |
cont = (Container) baseComponent; |
155 | 155 |
numChildren = cont.getComponentCount(); |
156 | 156 |
childComponents = cont.getComponents(); |
157 | 157 |
|
158 |
for (int i = 0; i < numChildren; i++) //for(int i = 0;i < numChildren;i++)
|
|
158 |
for (int i = 0; i < numChildren; i++) //for(int i = 0;i < numChildren;i++) |
|
159 | 159 |
{ |
160 |
// remove each component from the current container // each child component may be a container itself
|
|
160 |
// remove each component from the current container // each child component may be a container itself |
|
161 | 161 |
cleanComponent(childComponents[i], depth + 1); |
162 | 162 |
cont.remove(childComponents[i]); |
163 | 163 |
} |
... | ... | |
166 | 166 |
} |
167 | 167 |
} |
168 | 168 |
|
169 |
// if component is also a container
|
|
169 |
// if component is also a container |
|
170 | 170 |
} |
171 | 171 |
|
172 |
|
|
173 |
|
|
172 | 174 |
/** |
173 | 175 |
* Extrae un fichero zip en un directorio |
174 | 176 |
* |
... | ... | |
179 | 181 |
* @throws IOException Si se produce un error de entrada salida gen?rico |
180 | 182 |
*/ |
181 | 183 |
public static void extractTo(File file, File dir, SplashWindow splash) |
182 |
throws ZipException, IOException {
|
|
183 |
ZipFile zip = new ZipFile(file);
|
|
184 |
Enumeration e = zip.entries();
|
|
184 |
throws ZipException, IOException {
|
|
185 |
ZipFile zip = new ZipFile(file);
|
|
186 |
Enumeration e = zip.entries();
|
|
185 | 187 |
|
186 |
while (e.hasMoreElements()) { |
|
187 |
ZipEntry entry = (ZipEntry) e.nextElement(); |
|
188 |
System.out.println("Procesando " + entry.getName() + "..."); |
|
189 |
splash.process(30, "Procesando " + entry.getName() + "..."); |
|
190 |
if (entry.isDirectory()) { |
|
191 |
new File(dir.getAbsolutePath() + File.separator + |
|
192 |
entry.getName()).mkdir(); |
|
193 |
} else { |
|
194 |
InputStream in = zip.getInputStream(entry); |
|
195 |
OutputStream out = new FileOutputStream(dir + File.separator + |
|
196 |
entry.getName()); |
|
197 |
BufferedInputStream bin = new BufferedInputStream(in); |
|
198 |
BufferedOutputStream bout = new BufferedOutputStream(out); |
|
188 |
// Pasada para crear las carpetas |
|
189 |
while (e.hasMoreElements()) { |
|
190 |
ZipEntry entry = (ZipEntry) e.nextElement(); |
|
199 | 191 |
|
200 |
int i; |
|
192 |
if (entry.isDirectory()) { |
|
193 |
File directorio = new File(dir.getAbsolutePath() |
|
194 |
+ File.separator + entry.getName()); |
|
201 | 195 |
|
202 |
while ((i = bin.read()) != -1) { |
|
203 |
bout.write(i); |
|
204 |
} |
|
196 |
directorio.mkdirs(); |
|
197 |
} |
|
205 | 198 |
|
206 |
bout.flush(); |
|
207 |
bout.close(); |
|
208 |
bin.close(); |
|
209 |
} |
|
210 |
} |
|
211 |
} |
|
212 |
|
|
199 |
} |
|
200 |
|
|
201 |
// Pasada para crear los ficheros |
|
202 |
e = zip.entries(); |
|
203 |
while (e.hasMoreElements()) { |
|
204 |
ZipEntry entry = (ZipEntry) e.nextElement(); |
|
205 |
splash.process(30, "Procesando " + entry.getName() + "..."); |
|
206 |
if (!entry.isDirectory()) { |
|
207 |
InputStream in = zip.getInputStream(entry); |
|
208 |
OutputStream out = new FileOutputStream(dir + File.separator |
|
209 |
+ entry.getName()); |
|
210 |
BufferedInputStream bin = new BufferedInputStream(in); |
|
211 |
BufferedOutputStream bout = new BufferedOutputStream(out); |
|
212 |
|
|
213 |
int i; |
|
214 |
|
|
215 |
while ((i = bin.read()) != -1) { |
|
216 |
bout.write(i); |
|
217 |
} |
|
218 |
|
|
219 |
bout.flush(); |
|
220 |
bout.close(); |
|
221 |
bin.close(); |
|
222 |
|
|
223 |
} |
|
224 |
|
|
225 |
} |
|
226 |
|
|
227 |
zip.close(); |
|
228 |
zip = null; |
|
229 |
System.gc(); |
|
230 |
|
|
231 |
} |
|
232 |
|
|
213 | 233 |
/** |
214 | 234 |
* Returns the content of this URL as a file from the file system.<br> |
215 | 235 |
* <p> |
216 |
* If the URL has been already downloaded in this session and notified
|
|
236 |
* If the URL has been already downloaded in this session and notified |
|
217 | 237 |
* to the system using the static <b>Utilities.addDownloadedURL(URL)</b> |
218 | 238 |
* method, it can be restored faster from the file system avoiding to |
219 | 239 |
* download it again. |
... | ... | |
229 | 249 |
} |
230 | 250 |
return f; |
231 | 251 |
} |
232 |
|
|
252 |
|
|
233 | 253 |
/** |
234 | 254 |
* Adds an URL to the table of downloaded files for further uses. If the URL |
235 | 255 |
* already exists in the table its filePath value is updated to the new one and |
236 | 256 |
* the old file itself is removed from the file system. |
237 |
*
|
|
257 |
* |
|
238 | 258 |
* @param url |
239 | 259 |
* @param filePath |
240 | 260 |
*/ |
... | ... | |
252 | 272 |
} |
253 | 273 |
*/ |
254 | 274 |
} |
255 |
|
|
275 |
|
|
256 | 276 |
/** |
257 |
* Downloads an URL into a temporary file that is removed the next time the
|
|
277 |
* Downloads an URL into a temporary file that is removed the next time the |
|
258 | 278 |
* tempFileManager class is called, which means the next time gvSIG is launched. |
259 |
*
|
|
279 |
* |
|
260 | 280 |
* @param url |
261 | 281 |
* @param name |
262 | 282 |
* @return |
... | ... | |
273 | 293 |
File tempDirectory = new File(tempDirectoryPath); |
274 | 294 |
if (!tempDirectory.exists()) |
275 | 295 |
tempDirectory.mkdir(); |
276 |
|
|
296 |
|
|
277 | 297 |
f = new File(tempDirectoryPath+"/"+name+System.currentTimeMillis()); |
278 |
|
|
298 |
|
|
279 | 299 |
System.out.println("downloading '"+url.toString()+"' to: "+f.getAbsolutePath()); |
280 |
|
|
300 |
|
|
281 | 301 |
f.deleteOnExit(); |
282 | 302 |
DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f))); |
283 | 303 |
byte[] buffer = new byte[1024*256]; |
... | ... | |
293 | 313 |
} catch (IOException io) { |
294 | 314 |
io.printStackTrace(); |
295 | 315 |
} |
296 |
|
|
316 |
|
|
297 | 317 |
return f; |
298 | 318 |
} |
299 | 319 |
|
... | ... | |
303 | 323 |
public static void cleanUpTempFiles() { |
304 | 324 |
try{ |
305 | 325 |
File tempDirectory = new File(tempDirectoryPath); |
306 |
|
|
326 |
|
|
307 | 327 |
File[] files = tempDirectory.listFiles(); |
308 | 328 |
if (files!=null) { |
309 | 329 |
for (int i = 0; i < files.length; i++) { |
... | ... | |
314 | 334 |
} |
315 | 335 |
tempDirectory.delete(); |
316 | 336 |
} catch (Exception e) { } |
317 |
|
|
337 |
|
|
318 | 338 |
} |
319 | 339 |
/** |
320 | 340 |
* Recursive directory delete. |
... | ... | |
326 | 346 |
if (files[i].isDirectory()) deleteDirectory(files[i]); |
327 | 347 |
files[i].delete(); |
328 | 348 |
} |
329 |
|
|
349 |
|
|
330 | 350 |
} |
331 |
|
|
351 |
|
|
332 | 352 |
/** |
333 | 353 |
* Crea un fichero temporal con un nombre concreto y unos datos pasados por |
334 | 354 |
* par?metro. |
... | ... | |
342 | 362 |
dos.close(); |
343 | 363 |
f.deleteOnExit(); |
344 | 364 |
} |
345 |
|
|
365 |
|
|
346 | 366 |
/** |
347 | 367 |
* Remove an URL from the system cache. The file will remain in the file |
348 | 368 |
* system for further eventual uses. |
Also available in: Unified diff