Back to index

wims  3.65+svn20090927
ImageLoader.java
Go to the documentation of this file.
00001 /*
00002 $Id: ImageLoader.java,v 1.3 2003/02/18 11:48:47 sander Exp $
00003 */
00004 
00005 
00006 /*
00007 Copyright (C) 2001-2002 Mainline Project (I3S - ESSI - CNRS -UNSA)
00008 
00009 This library is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU Lesser General Public
00011 License as published by the Free Software Foundation; either
00012 version 2.1 of the License, or (at your option) any later version.
00013 
00014 This library is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public
00020 License along with this library; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023 For further information on the GNU Lesser General Public License,
00024 see: http://www.gnu.org/copyleft/lesser.html
00025 For further information on this library, contact: mainline@essi.fr
00026 */
00027 
00028 
00029 package fr.ove.openmath.jome.ctrlview.bidim.images;
00030 
00031 import java.applet.*;
00032 import java.awt.*;
00033 import java.util.*;
00034 import java.io.*;
00035 import fr.ove.openmath.jome.ctrlview.bidim.images.*;
00036 import fr.ove.utils.ByteVector;
00037 
00044 public class ImageLoader implements Serializable {
00048     private static Hashtable allImages;
00049     // On utilise un mediatracker pour pouvoir attendre que toutes les
00050     // images soient chargées avant de rendre la main.
00051        private static MediaTracker tracker;
00052     // On récupère la toolkit par défaut pour créer les images.
00053     private static Toolkit toolkit;
00054     private static Class theClass;
00055     
00059     private static ImagesResourcesManager imagesResourcesManager = new ImagesResourcesManager("fr.ove.openmath.jome.ctrlview.bidim.images.ImagesResources");
00060     
00067     public ImageLoader(Component component) {
00068               theClass = getClass();
00069 
00070         // On utilise un mediatracker pour pouvoir attendre que toutes les
00071         // images soient chargées avant de rendre la main.
00072               tracker = new MediaTracker(component);
00073         allImages = new Hashtable(10);  // On crée la table de Hashcode
00074         
00075         // On récupère la toolkit par défaut pour créer les images.
00076         toolkit = Toolkit.getDefaultToolkit();
00077         
00078         // On charge les parenthèses pour que ce soit un peu plus rapide (c'est a priori
00079         // les images les plus suceptibles d'être chargées)
00080         getImage("LeftTopPar");
00081         getImage("LeftMiddlePar");
00082         getImage("LeftBottomPar");
00083         getImage("RightTopPar");
00084         getImage("RightMiddlePar");
00085         getImage("RightBottomPar");
00086     }
00087 
00093     public static Image getImage(String name) {
00094         Image image = (Image) allImages.get(name);
00095 
00096         if (image == null)
00097             image = setImage(name);
00098 
00099         return image;
00100     }
00101     
00102     
00107     public static float getHeightFactor(String idImage) {
00108         float heightFactor = 1.0f;
00109         
00110         try {
00111             String height = imagesResourcesManager.getHeightFactor(idImage);
00112             if (height != null)
00113                 heightFactor = Float.valueOf(height).floatValue();
00114         }
00115         catch (NumberFormatException fne) {
00116             System.out.println("height factor is not a valid number.");
00117         }
00118         catch (Exception e) {
00119             System.out.println("height factor is not a valid number."); // erreur d'un autre type
00120         }
00121         
00122         return heightFactor;
00123     }    
00124     
00130     public static float getBaseline(String idImage) {
00131         float baseline = -1.0f; // Si -1, alors la baseline est celle de la police courante.
00132         
00133         try {
00134             String base = imagesResourcesManager.getBaseline(idImage);
00135             if (base != null)
00136                 baseline = Float.valueOf(base).floatValue();
00137         }
00138         catch (NumberFormatException fne) {
00139             System.out.println("baseline has not a valid number.");
00140         }
00141         catch (Exception e) {
00142             System.out.println("baseline has not a valid number."); // erreur d'un autre type
00143         }
00144         
00145         return baseline;
00146     }
00147     
00151     public static float getTopInset(String idImage) {
00152         float inset = 0.0f;
00153         
00154         try {
00155             String srtInset = imagesResourcesManager.getTopInset(idImage);
00156             if (srtInset != null)
00157                 inset = Float.valueOf(srtInset).floatValue();
00158         }
00159         catch (NumberFormatException fne) {
00160             System.out.println("top inset has not a valid number.");
00161         }
00162         catch (Exception e) {
00163             System.out.println("top inset has not a valid number."); // erreur d'un autre type
00164         }
00165         
00166         return inset;
00167     }
00168     
00172     public static float getBottomInset(String idImage) {
00173         float inset = 0.0f;
00174         
00175         try {
00176             String srtInset = imagesResourcesManager.getBottomInset(idImage);
00177             if (srtInset != null)
00178                 inset = Float.valueOf(srtInset).floatValue();
00179         }
00180         catch (NumberFormatException fne) {
00181             System.out.println("bottom inset has not a valid number.");
00182         }
00183         catch (Exception e) {
00184             System.out.println("bottom inset has not a valid number."); // erreur d'un autre type
00185         }
00186         
00187         return inset;
00188     }
00189     
00193     public static float getLeftInset(String idImage) {
00194         float inset = 0.0f;
00195         
00196         try {
00197             String srtInset = imagesResourcesManager.getLeftInset(idImage);
00198             if (srtInset != null)
00199                 inset = Float.valueOf(srtInset).floatValue();
00200         }
00201         catch (NumberFormatException fne) {
00202             System.out.println("left inset has not a valid number.");
00203         }
00204         catch (Exception e) {
00205             System.out.println("left inset has not a valid number."); // erreur d'un autre type
00206         }
00207         
00208         return inset;
00209     }
00210     
00214     public static float getRightInset(String idImage) {
00215         float inset = 0.0f;
00216         
00217         try {
00218             String srtInset = imagesResourcesManager.getRightInset(idImage);
00219             if (srtInset != null)
00220                 inset = Float.valueOf(srtInset).floatValue();
00221         }
00222         catch (NumberFormatException fne) {
00223             System.out.println("right inset has not a valid number.");
00224         }
00225         catch (Exception e) {
00226             System.out.println("right inset has not a valid number."); // erreur d'un autre type
00227         }
00228         
00229         return inset;
00230     }
00231         
00236     public static boolean exists(String idImage) {
00237         return (imagesResourcesManager.getImageName(idImage) == null);
00238     }
00239     
00240     
00241     
00248     private static Image loadImage(String name, String key) throws Exception {
00249         Image image = null;
00250               java.io.InputStream iStream = theClass.getResourceAsStream(name);
00251         try {
00252             int read;
00253             ByteVector byteImage = new ByteVector();
00254                   while ((read = iStream.read()) != -1) {
00255                       byteImage.addElement((byte) read);
00256                       if (iStream.available() == 0)
00257                           break;
00258                   }
00259             
00260             image = toolkit.createImage(byteImage.getBytes());
00261             iStream.close();
00262         }
00263         catch (IOException e){
00264             System.out.println("Impossible to read image : IO problems");
00265             e.printStackTrace();
00266         }
00267         catch (Exception e){
00268             System.out.println("Impossible to create image");
00269             e.printStackTrace();
00270         }
00271         
00272         if (image != null) {
00273             allImages.put(key, image);
00274             tracker.addImage(image, 0);
00275         
00276             // On attend que toutes les images soient chargées.
00277               try { 
00278                   tracker.waitForAll();
00279               }
00280               catch (InterruptedException e) {
00281                   System.out.print(e.toString());
00282               }
00283               
00284               if (tracker.isErrorAny())
00285                   throw new Exception("Impossible to load image");
00286         }
00287         return image;
00288     }
00289     
00290     private static Image setImage(String name) {
00291         Image image = null;
00292         try {
00293             String imgName = imagesResourcesManager.getImageName(name);
00294             
00295             if (imgName == null)
00296                 image = getImage("undef");
00297             else
00298                 image = loadImage(imgName, name);
00299                         
00300             return image;
00301         }
00302         catch (Exception e) {
00303             e.printStackTrace();
00304             return image;
00305         }
00306     }
00307 }