~smaioli/azureus/ubuntu-experimental

« back to all changes in this revision

Viewing changes to org/gudy/azureus2/ui/swt/ImageRepository.java

MergedĀ VuzeĀ 4.2.0.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
package org.gudy.azureus2.ui.swt;
22
22
 
23
23
import java.awt.image.BufferedImage;
24
 
import java.io.*;
 
24
import java.io.ByteArrayInputStream;
 
25
import java.io.ByteArrayOutputStream;
 
26
import java.io.File;
25
27
import java.lang.reflect.Method;
26
 
import java.util.HashMap;
27
 
import java.util.Iterator;
28
28
 
29
29
import javax.imageio.ImageIO;
30
30
 
31
31
import org.eclipse.swt.SWT;
32
 
import org.eclipse.swt.SWTException;
33
32
import org.eclipse.swt.events.ModifyEvent;
34
33
import org.eclipse.swt.events.ModifyListener;
35
34
import org.eclipse.swt.graphics.*;
41
40
import org.gudy.azureus2.core3.util.Debug;
42
41
import org.gudy.azureus2.core3.util.FileUtil;
43
42
 
 
43
import com.aelitis.azureus.ui.skin.SkinProperties;
 
44
import com.aelitis.azureus.ui.swt.imageloader.ImageLoader;
 
45
 
44
46
/**
45
47
 * @author Olivier
46
48
 *
47
49
 */
48
 
public class ImageRepository {
49
 
        private static boolean NO_IMAGES = false;
50
 
 
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;
57
 
 
58
 
    static {
59
 
    images = new HashMap(150);
60
 
    imagesToPath = new HashMap(150);
61
 
    registry = new HashMap();
62
 
  }
63
 
 
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");
71
 
  }
72
 
 
73
 
  public static void loadImages(Display display) {
74
 
          
75
 
                
76
 
                if(Constants.isOSX) {
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");
79
 
                }
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");
118
 
 
119
 
                //ToolBar Icons
120
 
 
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");
136
 
 
137
 
                //Status icons
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");
144
 
 
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");
151
 
 
152
 
                addPath("org/gudy/azureus2/ui/icons/status/explain.gif", "st_explain");
153
 
                addPath("org/gudy/azureus2/ui/icons/status/shared.gif", "st_shared");
154
 
 
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");
157
 
 
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");
162
 
 
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");
167
 
                
168
 
                //Progress icons
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");
177
 
                }else{
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
 
51
{
 
52
        private static final String[] noCacheExtList = new String[] {
 
53
                ".exe"
 
54
        };
 
55
 
 
56
        private static final boolean forceNoAWT = Constants.isOSX;
 
57
 
 
58
        /**public*/
 
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);
181
63
                }
182
 
                
183
 
        }
184
 
 
185
 
  public static void addPath(String path, String id) {
186
 
    imagesToPath.put(id,path);
187
 
        // 2x longer
188
 
    //loadImage(display, path, id);
189
 
  }
190
 
 
191
 
  private static Image loadImage(Display display, String res, String name){
192
 
    return loadImage(display,res,name,255);
193
 
  }
194
 
 
195
 
  private static Image loadImage(Display display, String res, String name,int alpha) {
196
 
    return loadImage(ImageRepository.class.getClassLoader(),display,res,name,alpha);
197
 
  }
198
 
 
199
 
  static Image onlyOneImage = null;
