Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert ExternalFileTypeEntryEditor to javafx #4885

Merged
merged 5 commits into from
Apr 14, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void initialize() {
.install(fileTypesTableIconColumn);
new ValueTableCellFactory<ExternalFileType, Boolean>()
.withGraphic(none -> IconTheme.JabRefIcons.EDIT.getGraphicNode())
.withOnMouseClickedEvent((type, none) -> event -> viewModel.edit(type))
.withOnMouseClickedEvent((type, none) -> event -> viewModel.edit(type, false))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the indent seems to be off

.install(fileTypesTableEditColumn);
new ValueTableCellFactory<ExternalFileType, Boolean>()
.withGraphic(none -> IconTheme.JabRefIcons.REMOVE.getGraphicNode())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import org.jabref.gui.icon.IconTheme;

public class CustomizeExternalFileTypesViewModel {
private ObservableList<ExternalFileType> fileTypes;

private final ObservableList<ExternalFileType> fileTypes;

public CustomizeExternalFileTypesViewModel() {
Set<ExternalFileType> types = ExternalFileTypes.getInstance().getExternalFileTypeSelection();
Expand All @@ -34,23 +35,24 @@ public void resetToDefaults() {
public void addNewType() {
CustomExternalFileType type = new CustomExternalFileType("", "", "", "", "new", IconTheme.JabRefIcons.FILE);
fileTypes.add(type);
edit(type);
edit(type, true);
}

public ObservableList<ExternalFileType> getFileTypes() {
return fileTypes;
}

public void edit(ExternalFileType type) {
public void edit(ExternalFileType type, boolean add) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a public add(ExternalFileType) method and make this one here private (I'm not a big fan of these boolean toggles that completely alter the nature of the method).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a better solution.

CustomExternalFileType typeForEdit;
if (type instanceof CustomExternalFileType) {
typeForEdit = (CustomExternalFileType) type;
} else {
typeForEdit = new CustomExternalFileType(type);
}

ExternalFileTypeEntryEditor entryEditor = new ExternalFileTypeEntryEditor(typeForEdit);
entryEditor.setVisible(true);
EditExternalFileTypeDialog dlg = new EditExternalFileTypeDialog(typeForEdit, add);
dlg.showAndWait();

}

public void remove(ExternalFileType type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?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.EditExternalFileTypeDialog">
<content>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<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>
</GridPane>
</content>
<ButtonType fx:constant="OK" />
<ButtonType fx:constant="CANCEL" />
</DialogPane>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.jabref.gui.externalfiletype;

import javax.inject.Inject;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;

import org.jabref.gui.DialogService;
import org.jabref.gui.desktop.JabRefDesktop;
import org.jabref.gui.desktop.os.NativeDesktop;
import org.jabref.gui.util.BaseDialog;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.logic.l10n.Localization;

import com.airhacks.afterburner.views.ViewLoader;

public class EditExternalFileTypeDialog extends BaseDialog<Void> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the Edit from the name since it is now add/edit combined.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would actually leave it with Edit, for the linked files dialog we also have edit, although one can add files there, too
And as we already have another dialog with the name customizeExternalFileTypeDialog, removing the edit would just make things confusing,
I therefore decided to rename it to EditExternalFileTypeEntryDialog


@FXML private RadioButton defaultApplication;
@FXML private ToggleGroup applicationToggleGroup;
@FXML private TextField extension;
@FXML private TextField name;
@FXML private TextField mimeType;
@FXML private RadioButton customApplication;
@FXML private TextField selectedApplication;
@FXML private Button btnBrowse;
@FXML private Label icon;
@Inject private DialogService dialogService;

private final NativeDesktop nativeDesktop = JabRefDesktop.getNativeDesktop();
private final FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder().withInitialDirectory(nativeDesktop.getApplicationDirectory()).build();
private final String editFileTitle = Localization.lang("Edit file type");
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
private final String newFileTitle = Localization.lang("Add new file type");

private EditExternalFileTypeViewModel viewModel;
private CustomExternalFileType entry;

public EditExternalFileTypeDialog(CustomExternalFileType entry, boolean add) {
this.entry = entry;

setTitleToAddOrEdit(add);

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

this.setResultConverter(button -> {
if (button == ButtonType.OK) {
viewModel.storeSettings();
}
return null;
});
}

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

icon.setGraphic(viewModel.getIcon());

defaultApplication.selectedProperty().bindBidirectional(viewModel.defaultApplicationSelectedProperty());
selectedApplication.disableProperty().bind(viewModel.defaultApplicationSelectedProperty());
btnBrowse.disableProperty().bind(viewModel.defaultApplicationSelectedProperty());

extension.textProperty().bindBidirectional(viewModel.extensionProperty());
name.textProperty().bindBidirectional(viewModel.nameProperty());
mimeType.textProperty().bindBidirectional(viewModel.mimeTypeProperty());
selectedApplication.textProperty().bindBidirectional(viewModel.selectedApplicationProperty());
}

@FXML
private void openFileChooser(ActionEvent event) {
dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(path -> viewModel.selectedApplicationProperty().setValue(path.toAbsolutePath().toString()));
}

private void setTitleToAddOrEdit(boolean add) {
if (add) {
this.setTitle(newFileTitle);
} else {
this.setTitle(editFileTitle);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.jabref.gui.externalfiletype;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Node;

import org.jabref.logic.util.OS;

public class EditExternalFileTypeViewModel {

private final StringProperty extensionProperty = new SimpleStringProperty("");
private final StringProperty nameProperty = new SimpleStringProperty("");
private final StringProperty mimeTypeProperty = new SimpleStringProperty("");
private final StringProperty selectedApplicationProperty = new SimpleStringProperty("");
private final BooleanProperty defaultApplicationSelectedProperty = 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.getFieldName());
mimeTypeProperty.setValue(fileType.getMimeType());
selectedApplicationProperty.setValue(fileType.getOpenWithApplication());
icon = fileType.getIcon().getGraphicNode();

if (fileType.getOpenWithApplication().isEmpty()) {
defaultApplicationSelectedProperty.setValue(true);
}

}

public StringProperty extensionProperty() {
return extensionProperty;
}

public StringProperty nameProperty() {
return nameProperty;
}

public StringProperty mimeTypeProperty() {
return mimeTypeProperty;
}

public StringProperty selectedApplicationProperty() {
return selectedApplicationProperty;
}

public BooleanProperty defaultApplicationSelectedProperty() {
return defaultApplicationSelectedProperty;
}

public Node getIcon() {
return icon;
}

public void storeSettings() {

fileType.setName(nameProperty.getValue().trim());
fileType.setMimeType(mimeTypeProperty.getValue().trim());

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

String application = selectedApplicationProperty.getValue().trim();
if (OS.WINDOWS) {
// On Windows, store application as empty if the "Default" option is selected,
// or if the application name is empty:
if (defaultApplicationSelectedProperty.getValue() || application.isEmpty()) {
fileType.setOpenWith("");
selectedApplicationProperty.setValue("");

} else {
fileType.setOpenWith(application);
}
} else {
fileType.setOpenWith(application);
}

}

}
Loading