// TODO Add a sort here to restore the sort after a filter
  public void refreshTable() {

    // First: Remember currently selected waypoint
    String wayPoint;
    Vector oldVisibleCaches = null;
    int sel = getSelectedCache();
    if ((sel >= 0)
        && (sel
            < cacheDB
                .size())) // sel > cacheDB.size() can happen if you load a new profile, which is
                          // smaller than the old profile and you selected one cache that exceeds
                          // the number of caches in the new profile
    wayPoint = cacheDB.get(sel).getWayPoint();
    else wayPoint = null;
    // Then: remember all caches that are visible before the refresh
    if (wayPoint != null) {
      oldVisibleCaches = new Vector(sel);
      for (int i = 0; i < sel; i++) {
        oldVisibleCaches.add(cacheDB.get(i));
      }
    }
    myMod.updateRows();

    // Check whether the currently selected cache is still visible
    int rownum = 0;
    if (wayPoint != null) {
      rownum =
          profile.cacheDB.getIndex(wayPoint); // Global.getProfile().cacheDB.getIndex(wayPoint);
      // If it is not visible: Go backward in the list of the
      // previously visible caches and look if you find
      // any cache that is now still visible.
      if ((rownum < 0) || (rownum >= myMod.numRows)) {
        if (oldVisibleCaches != null) {
          int i;
          for (i = sel - 1; i >= 0; i--) {
            CacheHolder checkCache = (CacheHolder) oldVisibleCaches.get(i);
            rownum =
                profile.cacheDB.getIndex(
                    checkCache
                        .getWayPoint()); // Global.getProfile().cacheDB.getIndex(checkCache.getWayPoint());
            if ((rownum >= 0) && (rownum < myMod.numRows)) break;
            rownum = 0;
          }
        }
      }
    }
    selectRow(rownum);

    tc.update(true); // Update and repaint
    if (statBar != null) statBar.updateDisplay("");
  }
  public void doIt() {
    CacheHolderDetail det;
    CacheHolder ch;
    ProgressBarForm pbf = new ProgressBarForm();
    Handle h = new Handle();
    int exportErrors = 0;

    new String();
    FileChooser fc = new FileChooser(FileChooserBase.DIRECTORY_SELECT, pref.getExportPath(expName));
    fc.setTitle("Select target directory:");
    String targetDir;
    if (fc.execute() != FormBase.IDCANCEL) {
      targetDir = fc.getChosen() + "/";
      pref.setExportPath(expName, targetDir);
      Vector cache_index = new Vector();
      Vector cacheImg = new Vector();
      Vector logImg = new Vector();
      Vector mapImg = new Vector();
      Vector usrImg = new Vector();
      Vector logIcons = new Vector(15);
      String icon;

      Hashtable varParams;
      Hashtable imgParams;
      Hashtable logImgParams;
      Hashtable usrImgParams;
      Hashtable mapImgParams;

      // Generate index page
      int counter = cacheDB.countVisible();

      pbf.showMainTask = false;
      pbf.setTask(h, "Exporting ...");
      pbf.exec();

      for (int i = 0; i < counter; i++) {
        h.progress = (float) (i + 1) / (float) counter;
        h.changed();

        ch = cacheDB.get(i);
        if (ch.isVisible()) {
          if (ch.is_incomplete()) {
            exportErrors++;
            Global.getPref()
                .log("HTMLExport: skipping export of incomplete waypoint " + ch.getWayPoint());
            continue;
          }
          det = ch.getCacheDetails(false, false);
          varParams = new Hashtable();
          varParams.put("TYPE", CacheType.cw2ExportString(ch.getType()));
          varParams.put("WAYPOINT", ch.getWayPoint());
          varParams.put("NAME", ch.getCacheName());
          varParams.put("OWNER", ch.getCacheOwner());
          if (ch.isAddiWpt() || CacheType.CW_TYPE_CUSTOM == ch.getType()) {
            varParams.put("SIZE", "");
            varParams.put("DIFFICULTY", "");
            varParams.put("TERRAIN", "");
          } else {
            varParams.put(
                "SIZE",
                CacheSize.isValidSize(ch.getCacheSize())
                    ? CacheSize.cw2ExportString(ch.getCacheSize())
                    : "");
            varParams.put(
                "DIFFICULTY",
                CacheTerrDiff.isValidTD(ch.getHard()) ? CacheTerrDiff.longDT(ch.getHard()) : "");
            varParams.put(
                "TERRAIN",
                CacheTerrDiff.isValidTD(ch.getTerrain())
                    ? CacheTerrDiff.longDT(ch.getTerrain())
                    : "");
          }
          varParams.put("DISTANCE", ch.getDistance());
          varParams.put("BEARING", ch.bearing);
          varParams.put("LATLON", ch.LatLon);
          varParams.put("STATUS", ch.getCacheStatus());
          varParams.put("DATE", ch.getDateHidden());
          cache_index.add(varParams);
          // We can generate the individual page here!
          try {
            Template page_tpl = new Template(template_init_page);
            page_tpl.setParam("TYPE", varParams.get("TYPE").toString());
            page_tpl.setParam("SIZE", varParams.get("SIZE").toString());
            page_tpl.setParam("WAYPOINT", ch.getWayPoint());
            page_tpl.setParam("NAME", ch.getCacheName());
            page_tpl.setParam("OWNER", ch.getCacheOwner());
            page_tpl.setParam("DIFFICULTY", varParams.get("DIFFICULTY").toString());
            page_tpl.setParam("TERRAIN", varParams.get("TERRAIN").toString());
            page_tpl.setParam("DISTANCE", ch.getDistance());
            page_tpl.setParam("BEARING", ch.bearing);
            page_tpl.setParam("LATLON", ch.LatLon);
            page_tpl.setParam("STATUS", ch.getCacheStatus());
            page_tpl.setParam("DATE", ch.getDateHidden());
            if (det != null) {
              if (ch.is_HTML()) {
                page_tpl.setParam("DESCRIPTION", modifyLongDesc(det, targetDir));
              } else {
                page_tpl.setParam(
                    "DESCRIPTION", STRreplace.replace(det.LongDescription, "\n", "<br>"));
              }
              page_tpl.setParam("HINTS", det.Hints);
              page_tpl.setParam("DECRYPTEDHINTS", Common.rot13(det.Hints));

              StringBuffer sb = new StringBuffer(2000);
              for (int j = 0; j < det.CacheLogs.size(); j++) {
                sb.append(
                    STRreplace.replace(
                        det.CacheLogs.getLog(j).toHtml(),
                        "http://www.geocaching.com/images/icons/",
                        null));
                sb.append("<br>");
                icon = det.CacheLogs.getLog(j).getIcon();
                if (logIcons.find(icon) < 0)
                  logIcons.add(icon); // Add the icon to list of icons to copy to dest directory
              }

              page_tpl.setParam("LOGS", sb.toString());
              page_tpl.setParam("NOTES", STRreplace.replace(det.getCacheNotes(), "\n", "<br>"));

              cacheImg.clear();
              for (int j = 0; j < det.images.size(); j++) {
                imgParams = new Hashtable();
                String imgFile = new String(det.images.get(j).getFilename());
                imgParams.put("FILE", imgFile);
                imgParams.put("TEXT", det.images.get(j).getTitle());
                if (DataMover.copy(profile.dataDir + imgFile, targetDir + imgFile))
                  cacheImg.add(imgParams);
                else exportErrors++;
              }
              page_tpl.setParam("cacheImg", cacheImg);

              // Log images
              logImg.clear();
              for (int j = 0; j < det.logImages.size(); j++) {
                logImgParams = new Hashtable();
                String logImgFile = det.logImages.get(j).getFilename();
                logImgParams.put("FILE", logImgFile);
                logImgParams.put("TEXT", det.logImages.get(j).getTitle());
                if (DataMover.copy(profile.dataDir + logImgFile, targetDir + logImgFile))
                  logImg.add(logImgParams);
                else exportErrors++;
              }
              page_tpl.setParam("logImg", logImg);

              // User images
              usrImg.clear();
              for (int j = 0; j < det.userImages.size(); j++) {
                usrImgParams = new Hashtable();
                String usrImgFile = new String(det.userImages.get(j).getFilename());
                usrImgParams.put("FILE", usrImgFile);
                usrImgParams.put("TEXT", det.userImages.get(j).getTitle());
                if (DataMover.copy(profile.dataDir + usrImgFile, targetDir + usrImgFile))
                  usrImg.add(usrImgParams);
                else exportErrors++;
              }
              page_tpl.setParam("userImg", usrImg);

              // Map images
              mapImg.clear();
              mapImgParams = new Hashtable();

              String mapImgFile = new String(ch.getWayPoint() + "_map.gif");
              // check if map file exists
              File test = new File(profile.dataDir + mapImgFile);

              if (test.exists()) {
                mapImgParams.put("FILE", mapImgFile);
                mapImgParams.put("TEXT", mapImgFile);
                if (DataMover.copy(profile.dataDir + mapImgFile, targetDir + mapImgFile))
                  mapImg.add(mapImgParams);
                else exportErrors++;

                mapImgParams = new Hashtable();
                mapImgFile = ch.getWayPoint() + "_map_2.gif";
                mapImgParams.put("FILE", mapImgFile);
                mapImgParams.put("TEXT", mapImgFile);
                if (DataMover.copy(profile.dataDir + mapImgFile, targetDir + mapImgFile))
                  mapImg.add(mapImgParams);
                else exportErrors++;

                page_tpl.setParam("mapImg", mapImg);
              }
            } else {
              page_tpl.setParam("DESCRIPTION", "");
              page_tpl.setParam("HINTS", "");
              page_tpl.setParam("DECRYPTEDHINTS", "");
              page_tpl.setParam("LOGS", "");
              page_tpl.setParam("NOTES", "");
              page_tpl.setParam("cacheImg", cacheImg);
              page_tpl.setParam("logImg", ""); // ???
              page_tpl.setParam("userImg", ""); // ???
              page_tpl.setParam("mapImg", ""); // ???
              exportErrors++;
            }

            PrintWriter pagefile =
                new PrintWriter(
                    new BufferedWriter(new FileWriter(targetDir + ch.getWayPoint() + ".html")));
            pagefile.print(page_tpl.output());
            pagefile.close();
          } catch (IllegalArgumentException e) {
            exportErrors++;
            ch.setIncomplete(true);
            Global.getPref()
                .log(
                    "HTMLExport: " + ch.getWayPoint() + " is incomplete reason: ",
                    e,
                    Global.getPref().debug);
          } catch (Exception e) {
            exportErrors++;
            Global.getPref()
                .log(
                    "HTMLExport: error wehen exporting " + ch.getWayPoint() + " reason: ",
                    e,
                    Global.getPref().debug);
          }
        } // if is black, filtered
      }

      // Copy the log-icons to the destination directory
      for (int j = 0; j < logIcons.size(); j++) {
        icon = (String) logIcons.elementAt(j);
        if (!DataMover.copy(FileBase.getProgramDirectory() + "/" + icon, targetDir + icon))
          exportErrors++;
      }
      if (!DataMover.copy(
          FileBase.getProgramDirectory() + "/recommendedlog.gif", targetDir + "recommendedlog.gif"))
        exportErrors++;

      try {
        Template tpl = new Template(template_init_index);
        tpl.setParam("cache_index", cache_index);
        PrintWriter detfile;
        detfile = new PrintWriter(new BufferedWriter(new FileWriter(targetDir + "/index.html")));
        detfile.print(tpl.output());
        detfile.close();
        // sort by waypoint
        sortAndPrintIndex(tpl, cache_index, targetDir + "/index_wp.html", "WAYPOINT");
        // sort by name
        sortAndPrintIndex(tpl, cache_index, targetDir + "/index_alpha.html", "NAME", false);
        // sort by type
        sortAndPrintIndex(tpl, cache_index, targetDir + "/index_type.html", "TYPE", true);
        // sort by size
        sortAndPrintIndex(tpl, cache_index, targetDir + "/index_size.html", "SIZE", true);
        // sort by distance
        sortAndPrintIndex(tpl, cache_index, targetDir + "/index_dist.html", "DISTANCE", 10.0);
      } catch (Exception e) {
        Vm.debug("Problem writing HTML files\n");
        e.printStackTrace();
      } // try
    } // if
    pbf.exit(0);

    if (exportErrors > 0) {
      new MessageBox(
              "Export Error",
              exportErrors + " errors during export. See log for details.",
              FormBase.OKB)
          .execute();
    }
  }