Skip to content

Commit

Permalink
Migrate showBottomPane to property
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Dec 2, 2024
1 parent 172b58f commit 21075bf
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 48 deletions.
43 changes: 2 additions & 41 deletions src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.entry.event.EntriesEventSource;
import org.jabref.model.entry.event.FieldChangedEvent;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.model.groups.GroupTreeNode;
import org.jabref.model.search.query.SearchQuery;
Expand Down Expand Up @@ -452,14 +451,6 @@ public void registerUndoableChanges(List<FieldChange> changes) {
}
}

public void editEntryAndFocusField(BibEntry entry, Field field) {
showAndEdit(entry);
Platform.runLater(() -> {
// Focus field and entry in main table (async to give entry editor time to load)
entryEditor.setFocusToField(field);
});
}

private void createMainTable() {
mainTable = new MainTable(tableModel,
this,
Expand Down Expand Up @@ -523,33 +514,10 @@ public SuggestionProvider<Author> getAutoCompleter() {
return searchAutoCompleter;
}

/**
* Sets the entry editor as the bottom component in the split pane. If an entry editor already was shown, makes sure that the divider doesn't move. Updates the mode to {@link PanelMode#MAIN_TABLE_AND_ENTRY_EDITOR}.
* Then shows the given entry.
*
* Additionally, selects the entry in the main table - so that the selected entry in the main table always corresponds to the edited entry.
*
* @param entry The entry to edit.
*/
public void showAndEdit(BibEntry entry) {
this.clearAndSelect(entry);
if (!splitPane.getItems().contains(entryEditor)) {
splitPane.getItems().addLast(entryEditor);
mode = PanelMode.MAIN_TABLE_AND_ENTRY_EDITOR;
splitPane.setDividerPositions(preferences.getEntryEditorPreferences().getDividerPosition());
}

// We use != instead of equals because of performance reasons
if (entry != showing) {
entryEditor.setCurrentlyEditedEntry(entry);
showing = entry;
}
entryEditor.requestFocus();
}

public void closeBottomPane() {
mode = PanelMode.MAIN_TABLE;
splitPane.getItems().remove(entryEditor);
// entryEditor.requestFocus();
}

/**
Expand All @@ -567,14 +535,6 @@ public void selectNextEntry() {
mainTable.getSelectionModel().clearAndSelect(mainTable.getSelectionModel().getSelectedIndex() + 1);
}

/**
* This method is called from an EntryEditor when it should be closed. We relay to the selection listener, which takes care of the rest.
*/
public void entryEditorClosing() {
closeBottomPane();
mainTable.requestFocus();
}

/**
* Put an asterisk behind the filename to indicate the database has changed.
*/
Expand Down Expand Up @@ -827,6 +787,7 @@ public void insertEntries(final List<BibEntry> entries) {
importHandler.importCleanedEntries(entries);
getUndoManager().addEdit(new UndoableInsertEntries(bibDatabaseContext.getDatabase(), entries));
markBaseChanged();
stateManager.setSelectedEntries(entries);
if (preferences.getEntryEditorPreferences().shouldOpenOnNewEntry()) {
showAndEdit(entries.getFirst());
} else {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/jabref/gui/StateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
Expand Down Expand Up @@ -78,6 +80,7 @@ public class StateManager {
private final ObjectProperty<LastAutomaticFieldEditorEdit> lastAutomaticFieldEditorEdit = new SimpleObjectProperty<>();
private final ObservableList<String> searchHistory = FXCollections.observableArrayList();
private final List<AiChatWindow> aiChatWindows = new ArrayList<>();
private final BooleanProperty editorShowing = new SimpleBooleanProperty(false);

public ObservableList<SidePaneType> getVisibleSidePaneComponents() {
return visibleSidePanes;
Expand Down Expand Up @@ -231,4 +234,8 @@ public void clearSearchHistory() {
public List<AiChatWindow> getAiChatWindows() {
return aiChatWindows;
}

public BooleanProperty getEditorShowing() {
return editorShowing;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private void setupKeyBindings() {

@FXML
private void close() {
tabSupplier.get().entryEditorClosing();
stateManager.getEditorShowing().set(false);
}

@FXML
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/org/jabref/gui/frame/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import javafx.beans.InvalidationListener;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.event.Event;
Expand Down Expand Up @@ -50,6 +53,7 @@
import org.jabref.gui.undo.CountingUndoManager;
import org.jabref.gui.undo.RedoAction;
import org.jabref.gui.undo.UndoAction;
import org.jabref.gui.util.BindingsHelper;
import org.jabref.logic.UiCommand;
import org.jabref.logic.ai.AiService;
import org.jabref.logic.journals.JournalAbbreviationRepository;
Expand Down Expand Up @@ -108,8 +112,7 @@ private enum PanelMode { MAIN_TABLE, MAIN_TABLE_AND_ENTRY_EDITOR }
private final SplitPane verticalSplit = new SplitPane();
private final TabPane tabbedPane = new TabPane();
private final EntryEditor entryEditor;

private PanelMode mode = PanelMode.MAIN_TABLE;
private ObjectProperty<PanelMode> panelMode = new SimpleObjectProperty<>(PanelMode.MAIN_TABLE);

// We need to keep a reference to the subscription, otherwise the binding gets garbage collected
private Subscription horizontalDividerSubscription;
Expand Down Expand Up @@ -183,6 +186,7 @@ public JabRefFrame(Stage mainStage,
// Actions are recreated here since this avoids passing more parameters and the amount of additional memory consumption is neglegtable.
new UndoAction(this::getCurrentLibraryTab, undoManager, dialogService, stateManager),
new RedoAction(this::getCurrentLibraryTab, undoManager, dialogService, stateManager));
Injector.setModelOrService(EntryEditor.class, entryEditor);

this.pushToApplicationCommand = new PushToApplicationCommand(
stateManager,
Expand Down Expand Up @@ -274,7 +278,7 @@ private void updateSidePane() {
}

private void updateEditorPane() {
if (mode == PanelMode.MAIN_TABLE) {
if (panelMode.get() == PanelMode.MAIN_TABLE) {
if (verticalDividerSubscription != null) {
verticalDividerSubscription.unsubscribe();
}
Expand All @@ -297,7 +301,7 @@ public void updateHorizontalDividerPosition() {
}

public void updateVerticalDividerPosition() {
if (mainStage.isShowing() && mode == PanelMode.MAIN_TABLE_AND_ENTRY_EDITOR) {
if (mainStage.isShowing() && panelMode.get() == PanelMode.MAIN_TABLE_AND_ENTRY_EDITOR) {
verticalSplit.setDividerPositions(preferences.getEntryEditorPreferences().getDividerPosition() / verticalSplit.getHeight());
// ToDo: Move DividerPosition to GuiPreferences
verticalDividerSubscription = EasyBind.valueAt(verticalSplit.getDividers(), 0)
Expand Down Expand Up @@ -444,6 +448,16 @@ private void initBindings() {
libraryTab.textProperty());
mainStage.titleProperty().bind(windowTitle);
});

BindingsHelper.bindBidirectional((ObservableValue<Boolean>) stateManager.getEditorShowing(), panelMode,
mode -> stateManager.getEditorShowing().setValue(mode == PanelMode.MAIN_TABLE_AND_ENTRY_EDITOR),
showing -> panelMode.setValue(showing ? PanelMode.MAIN_TABLE_AND_ENTRY_EDITOR : PanelMode.MAIN_TABLE));
EasyBind.subscribe(panelMode, mode -> {
updateEditorPane();
if (mode == PanelMode.MAIN_TABLE_AND_ENTRY_EDITOR) {
entryEditor.requestFocus();
}
});
}

/* ************************************************************************
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/org/jabref/gui/integrity/IntegrityCheckDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import java.util.function.Function;

import javafx.application.Platform;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.ListChangeListener;
import javafx.fxml.FXML;
Expand All @@ -14,6 +15,8 @@
import javafx.stage.Modality;

import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.gui.entryeditor.EntryEditor;
import org.jabref.gui.theme.ThemeManager;
import org.jabref.gui.util.BaseDialog;
import org.jabref.gui.util.ValueTableCellFactory;
Expand All @@ -34,7 +37,9 @@ public class IntegrityCheckDialog extends BaseDialog<Void> {
@FXML private MenuButton fieldFilterButton;
@FXML private MenuButton messageFilterButton;

@Inject private EntryEditor entryEditor;
@Inject private ThemeManager themeManager;
@Inject private StateManager stateManager;

private final List<IntegrityMessage> messages;
private final LibraryTab libraryTab;
Expand All @@ -56,8 +61,14 @@ public IntegrityCheckDialog(List<IntegrityMessage> messages, LibraryTab libraryT

private void onSelectionChanged(ListChangeListener.Change<? extends IntegrityMessage> change) {
if (change.next()) {
change.getAddedSubList().stream().findFirst().ifPresent(message ->
libraryTab.editEntryAndFocusField(message.entry(), message.field()));
change.getAddedSubList().stream().findFirst().ifPresent(message -> {
libraryTab.clearAndSelect(message.entry());

stateManager.getEditorShowing().setValue(true);

// Focus field async to give entry editor time to load
Platform.runLater(() -> entryEditor.setFocusToField(message.field()));
});
}
}

Expand Down

0 comments on commit 21075bf

Please sign in to comment.