public void actionPerformed(ActionEvent e) { BasePanel bp = frame.basePanel(); if (bp == null) return; if (bp.getSelectedEntries().length == 0) { // no entries selected, silently ignore action return; } // Lazy creation of the dialog: if (diag == null) { createDialog(); } cancelled = true; fillKeyWordList(); diag.pack(); Util.placeDialog(diag, frame); diag.setVisible(true); if (cancelled) return; HashSet<String> keywordsToAdd = new HashSet<String>(); HashSet<String> userSelectedKeywords = new HashSet<String>(); // build keywordsToAdd and userSelectedKeywords in parallel for (Enumeration keywords = keywordListModel.elements(); keywords.hasMoreElements(); ) { String keyword = (String)keywords.nextElement(); userSelectedKeywords.add(keyword); if (!sortedKeywordsOfAllEntriesBeforeUpdateByUser.contains(keyword)) { keywordsToAdd.add(keyword); } } HashSet<String> keywordsToRemove = new HashSet<String>(); for (String keyword: sortedKeywordsOfAllEntriesBeforeUpdateByUser) { if (!userSelectedKeywords.contains(keyword)) { keywordsToRemove.add(keyword); } } if (keywordsToAdd.isEmpty() && keywordsToRemove.isEmpty()) { // nothing to be done if nothing is new and nothing is obsolete return; } if (SpecialFieldsUtils.keywordSyncEnabled()) { if (!keywordsToAdd.isEmpty()) { // we need to check whether a special field is added // for each field: // check if something is added // if yes, add all keywords of that special fields to the keywords to be removed HashSet<String> clone; // Priority clone = (HashSet<String>) keywordsToAdd.clone(); clone.retainAll(Priority.getInstance().getKeyWords()); if (!clone.isEmpty()) { keywordsToRemove.addAll(Priority.getInstance().getKeyWords()); } // Quality clone = (HashSet<String>) keywordsToAdd.clone(); clone.retainAll(Quality.getInstance().getKeyWords()); if (!clone.isEmpty()) { keywordsToRemove.addAll(Quality.getInstance().getKeyWords()); } // Rank clone = (HashSet<String>) keywordsToAdd.clone(); clone.retainAll(Rank.getInstance().getKeyWords()); if (!clone.isEmpty()) { keywordsToRemove.addAll(Rank.getInstance().getKeyWords()); } // Relevance clone = (HashSet<String>) keywordsToAdd.clone(); clone.retainAll(Relevance.getInstance().getKeyWords()); if (!clone.isEmpty()) { keywordsToRemove.addAll(Relevance.getInstance().getKeyWords()); } } } BibtexEntry[] entries = bp.getSelectedEntries(); NamedCompound ce = new NamedCompound(Globals.lang("Update keywords")); for (BibtexEntry entry: entries) { ArrayList<String> separatedKeywords = Util.getSeparatedKeywords(entry); // we "intercept" with a treeset // pro: no duplicates // possible con: alphabetical sorting of the keywords TreeSet<String> keywords = new TreeSet<String>(); keywords.addAll(separatedKeywords); // update keywords keywords.removeAll(keywordsToRemove); keywords.addAll(keywordsToAdd); // put keywords back separatedKeywords.clear(); separatedKeywords.addAll(keywords); Util.putKeywords(entry, separatedKeywords, ce); if (SpecialFieldsUtils.keywordSyncEnabled()) { SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry, ce); } } ce.end(); bp.undoManager.addEdit(ce); bp.markBaseChanged(); }
/** * Take a list of external links and copy the referred files to a given directory. * This method should be run off the Event Dispatch Thread. A progress bar, if given, * will be updated on the EDT. * * @param files The list of file links. * @param toDir The directory to copy the files to. * @param metaData The MetaData for the database containing the external links. This is needed * because the database might have its own file directory. * @param prog A JProgressBar which will be updated to show the progress of the process. * This argument can be null if no progress bar is needed. * @param deleteOriginalFiles if true, the files in their original locations will be deleted * after copying, for each file whose source directory is different from the destination * directory differs. * @param callback An ActionListener which should be notified when the process is finished. * This parameter can be null if no callback is needed. */ public static void copyExternalLinksToDirectory(final List<FileListEntry> files, File toDir, MetaData metaData, final JProgressBar prog, boolean deleteOriginalFiles, final ActionListener callback) { if (prog != null) SwingUtilities.invokeLater(new Runnable() { public void run() { prog.setMaximum(files.size()); prog.setValue(0); prog.setIndeterminate(false); } }); Set<String> fileNames = new HashSet<String>(); int i=0; for (Iterator<FileListEntry> iterator = files.iterator(); iterator.hasNext();) { FileListEntry entry = iterator.next(); File file = new File(entry.getLink()); // We try to check the extension for the file: String name = file.getName(); int pos = name.lastIndexOf('.'); String extension = ((pos >= 0) && (pos < name.length() - 1)) ? name.substring(pos + 1) .trim().toLowerCase() : null; // Find the default directory for this field type, if any: String[] dir = metaData.getFileDirectory(extension); // Include the standard "file" directory: String[] fileDir = metaData.getFileDirectory(GUIGlobals.FILE_FIELD); // Include the directory of the bib file: ArrayList<String> al = new ArrayList<String>(); for (int i2 = 0; i2 < dir.length; i2++) if (!al.contains(dir[i2])) al.add(dir[i2]); for (int i2 = 0; i2 < fileDir.length; i2++) if (!al.contains(fileDir[i2])) al.add(fileDir[i2]); String[] dirs = al.toArray(new String[al.size()]); File tmp = Util.expandFilename(entry.getLink(), dirs); if (tmp != null) file = tmp; // Check if we have arrived at an existing file: if (file.exists()) { if (fileNames.contains(name)) { // Oops, a file of that name already exists.... } else { fileNames.add(name); File destination = new File(toDir, name); // Check if the source and destination locations differ: if (!destination.equals(file)) { try { // Copy the file: Util.copyFile(file, destination, false); // Delete the original file if requested: if (deleteOriginalFiles) file.delete(); } catch (IOException ex) { ex.printStackTrace(); } } else { // Destination and source is the same. Do nothing. } // Update progress bar: i++; final int j = i; if (prog != null) SwingUtilities.invokeLater(new Runnable() { public void run() { prog.setValue(j); } }); } } else { // The link could not be resolved to an existing file. } } if (callback != null) { callback.actionPerformed(null); } }
Clone fragments detected by clone detection tool
File path: /jabref-2.10/src/java/net/sf/jabref/util/ManageKeywordsAction.java File path: /jabref-2.10/src/java/net/sf/jabref/external/AccessLinksForEntries.java
Method name: void actionPerformed(ActionEvent) Method name: void copyExternalLinksToDirectory(List, File, MetaData, JProgressBar, boolean, ActionListener)
Number of AST nodes: 3 Number of AST nodes: 3
1
public void actionPerformed(ActionEvent e) {
1
/**
2
        BasePanel bp = frame.basePanel();
2
     * Take a list of external links and copy the referred files to a given directory.
3
        if (bp == null)
3
     * This method should be run off the Event Dispatch Thread. A progress bar, if given,
4
            return;
4
     * will be updated on the EDT.
5
        if (bp.getSelectedEntries().length == 0) {
5
     *
6
        	// no entries selected, silently ignore action
6
     * @param files The list of file links.
7
        	return;
7
     * @param toDir The directory to copy the files to.
8
        }
8
     * @param metaData The MetaData for the database containing the external links. This is needed
9
9
     *  because the database might have its own file directory.
10
        // Lazy creation of the dialog:
10
     * @param prog A JProgressBar which will be updated to show the progress of the process.
11
        if (diag == null) {
11
     *  This argument can be null if no progress bar is needed.
12
            createDialog();
12
     * @param deleteOriginalFiles if true, the files in their original locations will be deleted
13
        }
13
     *  after copying, for each file whose source directory is different from the destination
14
14
     *  directory differs.
15
        cancelled = true;
15
     * @param callback An ActionListener which should be notified when the process is finished.
16
16
     *  This parameter can be null if no callback is needed.
17
        fillKeyWordList();
17
     */
18
18
    public static void copyExternalLinksToDirectory(final List<FileListEntry> files, File toDir,
19
        diag.pack();
19
                                                    MetaData metaData, final JProgressBar prog,
20
        Util.placeDialog(diag, frame);
20
                                                    boolean deleteOriginalFiles,
21
        diag.setVisible(true);
21
                                                    final ActionListener callback) {
22
        if (cancelled)
22
23
            return;
23
        if (prog != null) SwingUtilities.invokeLater(new Runnable() {
24
        
24
            public void run() {
25
        HashSet<String> keywordsToAdd = new HashSet<String>();
25
                prog.setMaximum(files.size());
26
        HashSet<String> userSelectedKeywords = new HashSet<String>();
26
                prog.setValue(0);
27
        // build keywordsToAdd and userSelectedKeywords in parallel
27
                prog.setIndeterminate(false);
28
        for (Enumeration keywords = keywordListModel.elements(); keywords.hasMoreElements(); ) {
28
            }
29
        	String keyword = (String)keywords.nextElement();
29
        });
30
        	userSelectedKeywords.add(keyword);
30
31
        	if (!sortedKeywordsOfAllEntriesBeforeUpdateByUser.contains(keyword)) {
31
        Set<String> fileNames = new HashSet<String>();
32
        		keywordsToAdd.add(keyword);
32
33
        	}
33
        int i=0;
34
        }
34
35
        
35
        for (Iterator<FileListEntry> iterator = files.iterator(); iterator.hasNext();) {
36
        HashSet<String> keywordsToRemove = new HashSet<String>();
36
            FileListEntry entry = iterator.next();
37
        for (String keyword: sortedKeywordsOfAllEntriesBeforeUpdateByUser) {
37
            File file = new File(entry.getLink());
38
        	if (!userSelectedKeywords.contains(keyword)) {
38
39
        		keywordsToRemove.add(keyword);
39
            // We try to check the extension for the file:
40
        	}
40
            String name = file.getName();
41
        }
41
            int pos = name.lastIndexOf('.');
42
        
42
            String extension = ((pos >= 0) && (pos < name.length() - 1)) ? name.substring(pos + 1)
43
        if (keywordsToAdd.isEmpty() && keywordsToRemove.isEmpty()) {
43
                .trim().toLowerCase() : null;
44
        	// nothing to be done if nothing is new and nothing is obsolete
44
45
        	return;
45
            // Find the default directory for this field type, if any:
46
        }
46
            String[] dir = metaData.getFileDirectory(extension);
47
        
47
            // Include the standard "file" directory:
48
    	if (SpecialFieldsUtils.keywordSyncEnabled()) {
48
            String[] fileDir = metaData.getFileDirectory(GUIGlobals.FILE_FIELD);
49
	        if (!keywordsToAdd.isEmpty()) {
49
            // Include the directory of the bib file:
50
	        	// we need to check whether a special field is added
50
            ArrayList<String> al = new ArrayList<String>();
51
	        	// for each field:
51
            for (int i2 = 0; i2 < dir.length; i2++)
52
	        	//   check if something is added
52
                if (!al.contains(dir[i2])) al.add(dir[i2]);
53
	        	//   if yes, add all keywords of that special fields to the keywords to be removed
53
            for (int i2 = 0; i2 < fileDir.length; i2++)
54
	        	
54
                if (!al.contains(fileDir[i2])) al.add(fileDir[i2]);
55
	        	HashSet<String> clone;
55
56
	        	
56
            String[] dirs = al.toArray(new String[al.size()]);
57
	        	// Priority
57
            File tmp = Util.expandFilename(entry.getLink(), dirs);
58
	        	clone = (HashSet<String>) keywordsToAdd.clone();
58
            if (tmp != null)
59
	        	clone.retainAll(Priority.getInstance().getKeyWords());
59
                file = tmp;
60
	        	if (!clone.isEmpty()) {
60
61
	        		keywordsToRemove.addAll(Priority.getInstance().getKeyWords());
61
            // Check if we have arrived at an existing file:
62
	        	}
62
            if (file.exists()) {
63
	        	
63
                if (fileNames.contains(name)) {
64
	        	// Quality
64
                    // Oops, a file of that name already exists....
65
	        	clone = (HashSet<String>) keywordsToAdd.clone();
65
                }
66
	        	clone.retainAll(Quality.getInstance().getKeyWords());
66
                else {
67
	        	if (!clone.isEmpty()) {
67
                    fileNames.add(name);
68
	        		keywordsToRemove.addAll(Quality.getInstance().getKeyWords());
68
                    File destination = new File(toDir, name);
69
	        	}
69
70
	        	
70
                    // Check if the source and destination locations differ:
71
	        	// Rank
71
                    if (!destination.equals(file)) {
72
	        	clone = (HashSet<String>) keywordsToAdd.clone();
72
                        try {
73
	        	clone.retainAll(Rank.getInstance().getKeyWords());
73
                            // Copy the file:
74
	        	if (!clone.isEmpty()) {
74
                            Util.copyFile(file, destination, false);
75
	        		keywordsToRemove.addAll(Rank.getInstance().getKeyWords());
75
                            // Delete the original file if requested:
76
	        	}
76
                            if (deleteOriginalFiles)
77
	        	
77
                                file.delete();
78
	        	// Relevance
78
                            
79
	        	clone = (HashSet<String>) keywordsToAdd.clone();
79
                        } catch (IOException ex) {
80
	        	clone.retainAll(Relevance.getInstance().getKeyWords());
80
                            ex.printStackTrace();
81
	        	if (!clone.isEmpty()) {
81
                        }
82
	        		keywordsToRemove.addAll(Relevance.getInstance().getKeyWords());
82
                    }
83
	        	}
83
                    else {
84
	        }
84
                        // Destination and source is the same. Do nothing.
85
        }
85
                    }
86
86
                    // Update progress bar:
87
        BibtexEntry[] entries = bp.getSelectedEntries();
87
                    i++;
88
        NamedCompound ce = new NamedCompound(Globals.lang("Update keywords"));
88
                    final int j = i;
89
        for (BibtexEntry entry: entries) {
89
90
            ArrayList<String> separatedKeywords = Util.getSeparatedKeywords(entry);
90
                    if (prog != null) SwingUtilities.invokeLater(new Runnable() {
91
            
91
                        public void run() {
92
            // we "intercept" with a treeset
92
                            prog.setValue(j);
93
            // pro: no duplicates
93
                        }
94
            // possible con: alphabetical sorting of the keywords
94
                    });
95
            TreeSet<String> keywords = new TreeSet<String>();
95
                }
96
            keywords.addAll(separatedKeywords);
96
            }
97
            
97
            else {
98
            // update keywords
98
                // The link could not be resolved to an existing file.
99
            keywords.removeAll(keywordsToRemove);
99
                
100
            keywords.addAll(keywordsToAdd);
100
            }
101
            
101
        }
102
            // put keywords back
102
103
            separatedKeywords.clear();
103
        if (callback != null) {
104
            separatedKeywords.addAll(keywords);
104
            callback.actionPerformed(null);
105
            Util.putKeywords(entry, separatedKeywords, ce);
105
        }
106
            
106
    }
107
        	if (SpecialFieldsUtils.keywordSyncEnabled()) {
108
        		SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry, ce);
109
        	}
110
        }
111
        ce.end();
112
        bp.undoManager.addEdit(ce);
113
        bp.markBaseChanged();
114
    }
  1. {Non-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.600
    Clone typeType 2
    Mapped Statements
    ID Statement ID Statement
    23
    for (String keyword : sortedKeywordsOfAllEntriesBeforeUpdateByUser)
    23
    for (String keyword : sortedKeywordsOfAllEntriesBeforeUpdateByUser)
    17
    for (int i2 = 0; i2 < fileDir.length; i2++)
    Differences
    Expression1Expression2Difference
    java.util.TreeSetjava.lang.String[]VARIABLE_TYPE_MISMATCH
    java.util.TreeSetjava.lang.String[]VARIABLE_TYPE_MISMATCH
    java.util.TreeSetjava.lang.String[]VARIABLE_TYPE_MISMATCH
    Preondition Violations
    Type java.util.TreeSet<java.lang.String> of variable sortedKeywordsOfAllEntriesBeforeUpdateByUser does not match with type java.lang.String[] of variable fileDir
    • Make classes java.util.TreeSet and java.lang.String[] extend a common superclass
    Type java.util.TreeSet<java.lang.String> of variable sortedKeywordsOfAllEntriesBeforeUpdateByUser does not match with type java.lang.String[] of variable fileDir
    • Make classes java.util.TreeSet and java.lang.String[] extend a common superclass
    Type java.util.TreeSet<java.lang.String> of variable sortedKeywordsOfAllEntriesBeforeUpdateByUser does not match with type java.lang.String[] of variable fileDir
    • Make classes java.util.TreeSet and java.lang.String[] extend a common superclass
    17
    for (int i2 = 0; i2 < fileDir.length; i2++)
    24
    if (!userSelectedKeywords.contains(keyword))
    24
    if (!userSelectedKeywords.contains(keyword))
    18
    if (!al.contains(fileDir[i2]))
    Differences
    Expression1Expression2Difference
    keywordfileDir[i2]TYPE_COMPATIBLE_REPLACEMENT
    userSelectedKeywordsalVARIABLE_NAME_MISMATCH
    java.util.HashSetjava.util.ArrayListSUBCLASS_TYPE_MISMATCH
    Preondition Violations
    Expression keyword cannot be parameterized, because it has dependencies to/from statements that will be extracted
    Expression fileDir[i2] cannot be parameterized, because it has dependencies to/from statements that will be extracted
    18
    if (!al.contains(fileDir[i2]))
    25
    keywordsToRemove.add(keyword);
    25
    keywordsToRemove.add(keyword);
    19
    al.add(fileDir[i2]);
    Differences
    Expression1Expression2Difference
    keywordfileDir[i2]TYPE_COMPATIBLE_REPLACEMENT
    keywordsToRemovealVARIABLE_NAME_MISMATCH
    java.util.HashSetjava.util.ArrayListSUBCLASS_TYPE_MISMATCH
    Preondition Violations
    Expression keyword cannot be parameterized, because it has dependencies to/from statements that will be extracted
    Expression fileDir[i2] cannot be parameterized, because it has dependencies to/from statements that will be extracted
    19
    al.add(fileDir[i2]);
    Precondition Violations (7)
    Row Violation
    1Type java.util.TreeSet<java.lang.String> of variable sortedKeywordsOfAllEntriesBeforeUpdateByUser does not match with type java.lang.String[] of variable fileDir
    2Type java.util.TreeSet<java.lang.String> of variable sortedKeywordsOfAllEntriesBeforeUpdateByUser does not match with type java.lang.String[] of variable fileDir
    3Type java.util.TreeSet<java.lang.String> of variable sortedKeywordsOfAllEntriesBeforeUpdateByUser does not match with type java.lang.String[] of variable fileDir
    4Expression keyword cannot be parameterized, because it has dependencies to/from statements that will be extracted
    5Expression fileDir[i2] cannot be parameterized, because it has dependencies to/from statements that will be extracted
    6Expression keyword cannot be parameterized, because it has dependencies to/from statements that will be extracted
    7Expression fileDir[i2] cannot be parameterized, because it has dependencies to/from statements that will be extracted