Skip to content

Commit

Permalink
Convert "Show preferences" dialog to JavaFX (#4605)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Jan 24, 2019
1 parent c6a1fcb commit 5d2b765
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void construct() {
exportPreferences.setOnAction(e -> exportPreferences());
exportPreferences.setMaxWidth(Double.MAX_VALUE);
Button showPreferences = new Button(Localization.lang("Show preferences"));
showPreferences.setOnAction(e -> new PreferencesFilterDialog(new JabRefPreferencesFilter(prefs)).setVisible(true));
showPreferences.setOnAction(e -> new PreferencesFilterDialog(new JabRefPreferencesFilter(prefs)).showAndWait());
showPreferences.setMaxWidth(Double.MAX_VALUE);
Button resetPreferences = new Button(Localization.lang("Reset preferences"));
resetPreferences.setOnAction(e -> resetPreferences());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ButtonType?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<DialogPane xmlns:fx="http://javafx.com/fxml/1" prefHeight="600.0" prefWidth="950.0"
xmlns="http://javafx.com/javafx/8.0.121" fx:controller="org.jabref.gui.preferences.PreferencesFilterDialog">

<content>
<BorderPane>
<bottom>
<HBox>
<CheckBox fx:id="showOnlyDeviatingPreferenceOptions"
text="%Show only preferences deviating from their default value"/>
<HBox HBox.hgrow="ALWAYS"/>
<Label fx:id="count"/>
</HBox>
</bottom>
<center>
<TableView fx:id="table">
<columns>
<TableColumn fx:id="columnType" text="%type"/>
<TableColumn fx:id="columnKey" text="%key"/>
<TableColumn fx:id="columnValue" text="%value"/>
<TableColumn fx:id="columnDefaultValue" text="%default"/>
</columns>
<padding>
<Insets bottom="30.0"/>
</padding>
</TableView>
</center>
</BorderPane>
</content>
<ButtonType fx:constant="CLOSE"/>
</DialogPane>
137 changes: 38 additions & 99 deletions src/main/java/org/jabref/gui/preferences/PreferencesFilterDialog.java
Original file line number Diff line number Diff line change
@@ -1,125 +1,64 @@
package org.jabref.gui.preferences;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.List;
import java.util.Objects;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import org.jabref.gui.JabRefDialog;
import org.jabref.gui.WrapLayout;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import org.jabref.gui.util.BaseDialog;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.JabRefPreferencesFilter;

class PreferencesFilterDialog extends JabRefDialog {
import com.airhacks.afterburner.views.ViewLoader;

public class PreferencesFilterDialog extends BaseDialog<Void> {

private final JabRefPreferencesFilter preferencesFilter;
private final ObservableList<JabRefPreferencesFilter.PreferenceOption> preferenceOptions;

private final JTable table;
private final JCheckBox showOnlyDeviatingPreferenceOptions;
private final JLabel count;
@FXML private TableView<JabRefPreferencesFilter.PreferenceOption> table;
@FXML private TableColumn<JabRefPreferencesFilter.PreferenceOption, JabRefPreferencesFilter.PreferenceType> columnType;
@FXML private TableColumn<JabRefPreferencesFilter.PreferenceOption, String> columnKey;
@FXML private TableColumn<JabRefPreferencesFilter.PreferenceOption, Object> columnValue;
@FXML private TableColumn<JabRefPreferencesFilter.PreferenceOption, Object> columnDefaultValue;
@FXML private CheckBox showOnlyDeviatingPreferenceOptions;
@FXML private Label count;

public PreferencesFilterDialog(JabRefPreferencesFilter preferencesFilter) {
super(true, PreferencesFilterDialog.class);

this.preferencesFilter = Objects.requireNonNull(preferencesFilter);
this.preferenceOptions = FXCollections.observableArrayList();

this.setTitle(Localization.lang("Preferences"));
this.setSize(new Dimension(800, 600));
ViewLoader.view(this)
.load()
.setAsDialogPane(this);

JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());

JPanel northPanel = new JPanel();
northPanel.setLayout(new WrapLayout(FlowLayout.LEFT));
showOnlyDeviatingPreferenceOptions = new JCheckBox(Localization.lang("Show only preferences deviating from their default value"), false);
showOnlyDeviatingPreferenceOptions.addChangeListener(x -> updateModel());
northPanel.add(showOnlyDeviatingPreferenceOptions);
count = new JLabel();
northPanel.add(count);
panel.add(northPanel, BorderLayout.NORTH);

table = new JTable();
table.setAutoCreateRowSorter(true);
this.setTitle(Localization.lang("Preferences"));
}

@FXML
private void initialize() {
showOnlyDeviatingPreferenceOptions.setOnAction(event -> updateModel());
columnType.setCellValueFactory(data -> new ReadOnlyObjectWrapper<>(data.getValue().getType()));
columnKey.setCellValueFactory(data -> new ReadOnlyStringWrapper(data.getValue().getKey()));
columnValue.setCellValueFactory(data -> new ReadOnlyObjectWrapper<>(data.getValue().getValue()));
columnDefaultValue.setCellValueFactory(data -> new ReadOnlyObjectWrapper<>(data.getValue().getDefaultValue().orElse("")));
table.setItems(preferenceOptions);
updateModel();
panel.add(new JScrollPane(table), BorderLayout.CENTER);

this.getContentPane().add(panel);
}

private void updateModel() {
List<JabRefPreferencesFilter.PreferenceOption> preferenceOptions;

if (showOnlyDeviatingPreferenceOptions.isSelected()) {
preferenceOptions = preferencesFilter.getDeviatingPreferences();
preferenceOptions.setAll(preferencesFilter.getDeviatingPreferences());
} else {
preferenceOptions = preferencesFilter.getPreferenceOptions();
preferenceOptions.setAll(preferencesFilter.getPreferenceOptions());
}

table.setModel(new PreferencesTableModel(preferenceOptions));
count.setText(String.format("(%d)", preferenceOptions.size()));
}

private static class PreferencesTableModel extends AbstractTableModel {

private final List<JabRefPreferencesFilter.PreferenceOption> preferences;

public PreferencesTableModel(List<JabRefPreferencesFilter.PreferenceOption> preferences) {
this.preferences = Objects.requireNonNull(preferences);
}

@Override
public String getColumnName(int column) {
if (column == 0) {
return Localization.lang("type");
} else if (column == 1) {
return Localization.lang("key");
} else if (column == 2) {
return Localization.lang("value");
} else if (column == 3) {
return Localization.lang("default");
} else {
return "n/a";
}
}

@Override
public int getRowCount() {
return preferences.size();
}

@Override
public int getColumnCount() {
return 4;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if ((rowIndex < 0) || ((rowIndex - 1) > preferences.size())) {
return "n/a";
}

JabRefPreferencesFilter.PreferenceOption preferenceOption = preferences.get(rowIndex);
if (columnIndex == 0) {
return preferenceOption.getType();
} else if (columnIndex == 1) {
return preferenceOption.getKey();
} else if (columnIndex == 2) {
return preferenceOption.getValue();
} else if (columnIndex == 3) {
return preferenceOption.getDefaultValue().orElse("NULL");
} else {
return "n/a";
}
}
}

}

0 comments on commit 5d2b765

Please sign in to comment.