Skip to content

Commit

Permalink
Make menu items clickable and copy entry without checks
Browse files Browse the repository at this point in the history
  • Loading branch information
priyanshu16095 committed Jan 22, 2025
1 parent 24966f5 commit a283264
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 39 deletions.
18 changes: 9 additions & 9 deletions src/main/java/org/jabref/gui/actions/ActionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ public MenuItem createMenuItem(Action action, Command command) {
return menuItem;
}

public MenuItem createCustomMenuItem(Action action, Command command, String text) {
MenuItem menuItem = new MenuItem();
configureMenuItem(action, command, menuItem);
menuItem.textProperty().unbind();
menuItem.setText(text);

return menuItem;
}

public CheckMenuItem createCheckMenuItem(Action action, Command command, boolean selected) {
CheckMenuItem checkMenuItem = ActionUtils.createCheckMenuItem(new JabRefAction(action, command, keyBindingRepository));
checkMenuItem.setSelected(selected);
Expand All @@ -123,15 +132,6 @@ public CheckMenuItem createCheckMenuItem(Action action, Command command, Boolean
return checkMenuItem;
}

public CheckMenuItem createCustomCheckMenuItem(Action action, Command command, boolean selected, String text) {
CheckMenuItem checkMenuItem = ActionUtils.createCheckMenuItem(new JabRefAction(action, command, keyBindingRepository));
checkMenuItem.textProperty().unbind();
checkMenuItem.setText(text);
checkMenuItem.setSelected(selected);

return checkMenuItem;
}

public Menu createMenu(Action action) {
Menu menu = ActionUtils.createMenu(new JabRefAction(action, keyBindingRepository));

Expand Down
38 changes: 25 additions & 13 deletions src/main/java/org/jabref/gui/edit/CopyTo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,56 @@
import java.util.List;

import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.ActionHelper;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CopyTo extends SimpleCommand {

private static final Logger LOGGER = LoggerFactory.getLogger(CopyMoreAction.class);

private final DialogService dialogService;
private final StateManager stateManager;
private final CopyToPreferences copyToPreferences;
// For later use
private final List<String> checkedPaths;
private final String path;
private final LibraryTab libraryTab;
private final BibDatabaseContext sourceDatabaseContext;
private final BibDatabaseContext targetDatabaseContext;

public CopyTo(DialogService dialogService,
StateManager stateManager,
CopyToPreferences copyToPreferences,
List<String> checkedPaths,
String path) {
LibraryTab libraryTab,
BibDatabaseContext sourceDatabaseContext,
BibDatabaseContext targetDatabaseContext) {
this.dialogService = dialogService;
this.stateManager = stateManager;
this.copyToPreferences = copyToPreferences;
this.checkedPaths = checkedPaths;
this.path = path;
this.libraryTab = libraryTab;
this.sourceDatabaseContext = sourceDatabaseContext;
this.targetDatabaseContext = targetDatabaseContext;

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

@Override
public void execute() {
List<BibEntry> selectedEntries = stateManager.getSelectedEntries();
List<String> titles = selectedEntries.stream()
.filter(entry -> entry.getTitle().isPresent())
.map(entry -> entry.getTitle().get())
.toList();

boolean includeCrossReferences = askForCrossReferencedEntries();
copyToPreferences.setShouldIncludeCrossReferences(includeCrossReferences);

copyEntryToAnotherLibrary(sourceDatabaseContext, targetDatabaseContext);
}

public void copyEntryToAnotherLibrary(BibDatabaseContext sourceDatabaseContext, BibDatabaseContext targetDatabaseContext) {
List<BibEntry> selectedEntries = stateManager.getSelectedEntries();

targetDatabaseContext.getDatabase().insertEntries(selectedEntries);
}

private boolean askForCrossReferencedEntries() {
Expand Down
38 changes: 21 additions & 17 deletions src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.jabref.gui.maintable;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.swing.undo.UndoManager;

Expand Down Expand Up @@ -71,7 +70,7 @@ public static ContextMenu create(BibEntryTableViewModel entry,
contextMenu.getItems().addAll(
factory.createMenuItem(StandardActions.COPY, new EditAction(StandardActions.COPY, () -> libraryTab, stateManager, undoManager)),
createCopySubMenu(factory, dialogService, stateManager, preferences, clipBoardManager, abbreviationRepository, taskExecutor),
createCopyToMenu(factory, dialogService, stateManager, preferences),
createCopyToMenu(factory, dialogService, stateManager, preferences, libraryTab),
factory.createMenuItem(StandardActions.PASTE, new EditAction(StandardActions.PASTE, () -> libraryTab, stateManager, undoManager)),
factory.createMenuItem(StandardActions.CUT, new EditAction(StandardActions.CUT, () -> libraryTab, stateManager, undoManager)),
factory.createMenuItem(StandardActions.MERGE_ENTRIES, new MergeEntriesAction(dialogService, stateManager, undoManager, preferences)),
Expand Down Expand Up @@ -117,34 +116,39 @@ public static ContextMenu create(BibEntryTableViewModel entry,
private static Menu createCopyToMenu(ActionFactory factory,
DialogService dialogService,
StateManager stateManager,
GuiPreferences preferences
GuiPreferences preferences,
LibraryTab libraryTab
) {
Menu copyToMenu = factory.createMenu(StandardActions.COPY_TO);

ObservableList<BibDatabaseContext> openDatabases = stateManager.getOpenDatabases();
List<String> checkedPaths = new ArrayList<>();

BibDatabaseContext sourceDatabaseContext = libraryTab.getBibDatabaseContext();

Optional<Path> sourcePath = libraryTab.getBibDatabaseContext().getDatabasePath();
String sourceDatabaseName = FileUtil.getUniquePathFragment(stateManager.collectAllDatabasePaths(), sourcePath.get()).get();

if (!openDatabases.isEmpty()) {
openDatabases.forEach(bibDatabaseContext -> {
String path = " ";
Optional<Path> destinationPath = Optional.empty();
String destinationDatabaseName = " ";

if (bibDatabaseContext.getDatabasePath().isPresent()) {
Path databasePath = bibDatabaseContext.getDatabasePath().get();
path = FileUtil.getUniquePathFragment(stateManager.collectAllDatabasePaths(), databasePath).get();
destinationPath = bibDatabaseContext.getDatabasePath();
String uniquePathName = FileUtil.getUniquePathFragment(stateManager.collectAllDatabasePaths(), destinationPath.get()).get();
if (uniquePathName.equals(sourceDatabaseName)) {
return;
}
destinationDatabaseName = uniquePathName;
} else if (bibDatabaseContext.getLocation() == DatabaseLocation.SHARED) {
path = bibDatabaseContext.getDBMSSynchronizer().getDBName() + " [" + Localization.lang("shared") + "]";
}

if (!checkedPaths.contains(path)) {
checkedPaths.add(path);
destinationDatabaseName = bibDatabaseContext.getDBMSSynchronizer().getDBName() + " [" + Localization.lang("shared") + "]";
}

copyToMenu.getItems().addAll(
factory.createCustomCheckMenuItem(
factory.createCustomMenuItem(
StandardActions.COPY_TO,
new CopyTo(dialogService, stateManager, preferences.getCopyToPreferences(), checkedPaths, path),
true,
path
new CopyTo(dialogService, stateManager, preferences.getCopyToPreferences(), libraryTab, sourceDatabaseContext, bibDatabaseContext),
destinationDatabaseName
)
);
});
Expand Down

0 comments on commit a283264

Please sign in to comment.