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 all 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 @@ -8,9 +8,11 @@
import javafx.collections.ObservableList;

import org.jabref.gui.icon.IconTheme;
import org.jabref.logic.l10n.Localization;

public class CustomizeExternalFileTypesViewModel {
private ObservableList<ExternalFileType> fileTypes;

private final ObservableList<ExternalFileType> fileTypes;

public CustomizeExternalFileTypesViewModel() {
Set<ExternalFileType> types = ExternalFileTypes.getInstance().getExternalFileTypeSelection();
Expand All @@ -34,23 +36,27 @@ public void resetToDefaults() {
public void addNewType() {
CustomExternalFileType type = new CustomExternalFileType("", "", "", "", "new", IconTheme.JabRefIcons.FILE);
fileTypes.add(type);
edit(type);
showEditDialog(type, Localization.lang("Add new file type"));
}

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

public void edit(ExternalFileType type) {
private void showEditDialog(ExternalFileType type, String dialogTitle) {
CustomExternalFileType typeForEdit;
if (type instanceof CustomExternalFileType) {
typeForEdit = (CustomExternalFileType) type;
} else {
typeForEdit = new CustomExternalFileType(type);
}

ExternalFileTypeEntryEditor entryEditor = new ExternalFileTypeEntryEditor(typeForEdit);
entryEditor.setVisible(true);
EditExternalFileTypeEntryDialog dlg = new EditExternalFileTypeEntryDialog(typeForEdit, dialogTitle);
dlg.showAndWait();
}

public void edit(ExternalFileType type) {
showEditDialog(type, Localization.lang("Edit file type"));
}

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.EditExternalFileTypeEntryDialog">
<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,79 @@
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 com.airhacks.afterburner.views.ViewLoader;

public class EditExternalFileTypeEntryDialog extends BaseDialog<Void> {

@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 EditExternalFileTypeViewModel viewModel;
private CustomExternalFileType entry;

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

this.setTitle(dialogTitle);

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()));
}

}
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