//
// Checker.java
//

/*
LOCI Plugins for ImageJ: a collection of ImageJ plugins including the
Bio-Formats Importer, Bio-Formats Exporter, Bio-Formats Macro Extensions,
Data Browser, Stack Colorizer and Stack Slicer. Copyright (C) 2005-@year@
Melissa Linkert, Curtis Rueden and Christopher Peterson.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package loci.plugins;

import ij.IJ;
import java.util.HashSet;
import java.util.Iterator;

/**
 * Utility methods for verifying that classes
 * are present and versions are correct.
 *
 * <dl><dt><b>Source code:</b></dt>
 * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/components/loci-plugins/src/loci/plugins/Checker.java">Trac</a>,
 * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/components/loci-plugins/src/loci/plugins/Checker.java">SVN</a></dd></dl>
 */
public final class Checker {

  // -- Constants --

  /** Identifier for checking the Bio-Formats library is present. */
  public static final int BIO_FORMATS = 1;

  /** Identifier for checking the OME Java OME-XML library is present. */
  public static final int OME_JAVA_XML = 2;

  /** Identifier for checking the OME Java OMEDS library is present. */
  public static final int OME_JAVA_DS = 3;

  /** Identifier for checking the JGoodies Forms library is present. */
  public static final int FORMS = 4;

  // -- Constructor --

  private Checker() { }

  // -- Utility methods --

  /** Checks whether the given class is available. */
  public static boolean checkClass(String className) {
    try { Class.forName(className); }
    catch (Throwable t) { return false; }
    return true;
  }

  /**
   * Checks for a required library.
   * @param library One of:<ul>
   *   <li>BIO_FORMATS</li>
   *   <li>OME_JAVA_XML</li>
   *   <li>OME_JAVA_DS</li>
   *   <li>FORMS</li>
   *   </ul>
   */
  public static void checkLibrary(int library, HashSet missing) {
    switch (library) {
      case BIO_FORMATS:
        checkLibrary("loci.formats.FormatHandler", "bio-formats.jar", missing);
        checkLibrary("org.apache.poi.poifs.filesystem.POIFSFileSystem",
          "poi-loci.jar", missing);
        break;
      case OME_JAVA_XML:
        checkLibrary("ome.xml.OMEXMLNode", "ome-xml.jar", missing);
        break;
      case OME_JAVA_DS:
        checkLibrary("org.openmicroscopy.ds.DataServer",
          "ome-java.jar", missing);
        checkLibrary("org.apache.xmlrpc.XmlRpcClient",
          "xmlrpc-1.2-b1.jar", missing);
        checkLibrary("org.apache.commons.httpclient.HttpClient",
          "commons-httpclient-2.0-rc2.jar", missing);
        checkLibrary("org.apache.commons.logging.Log",
          "commons-logging.jar", missing);
        break;
      case FORMS:
        checkLibrary("com.jgoodies.forms.layout.FormLayout",
          "forms-1.0.4.jar", missing);
        break;
    }
  }

  /**
   * Checks whether the given class is available; if not,
   * adds the specified JAR file name to the hash set
   * (presumably to report it missing to the user).
   */
  public static void checkLibrary(String className,
    String jarFile, HashSet missing)
  {
    if (!checkClass(className)) missing.add(jarFile);
  }

  /** Checks for a new enough version of the Java Runtime Environment. */
  public static boolean checkJava() {
    String version = System.getProperty("java.version");
    double ver = Double.parseDouble(version.substring(0, 3));
    if (ver < 1.4) {
      IJ.error("LOCI Plugins",
        "Sorry, the LOCI plugins require Java 1.4 or later." +
        "\nYou can download ImageJ with JRE 5.0 from the ImageJ web site.");
      return false;
    }
    return true;
  }

  /** Checks whether the version of ImageJ is new enough. */
  public static boolean checkImageJ() {
    boolean success;
    try {
      String version = IJ.getVersion();
      success = version != null && version.compareTo("1.34") >= 0;
    }
    catch (NoSuchMethodError err) {
      success = false;
    }
    if (!success) {
      IJ.error("LOCI Plugins",
        "Sorry, the LOCI plugins require ImageJ v1.34 or later.");
    }
    return success;
  }

  /**
   * Reports missing libraries in the given hash set to the user.
   * @return true iff no libraries are missing (the hash set is empty).
   */
  public static boolean checkMissing(HashSet missing) {
    int num = missing.size();
    if (num == 0) return true;
    StringBuffer sb = new StringBuffer();
    sb.append("The following librar");
    sb.append(num == 1 ? "y was" : "ies were");
    sb.append(" not found:");
    Iterator iter = missing.iterator();
    for (int i=0; i<num; i++) sb.append("\n    " + iter.next());
    String them = num == 1 ? "it" : "them";
    sb.append("\nPlease download ");
    sb.append(them);
    sb.append(" from the LOCI website at");
    sb.append("\n    http://www.loci.wisc.edu/software/");
    sb.append("\nand place ");
    sb.append(them);
    sb.append(" in the ImageJ plugins folder.");
    IJ.error("LOCI Plugins", sb.toString());
    return false;
  }

}

@@ -2588,16 +2588,110 @@
 ORMATS:%0A
+        checkLibrary(%22loci.common.RandomAccessStream%22,%0A          %22loci-common.jar%22, missing);%0A
         
@@ -2825,18 +2825,16 @@
 OIFS
-FileSystem
+Document
 %22,%0A 
@@ -2860,32 +2860,111 @@
 jar%22, missing);%0A
+        checkLibrary(%22mdbtools.libmdb.MdbFile%22, %22mdbtools-java.jar%22, missing);%0A
         break;%0A 

Add loci-common and mdbtools-java to JAR checker. This is a temporary solution until the full-blown LOCI plugins dependency checker/installer plugin is finished.