200
 
  private static Image loadImage(ClassLoader loader,Display display, String res, String name,int alpha) {
201
 
        if (NO_IMAGES) {
202
 
                if (onlyOneImage == null) {
203
 
                        onlyOneImage = new Image(display, 1, 1);
204
 
                }
205
 
                return onlyOneImage;
206
 
        }
207
 
    imagesToPath.put(name,res);
208
 
    Image im = getImage(name,false);
209
 
    if(null == im) {
210
 
      try{
211
 
              InputStream is = loader.getResourceAsStream(res);
212
 
              if(null != is) {
213
 
                try { 
214
 
                        if(alpha == 255) {
215
 
                          im = new Image(display, is);
216
 
                        } else {
217
 
                          ImageData icone = new ImageData(is);
218
 
                          icone.alpha = alpha;
219
 
                          im = new Image(display,icone);
220
 
                        }
221
 
                        images.put(name, im);
222
 
                } catch (SWTException e) {
223
 
                        return null;
224
 
                }
225
 
              } else {
226
 
                Debug.outNoStack("ImageRepository:loadImage:: Resource not found: " + res);
227
 
                        
228
 
                        im = new Image( display, 1, 1 );
229
 
                        
230
 
                        images.put(name, im);
231
 
              }
232
 
      }catch( Throwable e ){
233
 
          
234
 
                // can get here is getResourceAsStream borks, which is known to do sometimes
235
 
          
236
 
          Debug.outNoStack("ImageRepository:loadImage:: Resource not found: " + res + " - " + Debug.getNestedExceptionMessage(e));
237
 
                        
238
 
                  im = new Image( display, 1, 1 );
239
 
                        
240
 
                  images.put(name, im);
241
 
      }
242
 
    }
243
 
    return im;
244
 
  }
245
 
 
246
 
    // dead
247
 
  public static void unLoadImages() {
248
 
    Iterator iter;
249
 
    iter = images.values().iterator();
250
 
    while (iter.hasNext()) {
251
 
      Image im = (Image) iter.next();
252
 
      im.dispose();
253
 
    }
254
 
 
255
 
    iter = registry.values().iterator();
256
 
    while (iter.hasNext()) {
257
 
      Image im = (Image) iter.next();
258
 
      if(im != null)
259
 
        im.dispose();
260
 
    }
261
 
  }
262
 
  
263
 
  public static Image getRandomImage() {
264
 
                Object[] imageArray = images.values().toArray();
265
 
                int rnd = (int) (Math.random() * imageArray.length);
266
 
                return (Image) imageArray[rnd];
267
 
        }
268
 
 
269
 
  public static Image getImage(String name) {
270
 
        if (NO_IMAGES) {
271
 
                if (onlyOneImage == null) {
272
 
                        onlyOneImage = new Image(display, 1, 1);
273
 
                }
274
 
                return onlyOneImage;
275
 
        }
276
 
    return getImage(name,true);
277
 
  }
278
 
  
279
 
  public static InputStream
280
 
  getImageAsStream(
281
 
         String name )
282
 
  {
283
 
          String path = (String) imagesToPath.get(name);
284
 
 
285
 
          if ( path == null ){
286
 
                  
287
 
                  System.out.println( "ImageRepository: Unknown image name '" + name  + "'" );
288
 
                  
289
 
                  return( null );
290
 
          }
291
 
          
292
 
          return( ImageRepository.class.getClassLoader().getResourceAsStream( path ));
293
 
  }
294
 
  
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);
299
 
      if(path != null) {
300
 
        return loadImage(display,path,name);
301
 
      }
302
 
    }
303
 
    return result;
304
 
  }
305
 
 
306
 
  public static Image getIconFromExtension(String ext) {
307
 
        return getIconFromExtension(ext, false, false);
308
 
  }
309
 
 
310
 
  /**
311
 
     * Gets an image for a file associated with a given program
312
 
     *
313
 
     * @param program the Program
314
 
     */
315
 
  public static Image getIconFromExtension(String ext, boolean bBig, boolean minifolder) {
 
64
        }
 
65
 
 
66
        /** 
 
67
         * @deprecated Use {@link ImageLoader#getImage(String)}
 
68
         */
 
69
        public static Image getImage(String name) {
 
70
                return ImageLoader.getInstance().getImage(name);
 
71
        }
 
72
 
 
73
        /**
 
74
           * Gets an image for a file associated with a given program
 
75
           *
 
76
           * @param program the Program
 
77
           */
 
