1 | public void actionPerformed(ActionEvent e) { | | 1 | public void run() { |
2 | ExportFormats.initAllExports(); | | 2 | BasePanel panel = frame.basePanel(); |
3 | JFileChooser fc = ExportFormats.createExportFileChooser( | | 3 | if (panel == null) |
4 | Globals.prefs.get("exportWorkingDirectory")); | | 4 | return; |
5 | fc.showSaveDialog(frame); | | 5 | if (panel.getSelectedEntries().length == 0) { |
6 | File file = fc.getSelectedFile(); | | 6 | message = Globals.lang("No entries selected."); |
7 | if (file == null) | | 7 | getCallBack().update(); |
8 | return; | | 8 | return; |
9 | FileFilter ff = fc.getFileFilter(); | | 9 | } |
10 | if (ff instanceof ExportFileFilter) { | | 10 | |
11 | | | 11 | Map<String, IExportFormat> m = ExportFormats.getExportFormats(); |
12 | | | 12 | IExportFormat[] formats = new ExportFormat[m.size()]; |
13 | ExportFileFilter eff = (ExportFileFilter) ff; | | 13 | String[] array = new String[formats.length]; |
14 | String path = file.getPath(); | | 14 | |
15 | if (!path.endsWith(eff.getExtension())) | | 15 | int piv = 0; |
16 | path = path + eff.getExtension(); | | 16 | for (IExportFormat format : m.values()) { |
17 | file = new File(path); | | 17 | formats[piv] = format; |
18 | if (file.exists()) { | | 18 | array[piv] = format.getDisplayName(); |
19 | // Warn that the file exists: | | 19 | piv++; |
20 | if (JOptionPane.showConfirmDialog(frame, "'" + file.getName() + "' " | | 20 | } |
21 | + Globals.lang("exists. Overwrite file?"), Globals.lang("Export"), | | 21 | |
22 | JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION) | | 22 | JList list = new JList(array); |
23 | return; | | 23 | list.setBorder(BorderFactory.createEtchedBorder()); |
24 | } | | 24 | list.setSelectionInterval(0, 0); |
25 | final IExportFormat format = eff.getExportFormat(); | | 25 | list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
26 | Set<String> entryIds = null; | | 26 | int answer = JOptionPane.showOptionDialog(frame, list, Globals.lang("Select format"), |
27 | if (selectedOnly) { | | 27 | JOptionPane.YES_NO_OPTION, |
28 | BibtexEntry[] selected = frame.basePanel().getSelectedEntries(); | | 28 | JOptionPane.QUESTION_MESSAGE, null, |
29 | entryIds = new HashSet<String>(); | | 29 | new String[]{Globals.lang("Ok"), Globals.lang("Cancel")}, |
30 | for (int i = 0; i < selected.length; i++) { | | 30 | Globals.lang("Ok")); |
31 | BibtexEntry bibtexEntry = selected[i]; | | 31 | |
32 | entryIds.add(bibtexEntry.getId()); | | 32 | if (answer == JOptionPane.NO_OPTION) |
33 | } | | 33 | return; |
34 | } | | 34 | |
35 | | | 35 | IExportFormat format = formats[list.getSelectedIndex()]; |
36 | // Set the global variable for this database's file directory before exporting, | | 36 | |
37 | // so formatters can resolve linked files correctly. | | 37 | // Set the global variable for this database's file directory before exporting, |
38 | // (This is an ugly hack!) | | 38 | // so formatters can resolve linked files correctly. |
39 | Globals.prefs.fileDirForDatabase = frame.basePanel().metaData() | | 39 | // (This is an ugly hack!) |
40 | .getFileDirectory(GUIGlobals.FILE_FIELD); | | 40 | Globals.prefs.fileDirForDatabase = frame.basePanel().metaData() |
41 | // Also store the database's file in a global variable: | | 41 | .getFileDirectory(GUIGlobals.FILE_FIELD); |
42 | Globals.prefs.databaseFile = frame.basePanel().metaData().getFile(); | | 42 | // Also store the database's file in a global variable: |
43 | | | 43 | Globals.prefs.databaseFile = frame.basePanel().metaData().getFile(); |
44 | // Make sure we remember which filter was used, to set | | 44 | |
45 | // the default for next time: | | 45 | |
46 | Globals.prefs.put("lastUsedExport", format.getConsoleName()); | | 46 | /*final boolean custom = (list.getSelectedIndex() >= Globals.STANDARD_EXPORT_COUNT); |
47 | Globals.prefs.put("exportWorkingDirectory", file.getParent()); | | 47 | String dir = null; |
48 | | | 48 | if (custom) { |
49 | final File finFile = file; | | 49 | int index = list.getSelectedIndex() - Globals.STANDARD_EXPORT_COUNT; |
50 | final Set<String> finEntryIDs = entryIds; | | 50 | dir = (String) (Globals.prefs.customExports.getElementAt(index)[1]); |
51 | AbstractWorker exportWorker = new AbstractWorker() { | | 51 | File f = new File(dir); |
52 | String errorMessage = null; | | 52 | lfName = f.getName(); |
53 | public void run() { | | 53 | lfName = lfName.substring(0, lfName.indexOf(".")); |
54 | try { | | 54 | // Remove file name - we want the directory only. |
55 | format.performExport(frame.basePanel().database(), | | 55 | dir = f.getParent() + System.getProperty("file.separator"); |
56 | frame.basePanel().metaData(), | | 56 | } |
57 | finFile.getPath(), frame | | 57 | final String format = lfName, |
58 | .basePanel().getEncoding(), finEntryIDs); | | 58 | directory = dir; |
59 | } catch (Exception ex) { | | 59 | */ |
60 | ex.printStackTrace(); | | 60 | File tmp = null; |
61 | if (ex.getMessage()==null ) { | | 61 | Reader reader = null; |
62 | errorMessage = ex.toString(); | | 62 | try { |
63 | } else { | | 63 | // To simplify the exporter API we simply do a normal export to a temporary |
64 | errorMessage = ex.getMessage(); | | 64 | // file, and read the contents afterwards: |
65 | } | | 65 | tmp = File.createTempFile("jabrefCb", ".tmp"); |
66 | } | | 66 | tmp.deleteOnExit(); |
67 | } | | 67 | BibtexEntry[] bes = panel.getSelectedEntries(); |
68 | | | 68 | HashSet<String> entries = new HashSet<String>(bes.length); |
69 | public void update() { | | 69 | for (BibtexEntry be : bes) |
70 | // No error message. Report success: | | 70 | entries.add(be.getId()); |
71 | if (errorMessage == null) { | | 71 | |
72 | frame.output(Globals.lang("%0 export successful", format.getDisplayName())); | | 72 | // Write to file: |
73 | } | | 73 | format.performExport(database, panel.metaData(), |
74 | // ... or show an error dialog: | | 74 | tmp.getPath(), panel.getEncoding(), entries); |
75 | else { | | 75 | // Read the file and put the contents on the clipboard: |
76 | frame.output(Globals.lang("Could not save file") | | 76 | StringBuffer sb = new StringBuffer(); |
77 | + " - " + errorMessage); | | 77 | reader = new InputStreamReader(new FileInputStream(tmp), panel.getEncoding()); |
78 | // Need to warn the user that saving failed! | | 78 | int s; |
79 | JOptionPane.showMessageDialog(frame, Globals.lang("Could not save file") | | 79 | while ((s = reader.read()) != -1) { |
80 | + ".\n" + errorMessage, Globals.lang("Save database"), | | 80 | sb.append((char)s); |
81 | JOptionPane.ERROR_MESSAGE); | | 81 | } |
82 | } | | 82 | ClipboardOwner owner = new ClipboardOwner() { |
83 | } | | 83 | public void lostOwnership(Clipboard clipboard, Transferable content) { |
84 | }; | | 84 | } |
85 | | | 85 | }; |
86 | // Run the export action in a background thread: | | 86 | //StringSelection ss = new StringSelection(sw.toString()); |
87 | (exportWorker.getWorker()).run(); | | 87 | RtfSelection rs = new RtfSelection(sb.toString()); |
88 | // Run the update method: | | 88 | Toolkit.getDefaultToolkit().getSystemClipboard() |
89 | exportWorker.update(); | | 89 | .setContents(rs, owner); |
90 | } | | 90 | message = Globals.lang("Entries exported to clipboard") + ": " + bes.length; |
91 | } | | 91 | |
| | | 92 | } catch (Exception e) { |
| | | 93 | e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. |
| | | 94 | message = Globals.lang("Error exporting to clipboard"); |
| | | 95 | return; |
| | | 96 | } finally { |
| | | 97 | // Clean up: |
| | | 98 | if (tmp != null) |
| | | 99 | tmp.delete(); |
| | | 100 | if (reader != null) |
| | | 101 | try { reader.close(); } catch (IOException ex) { ex.printStackTrace(); } |
| | | 102 | } |
| | | 103 | |
| | | 104 | } |