Skip to content

Commit

Permalink
TODO: Find out why dnd doesn't work on preview panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Siedlerchr committed May 10, 2018
1 parent 0c5eb0c commit 4dac741
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 39 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ public BasePanel(JabRefFrame frame, BasePanelPreferences preferences, BibDatabas

this.entryEditor = new EntryEditor(this, preferences.getEntryEditorPreferences(), Globals.getFileUpdateMonitor(), dialogService);

this.preview = new PreviewPanel(this, getBibDatabaseContext(), preferences.getKeyBindings(), preferences.getPreviewPreferences(), dialogService);
this.preview = new PreviewPanel(this, getBibDatabaseContext(), preferences.getKeyBindings(), preferences.getPreviewPreferences(), dialogService, externalFileTypes);
frame().getGlobalSearchBar().getSearchQueryHighlightObservable().addSearchListener(preview);


}

public static void runWorker(AbstractWorker worker) throws Exception {
Expand Down
112 changes: 82 additions & 30 deletions src/main/java/org/jabref/gui/PreviewPanel.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package org.jabref.gui;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javafx.print.PrinterJob;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.Dragboard;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.web.WebView;

import org.jabref.Globals;
import org.jabref.gui.externalfiles.NewDroppedFileHandler;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.keyboard.KeyBinding;
import org.jabref.gui.keyboard.KeyBindingRepository;
Expand Down Expand Up @@ -63,21 +70,27 @@ public class PreviewPanel extends ScrollPane implements SearchQueryHighlightList
* If a database is set, the preview will attempt to resolve strings in the previewed entry using that database.
*/
private Optional<BibDatabaseContext> databaseContext = Optional.empty();
private WebView previewView;
private final WebView previewView;
private Optional<Future<?>> citationStyleFuture = Optional.empty();

private final NewDroppedFileHandler fileHandler;
private final ExternalFileTypes externalFileTypes;

/**
* @param panel (may be null) Only set this if the preview is associated to the main window.
* @param databaseContext (may be null) Used for resolving pdf directories for links.
* @param preferences
* @param dialogService
*/
public PreviewPanel(BasePanel panel, BibDatabaseContext databaseContext, KeyBindingRepository keyBindingRepository, PreviewPreferences preferences, DialogService dialogService) {
public PreviewPanel(BasePanel panel, BibDatabaseContext databaseContext, KeyBindingRepository keyBindingRepository, PreviewPreferences preferences, DialogService dialogService, ExternalFileTypes externalFileTypes) {
this.databaseContext = Optional.ofNullable(databaseContext);
this.basePanel = Optional.ofNullable(panel);
this.dialogService = dialogService;
this.clipBoardManager = new ClipBoardManager();
this.keyBindingRepository = keyBindingRepository;
this.externalFileTypes = externalFileTypes;

fileHandler = new NewDroppedFileHandler(dialogService, databaseContext, externalFileTypes, Globals.prefs.getFileDirectoryPreferences(), Globals.prefs.getCleanupPreferences(Globals.journalAbbreviationLoader).getFileDirPattern(), Globals.prefs.getImportFormatPreferences());

// Set up scroll pane for preview pane
setFitToHeight(true);
Expand All @@ -91,17 +104,56 @@ public PreviewPanel(BasePanel panel, BibDatabaseContext databaseContext, KeyBind
// Handler for drag content of preview to different window
// only created for main window (not for windows like the search results dialog)
setOnDragDetected(event -> {
Dragboard dragboard = startDragAndDrop(TransferMode.COPY);
ClipboardContent content = new ClipboardContent();
content.putHtml((String) previewView.getEngine().executeScript("window.getSelection().toString()"));
dragboard.setContent(content);
startFullDrag();

event.consume();
}
);
Dragboard dragboard = startDragAndDrop(TransferMode.COPY);
ClipboardContent content = new ClipboardContent();
content.putHtml((String) previewView.getEngine().executeScript("window.getSelection().toString()"));
dragboard.setContent(content);

event.consume();
});
}
this.setOnDragOver(event -> {
System.out.println("drag over");
if (event.getDragboard().hasFiles()) {
event.acceptTransferModes(TransferMode.COPY, TransferMode.MOVE, TransferMode.LINK);
}
});

this.setOnDragDropped(event -> {
BibEntry entry = this.getEntry();
boolean success = false;
if (event.getDragboard().hasContent(DataFormat.FILES)) {
List<Path> files = event.getDragboard().getFiles().stream().map(File::toPath).collect(Collectors.toList());
System.out.println(files);

if (event.getTransferMode() == TransferMode.MOVE) {

System.out.println("Mode MOVE"); //shift on win or no modifier
fileHandler.addNewEntryFromXMPorPDFContent(entry, files);
success = true;
}
if (event.getTransferMode() == TransferMode.LINK) {
System.out.println("LINK"); //alt on win
fileHandler.addToEntryAndMoveToFileDir(entry, files);
success = true;

}
if (event.getTransferMode() == TransferMode.COPY) {
System.out.println("Mode Copy");

}
}

event.setDropCompleted(success);
event.consume();

});

createKeyBindings();
updateLayout(preferences);

}

