Skip to content

Commit

Permalink
revert wrongly commited changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Siedlerchr committed Oct 29, 2017
1 parent 4178789 commit 0235f63
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 100 deletions.
168 changes: 86 additions & 82 deletions src/main/java/org/jabref/gui/externalfiles/AutoSetLinks.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand All @@ -26,26 +25,21 @@
import org.jabref.gui.externalfiletype.ExternalFileType;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.externalfiletype.UnknownExternalFileType;
import org.jabref.gui.filelist.FileListEntry;
import org.jabref.gui.filelist.FileListTableModel;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableFieldChange;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.io.FileFinder;
import org.jabref.logic.util.io.FileFinders;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.FieldName;
import org.jabref.model.entry.FileFieldWriter;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.util.FileHelper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AutoSetLinks {

private static final Log LOGGER = LogFactory.getLog(AutoSetLinks.class);

private AutoSetLinks() {
}

Expand All @@ -56,7 +50,7 @@ private AutoSetLinks() {
* @param databaseContext the database for which links are set
*/
public static void autoSetLinks(List<BibEntry> entries, BibDatabaseContext databaseContext) {
autoSetLinks(entries, null, null, databaseContext, null, null);
autoSetLinks(entries, null, null, null, databaseContext, null, null);
}

/**
Expand Down Expand Up @@ -85,7 +79,7 @@ public static void autoSetLinks(List<BibEntry> entries, BibDatabaseContext datab
* @return the thread performing the automatically setting
*/
public static Runnable autoSetLinks(final List<BibEntry> entries, final NamedCompound ce,
final Set<BibEntry> changedEntries,
final Set<BibEntry> changedEntries, final FileListTableModel singleTableModel,
final BibDatabaseContext databaseContext, final ActionListener callback, final JDialog diag) {
final Collection<ExternalFileType> types = ExternalFileTypes.getInstance().getExternalFileTypeSelection();
if (diag != null) {
Expand All @@ -101,87 +95,97 @@ public static Runnable autoSetLinks(final List<BibEntry> entries, final NamedCom
diag.setLocationRelativeTo(diag.getParent());
}

Runnable r = () -> {

// determine directories to search in
final List<Path> dirs = databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());

// determine extensions
final List<String> extensions = types.stream().map(ExternalFileType::getExtension).collect(Collectors.toList());

// Run the search operation:
FileFinder fileFinder = FileFinders.constructFromConfiguration(Globals.prefs.getAutoLinkPreferences());
Map<BibEntry, List<Path>> result = fileFinder.findAssociatedFiles(entries, dirs, extensions);

boolean foundAny = false;
// Iterate over the entries:
for (Entry<BibEntry, List<Path>> entryFilePair : result.entrySet()) {
Optional<String> oldVal = entryFilePair.getKey().getField(FieldName.FILE);

for (Path foundFile : entryFilePair.getValue()) {
boolean existingSameFile = entryFilePair.getKey().getFiles().stream()
.map(file -> file.findIn(dirs))
.anyMatch(file -> {
try {
return file.isPresent() && Files.isSameFile(file.get(), foundFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
});
if (!existingSameFile) {

foundAny = true;
Optional<ExternalFileType> type = FileHelper.getFileExtension(foundFile)
.map(extension -> ExternalFileTypes.getInstance().getExternalFileTypeByExt(extension))
.orElse(Optional.of(new UnknownExternalFileType("")));

String strType = type.isPresent() ? type.get().getName() : "";
LinkedFile linkedFile = new LinkedFile("", foundFile.toString(), strType);

DefaultTaskExecutor.runInJavaFXThread(() -> {
entryFilePair.getKey().addFile(linkedFile);
});

String newVal = FileFieldWriter.getStringRepresentation(linkedFile);
if (ce != null) {
// store undo information
UndoableFieldChange change = new UndoableFieldChange(entryFilePair.getKey(),
FieldName.FILE, oldVal.orElse(null), newVal);
ce.addEdit(change);
Runnable r = new Runnable() {

@Override
public void run() {
// determine directories to search in
final List<Path> dirs = databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());

// determine extensions
final List<String> extensions = types.stream().map(ExternalFileType::getExtension).collect(Collectors.toList());

// Run the search operation:
FileFinder fileFinder = FileFinders.constructFromConfiguration(Globals.prefs.getAutoLinkPreferences());
Map<BibEntry, List<Path>> result = fileFinder.findAssociatedFiles(entries, dirs, extensions);

boolean foundAny = false;
// Iterate over the entries:
for (Entry<BibEntry, List<Path>> entryFilePair : result.entrySet()) {
FileListTableModel tableModel;
Optional<String> oldVal = entryFilePair.getKey().getField(FieldName.FILE);
if (singleTableModel == null) {
tableModel = new FileListTableModel();
oldVal.ifPresent(tableModel::setContent);
} else {
assert entries.size() == 1;
tableModel = singleTableModel;
}
List<Path> files = entryFilePair.getValue();
for (Path file : files) {
file = FileUtil.shortenFileName(file, dirs);
boolean alreadyHas = false;

for (int j = 0; j < tableModel.getRowCount(); j++) {
FileListEntry existingEntry = tableModel.getEntry(j);
if (Paths.get(existingEntry.getLink()).equals(file)) {
alreadyHas = true;
foundAny = true;
break;
}
}

if (changedEntries != null) {
changedEntries.add(entryFilePair.getKey());
if (!alreadyHas) {
foundAny = true;
Optional<ExternalFileType> type = FileHelper.getFileExtension(file)
.map(extension -> ExternalFileTypes.getInstance().getExternalFileTypeByExt(extension))
.orElse(Optional.of(new UnknownExternalFileType("")));
FileListEntry flEntry = new FileListEntry("", file.toString(), type);
tableModel.addEntry(tableModel.getRowCount(), flEntry);

String newVal = tableModel.getStringRepresentation();
if (newVal.isEmpty()) {
newVal = null;
}
if (ce != null) {
// store undo information
UndoableFieldChange change = new UndoableFieldChange(entryFilePair.getKey(),
FieldName.FILE, oldVal.orElse(null), newVal);
ce.addEdit(change);
}
// hack: if table model is given, do NOT modify entry
if (singleTableModel == null) {
entryFilePair.getKey().setField(FieldName.FILE, newVal);
}
if (changedEntries != null) {
changedEntries.add(entryFilePair.getKey());
}
}
break;
}

}

}
final int id = foundAny ? 1 : 0;
SwingUtilities.invokeLater(() -> {

if (diag != null) {
diag.dispose();
}
if (callback != null) {
callback.actionPerformed(new ActionEvent(AutoSetLinks.class, id, ""));
}

});
// handle callbacks and dialog
// FIXME: The ID signals if action was successful :/
final int id = foundAny ? 1 : 0;
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
if (diag != null) {
diag.dispose();
}
if (callback != null) {
callback.actionPerformed(new ActionEvent(this, id, ""));
}
}
});
}
};

SwingUtilities.invokeLater(() -> {
// show dialog which will be hidden when the task is done
if (diag != null) {
diag.setVisible(true);
}
});

return r;
}

Expand All @@ -202,9 +206,9 @@ public static Runnable autoSetLinks(final List<BibEntry> entries, final NamedCom
* parameter can be null, which means that no progress update will be shown.
* @return the runnable able to perform the automatically setting
*/
public static Runnable autoSetLinks(final BibEntry entry,
public static Runnable autoSetLinks(final BibEntry entry, final FileListTableModel singleTableModel,
final BibDatabaseContext databaseContext, final ActionListener callback, final JDialog diag) {
return autoSetLinks(Collections.singletonList(entry), null, null, databaseContext, callback,
return autoSetLinks(Collections.singletonList(entry), null, null, singleTableModel, databaseContext, callback,
diag);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void run() {
List<BibEntry> entries = new ArrayList<>(sel);

// Start the automatically setting process:
Runnable r = AutoSetLinks.autoSetLinks(entries, ce, changedEntries, panel.getBibDatabaseContext(), null, null);
Runnable r = AutoSetLinks.autoSetLinks(entries, ce, changedEntries, null, panel.getBibDatabaseContext(), null, null);
JabRefExecutorService.INSTANCE.executeAndWait(r);
}
progress += sel.size() * weightAutoSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -166,25 +165,14 @@ private List<LinkedFileViewModel> findAssociatedNotLinkedFiles(BibEntry entry) {
List<Path> newFiles = fileFinder.findAssociatedFiles(entry, dirs, extensions);

List<LinkedFileViewModel> result = new ArrayList<>();
for (Path foundFile : newFiles) {

boolean existingSameFile = files.get().stream()
for (Path newFile : newFiles) {
boolean alreadyLinked = files.get().stream()
.map(file -> file.findIn(dirs))
.anyMatch(file -> {
try {
return file.isPresent() && Files.isSameFile(file.get(), foundFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
});

if (!existingSameFile) {
LinkedFileViewModel newLinkedFile = new LinkedFileViewModel(fromFile(foundFile, dirs), entry, databaseContext);
.anyMatch(file -> file.isPresent() && file.get().equals(newFile));
if (!alreadyLinked) {
LinkedFileViewModel newLinkedFile = new LinkedFileViewModel(fromFile(newFile, dirs), entry, databaseContext);
newLinkedFile.markAsAutomaticallyFound();
result.add(newLinkedFile);
break; //only add first file, if it exists multiple times
}
}
return result;
Expand Down

0 comments on commit 0235f63

Please sign in to comment.