78
        public static Image getIconFromExtension(String ext, boolean bBig,
 
79
                        boolean minifolder) {
316
80
                Image image = null;
317
81
 
318
82
                try {
319
 
                        String id = ext;
320
 
                        
321
 
                        if(bBig)
322
 
                                id += "-big";
323
 
                        if(minifolder)
324
 
                                id += "-fold";
325
 
                        
326
 
                        image = (Image) images.get(id);
327
 
 
328
 
                        if (image == null)
329
 
                        {
330
 
                                ImageData imageData = null;
331
 
                                
332
 
                                if (Constants.isWindows && bBig)
333
 
                                {
334
 
                                        try
335
 
                                        {
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)
340
 
                                        {
341
 
                                                Debug.printStackTrace(e);
342
 
                                        }
343
 
                                }
344
 
                                
345
 
                                if (imageData == null)
346
 
                                {
347
 
                                        Program program = Program.findProgram(ext);
348
 
                                        if (program != null)
349
 
                                        {
350
 
                                                imageData = program.getImageData();
351
 
                                        }
352
 
                                }
353
 
                                
354
 
                                if (imageData != null)
355
 
                                {
356
 
                                        image = new Image(Display.getDefault(), imageData);
357
 
                                        if (!bBig)
358
 
                                                image = force16height(image);
359
 
                                        if(minifolder)
360
 
                                                image = minifolderize(image,bBig);
361
 
 
362
 
                                        images.put(id, image);
363
 
                                }
 
83
                        String key = "osicon" + ext;
 
84
 
 
85
                        if (bBig)
 
86
                                key += "-big";
 
87
                        if (minifolder)
 
88
                                key += "-fold";
 
89
 
 
90
                        image = ImageLoader.getInstance().getImage(key);
 
91
                        if (ImageLoader.isRealImage(image)) { 
 
92
                                return image;
 
93
                        }
 
94
 
 
95
                        ImageLoader.getInstance().releaseImage(key);
 
96
                        image = null;
 
97
 
 
98
                        ImageData imageData = null;
 
99
 
 
100
                        if (Constants.isWindows && bBig) {
 
101
                                try {
 
102
                                        Class ehancerClass = Class.forName("org.gudy.azureus2.ui.swt.win32.Win32UIEnhancer");
 
103
                                        Method method = ehancerClass.getMethod("getBigImageData",
 
104
                                                        new Class[] {
 
105
                                                                String.class
 
106
                                                        });
 
107
                                        imageData = (ImageData) method.invoke(null, new Object[] {
 
108
                                                ext
 
109
                                        });
 
110
                                } catch (Exception e) {
 
111
                                        Debug.printStackTrace(e);
 
112
                                }
 
113
                        }
 
114
 
 
115
                        if (imageData == null) {
 
116
                                Program program = Program.findProgram(ext);
 
117
                                if (program != null) {
 
118
                                        imageData = program.getImageData();
 
119
                                }
 
120
                        }
 
121
 
 
122
                        if (imageData != null) {
 
123
                                image = new Image(Display.getDefault(), imageData);
 
124
                                if (!bBig)
 
125
                                        image = force16height(image);
 
126
                                if (minifolder)
 
127
                                        image = minifolderize(image, bBig);
 
128
 
 
129
                                ImageLoader.getInstance().addImageNoDipose(key, image);
364
130
                        }
365
131
                } catch (Throwable e) {
366
132
                        // seen exceptions thrown here, due to images.get failing in Program.hashCode
372
138
                }
373
139
                return image;
374
140
        }
375
 
  
376
 
  private static Image minifolderize(Image img, boolean big)
377
 
  {
 
141
 
 
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);
382
 
                if (tempImg != null)
383
 
                {
 
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) {
384
150
                        img.dispose();
385
151
                        img = tempImg;
386
152
                }
387
153
                return img;
388
 
  }
389
 
  
390
 
  private static Image force16height(Image image) {
391
 
        if (image == null) {
392
 
                return image;
393
 
        }
394
 
 
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);
399
 
                try {
400
 
                        if (!Constants.isUnix) {
401
 
                                // drawImage doesn't work on GTK when advanced is on
402
 
                                gc.setAdvanced(true);
403
 
                        }
404
 
                        
405
 
                        gc.drawImage(image, 0, 0, bounds.width, bounds.height, 0, 0, 16, 16);
406
 
                } finally {
407
 
                        gc.dispose();
408
 
                }