private void createKeyBindings() {
Expand Down Expand Up @@ -168,10 +220,10 @@ public void updateLayout(PreviewPreferences previewPreferences) {
if (basePanel.isPresent()) {
layout = Optional.empty();
CitationStyle.createCitationStyleFromFile(style)
.ifPresent(citationStyle -> {
basePanel.get().getCitationStyleCache().setCitationStyle(citationStyle);
basePanel.get().output(Localization.lang("Preview style changed to: %0", citationStyle.getTitle()));
});
.ifPresent(citationStyle -> {
basePanel.get().getCitationStyleCache().setCitationStyle(citationStyle);
basePanel.get().output(Localization.lang("Preview style changed to: %0", citationStyle.getTitle()));
});
}
} else {
updatePreviewLayout(previewPreferences.getPreviewStyle(), previewPreferences.getLayoutFormatterPreferences());
Expand All @@ -185,8 +237,8 @@ private void updatePreviewLayout(String layoutFile, LayoutFormatterPreferences l
StringReader sr = new StringReader(layoutFile.replace("__NEWLINE__", "\n"));
try {
layout = Optional.of(
new LayoutHelper(sr, layoutFormatterPreferences)
.getLayoutFromText());
new LayoutHelper(sr, layoutFormatterPreferences)
.getLayoutFromText());
} catch (IOException e) {
layout = Optional.empty();
LOGGER.debug("no layout could be set", e);
Expand Down Expand Up @@ -231,22 +283,22 @@ public void update() {
if (layout.isPresent()) {
StringBuilder sb = new StringBuilder();
bibEntry.ifPresent(entry -> sb.append(layout.get()
.doLayout(entry, databaseContext.map(BibDatabaseContext::getDatabase).orElse(null))));
.doLayout(entry, databaseContext.map(BibDatabaseContext::getDatabase).orElse(null))));
setPreviewLabel(sb.toString());
} else if (basePanel.isPresent() && bibEntry.isPresent()) {
Future<?> citationStyleWorker = BackgroundTask
.wrap(() -> basePanel.get().getCitationStyleCache().getCitationFor(bibEntry.get()))
.onRunning(() -> {
CitationStyle citationStyle = basePanel.get().getCitationStyleCache().getCitationStyle();
setPreviewLabel("<i>" + Localization.lang("Processing %0", Localization.lang("Citation Style")) +
": " + citationStyle.getTitle() + " ..." + "</i>");
})
.onSuccess(this::setPreviewLabel)
.onFailure(exception -> {
LOGGER.error("Error while generating citation style", exception);
setPreviewLabel(Localization.lang("Error while generating citation style"));
})
.executeWith(Globals.TASK_EXECUTOR);
.wrap(() -> basePanel.get().getCitationStyleCache().getCitationFor(bibEntry.get()))
.onRunning(() -> {
CitationStyle citationStyle = basePanel.get().getCitationStyleCache().getCitationStyle();
setPreviewLabel("<i>" + Localization.lang("Processing %0", Localization.lang("Citation Style")) +
": " + citationStyle.getTitle() + " ..." + "</i>");
})
.onSuccess(this::setPreviewLabel)
.onFailure(exception -> {
LOGGER.error("Error while generating citation style", exception);
setPreviewLabel(Localization.lang("Error while generating citation style"));
})
.executeWith(Globals.TASK_EXECUTOR);
this.citationStyleFuture = Optional.of(citationStyleWorker);
}
}
Expand Down Expand Up @@ -287,8 +339,8 @@ public void print() {
job.endJob();
return null;
})
.onFailure(exception -> dialogService.showErrorDialogAndWait(Localization.lang("Could not print preview"), exception))
.executeWith(Globals.TASK_EXECUTOR);
.onFailure(exception -> dialogService.showErrorDialogAndWait(Localization.lang("Could not print preview"), exception))
.executeWith(Globals.TASK_EXECUTOR);
}

