Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
Util.java
/** |
* Apple Worldwide Developer Technical Support |
* |
* Sample demonstrating AppleScript for Java. |
* |
* by Michael Hopkins and Levi Brown, Apple Developer Technical Support |
* |
* File: Util.java |
* |
* Copyright ©1996 Apple Computer, Inc. |
* All rights reserved. |
* |
* 4/99 v. 1.0 Shipped as 'Magic Oracle AppleScript for Java' sample. |
* |
* You may incorporate this sample code into your applications without |
* restriction, though the sample code has been provided "AS IS" and the |
* responsibility for its operation is 100% yours. However, what you are |
* not permitted to do is to redistribute the source as "Apple Sample |
* Code" after having made changes. If you're going to re-distribute the |
* source, we require that you make it clear in the source that the code |
* was descended from Apple Sample Code, but that you've made changes. |
**/ |
import java.awt.*; |
import java.io.*; |
import java.net.URL; |
public class Util |
{ |
public static final Color GSBColor = Color.black; |
public static final Color GSWColor = Color.white; |
public static final Color GS1Color = new Color(238, 238, 238); |
public static final Color GS2Color = new Color(221, 221, 221); |
public static final Color GS3Color = new Color(204, 204, 204); |
public static final Color GS4Color = new Color(187, 187, 187); |
public static final Color GS5Color = new Color(170, 170, 170); |
public static final Color GS6Color = new Color(153, 153, 153); |
public static final Color GS7Color = new Color(136, 136, 136); |
public static final Color GS8Color = new Color(119, 119, 119); |
public static final Color GS9Color = new Color(102, 102, 102); |
public static final Color GS10Color = new Color( 85, 85, 85); |
public static final Color GS11Color = new Color( 68, 68, 68); |
public static final Color GS12Color = new Color( 34, 34, 34); |
public static final Color GSA1Color = new Color( 51, 51, 51); |
public static final Color GSA2Color = new Color( 17, 17, 17); |
/** |
* Completely loads the Image referenced by the given filename. |
* This will block until the image is loaded. |
* @param filename the path of the Image to load. |
* @param watcher the component to use to load the image. |
* @return the loaded Image, or null if the loading fails. |
*/ |
public static Image loadImage(String filename, Component watcher) |
{ |
Image image = null; |
if (filename != null) |
{ |
URL url = watcher.getClass().getResource(filename); |
if (url == null) |
{ |
System.err.println("loadImage() could not find \"" + filename + "\""); |
} |
else |
{ |
image = watcher.getToolkit().getImage(url); |
if (image == null) |
{ |
System.err.println("loadImage() getImage() failed for \"" + filename + "\""); |
} |
else |
{ |
MediaTracker tracker = new MediaTracker(watcher); |
try |
{ |
tracker.addImage(image, 0); |
tracker.waitForID(0); |
} |
catch (InterruptedException e) { System.err.println("loadImage(): " + e); } |
finally |
{ |
boolean isError = tracker.isErrorAny(); |
if (isError) |
{ |
System.err.println("loadImage() failed to load \"" + filename + "\""); |
int flags = tracker.statusAll(true); |
boolean loading = 0 != (flags & MediaTracker.LOADING); |
boolean aborted = 0 != (flags & MediaTracker.ABORTED); |
boolean errored = 0 != (flags & MediaTracker.ERRORED); |
boolean complete = 0 != (flags & MediaTracker.COMPLETE); |
System.err.println("loading: " + loading); |
System.err.println("aborted: " + aborted); |
System.err.println("errored: " + errored); |
System.err.println("complete: " + complete); |
} |
} |
} |
} |
} |
return image; |
} |
/** |
* Reads in a text file to a String. |
* There is probably a more elegant solution, but this works. |
* @param file the text file to read. |
* @return the string containing the contents of the file. |
* @throws java.io.FileNotFoundException if the given file can not |
* be found to be read. |
*/ |
public static String readTextResourceFile(String filePath, Object localRef) throws FileNotFoundException |
{ |
String result = ""; |
if (filePath != null && localRef != null) |
{ |
InputStream inStream = localRef.getClass().getResourceAsStream(filePath); |
if (inStream == null) |
throw new FileNotFoundException("Could not find \"" + filePath + "\""); |
//Create a new buffered input stream out of the raw input stream, with a buffer of 8k. |
BufferedInputStream in = new BufferedInputStream(inStream, 8192); |
try |
{ |
StringBuffer sb = new StringBuffer(); |
int c = in.read(); |
while (c != -1) |
{ |
sb = sb.append((char)c); |
c = in.read(); |
} |
result = new String(sb); |
} |
catch (IOException exc) |
{ |
exc.printStackTrace(); |
} |
} |
return result; |
} |
/** |
* Replaces a given substring with the supplied string in the source string. |
* @param what is the substring of the source string to replace. |
* @param with is the string to use instead. |
* @param source is the original string. |
* @return the modified string with all occurances of 'what' replaced by 'with' |
*/ |
public static String replace(String what, String with, String source) |
{ |
String result = new String(source); |
int index = source.indexOf(what); |
while (index >= 0) |
{ |
result = (result.substring(0, index) + with + result.substring(index + what.length())); |
index = result.indexOf(what); |
} |
return result; |
} |
} |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-07-22