Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Convert entry preview panel to JavaFX #3504

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 51 additions & 22 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import javax.swing.undo.CannotUndoException;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;

import org.jabref.Globals;
import org.jabref.JabRefExecutorService;
Expand All @@ -55,6 +57,7 @@
import org.jabref.gui.collab.DatabaseChangeMonitor;
import org.jabref.gui.collab.FileUpdatePanel;
import org.jabref.gui.contentselector.ContentSelectorDialog;
import org.jabref.gui.customjfx.CustomJFXPanel;
import org.jabref.gui.desktop.JabRefDesktop;
import org.jabref.gui.entryeditor.EntryEditor;
import org.jabref.gui.exporter.ExportToClipboardAction;
Expand Down Expand Up @@ -169,6 +172,8 @@ public class BasePanel extends JPanel implements ClipboardOwner {
// Keeps track of the string dialog if it is open.
private final Map<String, Object> actions = new HashMap<>();
private final SidePaneManager sidePaneManager;
private final PreviewPanel preview;
private final JFXPanel previewContainer;

// To contain instantiated entry editors. This is to save time
// As most enums, this must not be null
Expand Down Expand Up @@ -239,6 +244,10 @@ public BasePanel(JabRefFrame frame, BibDatabaseContext bibDatabaseContext) {
}

this.getDatabase().registerListener(new UpdateTimestampListener(Globals.prefs));

this.preview = new PreviewPanel(this, getBibDatabaseContext());
DefaultTaskExecutor.runInJavaFXThread(() -> frame().getGlobalSearchBar().getSearchQueryHighlightObservable().addSearchListener(preview));
this.previewContainer = CustomJFXPanel.wrap(new Scene(preview));
}

public static void runWorker(AbstractWorker worker) throws Exception {
Expand Down Expand Up @@ -687,8 +696,8 @@ public void update() {
frame.setPreviewToggle(enabled);
});

actions.put(Actions.NEXT_PREVIEW_STYLE, (BaseAction) selectionListener::nextPreviewStyle);
actions.put(Actions.PREVIOUS_PREVIEW_STYLE, (BaseAction) selectionListener::previousPreviewStyle);
actions.put(Actions.NEXT_PREVIEW_STYLE, (BaseAction) this::nextPreviewStyle);
actions.put(Actions.PREVIOUS_PREVIEW_STYLE, (BaseAction) this::previousPreviewStyle);

actions.put(Actions.MANAGE_SELECTORS, (BaseAction) () -> {
ContentSelectorDialog csd = new ContentSelectorDialog(frame, frame, BasePanel.this, false, null);
Expand Down Expand Up @@ -1526,14 +1535,39 @@ public void showEntryEditor(EntryEditor editor) {
/**
* Sets the given preview panel as the bottom component in the split panel. Updates the mode to SHOWING_PREVIEW.
*
* @param preview The preview to show.
* @param entry The entry to show in the preview.
*/
public void showPreview(PreviewPanel preview) {
public void showPreview(BibEntry entry) {
preview.setEntry(entry);
mode = BasePanelMode.SHOWING_PREVIEW;
splitPane.setBottomComponent(preview);
splitPane.setBottomComponent(previewContainer);
adjustSplitter();
}

private void showPreview() {
if (!mainTable.getSelected().isEmpty()) {
showPreview(mainTable.getSelected().get(0));
}
}

public void nextPreviewStyle() {
cyclePreview(Globals.prefs.getPreviewPreferences().getPreviewCyclePosition() + 1);
}

public void previousPreviewStyle() {
cyclePreview(Globals.prefs.getPreviewPreferences().getPreviewCyclePosition() - 1);
}

private void cyclePreview(int newPosition) {
PreviewPreferences previewPreferences = Globals.prefs.getPreviewPreferences()
.getBuilder()
.withPreviewCyclePosition(newPosition)
.build();
Globals.prefs.storePreviewPreferences(previewPreferences);

preview.updateLayout(previewPreferences);
}

/**
* Removes the bottom component.
*/
Expand Down Expand Up @@ -1755,14 +1789,6 @@ public void autoGenerateKeysBeforeSaving() {
}
}

/**
* Activates or deactivates the entry preview, depending on the argument. When deactivating, makes sure that any
* visible preview is hidden.
*/
private void setPreviewActive(boolean enabled) {
selectionListener.setPreviewActive(enabled);
}

/**
* Depending on whether a preview or an entry editor is showing, save the current divider location in the correct
* preference setting.
Expand Down Expand Up @@ -1937,6 +1963,14 @@ private void setPreviewActiveBasePanels(boolean enabled) {
}
}

private void setPreviewActive(boolean enabled) {
if (enabled) {
showPreview();
} else {
preview.close();
}
}

public CountingUndoManager getUndoManager() {
return undoManager;
}
Expand Down Expand Up @@ -1967,11 +2001,7 @@ public CitationStyleCache getCitationStyleCache() {
}

public PreviewPanel getPreviewPanel() {
if (selectionListener == null) {
// only occurs if this is called while instantiating this BasePanel
return null;
}
return selectionListener.getPreview();
return preview;
}

public FileAnnotationCache getAnnotationCache() {
Expand Down Expand Up @@ -2061,7 +2091,7 @@ public void listen(EntryRemovedEvent entryRemovedEvent) {

BibEntry previewEntry = selectionListener.getPreview().getEntry();
if ((previewEntry != null) && previewEntry.equals(entryRemovedEvent.getBibEntry())) {
selectionListener.setPreviewActive(false);
preview.close();
}
}
}
Expand Down Expand Up @@ -2208,9 +2238,8 @@ private class PrintPreviewAction implements BaseAction {

@Override
public void action() throws Exception {
selectionListener.setPreviewActive(true);
showPreview(selectionListener.getPreview());
selectionListener.getPreview().getPrintAction().actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null));
showPreview();
preview.print();
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/jabref/gui/DialogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Optional;

import javafx.concurrent.Task;
import javafx.print.PrinterJob;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
Expand Down Expand Up @@ -183,4 +184,13 @@ Optional<ButtonType> showCustomButtonDialogAndWait(Alert.AlertType type, String
*/
FileChooser getConfiguredFileChooser(FileDialogConfiguration fileDialogConfiguration);

/**
* Displays a Print Dialog. Allow the user to update job state such as printer and settings. These changes will be
* available in the appropriate properties after the print dialog has returned. The print dialog is also used to
* confirm the user wants to proceed with printing.
*
* @param job the print job to customize
* @return false if the user opts to cancel printing
*/
boolean showPrintDialog(PrinterJob job);
}
5 changes: 5 additions & 0 deletions src/main/java/org/jabref/gui/FXDialogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.stream.Collectors;

import javafx.concurrent.Task;
import javafx.print.PrinterJob;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
Expand Down Expand Up @@ -193,4 +194,8 @@ public FileChooser getConfiguredFileChooser(FileDialogConfiguration fileDialogCo
return chooser;
}

@Override
public boolean showPrintDialog(PrinterJob job) {
return job.showPrintDialog(null);
}
}
Loading