Coverage Report - net.sf.jabref.external.MoveFileAction
 
Classes in this File Line Coverage Branch Coverage Complexity
MoveFileAction
7%
6/80
0%
0/50
17
 
 1  
 package net.sf.jabref.external;
 2  
 
 3  
 import net.sf.jabref.*;
 4  
 import net.sf.jabref.gui.FileListEditor;
 5  
 import net.sf.jabref.gui.FileListEntry;
 6  
 import net.sf.jabref.gui.FileDialogs;
 7  
 
 8  
 import javax.swing.*;
 9  
 import java.awt.event.ActionEvent;
 10  
 import java.io.File;
 11  
 import java.io.IOException;
 12  
 
 13  
 /**
 14  
  * Action for moving or renaming a file that is linked to from an entry in JabRef.
 15  
  */
 16  
 public class MoveFileAction extends AbstractAction {
 17  
     private JabRefFrame frame;
 18  
     private EntryEditor eEditor;
 19  
     private FileListEditor editor;
 20  
     private boolean toFileDir;
 21  
 
 22  
     public MoveFileAction(JabRefFrame frame, EntryEditor eEditor, FileListEditor editor,
 23  482815084
                           boolean toFileDir) {
 24  482815084
         this.frame = frame;
 25  482815084
         this.eEditor = eEditor;
 26  482815084
         this.editor = editor;
 27  482815084
         this.toFileDir = toFileDir;
 28  482815084
     }
 29  
 
 30  
     public void actionPerformed(ActionEvent event) {
 31  0
         int selected = editor.getSelectedRow();
 32  0
         if (selected == -1)
 33  0
             return;
 34  0
         FileListEntry flEntry = editor.getTableModel().getEntry(selected);
 35  
         // Check if the current file exists:
 36  0
         String ln = flEntry.getLink();
 37  0
         boolean httpLink = ln.toLowerCase().startsWith("http");
 38  0
         if (httpLink) {
 39  
             // TODO: notify that this operation cannot be done on remote links
 40  
 
 41  
         }
 42  
 
 43  
         // Get an absolute path representation:
 44  0
         String dir = frame.basePanel().metaData().getFileDirectory(GUIGlobals.FILE_FIELD);
 45  0
         if ((dir == null) || (dir.trim().length() == 0) || !(new File(dir)).exists()) {
 46  0
             JOptionPane.showMessageDialog(frame, Globals.lang("File_directory_is_not_set_or_does_not_exist!"),
 47  
                     Globals.lang("Move/Rename file"), JOptionPane.ERROR_MESSAGE);
 48  0
             return;
 49  
         }
 50  0
         File file = new File(ln);
 51  0
         if (!file.isAbsolute()) {
 52  0
             file = Util.expandFilename(ln, new String[]{dir});
 53  
         }
 54  0
         if ((file != null) && file.exists()) {
 55  
             // Ok, we found the file. Now get a new name:
 56  0
             String extension = null;
 57  0
             if (flEntry.getType() != null)
 58  0
                 extension = "." + flEntry.getType().getExtension();
 59  
 
 60  0
             File newFile = null;
 61  0
             boolean repeat = true;
 62  0
             while (repeat) {
 63  0
                 repeat = false;
 64  
                 String chosenFile;
 65  0
                 if (toFileDir) {
 66  0
                     String suggName = eEditor.getEntry().getCiteKey()+extension;
 67  0
                     CheckBoxMessage cbm = new CheckBoxMessage(Globals.lang("Move file to file directory?"),
 68  
                             Globals.lang("Rename to '%0'",suggName),
 69  
                             Globals.prefs.getBoolean("renameOnMoveFileToFileDir"));
 70  
                     int answer;
 71  
                     // Only ask about renaming file if the file doesn't have the proper name already:
 72  0
                     if (!suggName.equals(file.getName()))
 73  0
                         answer = JOptionPane.showConfirmDialog(frame, cbm, Globals.lang("Move/Rename file"),
 74  
                                 JOptionPane.YES_NO_OPTION);
 75  
                     else
 76  0
                         answer = JOptionPane.showConfirmDialog(frame, Globals.lang("Move file to file directory?"),
 77  
                                 Globals.lang("Move/Rename file"), JOptionPane.YES_NO_OPTION);
 78  0
                     if (answer != JOptionPane.YES_OPTION)
 79  0
                         return;
 80  0
                     Globals.prefs.putBoolean("renameOnMoveFileToFileDir", cbm.isSelected());
 81  0
                     StringBuilder sb = new StringBuilder(dir);
 82  0
                     if (!dir.endsWith(File.separator))
 83  0
                         sb.append(File.separator);
 84  0
                     if (cbm.isSelected()) {
 85  
                         // Rename:
 86  0
                         sb.append(suggName);
 87  
                     }
 88  
                     else {
 89  
                         // Do not rename:
 90  0
                         sb.append(file.getName());
 91  
                     }
 92  0
                     chosenFile = sb.toString();
 93  0
                 } else {
 94  0
                     chosenFile = FileDialogs.getNewFile(frame, file, extension, JFileChooser.SAVE_DIALOG, false);
 95  
                 }
 96  0
                 if (chosenFile == null) {
 97  0
                     return; // cancelled
 98  
                 }
 99  0
                 newFile = new File(chosenFile);
 100  
                 // Check if the file already exists:
 101  0
                 if (newFile.exists() && (JOptionPane.showConfirmDialog
 102  
                         (frame, "'" + newFile.getName() + "' " + Globals.lang("exists. Overwrite file?"),
 103  
                                 Globals.lang("Move/Rename file"), JOptionPane.OK_CANCEL_OPTION)
 104  
                         != JOptionPane.OK_OPTION)) {
 105  0
                     if (!toFileDir)
 106  0
                         repeat = true;
 107  
                     else
 108  0
                         return;
 109  
                 }
 110  0
             }
 111  
 
 112  0
             if (!newFile.equals(file)) {
 113  
                 try {
 114  0
                     boolean success = file.renameTo(newFile);
 115  0
                     if (!success) {
 116  0
                         success = Util.copyFile(file, newFile, true);
 117  
                     }
 118  0
                     if (success) {
 119  
                         // Remove the original file:
 120  0
                         file.delete();
 121  
                         // Relativise path, if possible.
 122  0
                         String canPath = (new File(dir)).getCanonicalPath();
 123  0
                         if (newFile.getCanonicalPath().startsWith(canPath)) {
 124  0
                             if ((newFile.getCanonicalPath().length() > canPath.length()) &&
 125  
                                     (newFile.getCanonicalPath().charAt(canPath.length()) == File.separatorChar))
 126  0
                                 flEntry.setLink(newFile.getCanonicalPath().substring(1+canPath.length()));
 127  
                             else
 128  0
                                 flEntry.setLink(newFile.getCanonicalPath().substring(canPath.length()));
 129  
 
 130  
 
 131  
                         }
 132  
                         else
 133  0
                             flEntry.setLink(newFile.getCanonicalPath());
 134  0
                         eEditor.updateField(editor);
 135  0
                         JOptionPane.showMessageDialog(frame, Globals.lang("File moved"),
 136  
                                 Globals.lang("Move/Rename file"), JOptionPane.INFORMATION_MESSAGE);
 137  0
                     } else {
 138  0
                         JOptionPane.showMessageDialog(frame, Globals.lang("Move file failed"),
 139  
                                 Globals.lang("Move/Rename file"), JOptionPane.ERROR_MESSAGE);
 140  
                     }
 141  
 
 142  0
                 } catch (SecurityException ex) {
 143  0
                     ex.printStackTrace();
 144  0
                     JOptionPane.showMessageDialog(frame, Globals.lang("Could not move file") + ": " + ex.getMessage(),
 145  
                             Globals.lang("Move/Rename file"), JOptionPane.ERROR_MESSAGE);
 146  0
                 } catch (IOException ex) {
 147  0
                     ex.printStackTrace();
 148  0
                     JOptionPane.showMessageDialog(frame, Globals.lang("Could not move file") + ": " + ex.getMessage(),
 149  
                             Globals.lang("Move/Rename file"), JOptionPane.ERROR_MESSAGE);
 150  0
                 }
 151  
 
 152  
             }
 153  0
         }
 154  
         else {
 155  
 
 156  
             // File doesn't exist, so we can't move it.
 157  0
             JOptionPane.showMessageDialog(frame, Globals.lang("Could not find file '%0'.", flEntry.getLink()),
 158  
                     Globals.lang("File not found"), JOptionPane.ERROR_MESSAGE);
 159  
             
 160  
         }
 161  
 
 162  0
     }
 163  
 }