create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / help / tours / Slide.java
1 package net.sf.openrocket.gui.help.tours;
2
3 import java.awt.image.BufferedImage;
4 import java.io.IOException;
5 import java.lang.ref.SoftReference;
6 import java.net.URL;
7
8 import javax.imageio.ImageIO;
9
10 import net.sf.openrocket.util.BugException;
11
12 /**
13  * An individual slide in a guided tour.  It contains a image (or reference to an
14  * image file) plus a text description (in HTML).
15  * 
16  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
17  */
18 public class Slide {
19         private static final String NO_IMAGE = "none";
20         
21         private final String imageFile;
22         private SoftReference<BufferedImage> imageReference = null;
23         
24         private final String text;
25         
26         
27         
28         public Slide(String imageFile, String text) {
29                 this.imageFile = imageFile;
30                 this.text = text;
31         }
32         
33         
34         
35         public BufferedImage getImage() {
36                 
37                 if (imageFile.equals(NO_IMAGE)) {
38                         return new BufferedImage(0, 0, BufferedImage.TYPE_INT_ARGB);
39                 }
40                 
41                 // Check the cache
42                 if (imageReference != null) {
43                         BufferedImage image = imageReference.get();
44                         if (image != null) {
45                                 return image;
46                         }
47                 }
48                 
49                 // Otherwise load and cache
50                 BufferedImage image = loadImage();
51                 imageReference = new SoftReference<BufferedImage>(image);
52                 
53                 return image;
54         }
55         
56         public String getText() {
57                 return text;
58         }
59         
60         
61         
62         private BufferedImage loadImage() {
63                 BufferedImage img;
64                 
65                 try {
66                         URL url = ClassLoader.getSystemResource(imageFile);
67                         if (url != null) {
68                                 img = ImageIO.read(url);
69                         } else {
70                                 throw new BugException("Could not find image " + imageFile);
71                         }
72                 } catch (IOException e) {
73                         throw new BugException("Error reading image " + imageFile, e);
74                 }
75                 
76                 return img;
77         }
78 }