1136 } |
1127 } |
1137 return iSecurityIcon; |
1128 return iSecurityIcon; |
1138 } |
1129 } |
1139 |
1130 |
1140 /** |
1131 /** |
1141 * Loads image from specified InputStream. This method scales the image |
1132 * Loads image from specified file. |
1142 * to optimum size. |
1133 * |
|
1134 * @param aDisplay display to which the icon will be added |
|
1135 * @param aImageFilename name of the image file to be loaded |
|
1136 * @param aImageSize size to which the image should be scaled, can be null |
|
1137 * @return image from file or null if image cannot be loaded |
|
1138 */ |
|
1139 protected static Image loadImage( |
|
1140 Display aDisplay, String aImageFilename, Point aImageSize) |
|
1141 { |
|
1142 Image result = null; |
|
1143 InputStream imageInputStream = null; |
|
1144 try |
|
1145 { |
|
1146 FileUtility imageFile = new FileUtility(aImageFilename); |
|
1147 if (imageFile.exists()) |
|
1148 { |
|
1149 imageInputStream = imageFile.openInputStream(); |
|
1150 result = loadImage(aDisplay, imageInputStream, |
|
1151 aImageFilename, aImageSize); |
|
1152 } |
|
1153 } |
|
1154 catch (IOException ioe) |
|
1155 { |
|
1156 log("Can not get InputStream for " + aImageFilename + ": " + ioe); |
|
1157 } |
|
1158 finally |
|
1159 { |
|
1160 if (imageInputStream != null) |
|
1161 { |
|
1162 try |
|
1163 { |
|
1164 imageInputStream.close(); |
|
1165 imageInputStream = null; |
|
1166 } |
|
1167 catch (IOException ioe) |
|
1168 { |
|
1169 logError("Closing image InputStream failed", ioe); |
|
1170 } |
|
1171 } |
|
1172 } |
|
1173 return result; |
|
1174 } |
|
1175 |
|
1176 /** |
|
1177 * Loads image from specified InputStream. |
1143 * |
1178 * |
1144 * @param aDisplay display to which the icon will be added |
1179 * @param aDisplay display to which the icon will be added |
1145 * @param aInputStream InputStream where the image is loaded |
1180 * @param aInputStream InputStream where the image is loaded |
1146 * @param aImageName name of the image to be loaded |
1181 * @param aImageName name of the image to be loaded |
|
1182 * @param aImageSize size to which the image should be scaled, can be null |
1147 * @return image from InputStream or null if image cannot be loaded |
1183 * @return image from InputStream or null if image cannot be loaded |
1148 */ |
1184 */ |
1149 protected static Image loadImage( |
1185 protected static Image loadImage( |
1150 Display aDisplay, InputStream aInputStream, String aImageName) |
|
1151 { |
|
1152 return loadImage(aDisplay, aInputStream, aImageName, true); |
|
1153 } |
|
1154 |
|
1155 /** |
|
1156 * Loads image from specified file. |
|
1157 * |
|
1158 * @param aDisplay display to which the icon will be added |
|
1159 * @param aImageFilename name of the image file to be loaded |
|
1160 * @param aScaleImage flag telling if the loaded image should be scaled |
|
1161 * @return image from file or null if image cannot be loaded |
|
1162 */ |
|
1163 private static Image loadImage( |
|
1164 Display aDisplay, String aImageFilename, boolean aScaleImage) |
|
1165 { |
|
1166 Image result = null; |
|
1167 InputStream imageInputStream = null; |
|
1168 try |
|
1169 { |
|
1170 FileUtility imageFile = new FileUtility(aImageFilename); |
|
1171 if (imageFile.exists()) |
|
1172 { |
|
1173 imageInputStream = imageFile.openInputStream(); |
|
1174 result = loadImage(aDisplay, imageInputStream, |
|
1175 aImageFilename, aScaleImage); |
|
1176 } |
|
1177 } |
|
1178 catch (IOException ioe) |
|
1179 { |
|
1180 log("Can not get InputStream for " + aImageFilename + ": " + ioe); |
|
1181 } |
|
1182 finally |
|
1183 { |
|
1184 if (imageInputStream != null) |
|
1185 { |
|
1186 try |
|
1187 { |
|
1188 imageInputStream.close(); |
|
1189 imageInputStream = null; |
|
1190 } |
|
1191 catch (IOException ioe) |
|
1192 { |
|
1193 logError("Closing image InputStream failed", ioe); |
|
1194 } |
|
1195 } |
|
1196 } |
|
1197 return result; |
|
1198 } |
|
1199 |
|
1200 /** |
|
1201 * Loads image from specified InputStream. |
|
1202 * |
|
1203 * @param aDisplay display to which the icon will be added |
|
1204 * @param aInputStream InputStream where the image is loaded |
|
1205 * @param aImageName name of the image to be loaded |
|
1206 * @param aScaleImage flag telling if the loaded image should be scaled |
|
1207 * @return image from InputStream or null if image cannot be loaded |
|
1208 */ |
|
1209 private static Image loadImage( |
|
1210 Display aDisplay, InputStream aInputStream, |
1186 Display aDisplay, InputStream aInputStream, |
1211 String aImageName, boolean aScaleImage) |
1187 String aImageName, Point aImageSize) |
1212 { |
1188 { |
|
1189 String imageTableKey = aImageName + "," + aImageSize; |
1213 Image result = null; |
1190 Image result = null; |
1214 if (aImageName != null) |
1191 if (aImageName != null) |
1215 { |
1192 { |
1216 result = (Image)iImageTable.get(aImageName); |
1193 result = (Image)iImageTable.get(imageTableKey); |
1217 } |
1194 } |
1218 if (result != null) |
1195 if (result != null) |
1219 { |
1196 { |
1220 log("Using already loaded image " + aImageName); |
1197 log("Using already loaded image " + aImageName); |
1221 return result; |
1198 return result; |
1222 } |
1199 } |
1223 try |
1200 try |
1224 { |
1201 { |
1225 long startTime = System.currentTimeMillis(); |
1202 long startTime = System.currentTimeMillis(); |
1226 aDisplay.setData("org.eclipse.swt.internal.image.loadSize", |
1203 if (aImageSize != null) |
1227 iBestImageSize); |
1204 { |
1228 Image image = new Image(aDisplay, aInputStream); |
1205 aDisplay.setData("org.eclipse.swt.internal.image.loadSize", |
1229 if (aScaleImage) |
1206 aImageSize); |
1230 { |
1207 } |
1231 ImageData imageData = image.getImageData(); |
1208 result = new Image(aDisplay, aInputStream); |
1232 if (iBestImageSize.x != imageData.width || |
|
1233 iBestImageSize.y != imageData.height) |
|
1234 { |
|
1235 Point oldSize = |
|
1236 new Point(imageData.width, imageData.height); |
|
1237 imageData = imageData.scaledTo(iBestImageSize.x, iBestImageSize.y); |
|
1238 log("Image " + aImageName + " scaled from " + |
|
1239 oldSize.x + "x" + oldSize.y + " to " + |
|
1240 iBestImageSize.x + "x" + iBestImageSize.y); |
|
1241 image = new Image(aDisplay, imageData); |
|
1242 } |
|
1243 } |
|
1244 result = image; |
|
1245 long endTime = System.currentTimeMillis(); |
1209 long endTime = System.currentTimeMillis(); |
1246 log("Loaded image " + aImageName + " (load time " + |
1210 log("Loaded image " + aImageName + " (load time " + |
1247 (endTime - startTime) + " ms)"); |
1211 (endTime - startTime) + " ms)"); |
1248 iImageTable.put(aImageName, result); |
1212 iImageTable.put(imageTableKey, result); |
1249 } |
1213 } |
1250 catch (Throwable t) |
1214 catch (Throwable t) |
1251 { |
1215 { |
1252 log("Can not load image " + aImageName + ": " + t); |
1216 log("Can not load image " + aImageName + ": " + t); |
1253 //logError("Exception while loading image " + aImageName, t); |
1217 //logError("Exception while loading image " + aImageName, t); |