1 | public void actionPerformed(ActionEvent event) { | | 1 | public void run() { |
2 | ArrayList<BibtexEntry> entries = new ArrayList<BibtexEntry>(); | | 2 | if (!goOn) { |
3 | for (BibtexEntry e: JabRef.jrf.basePanel().getSelectedEntries()) { | | 3 | panel.output(Globals.lang("No entries selected.")); |
4 | entries.add(e); | | 4 | return; |
5 | } | | 5 | } |
6 | if (entries.isEmpty()) { | | 6 | panel.frame().setProgressBarValue(0); |
7 | JabRef.jrf.basePanel().output(Globals.lang("No entries selected.")); | | 7 | panel.frame().setProgressBarVisible(true); |
8 | return; | | 8 | int weightAutoSet = 10; // autoSet takes 10 (?) times longer than checkExisting |
9 | } | | 9 | int progressBarMax = (autoSet ? weightAutoSet * sel.length : 0) |
10 | JDialog diag = new JDialog(JabRef.jrf, true); | | 10 | + (checkExisting ? sel.length : 0); |
11 | final NamedCompound nc = new NamedCompound(Globals.lang("Automatically set file links")); | | 11 | panel.frame().setProgressBarMaximum(progressBarMax); |
12 | Util.autoSetLinks(entries, nc, null, null, JabRef.jrf.basePanel().metaData(), new ActionListener() { | | 12 | int progress = 0; |
13 | public void actionPerformed(ActionEvent e) { | | 13 | brokenLinks = 0; |
14 | if (e.getID() > 0) { | | 14 | final NamedCompound ce = new NamedCompound(Globals.lang("Autoset %0 field", fieldName)); |
15 | // entry has been updated in Util.autoSetLinks, only treat nc and status message | | 15 | |
16 | if (nc.hasEdits()) { | | 16 | //final OpenFileFilter off = Util.getFileFilterForField(fieldName); |
17 | nc.end(); | | 17 | |
18 | JabRef.jrf.basePanel().undoManager.addEdit(nc); | | 18 | //ExternalFilePanel extPan = new ExternalFilePanel(fieldName, panel.metaData(), null, null, off); |
19 | JabRef.jrf.basePanel().markBaseChanged(); | | 19 | //FieldTextField editor = new FieldTextField(fieldName, "", false); |
20 | } | | 20 | |
21 | JabRef.jrf.output(Globals.lang("Finished autosetting external links.")); | | 21 | Set<BibtexEntry> changedEntries = new HashSet<BibtexEntry>(); |
22 | } | | 22 | |
23 | else JabRef.jrf.output(Globals.lang("Finished autosetting external links.") | | 23 | // First we try to autoset fields |
24 | +" "+Globals.lang("No files found.")); | | 24 | if (autoSet) { |
25 | } | | 25 | Collection<BibtexEntry> entries = new ArrayList<BibtexEntry>(); |
26 | }, diag); | | 26 | for (int i = 0; i < sel.length; i++) { |
27 | } | | 27 | entries.add(sel[i]); |
| | | 28 | } |
| | | 29 | |
| | | 30 | // Start the autosetting process: |
| | | 31 | Thread t = Util.autoSetLinks(entries, ce, changedEntries, null, panel.metaData(), null, null); |
| | | 32 | // Wait for the autosetting to finish: |
| | | 33 | try { |
| | | 34 | t.join(); |
| | | 35 | } catch (InterruptedException e) { |
| | | 36 | e.printStackTrace(); |
| | | 37 | } |
| | | 38 | /* |
| | | 39 | progress += weightAutoSet; |
| | | 40 | panel.frame().setProgressBarValue(progress); |
| | | 41 | |
| | | 42 | Object old = sel[i].getField(fieldName); |
| | | 43 | FileListTableModel tableModel = new FileListTableModel(); |
| | | 44 | if (old != null) |
| | | 45 | tableModel.setContent((String)old); |
| | | 46 | Thread t = FileListEditor.autoSetLinks(sel[i], tableModel, null, null); |
| | | 47 | |
| | | 48 | if (!tableModel.getStringRepresentation().equals(old)) { |
| | | 49 | String toSet = tableModel.getStringRepresentation(); |
| | | 50 | if (toSet.length() == 0) |
| | | 51 | toSet = null; |
| | | 52 | ce.addEdit(new UndoableFieldChange(sel[i], fieldName, old, toSet)); |
| | | 53 | sel[i].setField(fieldName, toSet); |
| | | 54 | entriesChanged++; |
| | | 55 | } |
| | | 56 | } */ |
| | | 57 | |
| | | 58 | } |
| | | 59 | progress += sel.length*weightAutoSet; |
| | | 60 | panel.frame().setProgressBarValue(progress); |
| | | 61 | //System.out.println("Done setting"); |
| | | 62 | // The following loop checks all external links that are already set. |
| | | 63 | if (checkExisting) { |
| | | 64 | boolean removeAllBroken = false; |
| | | 65 | mainLoop: |
| | | 66 | for (int i = 0; i < sel.length; i++) { |
| | | 67 | panel.frame().setProgressBarValue(progress++); |
| | | 68 | final String old = sel[i].getField(fieldName); |
| | | 69 | // Check if a extension is set: |
| | | 70 | if ((old != null) && !old.equals("")) { |
| | | 71 | FileListTableModel tableModel = new FileListTableModel(); |
| | | 72 | tableModel.setContentDontGuessTypes(old); |
| | | 73 | |
| | | 74 | // We need to specify which directories to search in for Util.expandFilename: |
| | | 75 | String[] dirsS = panel.metaData().getFileDirectory(GUIGlobals.FILE_FIELD); |
| | | 76 | ArrayList<File> dirs = new ArrayList<File>(); |
| | | 77 | for (int k=0; k<dirsS.length; k++) |
| | | 78 | dirs.add(new File(dirsS[k])); |
| | | 79 | |
| | | 80 | for (int j=0; j<tableModel.getRowCount(); j++) { |
| | | 81 | FileListEntry flEntry = tableModel.getEntry(j); |
| | | 82 | // See if the link looks like an URL: |
| | | 83 | boolean httpLink = flEntry.getLink().toLowerCase().startsWith("http"); |
| | | 84 | if (httpLink) |
| | | 85 | continue; // Don't check the remote file. |
| | | 86 | // TODO: should there be an option to check remote links? |
| | | 87 | |
| | | 88 | // A variable to keep track of whether this link gets deleted: |
| | | 89 | boolean deleted = false; |
| | | 90 | |
| | | 91 | // Get an absolute path representation: |
| | | 92 | File file = Util.expandFilename(flEntry.getLink(), dirsS); |
| | | 93 | if ((file == null) || !file.exists()) { |
| | | 94 | int answer; |
| | | 95 | if (!removeAllBroken) { |
| | | 96 | answer = JOptionPane.showOptionDialog(panel.frame(), |
| | | 97 | Globals.lang("<HTML>Could not find file '%0'<BR>linked from entry '%1'</HTML>", |
| | | 98 | new String[]{flEntry.getLink(), sel[i].getCiteKey()}), |
| | | 99 | Globals.lang("Broken link"), |
| | | 100 | JOptionPane.YES_NO_CANCEL_OPTION, |
| | | 101 | JOptionPane.QUESTION_MESSAGE, null, brokenLinkOptions, brokenLinkOptions[0]); |
| | | 102 | } |
| | | 103 | else { |
| | | 104 | answer = 2; // We should delete this link. |
| | | 105 | } |
| | | 106 | switch (answer) { |
| | | 107 | case 1: |
| | | 108 | // Assign new file. |
| | | 109 | FileListEntryEditor flEditor = new FileListEntryEditor |
| | | 110 | (panel.frame(), flEntry, false, true, panel.metaData()); |
| | | 111 | flEditor.setVisible(true, true); |
| | | 112 | break; |
| | | 113 | case 2: |
| | | 114 | // Clear field: |
| | | 115 | tableModel.removeEntry(j); |
| | | 116 | deleted = true; // Make sure we don't investigate this link further. |
| | | 117 | j--; // Step back in the iteration, because we removed an entry. |
| | | 118 | break; |
| | | 119 | case 3: |
| | | 120 | // Clear field: |
| | | 121 | tableModel.removeEntry(j); |
| | | 122 | deleted = true; // Make sure we don't investigate this link further. |
| | | 123 | j--; // Step back in the iteration, because we removed an entry. |
| | | 124 | removeAllBroken = true; // Notify for further cases. |
| | | 125 | break; |
| | | 126 | case 4: |
| | | 127 | // Cancel |
| | | 128 | break mainLoop; |
| | | 129 | } |
| | | 130 | brokenLinks++; |
| | | 131 | } |
| | | 132 | |
| | | 133 | // Unless we deleted this link, see if its file type is recognized: |
| | | 134 | if (!deleted && (flEntry.getType() instanceof UnknownExternalFileType)) { |
| | | 135 | String[] options = new String[] |
| | | 136 | {Globals.lang("Define '%0'", flEntry.getType().getName()), |
| | | 137 | Globals.lang("Change file type"), Globals.lang("Cancel")}; |
| | | 138 | String defOption = options[0]; |
| | | 139 | int answer = JOptionPane.showOptionDialog(panel.frame(), Globals.lang("One or more file links are of the type '%0', which is undefined. What do you want to do?", |
| | | 140 | flEntry.getType().getName()), |
| | | 141 | Globals.lang("Undefined file type"), JOptionPane.YES_NO_CANCEL_OPTION, |
| | | 142 | JOptionPane.QUESTION_MESSAGE, null, options, defOption); |
| | | 143 | if (answer == JOptionPane.CANCEL_OPTION) { |
| | | 144 | // User doesn't want to handle this unknown link type. |
| | | 145 | } |
| | | 146 | else if (answer == JOptionPane.YES_OPTION) { |
| | | 147 | // User wants to define the new file type. Show the dialog: |
| | | 148 | ExternalFileType newType = new ExternalFileType(flEntry.getType().getName(), "", "", "", "new"); |
| | | 149 | ExternalFileTypeEntryEditor editor = new ExternalFileTypeEntryEditor(panel.frame(), newType); |
| | | 150 | editor.setVisible(true); |
| | | 151 | if (editor.okPressed()) { |
| | | 152 | // Get the old list of types, add this one, and update the list in prefs: |
| | | 153 | java.util.List<ExternalFileType> fileTypes = new ArrayList<ExternalFileType>(); |
| | | 154 | ExternalFileType[] oldTypes = Globals.prefs.getExternalFileTypeSelection(); |
| | | 155 | for (int k = 0; k < oldTypes.length; k++) { |
| | | 156 | fileTypes.add(oldTypes[k]); |
| | | 157 | } |
| | | 158 | fileTypes.add(newType); |
| | | 159 | Collections.sort(fileTypes); |
| | | 160 | Globals.prefs.setExternalFileTypes(fileTypes); |
| | | 161 | panel.mainTable.repaint(); |
| | | 162 | } |
| | | 163 | } |
| | | 164 | else { |
| | | 165 | // User wants to change the type of this link. |
| | | 166 | // First get a model of all file links for this entry: |
| | | 167 | FileListEntryEditor editor = new FileListEntryEditor |
| | | 168 | (panel.frame(), flEntry, false, true, panel.metaData()); |
| | | 169 | editor.setVisible(true, false); |
| | | 170 | } |
| | | 171 | } |
| | | 172 | } |
| | | 173 | |
| | | 174 | if (!tableModel.getStringRepresentation().equals(old)) { |
| | | 175 | // The table has been modified. Store the change: |
| | | 176 | String toSet = tableModel.getStringRepresentation(); |
| | | 177 | if (toSet.length() == 0) |
| | | 178 | toSet = null; |
| | | 179 | ce.addEdit(new UndoableFieldChange(sel[i], fieldName, old, |
| | | 180 | toSet)); |
| | | 181 | sel[i].setField(fieldName, toSet); |
| | | 182 | changedEntries.add(sel[i]); |
| | | 183 | //System.out.println("Changed to: "+tableModel.getStringRepresentation()); |
| | | 184 | } |
| | | 185 | |
| | | 186 | |
| | | 187 | } |
| | | 188 | } |
| | | 189 | } |
| | | 190 | |
| | | 191 | //for (BibtexEntry entr : changedEntries) |
| | | 192 | // System.out.println(entr.getCiteKey()); |
| | | 193 | if (!changedEntries.isEmpty()) { |
| | | 194 | // Add the undo edit: |
| | | 195 | ce.end(); |
| | | 196 | panel.undoManager.addEdit(ce); |
| | | 197 | panel.markBaseChanged(); |
| | | 198 | } |
| | | 199 | } |