package ls.util;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.ImageIcon;
import ls.util.msg.ErrorHandler;
import ls.util.msg.MessageHandler;
import ls.util.msg.MessageManager;
/**
* A general utility class
*
* @author Lior Schejter
*/
public class Utils
{
/**
* A message manager instance to handle various message managers
*/
private static MessageManager messageManager = new MessageManager();
/**
* Add a message handler to the list of handlers used by this class's message manager
*
* @param handler The handler to add
* @see #messageManager
* @see MessageManager
* @see MessageHandler
*/
public static void addMessageHandler(MessageHandler handler)
{
if (handler == null) return;
messageManager.registerMessageHandler(handler);
}
/**
* Add an error handler to this class's message manager
* @param eh The error handler to add
* @see #messageManager
* @see MessageManager
* @see ErrorHandler
*/
public static void addErrorHandler(ErrorHandler eh)
{
if (eh == null) return;
messageManager.registerErrorHandler(eh);
}
/* (non-Javadoc)
* @see ls.util.msg.ErrorHandler#handleException(java.lang.Exception)
*/
public static void exn(Exception exn)
{
messageManager.exn(exn);
}
/**
* Output an error message.
* @param errMsg The error message to output.
*/
public static void err(String errMsg)
{
messageManager.err("Error: " + errMsg);
}
/* (non-Javadoc)
* @see ls.util.msg.MessageHandler#handleMsg(java.lang.String)
*/
public static void msg(String _msg)
{
messageManager.msg(_msg);
}
public static final String EMPTY_STRING = "";
/**
* Return a new collection, with all the elements of the given iterator
* @param iter
* @return The new collection containing all elements of the given iterator
*/
public static <E> Collection<E> collectionFromIterator(Iterator<E> iter)
{
Collection<E> ret = new ArrayList<E>();
if (iter != null)
{
for (;iter.hasNext();)
{
ret.add(iter.next());
}
}
return ret;
}
/**
* Add the elments provided by the given iterator to the given collection
* @param coll The target collection to add all the elements to
* @param iter The source iterator
*/
public static <E> void addToCollection(Collection<E> coll,Iterator<? extends E> iter)
{
if (coll == null || iter == null) return;
for (; iter.hasNext();)
coll.add(iter.next());
}
/**
* Retrieve the given file's file extension
* @param f The file to inspect
* @return The file's extension or NULL if <code>f == null</code>.
* @throws IOException In case the file cannot be inspected for some reason
* @see File
*/
public static String fileExtension(File f) throws IOException
{
if (f == null) return null;
String filename = f.getCanonicalPath();
int lastDotInd = filename.lastIndexOf('.');
if (lastDotInd < 0) return "";
return filename.substring(lastDotInd+1);
}
/**
* Create and return an icon based on the given class's resource path and the
* path given
*
* @param classObj The class object used to located the resource
* @param path The path to the file
* @return The new {@link ImageIcon ImageIcon} instance or null if no resource is found
* @see Class#getResource(String)
*/
public static ImageIcon getIcon(Class<?> classObj,String path)
{
URL imgURL = classObj.getResource(path);
if (imgURL != null)
{
return new ImageIcon(imgURL);
}
else
{
Utils.err("Couldn't find file: " + path);
return null;
}
}
}