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 1 commit
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
82 changes: 59 additions & 23 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 @@ -114,6 +117,7 @@
import org.jabref.logic.pdf.FileAnnotationCache;
import org.jabref.logic.search.SearchQuery;
import org.jabref.logic.util.FileExtensions;
import org.jabref.logic.util.OS;
import org.jabref.logic.util.UpdateField;
import org.jabref.logic.util.io.FileFinder;
import org.jabref.logic.util.io.FileFinders;
Expand Down Expand Up @@ -169,6 +173,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 +245,16 @@ 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 = setupPreviewContainer(preview);
}

private JFXPanel setupPreviewContainer(PreviewPanel preview) {
JFXPanel container = OS.LINUX ? new CustomJFXPanel() : new JFXPanel();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the fact this chunk of three lines is duplicated all over the place. Maybe add a new factory class that provides a method to build a JFXPanel but hides the OS aspect.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good remark! Fixed.

DefaultTaskExecutor.runInJavaFXThread(() -> container.setScene(new Scene(preview)));
return container;
}

public static void runWorker(AbstractWorker worker) throws Exception {
Expand Down Expand Up @@ -687,8 +703,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 +1542,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 +1796,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 +1970,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 +2008,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 @@ -2059,8 +2096,8 @@ public void listen(EntryRemovedEvent entryRemovedEvent) {
currentEditor.close();
}

if (selectionListener.getPreview().getEntry().equals(entryRemovedEvent.getBibEntry())) {
selectionListener.setPreviewActive(false);
if (preview.getEntry().equals(entryRemovedEvent.getBibEntry())) {
preview.close();
}
}
}
Expand Down Expand Up @@ -2207,9 +2244,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
Loading