/**
  * Look up the external file type registered for this MIME type, if any.
  *
  * @param mimeType The MIME type.
  * @return The ExternalFileType registered, or null if none.
  */
 public ExternalFileType getExternalFileTypeByMimeType(String mimeType) {
   for (Iterator<ExternalFileType> iterator = externalFileTypes.iterator(); iterator.hasNext(); ) {
     ExternalFileType type = iterator.next();
     if ((type.getMimeType() != null) && type.getMimeType().equals(mimeType)) return type;
   }
   return null;
 }
 /**
  * Look up the external file type registered with this name, if any.
  *
  * @param name The file type name.
  * @return The ExternalFileType registered, or null if none.
  */
 public ExternalFileType getExternalFileTypeByName(String name) {
   for (Iterator<ExternalFileType> iterator = externalFileTypes.iterator(); iterator.hasNext(); ) {
     ExternalFileType type = iterator.next();
     if (type.getName().equals(name)) return type;
   }
   // Return an instance that signifies an unknown file type:
   return new UnknownExternalFileType(name);
 }
 /**
  * Look up the external file type registered for this extension, if any.
  *
  * @param extension The file extension.
  * @return The ExternalFileType registered, or null if none.
  */
 public ExternalFileType getExternalFileTypeByExt(String extension) {
   for (Iterator<ExternalFileType> iterator = externalFileTypes.iterator(); iterator.hasNext(); ) {
     ExternalFileType type = iterator.next();
     if ((type.getExtension() != null) && type.getExtension().equalsIgnoreCase(extension))
       return type;
   }
   return null;
 }
Exemple #4
0
  /**
   * Convenience menthod for findPDF. Searches for a file of the given type.
   *
   * @param entry The BibtexEntry to search for a link for.
   * @param fileType The file type to search for.
   * @return The link to the file found, or null if not found.
   */
  public static String findFile(
      BibtexEntry entry, ExternalFileType fileType, List<String> extraDirs) {

    List<String> dirs = new ArrayList<String>();
    dirs.addAll(extraDirs);
    if (Globals.prefs.hasKey(fileType.getExtension() + "Directory")) {
      dirs.add(Globals.prefs.get(fileType.getExtension() + "Directory"));
    }
    String[] directories = dirs.toArray(new String[dirs.size()]);
    return UtilFindFiles.findPdf(entry, fileType.getExtension(), directories);
  }
  /**
   * Reset the List of external file types after user customization.
   *
   * @param types The new List of external file types. This is the complete list, not just new
   *     entries.
   */
  public void setExternalFileTypes(List<ExternalFileType> types) {

    // First find a list of the default types:
    List<ExternalFileType> defTypes = getDefaultExternalFileTypes();
    // Make a list of types that are unchanged:
    List<ExternalFileType> unchanged = new ArrayList<ExternalFileType>();

    externalFileTypes.clear();
    for (Iterator<ExternalFileType> iterator = types.iterator(); iterator.hasNext(); ) {
      ExternalFileType type = iterator.next();
      externalFileTypes.add(type);

      // See if we can find a type with matching name in the default type list:
      ExternalFileType found = null;
      for (ExternalFileType defType : defTypes) {
        if (defType.getName().equals(type.getName())) {
          found = defType;
          break;
        }
      }
      if (found != null) {
        // Found it! Check if it is an exact match, or if it has been customized:
        if (found.equals(type)) unchanged.add(type);
        else {
          // It was modified. Remove its entry from the defaults list, since
          // the type hasn't been removed:
          defTypes.remove(found);
        }
      }
    }

    // Go through unchanged types. Remove them from the ones that should be stored,
    // and from the list of defaults, since we don't need to mention these in prefs:
    for (ExternalFileType type : unchanged) {
      defTypes.remove(type);
      types.remove(type);
    }

    // Now set up the array to write to prefs, containing all new types, all modified
    // types, and a flag denoting each default type that has been removed:
    String[][] array = new String[types.size() + defTypes.size()][];
    int i = 0;
    for (ExternalFileType type : types) {
      array[i] = type.getStringArrayRepresentation();
      i++;
    }
    for (ExternalFileType type : defTypes) {
      array[i] = new String[] {type.getName(), FILE_TYPE_REMOVED_FLAG};
      i++;
    }
    // System.out.println("Encoded: '"+Util.encodeStringArray(array)+"'");
    put("externalFileTypes", Util.encodeStringArray(array));
  }
  public List<ExternalFileType> getDefaultExternalFileTypes() {
    List<ExternalFileType> list = new ArrayList<ExternalFileType>();
    list.add(new ExternalFileType("PDF", "pdf", "application/pdf", "evince", "pdfSmall"));
    list.add(
        new ExternalFileType("PostScript", "ps", "application/postscript", "evince", "psSmall"));
    list.add(new ExternalFileType("Word", "doc", "application/msword", "oowriter", "openoffice"));
    list.add(
        new ExternalFileType(
            "OpenDocument text",
            "odt",
            "application/vnd.oasis.opendocument.text",
            "oowriter",
            "openoffice"));
    list.add(new ExternalFileType("Excel", "xls", "application/excel", "oocalc", "openoffice"));
    list.add(
        new ExternalFileType(
            "OpenDocument spreadsheet",
            "ods",
            "application/vnd.oasis.opendocument.spreadsheet",
            "oocalc",
            "openoffice"));
    list.add(new ExternalFileType("PowerPoint", "ppt", "", "ooimpress", "openoffice"));
    list.add(
        new ExternalFileType(
            "OpenDocument presentation",
            "odp",
            "application/vnd.oasis.opendocument.presentation",
            "ooimpress",
            "openoffice"));
    list.add(
        new ExternalFileType(
            "Rich Text Format", "rtf", "application/rtf", "oowriter", "openoffice"));
    list.add(new ExternalFileType("PNG image", "png", "image/png", "gimp", "picture"));
    list.add(new ExternalFileType("GIF image", "gif", "image/gif", "gimp", "picture"));
    list.add(new ExternalFileType("JPG image", "jpg", "image/jpeg", "gimp", "picture"));
    list.add(new ExternalFileType("Djvu", "djvu", "", "evince", "psSmall"));
    list.add(new ExternalFileType("Text", "txt", "text/plain", "emacs", "emacs"));
    list.add(new ExternalFileType("LaTeX", "tex", "", "emacs", "emacs"));
    list.add(new ExternalFileType("CHM", "chm", "", "gnochm", "www"));
    list.add(new ExternalFileType("TIFF image", "tiff", "image/tiff", "gimp", "picture"));
    ExternalFileType tp = new ExternalFileType("URL", "html", "text/html", "firefox", "www");
    list.add(tp);

    // On all OSes there is a generic application available to handle file opening,
    // so we don't need the default application settings anymore:
    for (Iterator<ExternalFileType> iterator = list.iterator(); iterator.hasNext(); ) {
      ExternalFileType type = iterator.next();
      type.setOpenWith("");
    }

    return list;
  }
  /**
   * Set up the list of external file types, either from default values, or from values recorded in
   * Preferences.
   */
  public void updateExternalFileTypes() {
    // First get a list of the default file types as a starting point:
    List<ExternalFileType> types = getDefaultExternalFileTypes();
    // If no changes have been stored, simply use the defaults:
    if (prefs.get("externalFileTypes", null) == null) {
      externalFileTypes.clear();
      externalFileTypes.addAll(types);
      return;
    }
    // Read the prefs information for file types:
    String[][] vals = Util.decodeStringDoubleArray(prefs.get("externalFileTypes", ""));
    for (int i = 0; i < vals.length; i++) {
      if ((vals[i].length == 2) && (vals[i][1].equals(FILE_TYPE_REMOVED_FLAG))) {
        // This entry indicates that a default entry type should be removed:
        ExternalFileType toRemove = null;
        for (ExternalFileType type : types) {
          if (type.getName().equals(vals[i][0])) {
            toRemove = type;
            break;
          }
        }
        // If we found it, remove it from the type list:
        if (toRemove != null) types.remove(toRemove);
      } else {
        // A new or modified entry type. Construct it from the string array:
        ExternalFileType type = new ExternalFileType(vals[i]);
        // Check if there is a default type with the same name. If so, this is a
        // modification of that type, so remove the default one:
        ExternalFileType toRemove = null;
        for (ExternalFileType defType : types) {
          if (type.getName().equals(defType.getName())) {
            toRemove = defType;
            break;
          }
        }
        // If we found it, remove it from the type list:
        if (toRemove != null) {
          types.remove(toRemove);
        }

        // Then add the new one:
        types.add(type);
      }
    }

    // Finally, build the list of types based on the modified defaults list:
    for (ExternalFileType type : types) {
      externalFileTypes.add(type);
    }
  }