Skip to content

Commit c16f733

Browse files
authored
Convert "Manage external file types" dialog to JavaFX (#4711)
* Convert "Manage external file types" dialog to JavaFX * Fix language file
1 parent f1de7bd commit c16f733

File tree

8 files changed

+191
-338
lines changed

8 files changed

+191
-338
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package org.jabref.gui.actions;
22

3-
import org.jabref.gui.externalfiletype.ExternalFileTypeEditor;
3+
import org.jabref.gui.externalfiletype.CustomizeExternalFileTypesDialog;
44

55
public class EditExternalFileTypesAction extends SimpleCommand {
66

77
@Override
88
public void execute() {
9-
ExternalFileTypeEditor editor = new ExternalFileTypeEditor();
10-
editor.show();
9+
CustomizeExternalFileTypesDialog editor = new CustomizeExternalFileTypesDialog();
10+
editor.showAndWait();
1111
}
1212
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.scene.control.Button?>
4+
<?import javafx.scene.control.ButtonType?>
5+
<?import javafx.scene.control.DialogPane?>
6+
<?import javafx.scene.control.TableColumn?>
7+
<?import javafx.scene.control.TableView?>
8+
<?import javafx.scene.layout.HBox?>
9+
<?import javafx.scene.layout.VBox?>
10+
<DialogPane xmlns:fx="http://javafx.com/fxml"
11+
xmlns="http://javafx.com/javafx"
12+
fx:controller="org.jabref.gui.externalfiletype.CustomizeExternalFileTypesDialog"
13+
prefHeight="500.0" prefWidth="750.0">
14+
<content>
15+
<VBox spacing="10">
16+
<TableView fx:id="fileTypesTable" VBox.vgrow="ALWAYS">
17+
<columns>
18+
<TableColumn fx:id="fileTypesTableIconColumn" minWidth="40.0" maxWidth="40.0"/>
19+
<TableColumn fx:id="fileTypesTableNameColumn" text="%Name"/>
20+
<TableColumn fx:id="fileTypesTableExtensionColumn" text="%Extension" prefWidth="120"/>
21+
<TableColumn fx:id="fileTypesTableTypeColumn" text="%MIME type" prefWidth="150"/>
22+
<TableColumn fx:id="fileTypesTableApplicationColumn" text="%Application" prefWidth="100"/>
23+
<TableColumn fx:id="fileTypesTableEditColumn" minWidth="40.0" maxWidth="40.0"/>
24+
<TableColumn fx:id="fileTypesTableDeleteColumn" minWidth="40.0" maxWidth="40.0"/>
25+
</columns>
26+
<columnResizePolicy>
27+
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
28+
</columnResizePolicy>
29+
</TableView>
30+
<HBox>
31+
<Button text="%Add new file type" onAction="#addNewType"/>
32+
<HBox HBox.hgrow="ALWAYS"/>
33+
<Button text="%Reset to default" onAction="#resetToDefault" styleClass="text-button"/>
34+
</HBox>
35+
</VBox>
36+
</content>
37+
<ButtonType fx:constant="OK"/>
38+
<ButtonType fx:constant="CANCEL"/>
39+
</DialogPane>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.jabref.gui.externalfiletype;
2+
3+
import javafx.fxml.FXML;
4+
import javafx.scene.control.ButtonType;
5+
import javafx.scene.control.TableColumn;
6+
import javafx.scene.control.TableView;
7+
8+
import org.jabref.gui.icon.IconTheme;
9+
import org.jabref.gui.icon.JabRefIcon;
10+
import org.jabref.gui.util.BaseDialog;
11+
import org.jabref.gui.util.BindingsHelper;
12+
import org.jabref.gui.util.ValueTableCellFactory;
13+
import org.jabref.logic.l10n.Localization;
14+
15+
import com.airhacks.afterburner.views.ViewLoader;
16+
17+
/**
18+
* Editor for external file types.
19+
*/
20+
public class CustomizeExternalFileTypesDialog extends BaseDialog<Void> {
21+
22+
@FXML private TableColumn<ExternalFileType, JabRefIcon> fileTypesTableIconColumn;
23+
@FXML private TableColumn<ExternalFileType, String> fileTypesTableNameColumn;
24+
@FXML private TableColumn<ExternalFileType, String> fileTypesTableExtensionColumn;
25+
@FXML private TableColumn<ExternalFileType, String> fileTypesTableTypeColumn;
26+
@FXML private TableColumn<ExternalFileType, String> fileTypesTableApplicationColumn;
27+
@FXML private TableColumn<ExternalFileType, Boolean> fileTypesTableEditColumn;
28+
@FXML private TableColumn<ExternalFileType, Boolean> fileTypesTableDeleteColumn;
29+
@FXML private TableView<ExternalFileType> fileTypesTable;
30+
31+
private CustomizeExternalFileTypesViewModel viewModel;
32+
33+
public CustomizeExternalFileTypesDialog() {
34+
this.setTitle(Localization.lang("Manage external file types"));
35+
36+
ViewLoader.view(this)
37+
.load()
38+
.setAsDialogPane(this);
39+
40+
this.setResultConverter(button -> {
41+
if (button == ButtonType.OK) {
42+
viewModel.storeSettings();
43+
}
44+
return null;
45+
});
46+
}
47+
48+
@FXML
49+
public void initialize() {
50+
viewModel = new CustomizeExternalFileTypesViewModel();
51+
52+
fileTypesTable.setItems(viewModel.getFileTypes());
53+
54+
fileTypesTableIconColumn.setCellValueFactory(data -> BindingsHelper.constantOf(data.getValue().getIcon()));
55+
fileTypesTableNameColumn.setCellValueFactory(data -> BindingsHelper.constantOf(data.getValue().getName()));
56+
fileTypesTableExtensionColumn.setCellValueFactory(data -> BindingsHelper.constantOf(data.getValue().getExtension()));
57+
fileTypesTableTypeColumn.setCellValueFactory(data -> BindingsHelper.constantOf(data.getValue().getMimeType()));
58+
fileTypesTableApplicationColumn.setCellValueFactory(data -> BindingsHelper.constantOf(data.getValue().getOpenWithApplication()));
59+
fileTypesTableEditColumn.setCellValueFactory(data -> BindingsHelper.constantOf(true));
60+
fileTypesTableDeleteColumn.setCellValueFactory(data -> BindingsHelper.constantOf(true));
61+
62+
new ValueTableCellFactory<ExternalFileType, JabRefIcon>()
63+
.withGraphic(JabRefIcon::getGraphicNode)
64+
.install(fileTypesTableIconColumn);
65+
new ValueTableCellFactory<ExternalFileType, Boolean>()
66+
.withGraphic(none -> IconTheme.JabRefIcons.EDIT.getGraphicNode())
67+
.withOnMouseClickedEvent((type, none) -> event -> viewModel.edit(type))
68+
.install(fileTypesTableEditColumn);
69+
new ValueTableCellFactory<ExternalFileType, Boolean>()
70+
.withGraphic(none -> IconTheme.JabRefIcons.REMOVE.getGraphicNode())
71+
.withOnMouseClickedEvent((type, none) -> event -> viewModel.remove(type))
72+
.install(fileTypesTableDeleteColumn);
73+
}
74+
75+
@FXML
76+
private void addNewType() {
77+
viewModel.addNewType();
78+
fileTypesTable.getSelectionModel().selectLast();
79+
fileTypesTable.scrollTo(viewModel.getFileTypes().size() - 1);
80+
}
81+
82+
@FXML
83+
private void resetToDefault() {
84+
viewModel.resetToDefaults();
85+
}
86+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.jabref.gui.externalfiletype;
2+
3+
import java.util.Comparator;
4+
import java.util.List;
5+
import java.util.Set;
6+
7+
import javafx.collections.FXCollections;
8+
import javafx.collections.ObservableList;
9+
10+
import org.jabref.gui.icon.IconTheme;
11+
12+
public class CustomizeExternalFileTypesViewModel {
13+
private ObservableList<ExternalFileType> fileTypes;
14+
15+
public CustomizeExternalFileTypesViewModel() {
16+
Set<ExternalFileType> types = ExternalFileTypes.getInstance().getExternalFileTypeSelection();
17+
fileTypes = FXCollections.observableArrayList(types);
18+
fileTypes.sort(Comparator.comparing(ExternalFileType::getName));
19+
}
20+
21+
/**
22+
* Stores the list of external entry types in the preferences.
23+
*/
24+
public void storeSettings() {
25+
ExternalFileTypes.getInstance().setExternalFileTypes(fileTypes);
26+
}
27+
28+
public void resetToDefaults() {
29+
List<ExternalFileType> list = ExternalFileTypes.getDefaultExternalFileTypes();
30+
fileTypes.setAll(list);
31+
fileTypes.sort(Comparator.comparing(ExternalFileType::getName));
32+
}
33+
34+
public void addNewType() {
35+
CustomExternalFileType type = new CustomExternalFileType("", "", "", "", "new", IconTheme.JabRefIcons.FILE);
36+
fileTypes.add(type);
37+
edit(type);
38+
}
39+
40+
public ObservableList<ExternalFileType> getFileTypes() {
41+
return fileTypes;
42+
}
43+
44+
public void edit(ExternalFileType type) {
45+
CustomExternalFileType typeForEdit;
46+
if (type instanceof CustomExternalFileType) {
47+
typeForEdit = (CustomExternalFileType) type;
48+
} else {
49+
typeForEdit = new CustomExternalFileType(type);
50+
}
51+
52+
ExternalFileTypeEntryEditor entryEditor = new ExternalFileTypeEntryEditor(typeForEdit);
53+
entryEditor.setVisible(true);
54+
}
55+
56+
public void remove(ExternalFileType type) {
57+
fileTypes.remove(type);
58+
}
59+
}

0 commit comments

Comments
 (0)