public void close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jabref.gui.FXDialogService;
import org.jabref.gui.PreviewPanel;
import org.jabref.gui.customjfx.CustomJFXPanel;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.keyboard.KeyBinding;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -58,7 +59,7 @@ public ResolveDuplicateLabelDialog(BasePanel panel, String key, List<BibEntry> e
JCheckBox cb = new JCheckBox(Localization.lang("Generate BibTeX key"), !first);
b.appendRows("1dlu, p");
b.add(cb).xy(1, row);
PreviewPanel previewPanel = new PreviewPanel(null, null, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), new FXDialogService());
PreviewPanel previewPanel = new PreviewPanel(null, null, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), new FXDialogService(), ExternalFileTypes.getInstance());
previewPanel.setEntry(entry);
JFXPanel container = CustomJFXPanel.wrap(new Scene(previewPanel));
container.setPreferredSize(new Dimension(800, 90));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jabref.gui.FXDialogService;
import org.jabref.gui.PreviewPanel;
import org.jabref.gui.customjfx.CustomJFXPanel;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableInsertEntry;
import org.jabref.logic.l10n.Localization;
Expand All @@ -27,7 +28,7 @@ public EntryAddChangeViewModel(BibEntry diskEntry) {
super(Localization.lang("Added entry"));
this.diskEntry = diskEntry;

PreviewPanel previewPanel = new PreviewPanel(null, null, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), new FXDialogService());
PreviewPanel previewPanel = new PreviewPanel(null, null, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), new FXDialogService(), ExternalFileTypes.getInstance());
previewPanel.setEntry(diskEntry);
container = CustomJFXPanel.wrap(new Scene(previewPanel));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jabref.gui.FXDialogService;
import org.jabref.gui.PreviewPanel;
import org.jabref.gui.customjfx.CustomJFXPanel;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableRemoveEntry;
import org.jabref.logic.bibtex.DuplicateCheck;
Expand Down Expand Up @@ -44,7 +45,7 @@ public EntryDeleteChangeViewModel(BibEntry memEntry, BibEntry tmpEntry) {
LOGGER.debug("Modified entry: " + memEntry.getCiteKeyOptional().orElse("<no BibTeX key set>")
+ "\n Modified locally: " + isModifiedLocally);

PreviewPanel previewPanel = new PreviewPanel(null, null, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), new FXDialogService());
PreviewPanel previewPanel = new PreviewPanel(null, null, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), new FXDialogService(), ExternalFileTypes.getInstance());
previewPanel.setEntry(memEntry);
container = CustomJFXPanel.wrap(new Scene(previewPanel));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public ImportInspectionDialog(JabRefFrame frame, BasePanel panel, String undoNam
this.undoName = undoName;
this.newDatabase = newDatabase;
setIconImages(IconTheme.getLogoSet());
preview = DefaultTaskExecutor.runInJavaFXThread(() -> new PreviewPanel(panel, bibDatabaseContext, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), frame.getDialogService()));
preview = DefaultTaskExecutor.runInJavaFXThread(() -> new PreviewPanel(panel, bibDatabaseContext, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), frame.getDialogService(), ExternalFileTypes.getInstance()));

