home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / magic oracle / util.java < prev   
Encoding:
Java Source  |  2000-06-23  |  5.4 KB  |  169 lines

  1. /**
  2.  *  Apple Worldwide Developer Technical Support
  3.  *
  4.  *  Sample demonstrating AppleScript for Java.
  5.  *
  6.  *  by Michael Hopkins and Levi Brown, Apple Developer Technical Support
  7.  *
  8.  *  File:   Util.java
  9.  *
  10.  *  Copyright ©1996 Apple Computer, Inc.
  11.  *  All rights reserved.
  12.  *
  13.  *    4/99    v. 1.0    Shipped as 'Magic Oracle AppleScript for Java' sample.
  14.  *
  15.  *  You may incorporate this sample code into your applications without
  16.  *  restriction, though the sample code has been provided "AS IS" and the
  17.  *  responsibility for its operation is 100% yours.  However, what you are
  18.  *  not permitted to do is to redistribute the source as "Apple Sample
  19.  *  Code" after having made changes. If you're going to re-distribute the
  20.  *  source, we require that you make it clear in the source that the code
  21.  *  was descended from Apple Sample Code, but that you've made changes.
  22. **/
  23.  
  24. import java.awt.*;
  25. import java.io.*;
  26. import java.net.URL;
  27.  
  28. public class Util
  29. {
  30.     public static final Color GSBColor  = Color.black;
  31.     public static final Color GSWColor  = Color.white;
  32.     public static final Color GS1Color  = new Color(238, 238, 238);
  33.     public static final Color GS2Color  = new Color(221, 221, 221);
  34.     public static final Color GS3Color  = new Color(204, 204, 204);
  35.     public static final Color GS4Color  = new Color(187, 187, 187);
  36.     public static final Color GS5Color  = new Color(170, 170, 170);
  37.     public static final Color GS6Color  = new Color(153, 153, 153);
  38.     public static final Color GS7Color  = new Color(136, 136, 136);
  39.     public static final Color GS8Color  = new Color(119, 119, 119);
  40.     public static final Color GS9Color  = new Color(102, 102, 102);
  41.     public static final Color GS10Color = new Color( 85,  85,  85);
  42.     public static final Color GS11Color = new Color( 68,  68,  68);
  43.     public static final Color GS12Color = new Color( 34,  34,  34);
  44.     public static final Color GSA1Color = new Color( 51,  51,  51);
  45.     public static final Color GSA2Color = new Color( 17,  17,  17);
  46.  
  47.     /**
  48.      * Completely loads the Image referenced by the given filename.
  49.      * This will block until the image is loaded.
  50.      * @param filename the path of the Image to load.
  51.      * @param watcher the component to use to load the image.
  52.      * @return the loaded Image, or null if the loading fails.
  53.      */
  54.     public static Image loadImage(String filename, Component watcher)
  55.     {
  56.         Image image = null;
  57.         
  58.         if (filename != null)
  59.         {
  60.             URL url = watcher.getClass().getResource(filename);
  61.             if (url == null)
  62.             {
  63.                 System.err.println("loadImage() could not find \"" + filename + "\"");
  64.             }
  65.             else
  66.             {
  67.                 image = watcher.getToolkit().getImage(url);
  68.                 if (image == null)
  69.                 {
  70.                     System.err.println("loadImage() getImage() failed for \"" + filename + "\"");
  71.                 }
  72.                 else
  73.                 {
  74.                     MediaTracker tracker = new MediaTracker(watcher);
  75.         
  76.                     try
  77.                     {
  78.                         tracker.addImage(image, 0);
  79.                         tracker.waitForID(0);
  80.                     }
  81.                     catch (InterruptedException e) { System.err.println("loadImage(): " + e); }
  82.                     finally
  83.                     {
  84.                         boolean isError = tracker.isErrorAny();
  85.                         if (isError)
  86.                         {
  87.                             System.err.println("loadImage() failed to load \"" + filename + "\"");
  88.                             int flags = tracker.statusAll(true);
  89.         
  90.                             boolean loading = 0 != (flags & MediaTracker.LOADING);
  91.                             boolean aborted = 0 != (flags & MediaTracker.ABORTED);
  92.                             boolean errored = 0 != (flags & MediaTracker.ERRORED);
  93.                             boolean complete = 0 != (flags & MediaTracker.COMPLETE);
  94.                             System.err.println("loading: " + loading);
  95.                             System.err.println("aborted: " + aborted);
  96.                             System.err.println("errored: " + errored);
  97.                             System.err.println("complete: " + complete);
  98.                         }
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.         
  104.         return image;
  105.     }
  106.  
  107.     /**
  108.      * Reads in a text file to a String.
  109.      * There is probably a more elegant solution, but this works.
  110.      * @param file the text file to read.
  111.      * @return the string containing the contents of the file.
  112.      * @throws java.io.FileNotFoundException if the given file can not
  113.      * be found to be read.
  114.      */
  115.     public static String readTextResourceFile(String filePath, Object localRef) throws FileNotFoundException
  116.     {
  117.         String result = "";
  118.         
  119.         if (filePath != null && localRef != null)
  120.         {
  121.             InputStream inStream = localRef.getClass().getResourceAsStream(filePath);
  122.             if (inStream == null)
  123.                 throw new FileNotFoundException("Could not find \"" + filePath + "\"");
  124.                 
  125.             //Create a new buffered input stream out of the raw input stream, with a buffer of 8k.
  126.             BufferedInputStream in = new BufferedInputStream(inStream, 8192);
  127.             try
  128.             {
  129.                 StringBuffer sb = new StringBuffer();
  130.                 
  131.                 int c = in.read();
  132.                 while (c != -1)
  133.                 {
  134.                     sb = sb.append((char)c);
  135.                     c = in.read();
  136.                 }
  137.                 
  138.                 result = new String(sb);
  139.             }
  140.             catch (IOException exc)
  141.             {
  142.                 exc.printStackTrace();
  143.             }
  144.         }
  145.         return result;
  146.     }
  147.     
  148.     /**
  149.      * Replaces a given substring with the supplied string in the source string.
  150.      * @param what is the substring of the source string to replace.
  151.      * @param with is the string to use instead.
  152.      * @param source is the original string.
  153.      * @return the modified string with all occurances of 'what' replaced by 'with'
  154.      */
  155.     public static String replace(String what, String with, String source)
  156.     {
  157.         String result = new String(source);
  158.         
  159.         int index = source.indexOf(what);
  160.         while (index >= 0)
  161.         {
  162.             result = (result.substring(0, index) + with + result.substring(index + what.length()));
  163.             index = result.indexOf(what);
  164.         }
  165.         
  166.         return result;
  167.     }
  168. }
  169.