1 package net.sf.jabref.export;
3 import net.sf.jabref.*;
4 import net.sf.jabref.gui.MainTable;
9 import java.awt.datatransfer.ClipboardOwner;
10 import java.awt.datatransfer.Clipboard;
11 import java.awt.datatransfer.Transferable;
15 * Created by IntelliJ IDEA.
19 * To change this template use File | Settings | File Templates.
21 public class ExportToClipboardAction extends AbstractWorker {
22 String message = null;
23 private JabRefFrame frame;
24 private BibtexDatabase database;
26 public ExportToClipboardAction(JabRefFrame frame, BibtexDatabase database) {
28 this.database = database;
31 BasePanel panel = frame.basePanel();
34 if (panel.getSelectedEntries().length == 0) {
35 message = Globals.lang("No entries selected") + ".";
36 getCallBack().update();
40 Map m = ExportFormats.getExportFormats();
41 ExportFormat[] formats = new ExportFormat[m.size()];
43 for (Iterator iterator = m.keySet().iterator(); iterator.hasNext();) {
44 formats[piv++] = (ExportFormat)m.get(iterator.next());
46 //ExportFormat[] formats = (ExportFormat[])(m.entrySet().toArray
47 // (new ExportFormat[m.size()]));
49 // Make a list of possible formats:
50 /*Map formats = new HashMap();
51 formats.put("BibTeXML", "bibtexml");
52 formats.put("DocBook", "docbook");
53 formats.put("HTML", "html");
54 formats.put("RTF (Harvard)", "harvard/harvard");
55 formats.put("Simple HTML", "simplehtml");
56 for (int i = 0; i < Globals.prefs.customExports.size(); i++) {
57 Object o = (Globals.prefs.customExports.getElementAt(i))[0];
60 String[] array = new String[formats.length];
61 for (int i=0; i<formats.length; i++) {
62 array[i] = formats[i].getDisplayName();
65 JList list = new JList(array);
66 list.setBorder(BorderFactory.createEtchedBorder());
67 list.setSelectionInterval(0, 0);
68 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
69 int answer = JOptionPane.showOptionDialog(frame, list, Globals.lang("Select format"),
70 JOptionPane.YES_NO_OPTION,
71 JOptionPane.QUESTION_MESSAGE, null,
72 new String[]{Globals.lang("Ok"), Globals.lang("Cancel")},
75 if (answer == JOptionPane.NO_OPTION)
78 ExportFormat format = formats[list.getSelectedIndex()];
80 /*final boolean custom = (list.getSelectedIndex() >= Globals.STANDARD_EXPORT_COUNT);
83 int index = list.getSelectedIndex() - Globals.STANDARD_EXPORT_COUNT;
84 dir = (String) (Globals.prefs.customExports.getElementAt(index)[1]);
85 File f = new File(dir);
87 lfName = lfName.substring(0, lfName.indexOf("."));
88 // Remove file name - we want the directory only.
89 dir = f.getParent() + System.getProperty("file.separator");
91 final String format = lfName,
97 // To simplify the exporter API we simply do a normal export to a temporary
98 // file, and read the contents afterwards:
99 tmp = File.createTempFile("jabrefCb", ".tmp");
101 BibtexEntry[] bes = panel.getSelectedEntries();
102 HashSet entries = new HashSet(bes.length);
103 for (int i = 0; i < bes.length; i++)
104 entries.add(bes[i].getId());
106 format.performExport(database, tmp.getPath(), panel.getEncoding(), entries);
107 // Read the file and put the contents on the clipboard:
108 StringBuffer sb = new StringBuffer();
109 reader = new InputStreamReader(new FileInputStream(tmp), panel.getEncoding());
111 while ((s = reader.read()) != -1) {
114 ClipboardOwner owner = new ClipboardOwner() {
115 public void lostOwnership(Clipboard clipboard, Transferable content) {
118 //StringSelection ss = new StringSelection(sw.toString());
119 RtfSelection rs = new RtfSelection(sb.toString());
120 Toolkit.getDefaultToolkit().getSystemClipboard()
121 .setContents(rs, owner);
122 message = Globals.lang("Entries exported to clipboard") + ": " + bes.length;
124 } catch (Exception e) {
125 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
126 message = Globals.lang("Error exporting to clipboard");
133 try { reader.close(); } catch (IOException ex) { ex.printStackTrace(); }
138 public void update() {
139 frame.output(message);