public void actionPerformed(ActionEvent event) { ArrayList<BibtexEntry> entries = new ArrayList<BibtexEntry>(); for (BibtexEntry e: JabRef.jrf.basePanel().getSelectedEntries()) { entries.add(e); } if (entries.isEmpty()) { JabRef.jrf.basePanel().output(Globals.lang("No entries selected.")); return; } JDialog diag = new JDialog(JabRef.jrf, true); final NamedCompound nc = new NamedCompound(Globals.lang("Automatically set file links")); Util.autoSetLinks(entries, nc, null, null, JabRef.jrf.basePanel().metaData(), new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getID() > 0) { // entry has been updated in Util.autoSetLinks, only treat nc and status message if (nc.hasEdits()) { nc.end(); JabRef.jrf.basePanel().undoManager.addEdit(nc); JabRef.jrf.basePanel().markBaseChanged(); } JabRef.jrf.output(Globals.lang("Finished autosetting external links.")); } else JabRef.jrf.output(Globals.lang("Finished autosetting external links.") +" "+Globals.lang("No files found.")); } }, diag); }
public void run() { if (!goOn) { panel.output(Globals.lang("No entries selected.")); return; } panel.frame().setProgressBarValue(0); panel.frame().setProgressBarVisible(true); int weightAutoSet = 10; // autoSet takes 10 (?) times longer than checkExisting int progressBarMax = (autoSet ? weightAutoSet * sel.length : 0) + (checkExisting ? sel.length : 0); panel.frame().setProgressBarMaximum(progressBarMax); int progress = 0; brokenLinks = 0; final NamedCompound ce = new NamedCompound(Globals.lang("Autoset %0 field", fieldName)); //final OpenFileFilter off = Util.getFileFilterForField(fieldName); //ExternalFilePanel extPan = new ExternalFilePanel(fieldName, panel.metaData(), null, null, off); //FieldTextField editor = new FieldTextField(fieldName, "", false); Set<BibtexEntry> changedEntries = new HashSet<BibtexEntry>(); // First we try to autoset fields if (autoSet) { Collection<BibtexEntry> entries = new ArrayList<BibtexEntry>(); for (int i = 0; i < sel.length; i++) { entries.add(sel[i]); } // Start the autosetting process: Thread t = Util.autoSetLinks(entries, ce, changedEntries, null, panel.metaData(), null, null); // Wait for the autosetting to finish: try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } /* progress += weightAutoSet; panel.frame().setProgressBarValue(progress); Object old = sel[i].getField(fieldName); FileListTableModel tableModel = new FileListTableModel(); if (old != null) tableModel.setContent((String)old); Thread t = FileListEditor.autoSetLinks(sel[i], tableModel, null, null); if (!tableModel.getStringRepresentation().equals(old)) { String toSet = tableModel.getStringRepresentation(); if (toSet.length() == 0) toSet = null; ce.addEdit(new UndoableFieldChange(sel[i], fieldName, old, toSet)); sel[i].setField(fieldName, toSet); entriesChanged++; } } */ } progress += sel.length*weightAutoSet; panel.frame().setProgressBarValue(progress); //System.out.println("Done setting"); // The following loop checks all external links that are already set. if (checkExisting) { boolean removeAllBroken = false; mainLoop: for (int i = 0; i < sel.length; i++) { panel.frame().setProgressBarValue(progress++); final String old = sel[i].getField(fieldName); // Check if a extension is set: if ((old != null) && !old.equals("")) { FileListTableModel tableModel = new FileListTableModel(); tableModel.setContentDontGuessTypes(old); // We need to specify which directories to search in for Util.expandFilename: String[] dirsS = panel.metaData().getFileDirectory(GUIGlobals.FILE_FIELD); ArrayList<File> dirs = new ArrayList<File>(); for (int k=0; k<dirsS.length; k++) dirs.add(new File(dirsS[k])); for (int j=0; j<tableModel.getRowCount(); j++) { FileListEntry flEntry = tableModel.getEntry(j); // See if the link looks like an URL: boolean httpLink = flEntry.getLink().toLowerCase().startsWith("http"); if (httpLink) continue; // Don't check the remote file. // TODO: should there be an option to check remote links? // A variable to keep track of whether this link gets deleted: boolean deleted = false; // Get an absolute path representation: File file = Util.expandFilename(flEntry.getLink(), dirsS); if ((file == null) || !file.exists()) { int answer; if (!removeAllBroken) { answer = JOptionPane.showOptionDialog(panel.frame(), Globals.lang("<HTML>Could not find file '%0'<BR>linked from entry '%1'</HTML>", new String[]{flEntry.getLink(), sel[i].getCiteKey()}), Globals.lang("Broken link"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, brokenLinkOptions, brokenLinkOptions[0]); } else { answer = 2; // We should delete this link. } switch (answer) { case 1: // Assign new file. FileListEntryEditor flEditor = new FileListEntryEditor (panel.frame(), flEntry, false, true, panel.metaData()); flEditor.setVisible(true, true); break; case 2: // Clear field: tableModel.removeEntry(j); deleted = true; // Make sure we don't investigate this link further. j--; // Step back in the iteration, because we removed an entry. break; case 3: // Clear field: tableModel.removeEntry(j); deleted = true; // Make sure we don't investigate this link further. j--; // Step back in the iteration, because we removed an entry. removeAllBroken = true; // Notify for further cases. break; case 4: // Cancel break mainLoop; } brokenLinks++; } // Unless we deleted this link, see if its file type is recognized: if (!deleted && (flEntry.getType() instanceof UnknownExternalFileType)) { String[] options = new String[] {Globals.lang("Define '%0'", flEntry.getType().getName()), Globals.lang("Change file type"), Globals.lang("Cancel")}; String defOption = options[0]; 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?", flEntry.getType().getName()), Globals.lang("Undefined file type"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, defOption); if (answer == JOptionPane.CANCEL_OPTION) { // User doesn't want to handle this unknown link type. } else if (answer == JOptionPane.YES_OPTION) { // User wants to define the new file type. Show the dialog: ExternalFileType newType = new ExternalFileType(flEntry.getType().getName(), "", "", "", "new"); ExternalFileTypeEntryEditor editor = new ExternalFileTypeEntryEditor(panel.frame(), newType); editor.setVisible(true); if (editor.okPressed()) { // Get the old list of types, add this one, and update the list in prefs: java.util.List<ExternalFileType> fileTypes = new ArrayList<ExternalFileType>(); ExternalFileType[] oldTypes = Globals.prefs.getExternalFileTypeSelection(); for (int k = 0; k < oldTypes.length; k++) { fileTypes.add(oldTypes[k]); } fileTypes.add(newType); Collections.sort(fileTypes); Globals.prefs.setExternalFileTypes(fileTypes); panel.mainTable.repaint(); } } else { // User wants to change the type of this link. // First get a model of all file links for this entry: FileListEntryEditor editor = new FileListEntryEditor (panel.frame(), flEntry, false, true, panel.metaData()); editor.setVisible(true, false); } } } if (!tableModel.getStringRepresentation().equals(old)) { // The table has been modified. Store the change: String toSet = tableModel.getStringRepresentation(); if (toSet.length() == 0) toSet = null; ce.addEdit(new UndoableFieldChange(sel[i], fieldName, old, toSet)); sel[i].setField(fieldName, toSet); changedEntries.add(sel[i]); //System.out.println("Changed to: "+tableModel.getStringRepresentation()); } } } } //for (BibtexEntry entr : changedEntries) // System.out.println(entr.getCiteKey()); if (!changedEntries.isEmpty()) { // Add the undo edit: ce.end(); panel.undoManager.addEdit(ce); panel.markBaseChanged(); } }
Clone fragments detected by clone detection tool
File path: /jabref-2.10/src/java/net/sf/jabref/AutoLinkFilesAction.java File path: /jabref-2.10/src/java/net/sf/jabref/external/SynchronizeFileField.java
Method name: void actionPerformed(ActionEvent) Method name: void run()
Number of AST nodes: 2 Number of AST nodes: 2
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
    }
  1. {Non-refactorable}
    Mapping Summary
    Number of mapped statements2
    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.400
    Clone typeType 2
    Mapped Statements
    ID Statement ID Statement
    2
    for (BibtexEntry e : JabRef.jrf.basePanel().getSelectedEntries())
    2
    for (BibtexEntry e : JabRef.jrf.basePanel().getSelectedEntries())
    15
    for (int i = 0; i < sel.length; i++)
    Differences
    Expression1Expression2Difference
    JabRef.jrf.basePanel().getSelectedEntries()selTYPE_COMPATIBLE_REPLACEMENT
    JabRef.jrf.basePanel().getSelectedEntries()selTYPE_COMPATIBLE_REPLACEMENT
    15
    for (int i = 0; i < sel.length; i++)
    3
    entries.add(e);
    3
    entries.add(e);
    16
    entries.add(sel[i]);
    Differences
    Expression1Expression2Difference
    esel[i]TYPE_COMPATIBLE_REPLACEMENT
    java.util.ArrayListjava.util.CollectionSUBCLASS_TYPE_MISMATCH
    Preondition Violations
    Expression e cannot be parameterized, because it has dependencies to/from statements that will be extracted
    Expression sel[i] cannot be parameterized, because it has dependencies to/from statements that will be extracted
    16
    entries.add(sel[i]);
    Precondition Violations (2)
    Row Violation
    1Expression e cannot be parameterized, because it has dependencies to/from statements that will be extracted
    2Expression sel[i] cannot be parameterized, because it has dependencies to/from statements that will be extracted