Skip to content

Commit

Permalink
Close the tab that the cursor is on when right-clicking and selecting…
Browse files Browse the repository at this point in the history
… close (#8193)
  • Loading branch information
HoussemNasri committed Oct 29, 2021
1 parent 2153a05 commit 05d801e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue where typing an invalid UNC path into the "Main file directory" text field caused an error. [#8107](https://github.com/JabRef/jabref/issues/8107)
- We fixed an issue where "Open Folder" didn't select the file on macOS in Finder [#8130](https://github.com/JabRef/jabref/issues/8130)
- We fixed an issue where importing PDFs resulted in an uncaught exception [#8143](https://github.com/JabRef/jabref/issues/8143)
- We fixed an issue where right-clicking on a tab and selecting close will close the focused tab even if it is not the tab we right-clicked [#8193](https://github.com/JabRef/jabref/pull/8193)

### Removed

Expand Down
41 changes: 30 additions & 11 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -1038,16 +1038,16 @@ public void setupAllTables() {
});
}

private ContextMenu createTabContextMenu(KeyBindingRepository keyBindingRepository) {
private ContextMenu createTabContextMenuFor(LibraryTab tab, KeyBindingRepository keyBindingRepository) {
ContextMenu contextMenu = new ContextMenu();
ActionFactory factory = new ActionFactory(keyBindingRepository);

contextMenu.getItems().addAll(
factory.createMenuItem(StandardActions.OPEN_DATABASE_FOLDER, new OpenDatabaseFolder()),
factory.createMenuItem(StandardActions.OPEN_CONSOLE, new OpenConsoleAction(stateManager, prefs)),
factory.createMenuItem(StandardActions.OPEN_DATABASE_FOLDER, new OpenDatabaseFolder(tab.getBibDatabaseContext())),
factory.createMenuItem(StandardActions.OPEN_CONSOLE, new OpenConsoleAction(tab.getBibDatabaseContext(), stateManager, prefs)),
new SeparatorMenuItem(),
factory.createMenuItem(StandardActions.CLOSE_LIBRARY, new CloseDatabaseAction()),
factory.createMenuItem(StandardActions.CLOSE_OTHER_LIBRARIES, new CloseOthersDatabaseAction()),
factory.createMenuItem(StandardActions.CLOSE_LIBRARY, new CloseDatabaseAction(tab)),
factory.createMenuItem(StandardActions.CLOSE_OTHER_LIBRARIES, new CloseOthersDatabaseAction(tab)),
factory.createMenuItem(StandardActions.CLOSE_ALL_LIBRARIES, new CloseAllDatabaseAction())
);

Expand All @@ -1063,7 +1063,7 @@ public void addTab(LibraryTab libraryTab, boolean raisePanel) {
event.consume();
});

libraryTab.setContextMenu(createTabContextMenu(Globals.getKeyPrefs()));
libraryTab.setContextMenu(createTabContextMenuFor(libraryTab, Globals.getKeyPrefs()));

if (raisePanel) {
tabbedPane.getSelectionModel().select(libraryTab);
Expand Down Expand Up @@ -1233,25 +1233,39 @@ public void execute() {
}

private class CloseDatabaseAction extends SimpleCommand {
private final LibraryTab libraryTab;

public CloseDatabaseAction(LibraryTab libraryTab) {
this.libraryTab = libraryTab;
}

/**
* Using this constructor will result in executing the command on the currently open library tab
* */
public CloseDatabaseAction() {
this(null);
}

@Override
public void execute() {
closeTab(getCurrentLibraryTab());
closeTab(Optional.ofNullable(libraryTab).orElse(getCurrentLibraryTab()));
}
}

private class CloseOthersDatabaseAction extends SimpleCommand {
private final LibraryTab libraryTab;

public CloseOthersDatabaseAction() {
public CloseOthersDatabaseAction(LibraryTab libraryTab) {
this.libraryTab = libraryTab;
this.executable.bind(ActionHelper.isOpenMultiDatabase(tabbedPane));
}

@Override
public void execute() {
LibraryTab currentLibraryTab = getCurrentLibraryTab();
LibraryTab toKeepLibraryTab = Optional.of(libraryTab).get();
for (Tab tab : tabbedPane.getTabs()) {
LibraryTab libraryTab = (LibraryTab) tab;
if (libraryTab != currentLibraryTab) {
if (libraryTab != toKeepLibraryTab) {
closeTab(libraryTab);
}
}
Expand All @@ -1269,10 +1283,15 @@ public void execute() {
}

private class OpenDatabaseFolder extends SimpleCommand {
private final BibDatabaseContext databaseContext;

public OpenDatabaseFolder(BibDatabaseContext databaseContext) {
this.databaseContext = databaseContext;
}

@Override
public void execute() {
stateManager.getActiveDatabase().flatMap(BibDatabaseContext::getDatabasePath).ifPresent(path -> {
Optional.of(databaseContext).flatMap(BibDatabaseContext::getDatabasePath).ifPresent(path -> {
try {
JabRefDesktop.openFolderAndSelectFile(path, prefs);
} catch (IOException e) {
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/jabref/gui/OpenConsoleAction.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui;

import java.io.IOException;
import java.util.Optional;

import org.jabref.gui.actions.ActionHelper;
import org.jabref.gui.actions.SimpleCommand;
Expand All @@ -14,19 +15,28 @@
public class OpenConsoleAction extends SimpleCommand {

private static final Logger LOGGER = LoggerFactory.getLogger(OpenConsoleAction.class);
private final BibDatabaseContext databaseContext;
private final StateManager stateManager;
private final PreferencesService preferencesService;

public OpenConsoleAction(StateManager stateManager, PreferencesService preferencesService) {
public OpenConsoleAction(BibDatabaseContext databaseContext, StateManager stateManager, PreferencesService preferencesService) {
this.databaseContext = databaseContext;
this.stateManager = stateManager;
this.preferencesService = preferencesService;

this.executable.bind(ActionHelper.needsDatabase(stateManager));
}

/**
* Using this constructor will result in executing the command on the active database
*/
public OpenConsoleAction(StateManager stateManager, PreferencesService preferencesService) {
this(null, stateManager, preferencesService);
}

@Override
public void execute() {
stateManager.getActiveDatabase().flatMap(BibDatabaseContext::getDatabasePath).ifPresent(path -> {
Optional.ofNullable(databaseContext).or(stateManager::getActiveDatabase).flatMap(BibDatabaseContext::getDatabasePath).ifPresent(path -> {
try {
JabRefDesktop.openConsole(path.toFile(), preferencesService);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UnknownField;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.testutils.category.FetcherTest;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -26,6 +27,7 @@
* Tests the CompositeIdFetcher, for which Fetchers implementing the
* IdBasedFetcher interface are a prerequisite. Excluding TitleFetcher.
*/
@FetcherTest
class CompositeIdFetcherTest {

private CompositeIdFetcher compositeIdFetcher;
Expand Down

0 comments on commit 05d801e

Please sign in to comment.