/*
 * @(#)JPTUtilities.java    1.0  21 October 2003
 *
 * Copyright 2004
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.util;

import java.awt.*;

/**
 * <P>Provides general utility methods 
 * used by various classes in the JPT.</P>
 *
 * <P>Several JPT classes can notfy a programmer
 * of a grave programming error by throwing a
 * <CODE>{@link JPTError JPTError}</CODE.
 *
 * If debug mode is set to <CODE>true</CODE> using the 
 * <CODE>{@link #setDebugMode(boolean) setDebugMode}</CODE>
 * method of this class, a <CODE>JPTError</CODE>
 * will be handled by the JPT before it is thrown,
 * and a dialog box will be shown
 * to notify the programmer of the error.
 *
 * If debug mode is set to <CODE>false</CODE>,
 * as it is by default, a <CODE>JPTError</CODE>
 * will be treated almost exactly the same as
 * any other instance of the <CODE>Error</CODE> class.</P>
 *
 * @author  Jeff Raab
 * @version 2.2
 * @since   1.0
 */
public class JPTUtilities implements JPTConstants {
    
    /** Debug flag for the toolkit. */
    private static boolean DEBUG = false;

    /**
     * Returns the 
     * <CODE>{@link BorderLayout BorderLayout}</CODE>
     * constant corresponding with 
     * the given alignment constant.
     *
     * @param alignment the desired alignment constant
     */
    public static String getBorderLayoutLocation(
        int alignment) 
    {
        switch (alignment) {
            case LEFT:
                return BorderLayout.WEST;
            case RIGHT:
                return BorderLayout.EAST;
            case ABOVE:
                return BorderLayout.NORTH;
            case BELOW:
                return BorderLayout.SOUTH;
            default:
                return BorderLayout.CENTER;
        }
    }

    /**
     * Returns the toolkit debug flag.
     *
     * @see #setDebugMode(boolean)
     */
    public static boolean isDebugModeSet() {
        return DEBUG;
    }

    /**
     * Pauses the calling thread for at most 
     * the given number of milliseconds.
     *
     * Since the <CODE>Thread.sleep</CODE> method 
     * is not perfectly timed to millisecond precision, 
     * the actual number of milliseconds the thread pauses
     * will be slightly greater than the given number.
     *
     * If the calling thread is interrupted, this method will return
     * before the given number of milliseconds have elapsed.
     *
     * If the given number of millseconds is less than zero,
     * the calling thread is not paused.
     *
     * @param milliseconds the length of time in milliseconds
     *      to pause the calling thread
     */
    public static void pauseThread(long milliseconds) {
        if (milliseconds <= 0)
            return;
            
        try {
            Thread.sleep(milliseconds);
        }
        catch (InterruptedException ex) {
        }
    }

    /**
     * Sets the debug flag for the toolkit
     * to the given value.
     *
     * @param flag whether or not the toolkit 
     *      is in debug state
     * @see #isDebugModeSet()
     */
    public static void setDebugMode(boolean flag) {
        DEBUG = flag;
    }
}
