public void actionPerformed(ActionEvent e) { ExportFormats.initAllExports(); JFileChooser fc = ExportFormats.createExportFileChooser( Globals.prefs.get("exportWorkingDirectory")); fc.showSaveDialog(frame); File file = fc.getSelectedFile(); if (file == null) return; FileFilter ff = fc.getFileFilter(); if (ff instanceof ExportFileFilter) { ExportFileFilter eff = (ExportFileFilter) ff; String path = file.getPath(); if (!path.endsWith(eff.getExtension())) path = path + eff.getExtension(); file = new File(path); if (file.exists()) { // Warn that the file exists: if (JOptionPane.showConfirmDialog(frame, "'" + file.getName() + "' " + Globals.lang("exists. Overwrite file?"), Globals.lang("Export"), JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION) return; } final IExportFormat format = eff.getExportFormat(); Set<String> entryIds = null; if (selectedOnly) { BibtexEntry[] selected = frame.basePanel().getSelectedEntries(); entryIds = new HashSet<String>(); for (int i = 0; i < selected.length; i++) { BibtexEntry bibtexEntry = selected[i]; entryIds.add(bibtexEntry.getId()); } } // Set the global variable for this database's file directory before exporting, // so formatters can resolve linked files correctly. // (This is an ugly hack!) Globals.prefs.fileDirForDatabase = frame.basePanel().metaData() .getFileDirectory(GUIGlobals.FILE_FIELD); // Also store the database's file in a global variable: Globals.prefs.databaseFile = frame.basePanel().metaData().getFile(); // Make sure we remember which filter was used, to set // the default for next time: Globals.prefs.put("lastUsedExport", format.getConsoleName()); Globals.prefs.put("exportWorkingDirectory", file.getParent()); final File finFile = file; final Set<String> finEntryIDs = entryIds; AbstractWorker exportWorker = new AbstractWorker() { String errorMessage = null; public void run() { try { format.performExport(frame.basePanel().database(), frame.basePanel().metaData(), finFile.getPath(), frame .basePanel().getEncoding(), finEntryIDs); } catch (Exception ex) { ex.printStackTrace(); if (ex.getMessage()==null ) { errorMessage = ex.toString(); } else { errorMessage = ex.getMessage(); } } } public void update() { // No error message. Report success: if (errorMessage == null) { frame.output(Globals.lang("%0 export successful", format.getDisplayName())); } // ... or show an error dialog: else { frame.output(Globals.lang("Could not save file") + " - " + errorMessage); // Need to warn the user that saving failed! JOptionPane.showMessageDialog(frame, Globals.lang("Could not save file") + ".\n" + errorMessage, Globals.lang("Save database"), JOptionPane.ERROR_MESSAGE); } } }; // Run the export action in a background thread: (exportWorker.getWorker()).run(); // Run the update method: exportWorker.update(); } }
public void run() { BasePanel panel = frame.basePanel(); if (panel == null) return; if (panel.getSelectedEntries().length == 0) { message = Globals.lang("No entries selected."); getCallBack().update(); return; } Map<String, IExportFormat> m = ExportFormats.getExportFormats(); IExportFormat[] formats = new ExportFormat[m.size()]; String[] array = new String[formats.length]; int piv = 0; for (IExportFormat format : m.values()) { formats[piv] = format; array[piv] = format.getDisplayName(); piv++; } JList list = new JList(array); list.setBorder(BorderFactory.createEtchedBorder()); list.setSelectionInterval(0, 0); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); int answer = JOptionPane.showOptionDialog(frame, list, Globals.lang("Select format"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[]{Globals.lang("Ok"), Globals.lang("Cancel")}, Globals.lang("Ok")); if (answer == JOptionPane.NO_OPTION) return; IExportFormat format = formats[list.getSelectedIndex()]; // Set the global variable for this database's file directory before exporting, // so formatters can resolve linked files correctly. // (This is an ugly hack!) Globals.prefs.fileDirForDatabase = frame.basePanel().metaData() .getFileDirectory(GUIGlobals.FILE_FIELD); // Also store the database's file in a global variable: Globals.prefs.databaseFile = frame.basePanel().metaData().getFile(); /*final boolean custom = (list.getSelectedIndex() >= Globals.STANDARD_EXPORT_COUNT); String dir = null; if (custom) { int index = list.getSelectedIndex() - Globals.STANDARD_EXPORT_COUNT; dir = (String) (Globals.prefs.customExports.getElementAt(index)[1]); File f = new File(dir); lfName = f.getName(); lfName = lfName.substring(0, lfName.indexOf(".")); // Remove file name - we want the directory only. dir = f.getParent() + System.getProperty("file.separator"); } final String format = lfName, directory = dir; */ File tmp = null; Reader reader = null; try { // To simplify the exporter API we simply do a normal export to a temporary // file, and read the contents afterwards: tmp = File.createTempFile("jabrefCb", ".tmp"); tmp.deleteOnExit(); BibtexEntry[] bes = panel.getSelectedEntries(); HashSet<String> entries = new HashSet<String>(bes.length); for (BibtexEntry be : bes) entries.add(be.getId()); // Write to file: format.performExport(database, panel.metaData(), tmp.getPath(), panel.getEncoding(), entries); // Read the file and put the contents on the clipboard: StringBuffer sb = new StringBuffer(); reader = new InputStreamReader(new FileInputStream(tmp), panel.getEncoding()); int s; while ((s = reader.read()) != -1) { sb.append((char)s); } ClipboardOwner owner = new ClipboardOwner() { public void lostOwnership(Clipboard clipboard, Transferable content) { } }; //StringSelection ss = new StringSelection(sw.toString()); RtfSelection rs = new RtfSelection(sb.toString()); Toolkit.getDefaultToolkit().getSystemClipboard() .setContents(rs, owner); message = Globals.lang("Entries exported to clipboard") + ": " + bes.length; } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. message = Globals.lang("Error exporting to clipboard"); return; } finally { // Clean up: if (tmp != null) tmp.delete(); if (reader != null) try { reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
Clone fragments detected by clone detection tool
File path: /jabref-2.10/src/java/net/sf/jabref/export/ExportFormats.java File path: /jabref-2.10/src/java/net/sf/jabref/export/ExportToClipboardAction.java
Method name: void actionPerformed(ActionEvent) Method name: void run()
Number of AST nodes: 3 Number of AST nodes: 2
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
    }
  1. {Refactorable}
    Mapping Summary
    Number of mapped statements3
    Number of unmapped statements in the first code fragment0
    Number of unmapped statements in the second code fragment0
    Time elapsed for statement mapping (ms)0.0
    Similarity Score0.667
    Clone typeType 2
    Mapped Statements
    ID Statement ID Statement
    22
    for (int i = 0; i < selected.length; i++)
    22
    for (int i = 0; i < selected.length; i++)
    33
    for (BibtexEntry be : bes)
    Differences
    Expression1Expression2Difference
    bibtexEntrybeVARIABLE_NAME_MISMATCH
    33
    for (BibtexEntry be : bes)
    23
    BibtexEntry bibtexEntry = selected[i];
    23
    BibtexEntry bibtexEntry = selected[i];
    Differences
    Expression1Expression2Difference
    bibtexEntrybeVARIABLE_NAME_MISMATCH
                                                                                    
    24
    entryIds.add(bibtexEntry.getId());
    24
    entryIds.add(bibtexEntry.getId());
    34
    entries.add(be.getId());
    Differences
    Expression1Expression2Difference
    bibtexEntrybeVARIABLE_NAME_MISMATCH
    entryIdsentriesVARIABLE_NAME_MISMATCH
    java.util.Setjava.util.HashSetSUBCLASS_TYPE_MISMATCH
    34
    entries.add(be.getId());
    Precondition Violations (0)
    Row Violation