]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2008, 2011 IBM Corporation and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * IBM Corporation - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.ui.viewsupport; | |
12 | ||
13 | import java.io.File; | |
14 | import java.net.MalformedURLException; | |
15 | import java.net.URL; | |
16 | import java.util.HashMap; | |
17 | ||
18 | import org.eclipse.swt.SWT; | |
19 | import org.eclipse.swt.graphics.ImageData; | |
20 | import org.eclipse.swt.graphics.ImageLoader; | |
21 | ||
22 | import org.eclipse.jface.resource.ImageDescriptor; | |
23 | ||
24 | import org.eclipse.jdt.core.IJavaElement; | |
25 | ||
26 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
27 | ||
28 | ||
29 | /** | |
30 | * Image registry that keeps its images on the local file system. | |
31 | * | |
32 | * @since 3.4 | |
33 | */ | |
34 | public class ImagesOnFileSystemRegistry { | |
35 | ||
36 | private static final String IMAGE_DIR= "jdt-images"; //$NON-NLS-1$ | |
37 | ||
38 | private HashMap<ImageDescriptor, URL> fURLMap; | |
39 | private final File fTempDir; | |
40 | private final JavaElementImageProvider fImageProvider; | |
41 | private int fImageCount; | |
42 | ||
43 | public ImagesOnFileSystemRegistry() { | |
44 | fURLMap= new HashMap<ImageDescriptor, URL>(); | |
45 | fTempDir= getTempDir(); | |
46 | fImageProvider= new JavaElementImageProvider(); | |
47 | fImageCount= 0; | |
48 | } | |
49 | ||
50 | private File getTempDir() { | |
51 | try { | |
52 | File imageDir= JavaPlugin.getDefault().getStateLocation().append(IMAGE_DIR).toFile(); | |
53 | if (imageDir.exists()) { | |
54 | // has not been deleted on previous shutdown | |
55 | delete(imageDir); | |
56 | } | |
57 | if (!imageDir.exists()) { | |
58 | imageDir.mkdir(); | |
59 | } | |
60 | if (!imageDir.isDirectory()) { | |
61 | JavaPlugin.logErrorMessage("Failed to create image directory " + imageDir.toString()); //$NON-NLS-1$ | |
62 | return null; | |
63 | } | |
64 | return imageDir; | |
65 | } catch (IllegalStateException e) { | |
66 | // no state location | |
67 | return null; | |
68 | } | |
69 | } | |
70 | ||
71 | private void delete(File file) { | |
72 | if (file.isDirectory()) { | |
73 | File[] listFiles= file.listFiles(); | |
74 | for (int i= 0; i < listFiles.length; i++) { | |
75 | delete(listFiles[i]); | |
76 | } | |
77 | } | |
78 | file.delete(); | |
79 | } | |
80 | ||
81 | public URL getImageURL(IJavaElement element) { | |
82 | ImageDescriptor descriptor= fImageProvider.getJavaImageDescriptor(element, JavaElementImageProvider.OVERLAY_ICONS | JavaElementImageProvider.SMALL_ICONS); | |
83 | if (descriptor == null) | |
84 | return null; | |
85 | return getImageURL(descriptor); | |
86 | } | |
87 | ||
88 | public URL getImageURL(ImageDescriptor descriptor) { | |
89 | if (fTempDir == null) | |
90 | return null; | |
91 | ||
92 | URL url= fURLMap.get(descriptor); | |
93 | if (url != null) | |
94 | return url; | |
95 | ||
96 | File imageFile= getNewFile(); | |
97 | ImageData imageData= descriptor.getImageData(); | |
98 | if (imageData == null) { | |
99 | return null; | |
100 | } | |
101 | ||
102 | ImageLoader loader= new ImageLoader(); | |
103 | loader.data= new ImageData[] { imageData }; | |
104 | loader.save(imageFile.getAbsolutePath(), SWT.IMAGE_PNG); | |
105 | ||
106 | try { | |
107 | url= imageFile.toURI().toURL(); | |
108 | fURLMap.put(descriptor, url); | |
109 | return url; | |
110 | } catch (MalformedURLException e) { | |
111 | JavaPlugin.log(e); | |
112 | } | |
113 | return null; | |
114 | } | |
115 | ||
116 | private File getNewFile() { | |
117 | File file; | |
118 | do { | |
119 | file= new File(fTempDir, String.valueOf(getImageCount()) + ".png"); //$NON-NLS-1$ | |
120 | } while (file.exists()); | |
121 | return file; | |
122 | } | |
123 | ||
124 | private synchronized int getImageCount() { | |
125 | return fImageCount++; | |
126 | } | |
127 | ||
128 | public void dispose() { | |
129 | if (fTempDir != null) { | |
130 | delete(fTempDir); | |
131 | } | |
132 | fURLMap= null; | |
133 | } | |
134 | } |