📄 iconmanager.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 + -