duplLabel.setToolTipText(Localization.lang("Possible duplicate of existing entry. Click to resolve."));

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/mergeentries/MergeEntries.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.jabref.gui.FXDialogService;
import org.jabref.gui.PreviewPanel;
import org.jabref.gui.customjfx.CustomJFXPanel;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.util.component.DiffHighlightingTextPane;
import org.jabref.logic.bibtex.BibEntryWriter;
import org.jabref.logic.bibtex.LatexFieldFormatter;
Expand Down Expand Up @@ -178,7 +179,7 @@ private void initialize() {
// Setup a PreviewPanel and a Bibtex source box for the merged entry
mainPanel.add(boldFontLabel(Localization.lang("Merged entry")), CELL_CONSTRAINTS.xyw(1, 6, 6));

entryPreview = new PreviewPanel(null, null, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), new FXDialogService());
entryPreview = new PreviewPanel(null, null, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), new FXDialogService(), ExternalFileTypes.getInstance());
entryPreview.setEntry(mergedEntry);
JFXPanel container = CustomJFXPanel.wrap(new Scene(entryPreview));
mainPanel.add(container, CELL_CONSTRAINTS.xyw(1, 8, 6));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private void init() {
// Create a preview panel for previewing styles
// Must be done before creating the table to avoid NPEs
DefaultTaskExecutor.runInJavaFXThread(() -> {
preview = new PreviewPanel(null, null, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), frame.getDialogService());
preview = new PreviewPanel(null, null, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), frame.getDialogService(), ExternalFileTypes.getInstance());
// Use the test entry from the Preview settings tab in Preferences:
preview.setEntry(prevEntry);
});
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/preftabs/PreviewPrefsTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.jabref.gui.BasePanel;
import org.jabref.gui.DialogService;
import org.jabref.gui.PreviewPanel;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.logic.citationstyle.CitationStyle;
import org.jabref.logic.l10n.Localization;
Expand Down Expand Up @@ -125,7 +126,7 @@ private void setupLogic() {
try {
DefaultTaskExecutor.runInJavaFXThread(() -> {

PreviewPanel testPane = new PreviewPanel(null, null, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), dialogService);
PreviewPanel testPane = new PreviewPanel(null, null, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), dialogService, ExternalFileTypes.getInstance());
testPane.setFixedLayout(layout.getText());
testPane.setEntry(TestEntry.getTestEntry());

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/search/SearchResultFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.jabref.gui.customjfx.CustomJFXPanel;
import org.jabref.gui.desktop.JabRefDesktop;
import org.jabref.gui.externalfiletype.ExternalFileMenuItem;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.filelist.FileListEntry;
import org.jabref.gui.filelist.FileListTableModel;
import org.jabref.gui.icon.IconTheme;
Expand Down Expand Up @@ -124,7 +125,7 @@ private void init(String title) {
searchResultFrame.setTitle(title);
searchResultFrame.setIconImages(IconTheme.getLogoSet());

preview = new PreviewPanel(null, null, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), frame.getDialogService());
preview = new PreviewPanel(null, null, Globals.getKeyPrefs(), Globals.prefs.getPreviewPreferences(), frame.getDialogService(), ExternalFileTypes.getInstance());

sortedEntries = new SortedList<>(entries, new EntryComparator(false, true, FieldName.AUTHOR));
model = (DefaultEventTableModel<BibEntry>) GlazedListsSwing.eventTableModelWithThreadProxyList(sortedEntries,
Expand Down

0 comments on commit 4dac741

Please sign in to comment.