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

📄 iconmanager.java

📁 MyDownloader 是一款使用 http 协议(RFC 1867)用于下载一个或多个文件到本地的简单易用的收费 Java 程序.使用托拽操作,你可以在一个页面内下载多个文件.在下载文件的过程当中
💻 JAVA
字号:
/*
 * Copyright 2007 JavaAtWork All rights reserved.
 * Use is subject to license terms.
 */
package com.javaatwork.mydownloader.utils;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileSystemView;


/**
 * Class for creating an ImageIcon.
 * 
 * @author Johannes Postma
 */
public class IconManager {

	private HashMap iconMap = null;
	private FileSystemView fileSystemView = null;
	private File tmpDirectory = null;
	
	/**
	 * Creates a new IconManager.
	 */
	public IconManager() {
		iconMap = new HashMap();
		fileSystemView = FileSystemView.getFileSystemView();
		tmpDirectory = TempDirectoryManager.getTempDirectory();
	}
	
	/**
	 * Returns the system icon of a file.
	 * 
	 * @param file The file.
	 * @return The system icon of the file.
	 */
	public Icon getSystemIcon(File file) {
		
		String fileName = null;
		String extension = null;
		
		fileName = file.getName();
		
		// get extension
		int index = fileName.indexOf(".");
		
		if (index != -1) {
			extension = fileName.substring(index);
		} else {
			extension = fileName;
		}
		
		Object object = iconMap.get(extension);
		
		if (object != null) {
			return (Icon)object;
		} else {
			File tmpFile = null;
			try {
				tmpFile = File.createTempFile("mydownloader", extension, tmpDirectory);
				 
			} catch (IOException e) {
				// do nothing
			}
			
			Icon icon = fileSystemView.getSystemIcon(tmpFile);
			iconMap.put(extension, icon);
			
			// delete tmp file
			tmpFile.delete();
			
			return icon;
		}
	}
	
	/**
	 * Creates a new ImageIcon.
	 * 
	 * @param value The name of the icon.
	 * @param codeBase The codebase of the applet.
	 * @return The ImageIcon.
	 */
	public synchronized static ImageIcon getImageIcon(String value, URL codeBase) {
		
		if (value == null) {
			return null;
		}
		
		URL iconURL = null;
		
		try {
			if (!value.startsWith("http")) {
				iconURL = new URL(codeBase, value);
			} else {
				iconURL = new URL(value);
			}
			
		} catch (MalformedURLException e) {
			Logger.log("IconManager", "getImageIcon()", "Incorrect URL format: " + value + " " + e.toString());
		}
		
		return iconURL == null ? null : new ImageIcon(iconURL);
	}
}

⌨️ 快捷键说明

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