Skip to content

Commit

Permalink
Convert "Protected terms" dialog to JavaFX (#4715)
Browse files Browse the repository at this point in the history
* Convert "Protected terms" dialog to JavaFX

* Improve code according to feedback
  • Loading branch information
tobiasdiez authored Mar 4, 2019
1 parent e189016 commit 1a198f7
Show file tree
Hide file tree
Showing 15 changed files with 335 additions and 642 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/jabref/gui/DefaultInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.journals.JournalAbbreviationLoader;
import org.jabref.logic.protectedterms.ProtectedTermsLoader;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.PreferencesService;

Expand Down Expand Up @@ -38,6 +39,8 @@ private static Object createDependency(Class<?> clazz) {
return Globals.stateManager;
} else if (clazz == FileUpdateMonitor.class) {
return Globals.getFileUpdateMonitor();
} else if (clazz == ProtectedTermsLoader.class) {
return Globals.protectedTermsLoader;
} else if (clazz == ClipBoardManager.class) {
return Globals.clipboardManager;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
import org.jabref.gui.actions.ManageCustomImportsAction;
import org.jabref.gui.actions.ManageJournalsAction;
import org.jabref.gui.actions.ManageKeywordsAction;
import org.jabref.gui.actions.ManageProtectedTermsAction;
import org.jabref.gui.actions.NewDatabaseAction;
import org.jabref.gui.actions.NewEntryAction;
import org.jabref.gui.actions.NewEntryFromPlainTextAction;
Expand Down Expand Up @@ -100,6 +99,7 @@
import org.jabref.gui.mergeentries.MergeEntriesAction;
import org.jabref.gui.metadata.BibtexStringEditorAction;
import org.jabref.gui.metadata.PreambleEditor;
import org.jabref.gui.protectedterms.ManageProtectedTermsAction;
import org.jabref.gui.push.PushToApplicationButton;
import org.jabref.gui.push.PushToApplications;
import org.jabref.gui.search.GlobalSearchBar;
Expand Down Expand Up @@ -936,7 +936,7 @@ private MenuBar createMenu() {
factory.createMenuItem(StandardActions.MANAGE_EXTERNAL_FILETYPES, new EditExternalFileTypesAction()),
factory.createMenuItem(StandardActions.MANAGE_JOURNALS, new ManageJournalsAction()),
factory.createMenuItem(StandardActions.CUSTOMIZE_KEYBINDING, new CustomizeKeyBindingAction()),
factory.createMenuItem(StandardActions.MANAGE_PROTECTED_TERMS, new ManageProtectedTermsAction(this, Globals.protectedTermsLoader)),
factory.createMenuItem(StandardActions.MANAGE_PROTECTED_TERMS, new ManageProtectedTermsAction()),

new SeparatorMenuItem(),

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.SwingUtilities;

import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.TextInputControl;

import org.jabref.Globals;
import org.jabref.JabRefGUI;
import org.jabref.gui.protectedterms.NewProtectedTermsFileDialog;
import org.jabref.logic.formatter.casechanger.ProtectTermsFormatter;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.protectedterms.ProtectedTermsList;
Expand Down Expand Up @@ -65,21 +61,5 @@ private void updateFiles() {
externalFiles.getItems().add(fileItem);
}
externalFiles.getItems().add(new SeparatorMenuItem());
MenuItem addToNewFileItem = new MenuItem(Localization.lang("New") + "...");
addToNewFileItem.setOnAction(event -> {
NewProtectedTermsFileDialog dialog = new NewProtectedTermsFileDialog(JabRefGUI.getMainFrame().getDialogService(),
loader);

SwingUtilities.invokeLater(() -> {
dialog.setVisible(true);

if (dialog.isOKPressed()) {
// Update preferences with new list
Globals.prefs.setProtectedTermsPreferences(loader);
this.updateFiles();
}
});
});
externalFiles.getItems().add(addToNewFileItem);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.jabref.gui.protectedterms;

import org.jabref.gui.actions.SimpleCommand;

public class ManageProtectedTermsAction extends SimpleCommand {

@Override
public void execute() {
ManageProtectedTermsDialog protectTermsDialog = new ManageProtectedTermsDialog();
protectTermsDialog.showAndWait();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonType?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<DialogPane xmlns:fx="http://javafx.com/fxml"
xmlns="http://javafx.com/javafx"
fx:controller="org.jabref.gui.protectedterms.ManageProtectedTermsDialog"
prefHeight="400.0" prefWidth="600.0">
<content>
<VBox spacing="10">
<TableView fx:id="filesTable" editable="true" VBox.vgrow="ALWAYS">
<columns>
<TableColumn fx:id="filesTableEnabledColumn" text="%Enabled" minWidth="80" maxWidth="80"/>
<TableColumn fx:id="filesTableDescriptionColumn" text="%Description"/>
<TableColumn fx:id="filesTableFileColumn" text="%File"/>
<TableColumn fx:id="filesTableEditColumn" maxWidth="40" minWidth="40.0"/>
<TableColumn fx:id="filesTableDeleteColumn" maxWidth="40" minWidth="40.0"/>
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
</columnResizePolicy>
</TableView>
<HBox>
<Button text="%Add protected terms file" onAction="#addFile"/>
<Button text="%New protected terms file" onAction="#createNewFile"/>
</HBox>
</VBox>
</content>
<ButtonType fx:constant="CANCEL"/>
<ButtonType fx:constant="APPLY"/>
</DialogPane>
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package org.jabref.gui.protectedterms;

import javax.inject.Inject;

import javafx.fxml.FXML;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;

import org.jabref.gui.DialogService;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.util.BaseDialog;
import org.jabref.gui.util.BindingsHelper;
import org.jabref.gui.util.ValueTableCellFactory;
import org.jabref.gui.util.ViewModelTableRowFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.protectedterms.ProtectedTermsList;
import org.jabref.logic.protectedterms.ProtectedTermsLoader;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.views.ViewLoader;

/**
* Dialog for managing term list files.
*/
public class ManageProtectedTermsDialog extends BaseDialog<Void> {

@FXML private TableView<ProtectedTermsList> filesTable;
@FXML private TableColumn<ProtectedTermsList, Boolean> filesTableEnabledColumn;
@FXML private TableColumn<ProtectedTermsList, String> filesTableDescriptionColumn;
@FXML private TableColumn<ProtectedTermsList, String> filesTableFileColumn;
@FXML private TableColumn<ProtectedTermsList, Boolean> filesTableEditColumn;
@FXML private TableColumn<ProtectedTermsList, Boolean> filesTableDeleteColumn;

@Inject private ProtectedTermsLoader termsLoader;
@Inject private DialogService dialogService;
@Inject private PreferencesService preferences;
private ManageProtectedTermsViewModel viewModel;

public ManageProtectedTermsDialog() {
this.setTitle(Localization.lang("Manage protected terms files"));

ViewLoader.view(this)
.load()
.setAsDialogPane(this);

setResultConverter(button -> {
if (button == ButtonType.APPLY) {
viewModel.save();
}
return null;
});
}

@FXML
public void initialize() {
viewModel = new ManageProtectedTermsViewModel(termsLoader, dialogService, preferences);

filesTable.setItems(viewModel.getTermsFiles());
new ViewModelTableRowFactory<ProtectedTermsList>()
.withContextMenu(this::createContextMenu)
.install(filesTable);
filesTableEnabledColumn.setCellValueFactory(data -> BindingsHelper.constantOf(data.getValue().isEnabled()));
filesTableEnabledColumn.setCellFactory(CheckBoxTableCell.forTableColumn(filesTableEnabledColumn));
filesTableDescriptionColumn.setCellValueFactory(data -> BindingsHelper.constantOf(data.getValue().getDescription()));
filesTableFileColumn.setCellValueFactory(data -> {
ProtectedTermsList list = data.getValue();
if (list.isInternalList()) {
return BindingsHelper.constantOf(Localization.lang("Internal list"));
} else {
return BindingsHelper.constantOf(data.getValue().getLocation());
}
});
filesTableEditColumn.setCellValueFactory(data -> BindingsHelper.constantOf(true));
filesTableDeleteColumn.setCellValueFactory(data -> BindingsHelper.constantOf(true));

new ValueTableCellFactory<ProtectedTermsList, Boolean>()
.withGraphic(none -> IconTheme.JabRefIcons.EDIT.getGraphicNode())
.withOnMouseClickedEvent((file, none) -> event -> viewModel.edit(file))
.install(filesTableEditColumn);
new ValueTableCellFactory<ProtectedTermsList, Boolean>()
.withGraphic(none -> IconTheme.JabRefIcons.REMOVE.getGraphicNode())
.withTooltip(none -> Localization.lang("Remove protected terms file"))
.withOnMouseClickedEvent((file, none) -> event -> viewModel.removeFile(file))
.install(filesTableDeleteColumn);
}

private ContextMenu createContextMenu(ProtectedTermsList file) {
MenuItem edit = new MenuItem(Localization.lang("Edit"));
edit.setOnAction(event -> viewModel.edit(file));
MenuItem show = new MenuItem(Localization.lang("View"));
show.setOnAction(event -> viewModel.displayContent(file));
MenuItem remove = new MenuItem(Localization.lang("Remove"));
remove.setOnAction(event -> viewModel.removeFile(file));
MenuItem reload = new MenuItem(Localization.lang("Reload"));
reload.setOnAction(event -> viewModel.reloadFile(file));

// Enable/disable context menu items
if (file.isInternalList()) {
edit.setDisable(true);
show.setDisable(false);
remove.setDisable(true);
reload.setDisable(true);
} else {
edit.setDisable(false);
show.setDisable(false);
remove.setDisable(false);
reload.setDisable(false);
}

final ContextMenu contextMenu = new ContextMenu();
contextMenu.getItems().addAll(edit, show, remove, reload);
return contextMenu;
}

@FXML
private void addFile() {
viewModel.addFile();
}

@FXML
private void createNewFile() {
viewModel.createNewFile();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.jabref.gui.protectedterms;

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

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import org.jabref.gui.DialogService;
import org.jabref.gui.desktop.JabRefDesktop;
import org.jabref.gui.externalfiletype.ExternalFileType;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.protectedterms.ProtectedTermsList;
import org.jabref.logic.protectedterms.ProtectedTermsLoader;
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.util.OptionalUtil;
import org.jabref.preferences.PreferencesService;

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

public class ManageProtectedTermsViewModel {
private static final Logger LOGGER = LoggerFactory.getLogger(ManageProtectedTermsViewModel.class);

private final ProtectedTermsLoader termsLoader;
private final ObservableList<ProtectedTermsList> termsFiles;
private final PreferencesService preferences;
private final DialogService dialogService;

public ManageProtectedTermsViewModel(ProtectedTermsLoader termsLoader, DialogService dialogService, PreferencesService preferences) {
this.termsLoader = termsLoader;
this.dialogService = dialogService;
this.termsFiles = FXCollections.observableArrayList(termsLoader.getProtectedTermsLists());
this.preferences = preferences;
}

public ObservableList<ProtectedTermsList> getTermsFiles() {
return termsFiles;
}

public void save() {
preferences.setProtectedTermsPreferences(termsLoader);
}

public void addFile() {
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.addExtensionFilter(Localization.lang("Protected terms file"), StandardFileType.TERMS)
.withDefaultExtension(Localization.lang("Protected terms file"), StandardFileType.TERMS)
.withInitialDirectory(preferences.getWorkingDir())
.build();

dialogService.showFileOpenDialog(fileDialogConfiguration)
.ifPresent(file -> termsLoader.addProtectedTermsListFromFile(file.toAbsolutePath().toString(), true));
}

public void removeFile(ProtectedTermsList list) {
if (!list.isInternalList() && dialogService.showConfirmationDialogAndWait(Localization.lang("Remove protected terms file"),
Localization.lang("Are you sure you want to remove the protected terms file?"),
Localization.lang("Remove protected terms file"),
Localization.lang("Cancel"))) {
if (!termsLoader.removeProtectedTermsList(list)) {
LOGGER.info("Problem removing protected terms file");
}
}
}

public void createNewFile() {
NewProtectedTermsFileDialog newDialog = new NewProtectedTermsFileDialog(termsLoader, dialogService);
newDialog.showAndWait();
}

public void edit(ProtectedTermsList file) {
Optional<ExternalFileType> termsFileType = OptionalUtil.orElse(
ExternalFileTypes.getInstance().getExternalFileTypeByExt("terms"),
ExternalFileTypes.getInstance().getExternalFileTypeByExt("txt")
);

String fileName = file.getLocation();
try {
JabRefDesktop.openExternalFileAnyFormat(new BibDatabaseContext(), fileName, termsFileType);
} catch (IOException e) {
LOGGER.warn("Problem open protected terms file editor", e);
}
}

public void displayContent(ProtectedTermsList list) {
dialogService.showInformationDialogAndWait(
list.getDescription() + " - " + list.getLocation(),
list.getTermListing()
);
}

public void reloadFile(ProtectedTermsList file) {
termsLoader.reloadProtectedTermsList(file);
}
}
Loading

0 comments on commit 1a198f7

Please sign in to comment.