📄 imagerepository.java
字号:
if (Constants.isWindows && bBig) {
try {
Class ehancerClass = Class.forName("org.gudy.azureus2.ui.swt.win32.Win32UIEnhancer");
Method method = ehancerClass.getMethod("getBigImageData",
new Class[] { String.class
});
imageData = (ImageData) method.invoke(null,
new Object[] { ext
});
} catch (Exception e) {
Debug.printStackTrace(e);
}
}
if (imageData == null) {
Program program = Program.findProgram(ext);
if (program != null) {
imageData = program.getImageData();
}
}
if (imageData != null) {
image = new Image(Display.getDefault(), imageData);
images.put(id, 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 = getImage("folder", true);
}
return image;
}
/**
* @deprecated Does not account for custom or native folder icons
* @see ImageRepository#getPathIcon(String)
*/
public static Image
getFolderImage()
{
return getImage("folder", true);
}
public static Image getPathIcon(final String path) {
return getPathIcon(path, false);
}
/**
* <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, boolean bBig) {
if (path == null)
return null;
File file = null;
boolean bDeleteFile = false;
try {
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 getIconFromExtension(ext, bBig);
// case-insensitive file systems
for (int i = 0; i < noCacheExtList.length; i++) {
if (noCacheExtList[i].equalsIgnoreCase(ext)) {
key = file.getPath();
break;
}
}
}
}
key += bBig ? "-big" : "";
// 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;
}
bDeleteFile = !file.exists();
if (bDeleteFile) {
file = File.createTempFile("AZ_", FileUtil.getExtension(path));
}
java.awt.Image awtImage = null;
final Class sfClass = Class.forName("sun.awt.shell.ShellFolder");
if (sfClass != null && file != null) {
Method method = sfClass.getMethod("getShellFolder",
new Class[] { File.class });
if (method != null) {
Object sfInstance = method.invoke(null, new Object[] { file });
if (sfInstance != null) {
method = sfClass.getMethod("getIcon", new Class[] { Boolean.TYPE });
if (method != null) {
awtImage = (java.awt.Image) method.invoke(sfInstance,
new Object[] { new Boolean(bBig) });
}
}
}
}
if (awtImage != null) {
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
ImageIO.write((BufferedImage)awtImage, "png", outStream);
final ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
image = new Image(Display.getDefault(), inStream);
registry.put(key, image);
return image;
}
} catch (Exception e) {
//Debug.printStackTrace(e);
}
if (bDeleteFile && file != null && file.exists()) {
file.delete();
}
// Possible scenario: Method call before file creation
String ext = FileUtil.getExtension(path);
if (ext.length() == 0) {
return getFolderImage();
}
return getIconFromExtension(ext, bBig);
}
/**
* <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;
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new FillLayout(SWT.VERTICAL));
final Label label = new Label(shell, SWT.BORDER);
final Text text = new Text(shell, SWT.BORDER);
text.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
Image pathIcon = getPathIcon(text.getText(), false);
label.setImage(pathIcon);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -