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

Close the tab that the cursor is on when right-clicking and selecting close #8193

Merged
merged 9 commits into from
Oct 29, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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 @@ -1004,16 +1004,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 @@ -1029,7 +1029,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 @@ -1199,25 +1199,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 @@ -1235,10 +1249,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