⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagerepository.java

📁 基于JXTA开发平台的下载软件开发源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	        if (imageData != null) {
	          image = new Image(null, imageData);
	          images.put(program, image);
	        }
	      }
	    }
    }catch( Throwable e ){
    	// seen exceptions thrown here, due to images.get failing in Program.hashCode
    	// ignore and use default icon
    }

    if (image == null) {
      image = (Image) images.get("folder");
    }
    return image;
  }

  /**
   * @deprecated Does not account for custom or native folder icons
   * @see ImageRepository#getPathIcon(String)
   */
  public static Image
  getFolderImage()
  {
  	return( (Image) images.get("folder"));
  }

    /**
	 * <p>Gets a small-sized iconic representation of the file or directory at the path</p>
	 * <p>For most platforms, the icon is a 16x16 image; weak-referencing caching is used to avoid abundant reallocation.</p>
	 * @param path Absolute path to the file or directory
	 * @return The image
	 */
	public static Image getPathIcon(final String path) {
		try {
			final File file = new File(path);

			// workaround for unsupported platforms
			// notes:
			// Mac OS X - Do not mix AWT with SWT (possible workaround: use IPC/Cocoa)

			String key;
			if (file.isDirectory()) {
				if (doNotUseAWTIcon)
					return getFolderImage();

				key = file.getPath();
			} else {
				final int lookIndex = file.getName().lastIndexOf(".");

				if (lookIndex == -1) {
					if (doNotUseAWTIcon)
						return getFolderImage();

					key = "?!blank";
				} else {
					final String ext = file.getName().substring(lookIndex);
					key = ext;

					if (doNotUseAWTIcon)
						return getIconFromProgram(Program.findProgram(ext));

					// case-insensitive file systems
					for (int i = 0; i < noCacheExtList.length; i++) {
						if (noCacheExtList[i].equalsIgnoreCase(ext)) {
							key = file.getPath();
							break;
						}
					}
				}
			}

			// this method mostly deals with incoming torrent files, so there's less concern for
			// custom icons (unless user sets a custom icon in a later session)

			// other platforms - try sun.awt
			Image image = (Image) registry.get(key);
			if (image != null)
				return image;

			final Class sfClass = Class.forName("sun.awt.shell.ShellFolder");
			final Object sfInstance = sfClass.getMethod("getShellFolder",
					new Class[] { File.class }).invoke(null, new Object[] { file });

			final java.awt.Image awtImage = (java.awt.Image) sfClass.getMethod(
					"getIcon", new Class[] { Boolean.TYPE }).invoke(sfInstance,
					new Object[] { new Boolean(false) });

			if (awtImage != null) {
        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        ImageIO.write((BufferedImage)awtImage, "png", outStream);
        final ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());

        image = new Image(null, inStream);

        if (Constants.isWindows) {
					// recomposite to avoid artifacts - transparency mask does not work
					final Image dstImage = new Image(Display.getCurrent(), image
							.getBounds().width, image.getBounds().height);
					GC gc = new GC(dstImage);
					gc.drawImage(image, 0, 0);
					gc.dispose();
					image.dispose();
					image = dstImage;
				}

				registry.put(key, image);

				return image;
			}
		} catch (Exception e) {
			//Debug.printStackTrace(e);
		}

		// Possible scenario: Method call before file creation
		final int fileSepIndex = path.lastIndexOf(File.separator);
		if (fileSepIndex == path.length() - 1)
			return getFolderImage();

		final int extIndex;
		if (fileSepIndex == -1)
			extIndex = path.indexOf('.');
		else
			extIndex = path.substring(fileSepIndex).indexOf('.');

		if (extIndex == -1)
			return getFolderImage();

		return getIconFromProgram(Program.findProgram(path.substring(extIndex)));
	}

    /**
     * <p>Gets an image with the specified canvas size</p>
     * <p>No scaling is performed on the original image, and a cached version will be used if found.</p>
     * @param name ImageRepository image resource name
     * @param canvasSize Size of image
     * @return The image
     */
    public static Image getImageWithSize(String name, Point canvasSize)
    {
        String key =
                new StringBuffer()
                    .append(name)
                    .append('.')
                    .append(canvasSize.x)
                    .append('.')
                    .append(canvasSize.y)
                .toString();

        Image newImage = (Image)images.get(key);

        if(newImage == null)
        {
            Image oldImage = getImage(name);

            if(oldImage == null)
                return null;

            newImage = new Image(Display.getCurrent(), canvasSize.x, canvasSize.y);
            GC gc = new GC(newImage);

            int x = Math.max(0, (canvasSize.x - oldImage.getBounds().width)/2);
            int y = Math.max(0, (canvasSize.y - oldImage.getBounds().height)/2);
            gc.drawImage(oldImage, x, y);

            gc.dispose();

            images.put(key, newImage);
        }

        return newImage;
    }
    
    public static void unloadImage(String name) {
      Image img = (Image) images.get(name);
      if(img != null) {
        images.remove(name);
        if(! img.isDisposed())
          img.dispose();        
      }
    }
    
    public static void unloadPathIcon(String path) {
     String key = getKey(path);
     Image img = (Image) registry.get(key);
     if(img != null) {
       registry.remove(key);
       if(! img.isDisposed())
         img.dispose();       
     }
    }
    
    private static String getKey(String path) {
      final File file = new File(path);

      String key;
      if(file.isDirectory())
      {
          key = file.getPath();
      }
      else
      {
          final int lookIndex = file.getName().lastIndexOf(".");
  
          if(lookIndex == -1)
          {
              key = "?!blank";
          }
          else
          {
              final String ext =  file.getName().substring(lookIndex);
              key = ext;
              
              // case-insensitive file systems
              for (int i = 0; i < noCacheExtList.length; i++)
              {
                  if(noCacheExtList[i].equalsIgnoreCase(ext))
                  {
                      key = file.getPath();
                  }
              }
          }
      } 
      
      return key;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -