コード例 #1
0
ファイル: CleanUpAction.java プロジェクト: jkulesza/jabref
 private void doMakePathsRelative(BibtexEntry entry, NamedCompound ce) {
   String oldValue = entry.getField(Globals.FILE_FIELD);
   if (oldValue == null) {
     return;
   }
   FileListTableModel flModel = new FileListTableModel();
   flModel.setContent(oldValue);
   if (flModel.getRowCount() == 0) {
     return;
   }
   boolean changed = false;
   for (int i = 0; i < flModel.getRowCount(); i++) {
     FileListEntry flEntry = flModel.getEntry(i);
     String oldFileName = flEntry.getLink();
     String newFileName =
         FileUtil.shortenFileName(
                 new File(oldFileName), panel.metaData().getFileDirectory(Globals.FILE_FIELD))
             .toString();
     if (!oldFileName.equals(newFileName)) {
       flEntry.setLink(newFileName);
       changed = true;
     }
   }
   if (changed) {
     String newValue = flModel.getStringRepresentation();
     assert (!oldValue.equals(newValue));
     entry.setField(Globals.FILE_FIELD, newValue);
     ce.addEdit(new UndoableFieldChange(entry, Globals.FILE_FIELD, oldValue, newValue));
   }
 }
コード例 #2
0
ファイル: FileLink.java プロジェクト: jkulesza/jabref
  @Override
  public String format(String field) {
    FileListTableModel tableModel = new FileListTableModel();
    if (field == null) {
      return "";
    }

    tableModel.setContent(field);
    String link = null;
    if (fileType == null) {
      // No file type specified. Simply take the first link.
      if (tableModel.getRowCount() > 0) {
        link = tableModel.getEntry(0).getLink();
      }
    } else {
      // A file type is specified:
      for (int i = 0; i < tableModel.getRowCount(); i++) {
        FileListEntry flEntry = tableModel.getEntry(i);
        if (flEntry.getType().getName().toLowerCase().equals(fileType)) {
          link = flEntry.getLink();
          break;
        }
      }
    }

    if (link == null) {
      return "";
    }

    String[] dirs;
    // We need to resolve the file directory from the database's metadata,
    // but that is not available from a formatter. Therefore, as an
    // ugly hack, the export routine has set a global variable before
    // starting the export, which contains the database's file directory:
    if (Globals.prefs.fileDirForDatabase != null) {
      dirs = Globals.prefs.fileDirForDatabase;
    } else {
      dirs = new String[] {Globals.prefs.get(Globals.FILE_FIELD + "Directory")};
    }

    File f = FileUtil.expandFilename(link, dirs);

    /*
     * Stumbled over this while investigating
     *
     * https://sourceforge.net/tracker/index.php?func=detail&aid=1469903&group_id=92314&atid=600306
     */
    if (f != null) {
      try {
        return f.getCanonicalPath(); // f.toURI().toString();
      } catch (IOException e) {
        e.printStackTrace();
        return f.getPath();
      }
    } else {
      return link;
    }
  }
コード例 #3
0
ファイル: CleanUpAction.java プロジェクト: jkulesza/jabref
 private static void fixWrongFileEntries(BibtexEntry entry, NamedCompound ce) {
   String oldValue = entry.getField(Globals.FILE_FIELD);
   if (oldValue == null) {
     return;
   }
   FileListTableModel flModel = new FileListTableModel();
   flModel.setContent(oldValue);
   if (flModel.getRowCount() == 0) {
     return;
   }
   boolean changed = false;
   for (int i = 0; i < flModel.getRowCount(); i++) {
     FileListEntry flEntry = flModel.getEntry(i);
     String link = flEntry.getLink();
     String description = flEntry.getDescription();
     if ("".equals(link) && (!"".equals(description))) {
       // link and description seem to be switched, quickly fix that
       flEntry.setLink(flEntry.getDescription());
       flEntry.setDescription("");
       changed = true;
     }
   }
   if (changed) {
     String newValue = flModel.getStringRepresentation();
     assert (!oldValue.equals(newValue));
     entry.setField(Globals.FILE_FIELD, newValue);
     ce.addEdit(new UndoableFieldChange(entry, Globals.FILE_FIELD, oldValue, newValue));
   }
 }
コード例 #4
0
ファイル: MoveFileAction.java プロジェクト: kifetew/SWAT
  public void actionPerformed(ActionEvent event) {
    int selected = editor.getSelectedRow();
    if (selected == -1) return;
    FileListEntry flEntry = editor.getTableModel().getEntry(selected);
    // Check if the current file exists:
    String ln = flEntry.getLink();
    boolean httpLink = ln.toLowerCase().startsWith("http");
    if (httpLink) {
      // TODO: notify that this operation cannot be done on remote links

    }

    // Get an absolute path representation:
    String dir = frame.basePanel().metaData().getFileDirectory(GUIGlobals.FILE_FIELD);
    if ((dir == null) || (dir.trim().length() == 0) || !(new File(dir)).exists()) {
      JOptionPane.showMessageDialog(
          frame,
          Globals.lang("File_directory_is_not_set_or_does_not_exist!"),
          Globals.lang("Move/Rename file"),
          JOptionPane.ERROR_MESSAGE);
      return;
    }
    File file = new File(ln);
    if (!file.isAbsolute()) {
      file = Util.expandFilename(ln, new String[] {dir});
    }
    if ((file != null) && file.exists()) {
      // Ok, we found the file. Now get a new name:
      String extension = null;
      if (flEntry.getType() != null) extension = "." + flEntry.getType().getExtension();

      File newFile = null;
      boolean repeat = true;
      while (repeat) {
        repeat = false;
        String chosenFile;
        if (toFileDir) {
          String suggName = eEditor.getEntry().getCiteKey() + extension;
          CheckBoxMessage cbm =
              new CheckBoxMessage(
                  Globals.lang("Move file to file directory?"),
                  Globals.lang("Rename to '%0'", suggName),
                  Globals.prefs.getBoolean("renameOnMoveFileToFileDir"));
          int answer;
          // Only ask about renaming file if the file doesn't have the proper name already:
          if (!suggName.equals(file.getName()))
            answer =
                JOptionPane.showConfirmDialog(
                    frame, cbm, Globals.lang("Move/Rename file"), JOptionPane.YES_NO_OPTION);
          else
            answer =
                JOptionPane.showConfirmDialog(
                    frame,
                    Globals.lang("Move file to file directory?"),
                    Globals.lang("Move/Rename file"),
                    JOptionPane.YES_NO_OPTION);
          if (answer != JOptionPane.YES_OPTION) return;
          Globals.prefs.putBoolean("renameOnMoveFileToFileDir", cbm.isSelected());
          StringBuilder sb = new StringBuilder(dir);
          if (!dir.endsWith(File.separator)) sb.append(File.separator);
          if (cbm.isSelected()) {
            // Rename:
            sb.append(suggName);
          } else {
            // Do not rename:
            sb.append(file.getName());
          }
          chosenFile = sb.toString();
        } else {
          chosenFile =
              FileDialogs.getNewFile(frame, file, extension, JFileChooser.SAVE_DIALOG, false);
        }
        if (chosenFile == null) {
          return; // cancelled
        }
        newFile = new File(chosenFile);
        // Check if the file already exists:
        if (newFile.exists()
            && (JOptionPane.showConfirmDialog(
                    frame,
                    "'" + newFile.getName() + "' " + Globals.lang("exists. Overwrite file?"),
                    Globals.lang("Move/Rename file"),
                    JOptionPane.OK_CANCEL_OPTION)
                != JOptionPane.OK_OPTION)) {
          if (!toFileDir) repeat = true;
          else return;
        }
      }

      if (!newFile.equals(file)) {
        try {
          boolean success = file.renameTo(newFile);
          if (!success) {
            success = Util.copyFile(file, newFile, true);
          }
          if (success) {
            // Remove the original file:
            file.delete();
            // Relativise path, if possible.
            String canPath = (new File(dir)).getCanonicalPath();
            if (newFile.getCanonicalPath().startsWith(canPath)) {
              if ((newFile.getCanonicalPath().length() > canPath.length())
                  && (newFile.getCanonicalPath().charAt(canPath.length()) == File.separatorChar))
                flEntry.setLink(newFile.getCanonicalPath().substring(1 + canPath.length()));
              else flEntry.setLink(newFile.getCanonicalPath().substring(canPath.length()));

            } else flEntry.setLink(newFile.getCanonicalPath());
            eEditor.updateField(editor);
            JOptionPane.showMessageDialog(
                frame,
                Globals.lang("File moved"),
                Globals.lang("Move/Rename file"),
                JOptionPane.INFORMATION_MESSAGE);
          } else {
            JOptionPane.showMessageDialog(
                frame,
                Globals.lang("Move file failed"),
                Globals.lang("Move/Rename file"),
                JOptionPane.ERROR_MESSAGE);
          }

        } catch (SecurityException ex) {
          ex.printStackTrace();
          JOptionPane.showMessageDialog(
              frame,
              Globals.lang("Could not move file") + ": " + ex.getMessage(),
              Globals.lang("Move/Rename file"),
              JOptionPane.ERROR_MESSAGE);
        } catch (IOException ex) {
          ex.printStackTrace();
          JOptionPane.showMessageDialog(
              frame,
              Globals.lang("Could not move file") + ": " + ex.getMessage(),
              Globals.lang("Move/Rename file"),
              JOptionPane.ERROR_MESSAGE);
        }
      }
    } else {

      // File doesn't exist, so we can't move it.
      JOptionPane.showMessageDialog(
          frame,
          Globals.lang("Could not find file '%0'.", flEntry.getLink()),
          Globals.lang("File not found"),
          JOptionPane.ERROR_MESSAGE);
    }
  }
コード例 #5
0
  public String format(String field) {
    StringBuilder sb = new StringBuilder();

    // Build the table model containing the links:
    FileListTableModel tableModel = new FileListTableModel();
    if (field == null) return "";
    tableModel.setContent(field);

    int piv = 1; // counter for relevant iterations
    for (int i = 0; i < tableModel.getRowCount(); i++) {
      FileListEntry flEntry = tableModel.getEntry(i);
      // Use this entry if we don't discriminate on types, or if the type fits:
      if ((fileType == null) || flEntry.getType().getName().toLowerCase().equals(fileType)) {

        for (FormatEntry entry : format) {
          switch (entry.getType()) {
            case STRING:
              sb.append(entry.getString());
              break;
            case ITERATION_COUNT:
              sb.append(String.valueOf(piv));
              break;
            case FILE_PATH:
              if (flEntry.getLink() == null) break;

              String dir;
              // We need to resolve the file directory from the database's metadata,
              // but that is not available from a formatter. Therefore, as an
              // ugly hack, the export routine has set a global variable before
              // starting the export, which contains the database's file directory:
              if (Globals.prefs.fileDirForDatabase != null) dir = Globals.prefs.fileDirForDatabase;
              else dir = Globals.prefs.get(GUIGlobals.FILE_FIELD + "Directory");

              File f = Util.expandFilename(flEntry.getLink(), new String[] {dir});
              /*
               * Stumbled over this while investigating
               *
               * https://sourceforge.net/tracker/index.php?func=detail&aid=1469903&group_id=92314&atid=600306
               */
              if (f != null) {
                try {
                  sb.append(replaceStrings(f.getCanonicalPath())); // f.toURI().toString();
                } catch (IOException ex) {
                  ex.printStackTrace();
                  sb.append(replaceStrings(f.getPath()));
                }
              } else {
                sb.append(replaceStrings(flEntry.getLink()));
              }

              break;
            case RELATIVE_FILE_PATH:
              if (flEntry.getLink() == null) break;

              /*
               * Stumbled over this while investigating
               *
               * https://sourceforge.net/tracker/index.php?func=detail&aid=1469903&group_id=92314&atid=600306
               */
              sb.append(replaceStrings(flEntry.getLink())); // f.toURI().toString();

              break;
            case FILE_EXTENSION:
              if (flEntry.getLink() == null) break;
              int index = flEntry.getLink().lastIndexOf('.');
              if ((index >= 0) && (index < flEntry.getLink().length() - 1))
                sb.append(replaceStrings(flEntry.getLink().substring(index + 1)));
              break;
            case FILE_TYPE:
              sb.append(replaceStrings(flEntry.getType().getName()));
              break;
            case FILE_DESCRIPTION:
              sb.append(replaceStrings(flEntry.getDescription()));
              break;
          }
        }

        piv++; // update counter
      }
    }

    return sb.toString();
  }
コード例 #6
0
ファイル: WriteXMPAction.java プロジェクト: GeorgLink/Desktop
  public void run() {

    if (!goOn) return;

    for (int i = 0; i < entries.length; i++) {

      BibtexEntry entry = entries[i];

      // Make a list of all PDFs linked from this entry:
      List<File> files = new ArrayList<File>();

      // First check the (legacy) "pdf" field:
      String pdf = entry.getField("pdf");
      String dir = panel.metaData().getFileDirectory("pdf");
      File f = Util.expandFilename(pdf, new String[] {dir, "."});
      if (f != null) files.add(f);

      // Then check the "file" field:
      dir = panel.metaData().getFileDirectory(GUIGlobals.FILE_FIELD);
      String field = entry.getField(GUIGlobals.FILE_FIELD);
      if (field != null) {
        FileListTableModel tm = new FileListTableModel();
        tm.setContent(field);
        for (int j = 0; j < tm.getRowCount(); j++) {
          FileListEntry flEntry = tm.getEntry(j);
          if ((flEntry.getType() != null)
              && (flEntry.getType().getName().toLowerCase().equals("pdf"))) {
            f = Util.expandFilename(flEntry.getLink(), new String[] {dir, "."});
            if (f != null) files.add(f);
          }
        }
      }

      optDiag.progressArea.append(entry.getCiteKey() + "\n");

      if (files.size() == 0) {
        skipped++;
        optDiag.progressArea.append("  " + Globals.lang("Skipped - No PDF linked") + ".\n");
      } else
        for (File file : files) {
          if (!file.exists()) {
            skipped++;
            optDiag.progressArea.append(
                "  " + Globals.lang("Skipped - PDF does not exist") + ":\n");
            optDiag.progressArea.append("    " + file.getPath() + "\n");

          } else {
            try {
              XMPUtil.writeXMP(file, entry, database);
              optDiag.progressArea.append("  " + Globals.lang("Ok") + ".\n");
              entriesChanged++;
            } catch (Exception e) {
              optDiag.progressArea.append(
                  "  " + Globals.lang("Error while writing") + " '" + file.getPath() + "':\n");
              optDiag.progressArea.append("    " + e.getLocalizedMessage() + "\n");
              errors++;
            }
          }
        }

      if (optDiag.canceled) {
        optDiag.progressArea.append("\n" + Globals.lang("Operation canceled.\n"));
        break;
      }
    }
    optDiag.progressArea.append(
        "\n"
            + Globals.lang(
                "Finished writing XMP for %0 file (%1 skipped, %2 errors).",
                String.valueOf(entriesChanged), String.valueOf(skipped), String.valueOf(errors)));
    optDiag.done();
  }