Skip to content

Commit

Permalink
Added a method to create a new link manually JabRef#11017
Browse files Browse the repository at this point in the history
- Overloaded the constructor so that one will take a LinkedFile for editing, another will not take any argument.
  • Loading branch information
Kunal77689 committed Jul 28, 2024
1 parent 782e5ae commit f1c5524
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public boolean delete() {
}

public void edit() {
Optional<LinkedFile> editedFile = dialogService.showCustomDialogAndWait(new LinkedFileDialogController(this.linkedFile, true));
Optional<LinkedFile> editedFile = dialogService.showCustomDialogAndWait(new LinkedFileDialogController(this.linkedFile));
editedFile.ifPresent(file -> {
this.linkedFile.setLink(file.getLink());
this.linkedFile.setDescription(file.getDescription());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public Parent getNode() {

@FXML
private void addNewFile() {
LinkedFileDialogController controller = new LinkedFileDialogController(null, false);
LinkedFileDialogController controller = new LinkedFileDialogController();

controller.showAndWait().ifPresent(result -> {
// Handle adding the new file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void execute() {
databaseContext.getFileDirectories(filePreferences),
filePreferences);

LinkedFileDialogController dialog = new LinkedFileDialogController(linkedFile, true);
LinkedFileDialogController dialog = new LinkedFileDialogController(linkedFile);

dialogService.showCustomDialogAndWait(dialog)
.ifPresent(editedLinkedFile -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;

import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.externalfiletype.ExternalFileType;
Expand All @@ -14,7 +13,6 @@
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.LinkedFile;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.views.ViewLoader;
import jakarta.inject.Inject;

Expand All @@ -31,20 +29,42 @@ public class LinkedFileDialogController extends BaseDialog<LinkedFile> {

private LinkedFilesEditDialogViewModel viewModel;
private final LinkedFile linkedFile;
private final boolean isEditMode;

public LinkedFileDialogController(LinkedFile linkedFile, boolean isEditMode) {
this.linkedFile = linkedFile != null ? linkedFile : new LinkedFile("", "", "");
this.isEditMode = isEditMode;
private static final ButtonType ADD_BUTTON = new ButtonType(Localization.lang("Add"), ButtonType.OK.getButtonData());
private static final ButtonType EDIT_BUTTON = ButtonType.APPLY;

/**
* Constructor for adding a new LinkedFile.
*/
public LinkedFileDialogController() {
this(new LinkedFile("", "", ""));
initializeDialog(Localization.lang("Add file link"), ADD_BUTTON);
}

/**
* Constructor for editing an existing LinkedFile.
*
* @param linkedFile The linked file to be edited.
*/
public LinkedFileDialogController(LinkedFile linkedFile) {
this.linkedFile = linkedFile;
initializeDialog(Localization.lang("Edit file link"), EDIT_BUTTON);
}

/**
* Initializes the dialog with the given title and button type.
*
* @param title The title of the dialog.
* @param primaryButtonType The primary button type for the dialog.
*/
private void initializeDialog(String title, ButtonType primaryButtonType) {
ViewLoader.view(this)
.load()
.setAsContent(this.getDialogPane());

ButtonType primaryButtonType = isEditMode ? ButtonType.APPLY : new ButtonType(Localization.lang("Add"), ButtonType.OK.getButtonData());
this.getDialogPane().getButtonTypes().addAll(primaryButtonType, ButtonType.CANCEL);
this.setTitle(title);
this.setResizable(false);
this.setTitle(isEditMode ? Localization.lang("Edit file link") : Localization.lang("Add file link"));
this.getDialogPane().getButtonTypes().setAll(primaryButtonType, ButtonType.CANCEL);

this.setResultConverter(button -> {
if (button == primaryButtonType) {
Expand All @@ -58,6 +78,7 @@ public LinkedFileDialogController(LinkedFile linkedFile, boolean isEditMode) {
@FXML
private void initialize() {
viewModel = new LinkedFilesEditDialogViewModel(linkedFile, stateManager.getActiveDatabase().get(), dialogService, preferences.getFilePreferences());

fileType.itemsProperty().bindBidirectional(viewModel.externalFileTypeProperty());
new ViewModelListCellFactory<ExternalFileType>()
.withIcon(ExternalFileType::getIcon)
Expand Down

0 comments on commit f1c5524

Please sign in to comment.