1 | /** | | 1 | public List<String> findCitedKeys() throws com.sun.star.container.NoSuchElementException, WrappedTargetException { |
2 | * Take a list of external links and copy the referred files to a given directory. | | 2 | |
3 | * This method should be run off the Event Dispatch Thread. A progress bar, if given, | | 3 | XReferenceMarksSupplier supplier = (XReferenceMarksSupplier) UnoRuntime.queryInterface( |
4 | * will be updated on the EDT. | | 4 | XReferenceMarksSupplier.class, xCurrentComponent); |
5 | * | | 5 | XNameAccess xNamedMarks = supplier.getReferenceMarks(); |
6 | * @param files The list of file links. | | 6 | String[] names = xNamedMarks.getElementNames(); |
7 | * @param toDir The directory to copy the files to. | | 7 | ArrayList<String> keys = new ArrayList<String>(); |
8 | * @param metaData The MetaData for the database containing the external links. This is needed | | 8 | for (int i = 0; i < names.length; i++) { |
9 | * because the database might have its own file directory. | | 9 | Object bookmark = xNamedMarks.getByName(names[i]); |
10 | * @param prog A JProgressBar which will be updated to show the progress of the process. | | 10 | XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface( |
11 | * This argument can be null if no progress bar is needed. | | 11 | XTextContent.class, bookmark); |
12 | * @param deleteOriginalFiles if true, the files in their original locations will be deleted | | 12 | |
13 | * after copying, for each file whose source directory is different from the destination | | 13 | String name = names[i]; |
14 | * directory differs. | | 14 | List<String> newKeys = parseRefMarkName(name); |
15 | * @param callback An ActionListener which should be notified when the process is finished. | | 15 | for (String key : newKeys) |
16 | * This parameter can be null if no callback is needed. | | 16 | if (!keys.contains(key)) |
17 | */ | | 17 | keys.add(key); |
18 | public static void copyExternalLinksToDirectory(final List<FileListEntry> files, File toDir, | | 18 | } |
19 | MetaData metaData, final JProgressBar prog, | | 19 | |
20 | boolean deleteOriginalFiles, | | 20 | return keys; |
21 | final ActionListener callback) { | | 21 | } |
22 | | | | |
23 | if (prog != null) SwingUtilities.invokeLater(new Runnable() { | | | |
24 | public void run() { | | | |
25 | prog.setMaximum(files.size()); | | | |
26 | prog.setValue(0); | | | |
27 | prog.setIndeterminate(false); | | | |
28 | } | | | |
29 | }); | | | |
30 | | | | |
31 | Set<String> fileNames = new HashSet<String>(); | | | |
32 | | | | |
33 | int i=0; | | | |
34 | | | | |
35 | for (Iterator<FileListEntry> iterator = files.iterator(); iterator.hasNext();) { | | | |
36 | FileListEntry entry = iterator.next(); | | | |
37 | File file = new File(entry.getLink()); | | | |
38 | | | | |
39 | // We try to check the extension for the file: | | | |
40 | String name = file.getName(); | | | |
41 | int pos = name.lastIndexOf('.'); | | | |
42 | String extension = ((pos >= 0) && (pos < name.length() - 1)) ? name.substring(pos + 1) | | | |
43 | .trim().toLowerCase() : null; | | | |
44 | | | | |
45 | // Find the default directory for this field type, if any: | | | |
46 | String[] dir = metaData.getFileDirectory(extension); | | | |
47 | // Include the standard "file" directory: | | | |
48 | String[] fileDir = metaData.getFileDirectory(GUIGlobals.FILE_FIELD); | | | |
49 | // Include the directory of the bib file: | | | |
50 | ArrayList<String> al = new ArrayList<String>(); | | | |
51 | for (int i2 = 0; i2 < dir.length; i2++) | | | |
52 | if (!al.contains(dir[i2])) al.add(dir[i2]); | | | |
53 | for (int i2 = 0; i2 < fileDir.length; i2++) | | | |
54 | if (!al.contains(fileDir[i2])) al.add(fileDir[i2]); | | | |
55 | | | | |
56 | String[] dirs = al.toArray(new String[al.size()]); | | | |
57 | File tmp = Util.expandFilename(entry.getLink(), dirs); | | | |
58 | if (tmp != null) | | | |
59 | file = tmp; | | | |
60 | | | | |
61 | // Check if we have arrived at an existing file: | | | |
62 | if (file.exists()) { | | | |
63 | if (fileNames.contains(name)) { | | | |
64 | // Oops, a file of that name already exists.... | | | |
65 | } | | | |
66 | else { | | | |
67 | fileNames.add(name); | | | |
68 | File destination = new File(toDir, name); | | | |
69 | | | | |
70 | // Check if the source and destination locations differ: | | | |
71 | if (!destination.equals(file)) { | | | |
72 | try { | | | |
73 | // Copy the file: | | | |
74 | Util.copyFile(file, destination, false); | | | |
75 | // Delete the original file if requested: | | | |
76 | if (deleteOriginalFiles) | | | |
77 | file.delete(); | | | |
78 | | | | |
79 | } catch (IOException ex) { | | | |
80 | ex.printStackTrace(); | | | |
81 | } | | | |
82 | } | | | |
83 | else { | | | |
84 | // Destination and source is the same. Do nothing. | | | |
85 | } | | | |
86 | // Update progress bar: | | | |
87 | i++; | | | |
88 | final int j = i; | | | |
89 | | | | |
90 | if (prog != null) SwingUtilities.invokeLater(new Runnable() { | | | |
91 | public void run() { | | | |
92 | prog.setValue(j); | | | |
93 | } | | | |
94 | }); | | | |
95 | } | | | |
96 | } | | | |
97 | else { | | | |
98 | // The link could not be resolved to an existing file. | | | |
99 | | | | |
100 | } | | | |
101 | } | | | |
102 | | | | |
103 | if (callback != null) { | | | |
104 | callback.actionPerformed(null); | | | |
105 | } | | | |
106 | } | | | |