01: /*
02: * ImageLoader.java
03: *
04: * Copyright (C) 2000-2002 Peter Graves
05: * $Id: ImageLoader.java,v 1.2 2002/11/27 23:59:50 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.j;
23:
24: import java.awt.Image;
25: import java.awt.MediaTracker;
26: import java.awt.Toolkit;
27: import java.lang.reflect.Method;
28:
29: public final class ImageLoader {
30: private File file;
31: private MediaTracker mt;
32: private Image image;
33:
34: public ImageLoader(File file) {
35: this .file = file;
36: }
37:
38: public Image loadImage() {
39: final Editor editor = Editor.currentEditor();
40: editor.setWaitCursor();
41: image = Toolkit.getDefaultToolkit().createImage(
42: file.canonicalPath());
43: mt = new MediaTracker(editor);
44: try {
45: mt.addImage(image, 0);
46: mt.waitForID(0);
47: } catch (Exception e) {
48: Log.error(e);
49: }
50: if (mt.isErrorAny())
51: image = null;
52: if (image == null) {
53: // Try again using JIMI.
54: try {
55: Class c = Class.forName("com.sun.jimi.core.Jimi");
56: Class[] parameterTypes = new Class[1];
57: parameterTypes[0] = Class.forName("java.lang.String");
58: Method method = c.getMethod("getImage", parameterTypes);
59: Object[] args = new Object[1];
60: args[0] = file.canonicalPath();
61: Object returned = method.invoke(null, args);
62: if (returned instanceof Image)
63: image = (Image) returned;
64: } catch (ClassNotFoundException e) {
65: // JIMI not found.
66: } catch (Exception e) {
67: Log.error(e);
68: }
69: mt = new MediaTracker(editor);
70: try {
71: mt.addImage(image, 0);
72: mt.waitForID(0);
73: } catch (Exception e) {
74: Log.error(e);
75: }
76: if (mt.isErrorAny())
77: image = null;
78: }
79: editor.setDefaultCursor();
80: return image;
81: }
82:
83: public void dispose() {
84: if (image != null && mt != null) {
85: mt.removeImage(image);
86: image.flush();
87: image = null;
88: mt = null;
89: }
90: }
91: }
|