-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert "Protected terms" dialog to JavaFX (#4715)
* Convert "Protected terms" dialog to JavaFX * Improve code according to feedback
- Loading branch information
1 parent
e189016
commit 1a198f7
Showing
15 changed files
with
335 additions
and
642 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
src/main/java/org/jabref/gui/actions/ManageProtectedTermsAction.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/org/jabref/gui/protectedterms/ManageProtectedTermsAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/org/jabref/gui/protectedterms/ManageProtectedTermsDialog.fxml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
128 changes: 128 additions & 0 deletions
128
src/main/java/org/jabref/gui/protectedterms/ManageProtectedTermsDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
src/main/java/org/jabref/gui/protectedterms/ManageProtectedTermsViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.