View Javadoc

1   package fr.ove.openmath.jome.ctrlview.bidim.images;
2   
3   import java.applet.*;
4   import java.awt.*;
5   import java.util.*;
6   import java.io.*;
7   import fr.ove.openmath.jome.ctrlview.bidim.images.*;
8   import fr.ove.utils.ByteVector;
9   
10  /***
11  * This class is a little tool to load images.
12  *
13  * @author © 1998 DIRAT Laurent
14  * @version 1.0  17/02/99
15  */
16  public class ImageLoader implements Serializable {
17      /***
18      * The hashtable which contains all the images loaded.
19      */
20      private static Hashtable allImages;
21      // On utilise un mediatracker pour pouvoir attendre que toutes les
22      // images soient chargÈes avant de rendre la main.
23  	private static MediaTracker tracker;
24      // On rÈcupËre la toolkit par dÈfaut pour crÈer les images.
25      private static Toolkit toolkit;
26      private static Class theClass;
27      
28      /***
29      * The image resources manager.
30      */
31      private static ImagesResourcesManager imagesResourcesManager = new ImagesResourcesManager("fr.ove.openmath.jome.ctrlview.bidim.images.ImagesResources");
32      
33      /***
34      * The Constructor.<BR>
35      * This constructor is used in the case of an application, and not an applet such
36      * as the precedent does.
37      * @param component the component which needs the images to load.
38      */
39      public ImageLoader(Component component) {
40  		theClass = getClass();
41  
42          // On utilise un mediatracker pour pouvoir attendre que toutes les
43          // images soient chargÈes avant de rendre la main.
44  		tracker = new MediaTracker(component);
45          allImages = new Hashtable(10);  // On crÈe la table de Hashcode
46          
47          // On rÈcupËre la toolkit par dÈfaut pour crÈer les images.
48          toolkit = Toolkit.getDefaultToolkit();
49          
50          // On charge les parenthËses pour que ce soit un peu plus rapide (c'est a priori
51          // les images les plus suceptibles d'Ítre chargÈes)
52          getImage("LeftTopPar");
53          getImage("LeftMiddlePar");
54          getImage("LeftBottomPar");
55          getImage("RightTopPar");
56          getImage("RightMiddlePar");
57          getImage("RightBottomPar");
58      }
59  
60      /***
61      * Returns the image with the specified name.
62      * @param name the name of the desired image.
63      * @return the desired image.
64      */
65      public static Image getImage(String name) {
66          Image image = (Image) allImages.get(name);
67  
68          if (image == null)
69              image = setImage(name);
70  
71          return image;
72      }
73      
74      
75      /***
76      * Returns the height factor to apply to the orignal size of the wanted image.
77      * @param idImage the identifier of the wanted image
78      */
79      public static float getHeightFactor(String idImage) {
80          float heightFactor = 1.0f;
81          
82          try {
83              String height = imagesResourcesManager.getHeightFactor(idImage);
84              if (height != null)
85                  heightFactor = Float.valueOf(height).floatValue();
86          }
87          catch (NumberFormatException fne) {
88              System.out.println("height factor is not a valid number.");
89          }
90          catch (Exception e) {
91              System.out.println("height factor is not a valid number."); // erreur d'un autre type
92          }
93          
94          return heightFactor;
95      }    
96      
97      /***
98      * Returns the baseline of the wanted image. If the returned value equals -1n the baseline corresponds
99      * the one of the current font.
100     * @param idImage the identifier of the wanted image
101     */
102     public static float getBaseline(String idImage) {
103         float baseline = -1.0f; // Si -1, alors la baseline est celle de la police courante.
104         
105         try {
106             String base = imagesResourcesManager.getBaseline(idImage);
107             if (base != null)
108                 baseline = Float.valueOf(base).floatValue();
109         }
110         catch (NumberFormatException fne) {
111             System.out.println("baseline has not a valid number.");
112         }
113         catch (Exception e) {
114             System.out.println("baseline has not a valid number."); // erreur d'un autre type
115         }
116         
117         return baseline;
118     }
119     
120     /***
121     * @param idImage the identifier of the wanted image
122     */
123     public static float getTopInset(String idImage) {
124         float inset = 0.0f;
125         
126         try {
127             String srtInset = imagesResourcesManager.getTopInset(idImage);
128             if (srtInset != null)
129                 inset = Float.valueOf(srtInset).floatValue();
130         }
131         catch (NumberFormatException fne) {
132             System.out.println("top inset has not a valid number.");
133         }
134         catch (Exception e) {
135             System.out.println("top inset has not a valid number."); // erreur d'un autre type
136         }
137         
138         return inset;
139     }
140     
141     /***
142     * @param idImage the identifier of the wanted image
143     */
144     public static float getBottomInset(String idImage) {
145         float inset = 0.0f;
146         
147         try {
148             String srtInset = imagesResourcesManager.getBottomInset(idImage);
149             if (srtInset != null)
150                 inset = Float.valueOf(srtInset).floatValue();
151         }
152         catch (NumberFormatException fne) {
153             System.out.println("bottom inset has not a valid number.");
154         }
155         catch (Exception e) {
156             System.out.println("bottom inset has not a valid number."); // erreur d'un autre type
157         }
158         
159         return inset;
160     }
161     
162     /***
163     * @param idImage the identifier of the wanted image
164     */
165     public static float getLeftInset(String idImage) {
166         float inset = 0.0f;
167         
168         try {
169             String srtInset = imagesResourcesManager.getLeftInset(idImage);
170             if (srtInset != null)
171                 inset = Float.valueOf(srtInset).floatValue();
172         }
173         catch (NumberFormatException fne) {
174             System.out.println("left inset has not a valid number.");
175         }
176         catch (Exception e) {
177             System.out.println("left inset has not a valid number."); // erreur d'un autre type
178         }
179         
180         return inset;
181     }
182     
183     /***
184     * @param idImage the identifier of the wanted image
185     */
186     public static float getRightInset(String idImage) {
187         float inset = 0.0f;
188         
189         try {
190             String srtInset = imagesResourcesManager.getRightInset(idImage);
191             if (srtInset != null)
192                 inset = Float.valueOf(srtInset).floatValue();
193         }
194         catch (NumberFormatException fne) {
195             System.out.println("right inset has not a valid number.");
196         }
197         catch (Exception e) {
198             System.out.println("right inset has not a valid number."); // erreur d'un autre type
199         }
200         
201         return inset;
202     }
203         
204     /***
205     * Checks if there exist an image with the specified identifier.
206     * @param idImage the identifier of the wanted image
207     */
208     public static boolean exists(String idImage) {
209         return (imagesResourcesManager.getImageName(idImage) == null);
210     }
211     
212     
213     
214     /***
215     * Loads the image with the specified name and put it in the hashtable with the specified key.
216     * @param name the name of theimage to load
217     * @param key the key of the image in the hastable
218     * @return the desired image
219     */
220     private static Image loadImage(String name, String key) throws Exception {
221         Image image = null;
222 		java.io.InputStream iStream = theClass.getResourceAsStream(name);
223         try {
224             int read;
225             ByteVector byteImage = new ByteVector();
226 		    while ((read = iStream.read()) != -1) {
227 		        byteImage.addElement((byte) read);
228 		        if (iStream.available() == 0)
229 		            break;
230 		    }
231             
232             image = toolkit.createImage(byteImage.getBytes());
233             iStream.close();
234         }
235         catch (IOException e){
236             System.out.println("Impossible to read image : IO problems");
237             e.printStackTrace();
238         }
239         catch (Exception e){
240             System.out.println("Impossible to create image");
241             e.printStackTrace();
242         }
243         
244         if (image != null) {
245             allImages.put(key, image);
246             tracker.addImage(image, 0);
247         
248             // On attend que toutes les images soient chargÈes.
249     		try { 
250     		    tracker.waitForAll();
251     		}
252     		catch (InterruptedException e) {
253     		    System.out.print(e.toString());
254     		}
255     		
256     		if (tracker.isErrorAny())
257     		    throw new Exception("Impossible to load image");
258         }
259         return image;
260     }
261     
262     private static Image setImage(String name) {
263         Image image = null;
264         try {
265             String imgName = imagesResourcesManager.getImageName(name);
266             
267             if (imgName == null)
268                 image = getImage("undef");
269             else
270                 image = loadImage(imgName, name);
271                         
272             return image;
273         }
274         catch (Exception e) {
275             e.printStackTrace();
276             return image;
277         }
278     }
279 }