41
40
import org.gudy.azureus2.core3.util.Debug;
42
41
import org.gudy.azureus2.core3.util.FileUtil;
43
import com.aelitis.azureus.ui.skin.SkinProperties;
44
import com.aelitis.azureus.ui.swt.imageloader.ImageLoader;
48
public class ImageRepository {
49
private static boolean NO_IMAGES = false;
51
private static Display display;
52
private static final HashMap imagesToPath;
53
private static final HashMap images;
54
private static final HashMap registry;
55
private static final String[] noCacheExtList = new String[] {".exe"};
56
private static final boolean forceNoAWT = Constants.isOSX;
59
images = new HashMap(150);
60
imagesToPath = new HashMap(150);
61
registry = new HashMap();
64
public static void loadImagesForSplashWindow(Display display) {
65
ImageRepository.display = display;
66
addPath("org/gudy/azureus2/ui/icons/a16.png", "azureus");
67
addPath("org/gudy/azureus2/ui/icons/a32.png", "azureus32");
68
addPath("org/gudy/azureus2/ui/icons/a64.png", "azureus64");
69
addPath("org/gudy/azureus2/ui/icons/a128.jpg", "azureus128");
70
addPath("org/gudy/azureus2/ui/splash/splash_frog.jpg", "azureus_splash");
73
public static void loadImages(Display display) {
77
addPath("org/gudy/azureus2/ui/icons/az3_osx_tray_white.png", "azureus_white");
78
addPath("org/gudy/azureus2/ui/icons/az3_osx_tray.png", "azureus_grey");
80
addPath("org/gudy/azureus2/ui/icons/Azureus_big.png", "tray");
81
addPath("org/gudy/azureus2/ui/icons/dragger.gif", "dragger");
82
addPath("org/gudy/azureus2/ui/icons/folder.gif", "folder");
83
addPath("org/gudy/azureus2/ui/icons/ipfilter.png", "ipfilter");
84
addPath("org/gudy/azureus2/ui/icons/start.gif", "start");
85
addPath("org/gudy/azureus2/ui/icons/stop.gif", "stop");
86
addPath("org/gudy/azureus2/ui/icons/bar.gif", "downloadBar");
87
addPath("org/gudy/azureus2/ui/icons/delete.gif", "delete");
88
addPath("org/gudy/azureus2/ui/icons/lock.gif", "lock");
89
addPath("org/gudy/azureus2/ui/icons/host.gif", "host");
90
addPath("org/gudy/azureus2/ui/icons/publish.gif", "publish");
91
addPath("org/gudy/azureus2/ui/icons/run.gif", "run");
92
addPath("org/gudy/azureus2/ui/icons/details.gif", "details");
93
addPath("org/gudy/azureus2/ui/icons/up.png", "up");
94
addPath("org/gudy/azureus2/ui/icons/down.png", "down");
95
addPath("org/gudy/azureus2/ui/icons/top.gif", "top");
96
addPath("org/gudy/azureus2/ui/icons/bottom.gif", "bottom");
97
addPath("org/gudy/azureus2/ui/icons/recheck.gif", "recheck");
98
addPath("org/gudy/azureus2/ui/icons/export.gif", "export");
99
addPath("org/gudy/azureus2/ui/icons/move.gif", "move");
100
addPath("org/gudy/azureus2/ui/icons/add_tracker.gif", "add_tracker");
101
addPath("org/gudy/azureus2/ui/icons/edit_trackers.gif", "edit_trackers");
102
addPath("org/gudy/azureus2/ui/icons/columns.gif", "columns");
103
addPath("org/gudy/azureus2/ui/icons/speed.gif", "speed");
104
addPath("org/gudy/azureus2/ui/icons/openFolder16x12.gif","openFolderButton");
105
addPath("org/gudy/azureus2/ui/icons/forcestart.gif", "forcestart");
106
addPath("org/gudy/azureus2/ui/icons/greenled.gif", "greenled");
107
addPath("org/gudy/azureus2/ui/icons/redled.gif", "redled");
108
addPath("org/gudy/azureus2/ui/icons/yellowled.gif", "yellowled");
109
addPath("org/gudy/azureus2/ui/icons/grayled.gif", "grayled");
110
addPath("org/gudy/azureus2/ui/icons/popup.png", "popup");
111
addPath("org/gudy/azureus2/ui/icons/error.gif", "error");
112
addPath("org/gudy/azureus2/ui/icons/info.gif", "info");
113
addPath("org/gudy/azureus2/ui/icons/warning.gif", "warning");
114
addPath("org/gudy/azureus2/ui/icons/subitem.gif", "subitem");
115
addPath("org/gudy/azureus2/ui/icons/comment.png", "comment");
116
addPath("org/gudy/azureus2/ui/icons/comment_blank.png", "no_comment");
117
addPath("org/gudy/azureus2/ui/icons/foldersmall.png", "foldersmall");
121
addPath("org/gudy/azureus2/ui/icons/toolbar/open.gif", "cb_open");
122
addPath("org/gudy/azureus2/ui/icons/toolbar/open_no_default.gif","cb_open_no_default");
123
addPath("org/gudy/azureus2/ui/icons/toolbar/open_folder.gif","cb_open_folder");
124
addPath("org/gudy/azureus2/ui/icons/toolbar/open_url.gif", "cb_open_url");
125
addPath("org/gudy/azureus2/ui/icons/toolbar/new.gif", "cb_new");
126
addPath("org/gudy/azureus2/ui/icons/toolbar/up.gif", "cb_up");
127
addPath("org/gudy/azureus2/ui/icons/toolbar/down.gif", "cb_down");
128
addPath("org/gudy/azureus2/ui/icons/toolbar/top.gif", "cb_top");
129
addPath("org/gudy/azureus2/ui/icons/toolbar/bottom.gif", "cb_bottom");
130
addPath("org/gudy/azureus2/ui/icons/toolbar/run.gif", "cb_run");
131
addPath("org/gudy/azureus2/ui/icons/toolbar/start.gif", "cb_start");
132
addPath("org/gudy/azureus2/ui/icons/toolbar/stop.gif", "cb_stop");
133
addPath("org/gudy/azureus2/ui/icons/toolbar/remove.gif", "cb_remove");
134
addPath("org/gudy/azureus2/ui/icons/toolbar/switchui.png", "cb_switch");
135
addPath("org/gudy/azureus2/ui/icons/toolbar/editcolumns.gif", "cb_editcolumns");
138
addPath("org/gudy/azureus2/ui/icons/status/ok.gif", "st_ok");
139
addPath("org/gudy/azureus2/ui/icons/status/ko.gif", "st_ko");
140
addPath("org/gudy/azureus2/ui/icons/status/stopped.gif", "st_stopped");
141
addPath("org/gudy/azureus2/ui/icons/status/error.gif", "st_error");
142
addPath("org/gudy/azureus2/ui/icons/status/no_tracker.gif", "st_no_tracker");
143
addPath("org/gudy/azureus2/ui/icons/status/no_remote.gif", "st_no_remote");
145
addPath("org/gudy/azureus2/ui/icons/status/ok_shared.gif", "st_ok_shared");
146
addPath("org/gudy/azureus2/ui/icons/status/ko_shared.gif", "st_ko_shared");
147
addPath("org/gudy/azureus2/ui/icons/status/error_shared.gif","st_error_shared");
148
addPath("org/gudy/azureus2/ui/icons/status/stopped_shared.gif","st_stopped_shared");
149
addPath("org/gudy/azureus2/ui/icons/status/no_tracker_shared.gif","st_no_tracker_shared");
150
addPath("org/gudy/azureus2/ui/icons/status/no_remote_shared.gif","st_no_remote_shared");
152
addPath("org/gudy/azureus2/ui/icons/status/explain.gif", "st_explain");
153
addPath("org/gudy/azureus2/ui/icons/status/shared.gif", "st_shared");
155
addPath("org/gudy/azureus2/ui/icons/statusbar/status_warning.gif","sb_warning");
156
addPath("org/gudy/azureus2/ui/icons/statusbar/user_count.png", "sb_count");
158
addPath("org/gudy/azureus2/ui/icons/statusbar/bbb_notloggedin.png", "bbb_nli");
159
addPath("org/gudy/azureus2/ui/icons/statusbar/bbb_idle.png", "bbb_idle");
160
addPath("org/gudy/azureus2/ui/icons/statusbar/bbb_boosting.png", "bbb_out");
161
addPath("org/gudy/azureus2/ui/icons/statusbar/bbb_boosted.png", "bbb_in");
163
addPath("org/gudy/azureus2/ui/icons/smallx.png", "smallx");
164
addPath("org/gudy/azureus2/ui/icons/smallx-gray.png", "smallx-gray");
165
addPath("org/gudy/azureus2/ui/icons/working.gif", "working");
166
addPath("org/gudy/azureus2/ui/icons/search.png", "search");
169
addPath("org/gudy/azureus2/ui/icons/progress_viewer_has_error.png", "progress_error");
170
addPath("org/gudy/azureus2/ui/icons/progress_viewer_has_info.png", "progress_info");
171
addPath("org/gudy/azureus2/ui/icons/progress_viewer.png", "progress_viewer");
172
addPath("org/gudy/azureus2/ui/icons/spinning_indicator_big_black_white.gif", "spinner_big");
173
if ( Utils.isAZ2UI()){
174
addPath("org/gudy/azureus2/ui/icons/recheck.gif", "progress_retry");
175
addPath("org/gudy/azureus2/ui/icons/stop.gif", "progress_cancel");
176
addPath("org/gudy/azureus2/ui/icons/delete.gif", "progress_remove");
178
addPath("org/gudy/azureus2/ui/icons/progress_retry.png", "progress_retry");
179
addPath("org/gudy/azureus2/ui/icons/progress_cancel.png", "progress_cancel");
180
addPath("org/gudy/azureus2/ui/icons/progress_remove.png", "progress_remove");
50
public class ImageRepository
52
private static final String[] noCacheExtList = new String[] {
56
private static final boolean forceNoAWT = Constants.isOSX;
59
static void addPath(String path, String id) {
60
SkinProperties[] skinProperties = ImageLoader.getInstance().getSkinProperties();
61
if (skinProperties != null && skinProperties.length > 0) {
62
skinProperties[0].addProperty(id, path);
185
public static void addPath(String path, String id) {
186
imagesToPath.put(id,path);
188
//loadImage(display, path, id);
191
private static Image loadImage(Display display, String res, String name){
192
return loadImage(display,res,name,255);
195
private static Image loadImage(Display display, String res, String name,int alpha) {
196
return loadImage(ImageRepository.class.getClassLoader(),display,res,name,alpha);
199
static Image onlyOneImage = null;
200
private static Image loadImage(ClassLoader loader,Display display, String res, String name,int alpha) {
202
if (onlyOneImage == null) {
203
onlyOneImage = new Image(display, 1, 1);
207
imagesToPath.put(name,res);
208
Image im = getImage(name,false);
211
InputStream is = loader.getResourceAsStream(res);
215
im = new Image(display, is);
217
ImageData icone = new ImageData(is);
219
im = new Image(display,icone);
221
images.put(name, im);
222
} catch (SWTException e) {
226
Debug.outNoStack("ImageRepository:loadImage:: Resource not found: " + res);
228
im = new Image( display, 1, 1 );
230
images.put(name, im);
232
}catch( Throwable e ){
234
// can get here is getResourceAsStream borks, which is known to do sometimes
236
Debug.outNoStack("ImageRepository:loadImage:: Resource not found: " + res + " - " + Debug.getNestedExceptionMessage(e));
238
im = new Image( display, 1, 1 );
240
images.put(name, im);
247
public static void unLoadImages() {
249
iter = images.values().iterator();
250
while (iter.hasNext()) {
251
Image im = (Image) iter.next();
255
iter = registry.values().iterator();
256
while (iter.hasNext()) {
257
Image im = (Image) iter.next();
263
public static Image getRandomImage() {
264
Object[] imageArray = images.values().toArray();
265
int rnd = (int) (Math.random() * imageArray.length);
266
return (Image) imageArray[rnd];
269
public static Image getImage(String name) {
271
if (onlyOneImage == null) {
272
onlyOneImage = new Image(display, 1, 1);
276
return getImage(name,true);
279
public static InputStream
283
String path = (String) imagesToPath.get(name);
287
System.out.println( "ImageRepository: Unknown image name '" + name + "'" );
292
return( ImageRepository.class.getClassLoader().getResourceAsStream( path ));
295
private static Image getImage(String name,boolean allowLoading) {
296
Image result = (Image) images.get(name);
297
if(allowLoading && result == null) {
298
String path = (String) imagesToPath.get(name);
300
return loadImage(display,path,name);
306
public static Image getIconFromExtension(String ext) {
307
return getIconFromExtension(ext, false, false);
311
* Gets an image for a file associated with a given program
313
* @param program the Program
315
public static Image getIconFromExtension(String ext, boolean bBig, boolean minifolder) {
67
* @deprecated Use {@link ImageLoader#getImage(String)}
69
public static Image getImage(String name) {
70
return ImageLoader.getInstance().getImage(name);
74
* Gets an image for a file associated with a given program
76
* @param program the Program
78
public static Image getIconFromExtension(String ext, boolean bBig,
316
80
Image image = null;
326
image = (Image) images.get(id);
330
ImageData imageData = null;
332
if (Constants.isWindows && bBig)
336
Class ehancerClass = Class.forName("org.gudy.azureus2.ui.swt.win32.Win32UIEnhancer");
337
Method method = ehancerClass.getMethod("getBigImageData", new Class[] { String.class });
338
imageData = (ImageData) method.invoke(null, new Object[] { ext });
339
} catch (Exception e)
341
Debug.printStackTrace(e);
345
if (imageData == null)
347
Program program = Program.findProgram(ext);
350
imageData = program.getImageData();
354
if (imageData != null)
356
image = new Image(Display.getDefault(), imageData);
358
image = force16height(image);
360
image = minifolderize(image,bBig);
362
images.put(id, image);
83
String key = "osicon" + ext;
90
image = ImageLoader.getInstance().getImage(key);
91
if (ImageLoader.isRealImage(image)) {
95
ImageLoader.getInstance().releaseImage(key);
98
ImageData imageData = null;
100
if (Constants.isWindows && bBig) {
102
Class ehancerClass = Class.forName("org.gudy.azureus2.ui.swt.win32.Win32UIEnhancer");
103
Method method = ehancerClass.getMethod("getBigImageData",
107
imageData = (ImageData) method.invoke(null, new Object[] {
110
} catch (Exception e) {
111
Debug.printStackTrace(e);
115
if (imageData == null) {
116
Program program = Program.findProgram(ext);
117
if (program != null) {
118
imageData = program.getImageData();
122
if (imageData != null) {
123
image = new Image(Display.getDefault(), imageData);
125
image = force16height(image);
127
image = minifolderize(image, bBig);
129
ImageLoader.getInstance().addImageNoDipose(key, image);
365
131
} catch (Throwable e) {
366
132
// seen exceptions thrown here, due to images.get failing in Program.hashCode
376
private static Image minifolderize(Image img, boolean big)
142
private static Image minifolderize(Image img, boolean big) {
378
143
Image imgFolder = getImage(big ? "folder" : "foldersmall");
379
144
Rectangle folderBounds = imgFolder.getBounds();
380
145
Rectangle dstBounds = img.getBounds();
381
Image tempImg = Utils.renderTransparency(Display.getCurrent(), img, imgFolder, new Point(dstBounds.width - folderBounds.width, dstBounds.height - folderBounds.height),204);
146
Image tempImg = Utils.renderTransparency(Display.getCurrent(), img,
147
imgFolder, new Point(dstBounds.width - folderBounds.width,
148
dstBounds.height - folderBounds.height), 204);
149
if (tempImg != null) {
390
private static Image force16height(Image image) {
395
Rectangle bounds = image.getBounds();
396
if (bounds.height != 16) {
397
Image newImage = new Image(image.getDevice(), 16, 16);
398
GC gc = new GC(newImage);
400
if (!Constants.isUnix) {
401
// drawImage doesn't work on GTK when advanced is on
402
gc.setAdvanced(true);
405
gc.drawImage(image, 0, 0, bounds.width, bounds.height, 0, 0, 16, 16);
418
* @deprecated Does not account for custom or native folder icons
419
* @see ImageRepository#getPathIcon(String)
424
return getImage("folder", true);
427
public static Image getPathIcon(final String path) {
428
return getPathIcon(path, false, false);
431
public static Image getPathIcon(final String path, boolean minifolder) {
432
return getPathIcon(path, false, minifolder);
436
* <p>Gets a small-sized iconic representation of the file or directory at the path</p>
437
* <p>For most platforms, the icon is a 16x16 image; weak-referencing caching is used to avoid abundant reallocation.</p>
438
* @param path Absolute path to the file or directory
441
public static Image getPathIcon(final String path, boolean bBig, boolean minifolder) {
156
private static Image force16height(Image image) {
161
Rectangle bounds = image.getBounds();
162
if (bounds.height != 16) {
163
Image newImage = new Image(image.getDevice(), 16, 16);
164
GC gc = new GC(newImage);
166
if (!Constants.isUnix) {
167
// drawImage doesn't work on GTK when advanced is on
168
gc.setAdvanced(true);
171
gc.drawImage(image, 0, 0, bounds.width, bounds.height, 0, 0, 16, 16);
184
* <p>Gets a small-sized iconic representation of the file or directory at the path</p>
185
* <p>For most platforms, the icon is a 16x16 image; weak-referencing caching is used to avoid abundant reallocation.</p>
186
* @param path Absolute path to the file or directory
189
public static Image getPathIcon(final String path, boolean bBig,
190
boolean minifolder) {
442
191
if (path == null)
445
194
File file = null;
446
195
boolean bDeleteFile = false;
448
197
boolean noAWT = forceNoAWT || !bBig;
451
200
file = new File(path);
554
317
// Possible scenario: Method call before file creation
555
318
String ext = FileUtil.getExtension(path);
556
319
if (ext.length() == 0) {
557
return getFolderImage();
320
return getImage("folder");
560
323
return getIconFromExtension(ext, bBig, minifolder);
564
* <p>Gets an image with the specified canvas size</p>
565
* <p>No scaling is performed on the original image, and a cached version will be used if found.</p>
566
* @param name ImageRepository image resource name
567
* @param canvasSize Size of image
571
public static Image getImageWithSize(String name, Point canvasSize)
577
.append(canvasSize.x)
579
.append(canvasSize.y)
582
Image newImage = (Image)images.get(key);
586
Image oldImage = getImage(name);
591
newImage = new Image(Display.getCurrent(), canvasSize.x, canvasSize.y);
592
GC gc = new GC(newImage);
594
int x = Math.max(0, (canvasSize.x - oldImage.getBounds().width)/2);
595
int y = Math.max(0, (canvasSize.y - oldImage.getBounds().height)/2);
596
gc.drawImage(oldImage, x, y);
600
images.put(key, newImage);
606
public static void unloadImage(String name) {
607
Image img = (Image) images.get(name);
610
if(! img.isDisposed())
616
public static void unloadPathIcon(String path) {
617
String key = getKey(path);
618
Image img = (Image) registry.get(key);
620
registry.remove(key);
621
if(! img.isDisposed())
626
private static String getKey(String path) {
627
final File file = new File(path);
630
if(file.isDirectory())
632
key = file.getPath();
636
final int lookIndex = file.getName().lastIndexOf(".");
644
final String ext = file.getName().substring(lookIndex);
647
// case-insensitive file systems
648
for (int i = 0; i < noCacheExtList.length; i++)
650
if(noCacheExtList[i].equalsIgnoreCase(ext))
652
key = file.getPath();
661
public static void main(String[] args) {
662
Display display = new Display();
663
Shell shell = new Shell(display, SWT.SHELL_TRIM);
664
shell.setLayout(new FillLayout(SWT.VERTICAL));
666
final Label label = new Label(shell, SWT.BORDER);
668
final Text text = new Text(shell, SWT.BORDER);
669
text.addModifyListener(new ModifyListener() {
671
public void modifyText(ModifyEvent e) {
672
Image pathIcon = getPathIcon(text.getText(), false);
673
label.setImage(pathIcon);
680
while (!shell.isDisposed()) {
681
if (!display.readAndDispatch()) {
326
public static void main(String[] args) {
327
Display display = new Display();
328
Shell shell = new Shell(display, SWT.SHELL_TRIM);
329
shell.setLayout(new FillLayout(SWT.VERTICAL));
331
final Label label = new Label(shell, SWT.BORDER);
333
final Text text = new Text(shell, SWT.BORDER);
334
text.addModifyListener(new ModifyListener() {
336
public void modifyText(ModifyEvent e) {
337
Image pathIcon = getPathIcon(text.getText(), false, false);
338
label.setImage(pathIcon);
344
while (!shell.isDisposed()) {
345
if (!display.readAndDispatch()) {
b'\\ No newline at end of file'