409
 
                
410
 
                image.dispose();
411
 
                image = newImage;
412
 
        }
413
 
        
414
 
        return image;
415
 
  }
416
 
  
417
 
  /**
418
 
   * @deprecated Does not account for custom or native folder icons
419
 
   * @see ImageRepository#getPathIcon(String)
420
 
   */
421
 
  public static Image
422
 
  getFolderImage()
423
 
  {
424
 
        return getImage("folder", true);
425
 
  }
426
 
 
427
 
        public static Image getPathIcon(final String path) {
428
 
                return getPathIcon(path, false, false);
429
 
        }
430
 
        
431
 
        public static Image getPathIcon(final String path, boolean minifolder) {
432
 
                return getPathIcon(path, false, minifolder);
433
 
        }
434
 
 
435
 
    /**
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
439
 
         * @return The image
440
 
         */
441
 
        public static Image getPathIcon(final String path, boolean bBig, boolean minifolder) {
 
154
        }
 
155
 
 
156
        private static Image force16height(Image image) {
 
157
                if (image == null) {
 
158
                        return image;
 
159
                }
 
160
 
 
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);
 
165
                        try {
 
166
                                if (!Constants.isUnix) {
 
167
                                        // drawImage doesn't work on GTK when advanced is on
 
168
                                        gc.setAdvanced(true);
 
169
                                }
 
170
 
 
171
                                gc.drawImage(image, 0, 0, bounds.width, bounds.height, 0, 0, 16, 16);
 
172
                        } finally {
 
173
                                gc.dispose();
 
174
                        }
 
175
 
 
176
                        image.dispose();
 
177
                        image = newImage;
 
178
                }
 
179
 
 
180
                return image;
 
181
        }
 
182
 
 
183
        /**
 
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
 
187
        * @return The image
 
188
        */
 
