javamanager/javainstaller/installer/javasrc/com/nokia/mj/impl/installer/midp2/install/steps/PrepareSplashScreen.java
changeset 56 abc41079b313
parent 50 023eef975703
equal deleted inserted replaced
50:023eef975703 56:abc41079b313
    33 import java.util.jar.JarEntry;
    33 import java.util.jar.JarEntry;
    34 
    34 
    35 import org.eclipse.swt.graphics.ImageData;
    35 import org.eclipse.swt.graphics.ImageData;
    36 import org.eclipse.swt.graphics.ImageLoader;
    36 import org.eclipse.swt.graphics.ImageLoader;
    37 import org.eclipse.swt.graphics.Point;
    37 import org.eclipse.swt.graphics.Point;
       
    38 import org.eclipse.swt.internal.extension.ImageUtil;
    38 
    39 
    39 /**
    40 /**
    40  * Installation step PrepareSplashScreen prepares splash
    41  * Installation step PrepareSplashScreen prepares splash
    41  * screen images from Nokia-MIDlet-Splash-Screen-Image attribute.
    42  * screen images from Nokia-MIDlet-Splash-Screen-Image attribute.
    42  */
    43  */
   104             String[] tokens = Tokenizer.split(attrValue, ",");
   105             String[] tokens = Tokenizer.split(attrValue, ",");
   105             Point[] imageSizes = new Point[tokens.length];
   106             Point[] imageSizes = new Point[tokens.length];
   106             for (int i = 0; i < tokens.length; i++)
   107             for (int i = 0; i < tokens.length; i++)
   107             {
   108             {
   108                 tokens[i] = tokens[i].trim();
   109                 tokens[i] = tokens[i].trim();
   109                 currentImage = loadImage(tokens[i], ball.iJarFilename);
   110                 imageSizes[i] = getImageSize(tokens[i], ball.iJarFilename);
   110                 if (currentImage == null)
   111                 if (imageSizes[i] == null)
   111                 {
   112                 {
   112                     // Image loading failed, proceed to the next image.
   113                     // Couldn't get image size, try to get it by loading
   113                     imageSizes[i] = new Point(0, 0);
   114                     // the image.
   114                     continue;
   115                     currentImage = loadImage(tokens[i], ball.iJarFilename);
   115                 }
   116                     if (currentImage == null)
   116                 imageSizes[i] = new Point(
   117                     {
   117                     currentImage[0].width, currentImage[0].height);
   118                         // Image loading failed, proceed to the next image.
       
   119                         imageSizes[i] = new Point(0, 0);
       
   120                         continue;
       
   121                     }
       
   122                     imageSizes[i] = new Point(
       
   123                         currentImage[0].width, currentImage[0].height);
       
   124                 }
   118             }
   125             }
   119             // Choose the images which best fill the portrait and
   126             // Choose the images which best fill the portrait and
   120             // landscape screens.
   127             // landscape screens.
   121             String portraitImageName = null;
   128             String portraitImageName = null;
   122             String landscapeImageName = null;
   129             String landscapeImageName = null;
   235         }
   242         }
   236         return result;
   243         return result;
   237     }
   244     }
   238 
   245 
   239     /**
   246     /**
       
   247      * Gets image size for specified image from given jar file.
       
   248      *
       
   249      * @param aResource image file name
       
   250      * @param aJar jar file name
       
   251      * @return image size, or null if getting image size fails
       
   252      */
       
   253     private static Point getImageSize(String aResource, String aJar)
       
   254     {
       
   255         Point result = null;
       
   256         JarFile jarFile = null;
       
   257         InputStream is = null;
       
   258         try
       
   259         {
       
   260             // Open jar file and input stream.
       
   261             jarFile = new JarFile(aJar);
       
   262             is = jarFile.getInputStream(
       
   263                 new JarEntry(FileUtils.trimJarEntry(aResource)));
       
   264             if (is != null)
       
   265             {
       
   266                 result = ImageUtil.getImageSize(is);
       
   267                 if (result != null)
       
   268                 {
       
   269                     Log.log("Image size for " + aResource + " from " +
       
   270                             aJar + ": " + result);
       
   271                 }
       
   272             }
       
   273             else
       
   274             {
       
   275                 Log.logWarning("Image " + aResource + " not found from " + aJar);
       
   276             }
       
   277         }
       
   278         catch (Throwable t)
       
   279         {
       
   280             Log.logWarning("Loading image " + aResource + " from " +
       
   281                            aJar + " failed", t);
       
   282         }
       
   283         finally
       
   284         {
       
   285             // Close streams and jar file.
       
   286             if (is != null)
       
   287             {
       
   288                 try
       
   289                 {
       
   290                     is.close();
       
   291                     is = null;
       
   292                 }
       
   293                 catch (IOException ioe)
       
   294                 {
       
   295                     Log.logWarning("Closing InputStream failed", ioe);
       
   296                 }
       
   297             }
       
   298             if (jarFile != null)
       
   299             {
       
   300                 try
       
   301                 {
       
   302                     jarFile.close();
       
   303                     jarFile = null;
       
   304                 }
       
   305                 catch (IOException ioe)
       
   306                 {
       
   307                     Log.logWarning("Closing " + aJar + " failed", ioe);
       
   308                 }
       
   309             }
       
   310         }
       
   311         return result;
       
   312     }
       
   313 
       
   314     /**
   240      * Loads image from specified resource from given jar file.
   315      * Loads image from specified resource from given jar file.
   241      *
   316      *
   242      * @param aResource resource file name
   317      * @param aResource resource file name
   243      * @param aJar jar file name
   318      * @param aJar jar file name
   244      * @return loaded image, or null if image loading fails
   319      * @return loaded image, or null if image loading fails
   250         InputStream is = null;
   325         InputStream is = null;
   251         try
   326         try
   252         {
   327         {
   253             // Open jar file and input stream.
   328             // Open jar file and input stream.
   254             jarFile = new JarFile(aJar);
   329             jarFile = new JarFile(aJar);
   255             is = jarFile.getInputStream
   330             is = jarFile.getInputStream(
   256                  (new JarEntry(FileUtils.trimJarEntry(aResource)));
   331                 new JarEntry(FileUtils.trimJarEntry(aResource)));
   257             if (is != null)
   332             if (is != null)
   258             {
   333             {
   259                 result = (new ImageLoader()).load(is);
   334                 result = (new ImageLoader()).load(is);
   260                 Log.log("Loaded image " + aResource + " from " + aJar);
   335                 Log.log("Loaded image " + aResource + " from " + aJar);
   261             }
   336             }
   317         String imageFilename = aFilename + ".img";
   392         String imageFilename = aFilename + ".img";
   318         try
   393         try
   319         {
   394         {
   320             // Open jar file and input and output streams.
   395             // Open jar file and input and output streams.
   321             jarFile = new JarFile(aJar);
   396             jarFile = new JarFile(aJar);
   322             is = jarFile.getInputStream
   397             is = jarFile.getInputStream(
   323                  (new JarEntry(FileUtils.trimJarEntry(aResource)));
   398                 new JarEntry(FileUtils.trimJarEntry(aResource)));
   324             os = FileUtils.getOutputStream(imageFilename);
   399             os = FileUtils.getOutputStream(imageFilename);
   325             // Copy the image data from InputStream to OutputStream.
   400             // Copy the image data from InputStream to OutputStream.
   326             byte[] buf = new byte[16384];
   401             byte[] buf = new byte[16384];
   327             int i = 0;
   402             int i = 0;
   328             while ((i = is.read(buf)) != -1)
   403             while ((i = is.read(buf)) != -1)