1 11 package org.eclipse.jface.resource; 12 13 import java.io.BufferedInputStream ; 14 import java.io.FileInputStream ; 15 import java.io.FileNotFoundException ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.SWTException; 21 import org.eclipse.swt.graphics.ImageData; 22 23 27 class FileImageDescriptor extends ImageDescriptor { 28 29 33 private Class location; 34 35 38 private String name; 39 40 54 FileImageDescriptor(Class clazz, String filename) { 55 this.location = clazz; 56 this.name = filename; 57 } 58 59 62 public boolean equals(Object o) { 63 if (!(o instanceof FileImageDescriptor)) { 64 return false; 65 } 66 FileImageDescriptor other = (FileImageDescriptor) o; 67 if (location != null) { 68 if (!location.equals(other.location)) { 69 return false; 70 } 71 } else { 72 if (other.location != null) { 73 return false; 74 } 75 } 76 return name.equals(other.name); 77 } 78 79 83 public ImageData getImageData() { 84 InputStream in = getStream(); 85 ImageData result = null; 86 if (in != null) { 87 try { 88 result = new ImageData(in); 89 } catch (SWTException e) { 90 if (e.code != SWT.ERROR_INVALID_IMAGE) { 91 throw e; 92 } 94 } finally { 95 try { 96 in.close(); 97 } catch (IOException e) { 98 } 101 } 102 } 103 return result; 104 } 105 106 113 private InputStream getStream() { 114 InputStream is = null; 115 116 if (location != null) { 117 is = location.getResourceAsStream(name); 118 119 } else { 120 try { 121 is = new FileInputStream (name); 122 } catch (FileNotFoundException e) { 123 return null; 124 } 125 } 126 if (is == null) { 127 return null; 128 } else { 129 return new BufferedInputStream (is); 130 } 131 } 132 133 136 public int hashCode() { 137 int code = name.hashCode(); 138 if (location != null) { 139 code += location.hashCode(); 140 } 141 return code; 142 } 143 144 147 151 public String toString() { 152 return "FileImageDescriptor(location=" + location + ", name=" + name + ")"; } 154 } 155 | Popular Tags |