189
        public static Image getPathIcon(final String path, boolean bBig,
 
190
                        boolean minifolder) {
442
191
                if (path == null)
443
192
                        return null;
444
193
 
445
194
                File file = null;
446
195
                boolean bDeleteFile = false;
447
 
                
 
196
 
448
197
                boolean noAWT = forceNoAWT || !bBig;
449
 
                                
 
198
 
450
199
                try {
451
200
                        file = new File(path);
452
201
 
456
205
 
457
206
                        String key;
458
207
                        if (file.isDirectory()) {
459
 
                                if (noAWT)
460
 
                                        return getFolderImage();
 
208
                                if (noAWT) {
 
209
                                        return getImage("folder");
 
210
                                }
461
211
 
462
212
                                key = file.getPath();
463
213
                        } else {
464
 
                                final int lookIndex = file.getName().lastIndexOf(".");
 
214
                                final int idxDot = file.getName().lastIndexOf(".");
465
215
 
466
 
                                if (lookIndex == -1) {
467
 
                                        if (noAWT)
468
 
                                                return getFolderImage();
 
216
                                if (idxDot == -1) {
 
217
                                        if (noAWT) {
 
218
                                                return getImage("folder");
 
219
                                        }
469
220
 
470
221
                                        key = "?!blank";
471
222
                                } else {
472
 
                                        final String ext = file.getName().substring(lookIndex);
 
223
                                        final String ext = file.getName().substring(idxDot);
473
224
                                        key = ext;
474
 
 
 
225
                                        
475
226
                                        if (noAWT)
476
227
                                                return getIconFromExtension(ext, bBig, minifolder);
477
228
 
484
235
                                        }
485
236
                                }
486
237
                        }
487
 
                        
488
 
                        if(bBig)
 
238
 
 
239
                        if (bBig)
489
240
                                key += "-big";
490
 
                        if(minifolder)
 
241
                        if (minifolder)
491
242
                                key += "-fold";
 
243
                        
 
244
                        key = "osicon" + key;
492
245
 
493
246
                        // this method mostly deals with incoming torrent files, so there's less concern for
494
247
                        // custom icons (unless user sets a custom icon in a later session)
495
248
 
496
249
                        // other platforms - try sun.awt
497
 
                        Image image = (Image) registry.get(key);
498
 
                        if (image != null) {
 
250
                        Image image = ImageLoader.getInstance().getImage(key);
 
251
                        if (ImageLoader.isRealImage(image)) { 
499
252
                                return image;
500
253
                        }
501
 
                        
 
254
                        ImageLoader.getInstance().releaseImage(key);
 
255
                        image = null;
502
256
 
503
257
                        bDeleteFile = !file.exists();
504
258
                        if (bDeleteFile) {
506
260
                        }
507
261
 
508
262
                        java.awt.Image awtImage = null;
509
 
                        
 
263
 
510
264
                        final Class sfClass = Class.forName("sun.awt.shell.ShellFolder");
511
265
                        if (sfClass != null && file != null) {
512
 
                                Method method = sfClass.getMethod("getShellFolder",
513
 
                                                new Class[] { File.class });
 
266
                                Method method = sfClass.getMethod("getShellFolder", new Class[] {
 
267
                                        File.class
 
268
                                });
514
269
                                if (method != null) {
515
 
                                        Object sfInstance = method.invoke(null, new Object[] { file });
 
270
                                        Object sfInstance = method.invoke(null, new Object[] {
 
271
                                                file
 
272
                                        });
516
273
 
517
274
                                        if (sfInstance != null) {
518
 
                                                method = sfClass.getMethod("getIcon", new Class[] { Boolean.TYPE });
 
275
                                                method = sfClass.getMethod("getIcon", new Class[] {
 
276
                                                        Boolean.TYPE
 
277
                                                });
519
278
                                                if (method != null) {
520
279
                                                        awtImage = (java.awt.Image) method.invoke(sfInstance,
521
 
                                                                        new Object[] { new Boolean(bBig) });
 
280
                                                                        new Object[] {
 
281
                                                                                new Boolean(bBig)
 
282
                                                                        });
522
283
                                                }
523
284
                                        }
524
285
                                }
525
286
                        }
526
287
 
527
288
                        if (awtImage != null) {
528
 
        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
529
 
        ImageIO.write((BufferedImage)awtImage, "png", outStream);
530
 
        final ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
 
289
                                final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
 
290
                                ImageIO.write((BufferedImage) awtImage, "png", outStream);
 
291
                                final ByteArrayInputStream inStream = new ByteArrayInputStream(
 
292
                                                outStream.toByteArray());
531
293
 
532
 
        image = new Image(Display.getDefault(), inStream);
 
294
                                image = new Image(Display.getDefault(), inStream);
533
295
                                if (!bBig) {
534
296
                                        image = force16height(image);
535
297
                                }
536
 
                                if(minifolder)
537
 
                                        image = minifolderize(image,bBig);
 
298
                                if (minifolder)
 
299
                                        image = minifolderize(image, bBig);
538
300
 
539
 
                                registry.put(key, image);
 
301
                                
 
302
                                ImageLoader.getInstance().addImageNoDipose(key, image);
540
303
 
541
304
                                if (bDeleteFile && file != null && file.exists()) {
542
305
                                        file.delete();
546
309
                } catch (Exception e) {
547
310
                        //Debug.printStackTrace(e);
548
311
                }
549
 
                
 
312
 
550
313
                if (bDeleteFile && file != null && file.exists()) {
551
314
                        file.delete();
552
315
                }
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");
558
321
                }
559
322
 
560
323
                return getIconFromExtension(ext, bBig, minifolder);
561
324
        }
562
325
 
563
 
    /**
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
568
 
     * @return The image
569
 
     */
570
 
    // dead
571
 
    public static Image getImageWithSize(String name, Point canvasSize)
572
 
    {
573
 
        String key =
574
 
                new StringBuffer()
575
 
                    .append(name)
576
 
                    .append('.')
577
 
                    .append(canvasSize.x)
578
 
                    .append('.')
579
 
                    .append(canvasSize.y)
580
 
                .toString();
581
 
 
582
 
        Image newImage = (Image)images.get(key);
583
 
 
584
 
        if(newImage == null)
585
 
        {
586
 
            Image oldImage = getImage(name);
587
 
 
588
 
            if(oldImage == null)
589
 
                return null;
590
 
 
591
 
            newImage = new Image(Display.getCurrent(), canvasSize.x, canvasSize.y);
592
 
            GC gc = new GC(newImage);
593
 
 
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);
597
 
 
598
 
            gc.dispose();
599
 
 
600
 
            images.put(key, newImage);
601
 
        }
602
 
 
603
 
        return newImage;
604
 
    }
605
 
    
606
 
    public static void unloadImage(String name) {
607
 
      Image img = (Image) images.get(name);
608
 
      if(img != null) {
609
 
        images.remove(name);
610
 
        if(! img.isDisposed())
611
 
          img.dispose();        
612
 
      }
613
 
    }
614
 
 
615
 
    // dead
616
 
    public static void unloadPathIcon(String path) {
617
 
     String key = getKey(path);
618
 
     Image img = (Image) registry.get(key);
619
 
     if(img != null) {
620
 
       registry.remove(key);
621
 
       if(! img.isDisposed())
622
 
         img.dispose();       
623
 
     }
624
 
    }
625
 
    
626
 
    private static String getKey(String path) {
627
 
      final File file = new File(path);
628
 
 
629
 
      String key;
630
 
      if(file.isDirectory())
631
 
      {
632
 
          key = file.getPath();
633
 
      }
634
 
      else
635
 
      {
636
 
          final int lookIndex = file.getName().lastIndexOf(".");
637
 
  
638
 
          if(lookIndex == -1)
639
 
          {
640
 
              key = "?!blank";
641
 
          }
642
 
          else
643
 
          {
644
 
              final String ext =  file.getName().substring(lookIndex);
645
 
              key = ext;
646
 
              
647
 
              // case-insensitive file systems
648
 
              for (int i = 0; i < noCacheExtList.length; i++)
649
 
              {
650
 
                  if(noCacheExtList[i].equalsIgnoreCase(ext))
651
 
                  {
652
 
                      key = file.getPath();
653
 
                  }
654
 
              }
655
 
          }
656
 
      } 
657
 
      
658
 
      return key;
659
 
    }
660
 
    
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));
665
 
                        
666
 
                        final Label label = new Label(shell, SWT.BORDER);
667
 
                        
668
 
                        final Text text = new Text(shell, SWT.BORDER);
669
 
                        text.addModifyListener(new ModifyListener() {
670
 
                        
671
 
                                public void modifyText(ModifyEvent e) {
672
 
                                        Image pathIcon = getPathIcon(text.getText(), false);
673
 
                                        label.setImage(pathIcon);
674
 
                                }
675
 
                        });
676
 
                        
677
 
                        
678
 
                        shell.open();
679
 
                        
680
 
                        while (!shell.isDisposed()) {
681
 
                                if (!display.readAndDispatch()) {
682
 
                                        display.sleep();
683
 
                                }
 
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));
 
330
 
 
331
                final Label label = new Label(shell, SWT.BORDER);
 
332
 
 
333
                final Text text = new Text(shell, SWT.BORDER);
 
334
                text.addModifyListener(new ModifyListener() {
 
335
 
 
336
                        public void modifyText(ModifyEvent e) {
 
337
                                Image pathIcon = getPathIcon(text.getText(), false, false);
 
338
                                label.setImage(pathIcon);
 
339
                        }
 
340
                });
 
341
 
 
342
                shell.open();
 
343
 
 
344
                while (!shell.isDisposed()) {
 
345
                        if (!display.readAndDispatch()) {
 
346
                                display.sleep();
684
347
                        }
685
348
                }
 
349
        }
686
350
}
 
 
b'\\ No newline at end of file'