Skip to content

Commit

Permalink
Fixed table update in eft preferences (#9051)
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus authored Aug 12, 2022
1 parent c82fc17 commit 0b58079
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<DialogPane minHeight="-Infinity" prefHeight="302.0" prefWidth="508.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.jabref.gui.externalfiletype.EditExternalFileTypeEntryDialog">
<DialogPane minHeight="-Infinity" prefHeight="302.0" prefWidth="508.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.jabref.gui.preferences.externalfiletypes.EditExternalFileTypeEntryDialog">
<content>
<GridPane>
<columnConstraints>
Expand All @@ -27,25 +27,23 @@
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
</rowConstraints>
<children>
<Label text="%Icon"/>
<Label text="%Name" GridPane.rowIndex="1"/>
<Label text="%Extension" GridPane.rowIndex="2"/>
<Label text="%MIME type" GridPane.rowIndex="3"/>
<Label text="%Application" GridPane.rowIndex="4"/>
<RadioButton fx:id="defaultApplication" mnemonicParsing="false" selected="true" text="%Default" GridPane.columnIndex="1" GridPane.rowIndex="4">
<toggleGroup>
<ToggleGroup fx:id="applicationToggleGroup"/>
</toggleGroup>
</RadioButton>
<TextField fx:id="name" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="1"/>
<TextField fx:id="extension" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="2"/>
<TextField fx:id="mimeType" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="3"/>
<RadioButton fx:id="customApplication" mnemonicParsing="false" text="Custom" toggleGroup="$applicationToggleGroup" GridPane.columnIndex="1" GridPane.rowIndex="5"/>
<TextField fx:id="selectedApplication" prefWidth="152.0" GridPane.columnIndex="2" GridPane.rowIndex="5"/>
<Button fx:id="btnBrowse" mnemonicParsing="false" onAction="#openFileChooser" text="%Browse" GridPane.columnIndex="3" GridPane.rowIndex="5"/>
<Label fx:id="icon" GridPane.columnIndex="1"/>
</children>
<Label text="%Icon"/>
<Label text="%Name" GridPane.rowIndex="1"/>
<Label text="%Extension" GridPane.rowIndex="2"/>
<Label text="%MIME type" GridPane.rowIndex="3"/>
<Label text="%Application" GridPane.rowIndex="4"/>
<RadioButton fx:id="defaultApplication" mnemonicParsing="false" selected="true" text="%Default" GridPane.columnIndex="1" GridPane.rowIndex="4">
<toggleGroup>
<ToggleGroup fx:id="applicationToggleGroup"/>
</toggleGroup>
</RadioButton>
<TextField fx:id="name" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="1"/>
<TextField fx:id="extension" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="2"/>
<TextField fx:id="mimeType" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="3"/>
<RadioButton fx:id="customApplication" mnemonicParsing="false" text="Custom" toggleGroup="$applicationToggleGroup" GridPane.columnIndex="1" GridPane.rowIndex="5"/>
<TextField fx:id="selectedApplication" prefWidth="152.0" GridPane.columnIndex="2" GridPane.rowIndex="5"/>
<Button fx:id="btnBrowse" mnemonicParsing="false" onAction="#openFileChooser" text="%Browse" GridPane.columnIndex="3" GridPane.rowIndex="5"/>
<Label fx:id="icon" GridPane.columnIndex="1"/>
</GridPane>
</content>
<ButtonType fx:constant="OK"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jabref.gui.externalfiletype;
package org.jabref.gui.preferences.externalfiletypes;

import javax.inject.Inject;

Expand Down Expand Up @@ -35,11 +35,12 @@ public class EditExternalFileTypeEntryDialog extends BaseDialog<Void> {
private final NativeDesktop nativeDesktop = JabRefDesktop.getNativeDesktop();
private final FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder().withInitialDirectory(nativeDesktop.getApplicationDirectory()).build();

private final ExternalFileTypeItemViewModel item;

private EditExternalFileTypeViewModel viewModel;
private CustomExternalFileType entry;

public EditExternalFileTypeEntryDialog(CustomExternalFileType entry, String dialogTitle) {
this.entry = entry;
public EditExternalFileTypeEntryDialog(ExternalFileTypeItemViewModel item, String dialogTitle) {
this.item = item;

this.setTitle(dialogTitle);

Expand All @@ -57,7 +58,7 @@ public EditExternalFileTypeEntryDialog(CustomExternalFileType entry, String dial

@FXML
public void initialize() {
viewModel = new EditExternalFileTypeViewModel(entry);
viewModel = new EditExternalFileTypeViewModel(item);

icon.setGraphic(viewModel.getIcon());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jabref.gui.externalfiletype;
package org.jabref.gui.preferences.externalfiletypes;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
Expand All @@ -7,39 +7,41 @@
import javafx.scene.Node;

public class EditExternalFileTypeViewModel {
private final ExternalFileTypeItemViewModel fileTypeViewModel;

private final StringProperty extensionProperty = new SimpleStringProperty("");
private final StringProperty nameProperty = new SimpleStringProperty("");
private final StringProperty mimeTypeProperty = new SimpleStringProperty("");
private final StringProperty extensionProperty = new SimpleStringProperty("");
private final StringProperty selectedApplicationProperty = new SimpleStringProperty("");
private final BooleanProperty defaultApplicationSelectedProperty = new SimpleBooleanProperty(false);
private final BooleanProperty customApplicationSelectedProperty = new SimpleBooleanProperty(false);
private final Node icon;
private final CustomExternalFileType fileType;

public EditExternalFileTypeViewModel(CustomExternalFileType fileType) {
this.fileType = fileType;
extensionProperty.setValue(fileType.getExtension());
nameProperty.setValue(fileType.getName());
mimeTypeProperty.setValue(fileType.getMimeType());
selectedApplicationProperty.setValue(fileType.getOpenWithApplication());
icon = fileType.getIcon().getGraphicNode();

if (fileType.getOpenWithApplication().isEmpty()) {

public EditExternalFileTypeViewModel(ExternalFileTypeItemViewModel fileTypeViewModel) {
this.fileTypeViewModel = fileTypeViewModel;

extensionProperty.setValue(fileTypeViewModel.extensionProperty().getValue());
nameProperty.setValue(fileTypeViewModel.nameProperty().getValue());
mimeTypeProperty.setValue(fileTypeViewModel.mimetypeProperty().getValue());

if (fileTypeViewModel.applicationProperty().getValue().isEmpty()) {
defaultApplicationSelectedProperty.setValue(true);
} else {
customApplicationSelectedProperty.setValue(true);
}
}

public StringProperty extensionProperty() {
return extensionProperty;
public Node getIcon() {
return fileTypeViewModel.iconProperty().getValue().getGraphicNode();
}

public StringProperty nameProperty() {
return nameProperty;
}

public StringProperty extensionProperty() {
return extensionProperty;
}

public StringProperty mimeTypeProperty() {
return mimeTypeProperty;
}
Expand All @@ -56,29 +58,25 @@ public BooleanProperty customApplicationSelectedProperty() {
return customApplicationSelectedProperty;
}

public Node getIcon() {
return icon;
}

public void storeSettings() {
fileType.setName(nameProperty.getValue().trim());
fileType.setMimeType(mimeTypeProperty.getValue().trim());
fileTypeViewModel.nameProperty().setValue(nameProperty.getValue().trim());
fileTypeViewModel.mimetypeProperty().setValue(mimeTypeProperty.getValue().trim());

String ext = extensionProperty.getValue().trim();
if (!ext.isEmpty() && (ext.charAt(0) == '.')) {
fileType.setExtension(ext.substring(1));
fileTypeViewModel.extensionProperty().setValue(ext.substring(1));
} else {
fileType.setExtension(ext);
fileTypeViewModel.extensionProperty().setValue(ext);
}

String application = selectedApplicationProperty.getValue().trim();

// store application as empty if the "Default" option is selected, or if the application name is empty:
if (defaultApplicationSelectedProperty.getValue() || application.isEmpty()) {
fileType.setOpenWith("");
fileTypeViewModel.applicationProperty().setValue("");
selectedApplicationProperty.setValue("");
} else {
fileType.setOpenWith(application);
fileTypeViewModel.applicationProperty().setValue(application);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.jabref.gui.preferences.externalfiletypes;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import org.jabref.gui.externalfiletype.CustomExternalFileType;
import org.jabref.gui.externalfiletype.ExternalFileType;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.icon.JabRefIcon;

public class ExternalFileTypeItemViewModel {
private final ObjectProperty<JabRefIcon> icon = new SimpleObjectProperty<>();
private final StringProperty name = new SimpleStringProperty();
private final StringProperty extension = new SimpleStringProperty();
private final StringProperty mimetype = new SimpleStringProperty();
private final StringProperty application = new SimpleStringProperty();

public ExternalFileTypeItemViewModel(ExternalFileType fileType) {
this.icon.setValue(fileType.getIcon());
this.name.setValue(fileType.getName());
this.extension.setValue(fileType.getExtension());
this.mimetype.setValue(fileType.getMimeType());
this.application.setValue(fileType.getOpenWithApplication());
}

public ExternalFileTypeItemViewModel() {
this(new CustomExternalFileType("", "", "", "", "new", IconTheme.JabRefIcons.FILE));
}

public ObjectProperty<JabRefIcon> iconProperty() {
return icon;
}

/**
* Used for sorting in the table
*/
public String getName() {
return name.get();
}

public StringProperty nameProperty() {
return name;
}

public StringProperty extensionProperty() {
return extension;
}

public StringProperty mimetypeProperty() {
return mimetype;
}

public StringProperty applicationProperty() {
return application;
}

public ExternalFileType toExternalFileType() {
return new CustomExternalFileType(
this.name.get(),
this.extension.get(),
this.mimetype.get(),
this.application.get(),
this.icon.get().name(),
this.icon.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<TableColumn fx:id="fileTypesTableIconColumn" minWidth="40.0" maxWidth="40.0"/>
<TableColumn fx:id="fileTypesTableNameColumn" text="%Name"/>
<TableColumn fx:id="fileTypesTableExtensionColumn" text="%Extension" prefWidth="120"/>
<TableColumn fx:id="fileTypesTableTypeColumn" text="%MIME type" prefWidth="150"/>
<TableColumn fx:id="fileTypesTableMimeTypeColumn" text="%MIME type" prefWidth="150"/>
<TableColumn fx:id="fileTypesTableApplicationColumn" text="%Application" prefWidth="100"/>
<TableColumn fx:id="fileTypesTableEditColumn" minWidth="40.0" maxWidth="40.0"/>
<TableColumn fx:id="fileTypesTableDeleteColumn" minWidth="40.0" maxWidth="40.0"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import org.jabref.gui.externalfiletype.ExternalFileType;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.icon.JabRefIcon;
import org.jabref.gui.preferences.AbstractPreferenceTabView;
Expand All @@ -20,14 +19,14 @@
*/
public class ExternalFileTypesTab extends AbstractPreferenceTabView<ExternalFileTypesTabViewModel> implements PreferencesTab {

@FXML private TableColumn<ExternalFileType, JabRefIcon> fileTypesTableIconColumn;
@FXML private TableColumn<ExternalFileType, String> fileTypesTableNameColumn;
@FXML private TableColumn<ExternalFileType, String> fileTypesTableExtensionColumn;
@FXML private TableColumn<ExternalFileType, String> fileTypesTableTypeColumn;
@FXML private TableColumn<ExternalFileType, String> fileTypesTableApplicationColumn;
@FXML private TableColumn<ExternalFileType, Boolean> fileTypesTableEditColumn;
@FXML private TableColumn<ExternalFileType, Boolean> fileTypesTableDeleteColumn;
@FXML private TableView<ExternalFileType> fileTypesTable;
@FXML private TableColumn<ExternalFileTypeItemViewModel, JabRefIcon> fileTypesTableIconColumn;
@FXML private TableColumn<ExternalFileTypeItemViewModel, String> fileTypesTableNameColumn;
@FXML private TableColumn<ExternalFileTypeItemViewModel, String> fileTypesTableExtensionColumn;
@FXML private TableColumn<ExternalFileTypeItemViewModel, String> fileTypesTableMimeTypeColumn;
@FXML private TableColumn<ExternalFileTypeItemViewModel, String> fileTypesTableApplicationColumn;
@FXML private TableColumn<ExternalFileTypeItemViewModel, Boolean> fileTypesTableEditColumn;
@FXML private TableColumn<ExternalFileTypeItemViewModel, Boolean> fileTypesTableDeleteColumn;
@FXML private TableView<ExternalFileTypeItemViewModel> fileTypesTable;

public ExternalFileTypesTab() {
ViewLoader.view(this)
Expand All @@ -42,26 +41,46 @@ public String getTabName() {

@FXML
public void initialize() {
viewModel = new ExternalFileTypesTabViewModel(preferencesService.getFilePreferences());
viewModel = new ExternalFileTypesTabViewModel(preferencesService.getFilePreferences(), dialogService);

fileTypesTable.setItems(viewModel.getFileTypes());

fileTypesTableIconColumn.setCellValueFactory(data -> BindingsHelper.constantOf(data.getValue().getIcon()));
fileTypesTableNameColumn.setCellValueFactory(data -> BindingsHelper.constantOf(data.getValue().getName()));
fileTypesTableExtensionColumn.setCellValueFactory(data -> BindingsHelper.constantOf(data.getValue().getExtension()));
fileTypesTableTypeColumn.setCellValueFactory(data -> BindingsHelper.constantOf(data.getValue().getMimeType()));
fileTypesTableApplicationColumn.setCellValueFactory(data -> BindingsHelper.constantOf(data.getValue().getOpenWithApplication()));
fileTypesTableIconColumn.setCellValueFactory(cellData -> cellData.getValue().iconProperty());
new ValueTableCellFactory<ExternalFileTypeItemViewModel, JabRefIcon>()
.withGraphic(JabRefIcon::getGraphicNode)
.install(fileTypesTableIconColumn);

fileTypesTableNameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
new ValueTableCellFactory<ExternalFileTypeItemViewModel, String>()
.withText(name -> name)
.install(fileTypesTableNameColumn);

fileTypesTableExtensionColumn.setCellValueFactory(cellData -> cellData.getValue().extensionProperty());
new ValueTableCellFactory<ExternalFileTypeItemViewModel, String>()
.withText(extension -> extension)
.install(fileTypesTableExtensionColumn);

fileTypesTableMimeTypeColumn.setCellValueFactory(cellData -> cellData.getValue().mimetypeProperty());
new ValueTableCellFactory<ExternalFileTypeItemViewModel, String>()
.withText(mimetype -> mimetype)
.install(fileTypesTableMimeTypeColumn);

fileTypesTableApplicationColumn.setCellValueFactory(cellData -> cellData.getValue().applicationProperty());
new ValueTableCellFactory<ExternalFileTypeItemViewModel, String>()
.withText(extension -> extension)
.install(fileTypesTableApplicationColumn);

fileTypesTableEditColumn.setCellValueFactory(data -> BindingsHelper.constantOf(true));
fileTypesTableDeleteColumn.setCellValueFactory(data -> BindingsHelper.constantOf(true));

new ValueTableCellFactory<ExternalFileType, JabRefIcon>()
new ValueTableCellFactory<ExternalFileTypeItemViewModel, JabRefIcon>()
.withGraphic(JabRefIcon::getGraphicNode)
.install(fileTypesTableIconColumn);
new ValueTableCellFactory<ExternalFileType, Boolean>()
new ValueTableCellFactory<ExternalFileTypeItemViewModel, Boolean>()
.withGraphic(none -> IconTheme.JabRefIcons.EDIT.getGraphicNode())
.withOnMouseClickedEvent((type, none) -> event -> viewModel.edit(type))
.install(fileTypesTableEditColumn);
new ValueTableCellFactory<ExternalFileType, Boolean>()
new ValueTableCellFactory<ExternalFileTypeItemViewModel, Boolean>()
.withGraphic(none -> IconTheme.JabRefIcons.DELETE_ENTRY.getGraphicNode())
.withOnMouseClickedEvent((type, none) -> event -> viewModel.remove(type))
.install(fileTypesTableDeleteColumn);
Expand Down
Loading

0 comments on commit 0b58079

Please sign in to comment.