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

Completed General Fields Customizer conversion to JavaFX #4346

Merged
merged 26 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6e5694a
temp commit 09 06 18
abepolk Sep 6, 2018
92cddb1
starting work on converting Customize General Fields dialog from Swin…
abepolk Sep 7, 2018
24eb0a6
More work on converting JavaFX dialog
abepolk Sep 7, 2018
770a771
Even more work on converting JavaFX dialog
abepolk Sep 7, 2018
6a0b878
more work JavaFX
abepolk Sep 9, 2018
4058a70
Further work, now cannot load
abepolk Sep 11, 2018
b3d5238
Cleaned up comments
abepolk Sep 15, 2018
51712e2
create newlines at ends of files
abepolk Sep 15, 2018
ee919a4
fix missing ids change bidirectional binding use string property
Siedlerchr Sep 16, 2018
6eabd86
Fix default and help
abepolk Sep 18, 2018
3435997
Got the dialog running, but it is not saving
abepolk Sep 18, 2018
5133d44
Removed _def
abepolk Sep 20, 2018
4c6e309
fix bug
abepolk Sep 20, 2018
b726a81
Reformat customize general fields dialog as JavaFX
abepolk Sep 21, 2018
0d9a2a8
Merge branch 'master' into gen_javafx_dialog
abepolk Sep 21, 2018
78c8748
Small code fixes
abepolk Sep 21, 2018
857b9ea
make fxml support multiple languages
abepolk Sep 22, 2018
c5ce5b1
minor code fixes
abepolk Sep 23, 2018
4e9d78a
added javadoc
abepolk Sep 23, 2018
bf1c5d3
change class name and code fix
abepolk Sep 24, 2018
5bb9195
Add English localization
abepolk Sep 24, 2018
a22fcf0
remove old Swing dialog
abepolk Sep 24, 2018
7ae7a0e
comment fix
abepolk Sep 24, 2018
fb22955
English localization change
abepolk Sep 24, 2018
ae9ff0d
fix indentation
abepolk Sep 25, 2018
6cd1acd
remove cancelButton from controller
abepolk Sep 25, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
## [Unreleased]

### Changed
- We updated the dialog for setting up general fields.
- URL field formatting is updated. All whitespace chars, located at the beginning/ending of the url, are trimmed automatically
- We changed the behavior of the field formatting dialog such that the `bibtexkey` is not changed when formatting all fields or all text fields.
- We added a "Move file to file directory and rename file" option for simultaneously moving and renaming of document file. [#4166](https://github.com/JabRef/jabref/issues/4166)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ private MenuBar createMenu() {

new SeparatorMenuItem(),

factory.createMenuItem(StandardActions.SETUP_GENERAL_FIELDS, new SetupGeneralFieldsAction(this)),
factory.createMenuItem(StandardActions.SETUP_GENERAL_FIELDS, new SetupGeneralFieldsAction()),
Copy link
Member

Choose a reason for hiding this comment

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

Please align/indent it with the other ones below

factory.createMenuItem(StandardActions.MANAGE_CUSTOM_IMPORTS, new ManageCustomImportsAction(this)),
factory.createMenuItem(StandardActions.MANAGE_CUSTOM_EXPORTS, new ManageCustomExportsAction(this)),
factory.createMenuItem(StandardActions.MANAGE_EXTERNAL_FILETYPES, new EditExternalFileTypesAction()),
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/org/jabref/gui/actions/SetupGeneralFieldsAction.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
package org.jabref.gui.actions;

import org.jabref.gui.GenFieldsCustomizer;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.genfields.GenFieldsCustomizerDialogView;

public class SetupGeneralFieldsAction extends SimpleCommand {

private final JabRefFrame jabRefFrame;

public SetupGeneralFieldsAction(JabRefFrame jabRefFrame) {
this.jabRefFrame = jabRefFrame;
}

@Override
public void execute() {
GenFieldsCustomizer gf = new GenFieldsCustomizer(jabRefFrame);
gf.setVisible(true);
new GenFieldsCustomizerDialogView().show();

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ButtonType?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.VBox?>
<DialogPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="org.jabref.gui.genfields.GenFieldsCustomizerDialogView">
<content>
<VBox prefHeight="300.0" prefWidth="650.0">
<children>
<Label text="%General Fields" />
<Label text="%Create custom fields for each BibTeX entry" />
<TextArea fx:id="fieldsTextArea" minHeight="200.0" minWidth="628.0" />
<Label text="%Format: Tab:field;field;... (e.g. General:url;pdf;note...)" />
</children>
</VBox>
</content>
<ButtonType fx:id="okButton" fx:constant="OK" />
<ButtonType fx:id="resetButton" buttonData="LEFT" text="%Default" />
<ButtonType fx:id="helpButton" text="%Help" />
<ButtonType fx:constant="CANCEL" />
</DialogPane>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.jabref.gui.genfields;

import javax.inject.Inject;

import javafx.fxml.FXML;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TextArea;

import org.jabref.gui.DialogService;
import org.jabref.gui.help.HelpAction;
import org.jabref.gui.util.BaseDialog;
import org.jabref.gui.util.ControlHelper;
import org.jabref.logic.help.HelpFile;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.views.ViewLoader;

public class GenFieldsCustomizerDialogView extends BaseDialog<Void> {

@FXML private ButtonType resetButton;
@FXML private ButtonType helpButton;
@FXML private ButtonType okButton;
@FXML private ButtonType cancelButton;
@FXML private TextArea fieldsTextArea;

@Inject private DialogService dialogService;
@Inject private PreferencesService preferences;
private GenFieldsCustomizerDialogViewModel viewModel;

public GenFieldsCustomizerDialogView() {
Copy link
Member

Choose a reason for hiding this comment

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

Can you please rename all classes related to the dialog to something like CustomizeGeneralFieldsDialog

this.setTitle(Localization.lang("Set General Fields"));
this.setResizable(true);
this.getDialogPane().setPrefSize(300, 650);

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

HelpAction helpCommand = new HelpAction(HelpFile.GENERAL_FIELDS);
//HelpAction is written with Swing, not JavaFX, so runCommand() cannot be used normally. Here I am reaching into
//the command and running execute. When HelpAction is converted to JavaFX,
//the following will need to be changed.
ControlHelper.setAction(helpButton, getDialogPane(), event -> helpCommand.getCommand().execute());
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
ControlHelper.setAction(resetButton, getDialogPane(), event -> resetFields());
ControlHelper.setAction(okButton, getDialogPane(), event -> saveFieldsAndCloseDialog());

}

@FXML
private void initialize() {
viewModel = new GenFieldsCustomizerDialogViewModel(dialogService, preferences);
fieldsTextArea.textProperty().bindBidirectional(viewModel.fieldsTextProperty());

}

@FXML
private void closeDialog() {
close();
}

@FXML
private void saveFieldsAndCloseDialog() {
viewModel.saveFields();
closeDialog();
}

private void resetFields() {
viewModel.resetFields();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.jabref.gui.genfields;

import java.util.List;
import java.util.Map;

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

import org.jabref.gui.DialogService;
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;

public class GenFieldsCustomizerDialogViewModel {

private final DialogService dialogService;
private final PreferencesService preferences;
private final StringProperty fieldsText = new SimpleStringProperty("");

public GenFieldsCustomizerDialogViewModel(DialogService dialogService, PreferencesService preferences) {
this.dialogService = dialogService;
this.preferences = preferences;
setInitialFieldsText();
}

private void setInitialFieldsText() {
StringBuilder sb = new StringBuilder();

for (Map.Entry<String, List<String>> tab : preferences.getEntryEditorTabList().entrySet()) {
sb.append(tab.getKey());
sb.append(':');
sb.append(String.join(";", tab.getValue()));
sb.append('\n');
}

fieldsText.set(sb.toString());
}

public StringProperty fieldsTextProperty() {
return fieldsText;
}

public void saveFields() {
String[] lines = fieldsText.get().split("\n");
int i = 0;
for (; i < lines.length; i++) {
String[] parts = lines[i].split(":");
if (parts.length != 2) {
// Report error and exit.
String field = Localization.lang("field");
String title = Localization.lang("Error");
String content = Localization.lang("Each line must be of the following form") + " '" +
Localization.lang("Tabname") + ':' + field + "1;" + field + "2;...;" + field + "N'";
dialogService.showInformationDialogAndWait(title, content);
return;
}

String testString = BibtexKeyGenerator.cleanKey(parts[1], preferences.getEnforceLegalKeys());
if (!testString.equals(parts[1]) || (parts[1].indexOf('&') >= 0)) {
String title = Localization.lang("Error");
String content = Localization.lang("Field names are not allowed to contain white space or the following "
+ "characters")
+ ": # { } ~ , ^ &";
dialogService.showInformationDialogAndWait(title, content);
return;
}
preferences.setCustomTabsNameAndFields(parts[0], parts[1], i);

}
preferences.purgeSeries(JabRefPreferences.CUSTOM_TAB_NAME, i);
preferences.purgeSeries(JabRefPreferences.CUSTOM_TAB_FIELDS, i);
preferences.updateEntryEditorTabList();
}

public void resetFields() {

StringBuilder sb = new StringBuilder();
Map<String,String> customTabNamesFields = preferences.getCustomTabsNamesAndFields();
for (Map.Entry<String,String>entry : customTabNamesFields.entrySet()) {
sb.append(entry.getKey());
sb.append(':');
sb.append(entry.getValue());
sb.append('\n');
fieldsText.set(sb.toString());
Copy link
Member

Choose a reason for hiding this comment

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

The fieldsText.set should probably come after the for loop.

}
}
}
44 changes: 43 additions & 1 deletion src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,40 @@ public String getUser() {
}
}

/**
* Get a Map of default tab names to deafult tab fields.
* The fields are returned as a String with fields separated by ;
* They are combined into one string in order to feed into GenFieldsCustomizerDialogViewModel.resetFields()
* so that they do not have to be parsed in order to fit there
*
* @return A map of keys with tab names and values as a string containing
* fields for the given name separated by ;
*/
@Override
public Map<String, String> getCustomTabsNamesAndFields() {
Map<String, String> customTabsMap = new HashMap<>();

int defNumber = 0;
while (true) {
//Saved as CUSTOMTABNAME_def{number} and ; separated
String name = (String) defaults.get(CUSTOM_TAB_NAME + "_def" + defNumber);
tobiasdiez marked this conversation as resolved.
Show resolved Hide resolved
String fields = (String) defaults.get(CUSTOM_TAB_FIELDS + "_def" + defNumber);

if (StringUtil.isNullOrEmpty(name) || StringUtil.isNullOrEmpty(fields)) {
break;
}
customTabsMap.put(name, fields);
defNumber++;
}
return customTabsMap;
}

@Override
public void setCustomTabsNameAndFields(String name, String fields, int defNumber) {
prefs.put(CUSTOM_TAB_NAME + defNumber, name);
prefs.put(CUSTOM_TAB_FIELDS + defNumber, fields);
}

public List<String> getCustomTabFieldNames() {
List<String> customFields = new ArrayList<>();

Expand All @@ -943,7 +977,7 @@ public List<String> getCustomTabFieldNames() {
// saved as CUSTOMTABNAME_def{number} and ; separated
String fields = (String) defaults.get(CUSTOM_TAB_FIELDS + "_def" + defNumber);

if ((fields == null) || fields.isEmpty()) {
if (StringUtil.isNullOrEmpty(fields)) {
break;
}

Expand Down Expand Up @@ -1295,6 +1329,7 @@ private Object getObject(String key) {
*
* @param number or higher.
*/
@Override
public void purgeSeries(String prefix, int number) {
int n = number;
while (get(prefix + n) != null) {
Expand All @@ -1303,13 +1338,20 @@ public void purgeSeries(String prefix, int number) {
}
}

@Override
public Map<String, List<String>> getEntryEditorTabList() {
if (tabList == null) {
updateEntryEditorTabList();
}
return tabList;
}

@Override
public Boolean getEnforceLegalKeys() {
return getBoolean(ENFORCE_LEGAL_BIBTEX_KEY);
}

@Override
public void updateEntryEditorTabList() {
tabList = EntryEditorTabList.create(this);
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/jabref/preferences/PreferencesService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jabref.preferences;

import java.nio.file.Path;
import java.util.List;
import java.util.Map;

import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.logic.journals.JournalAbbreviationPreferences;
Expand All @@ -21,4 +23,16 @@ public interface PreferencesService {

void setWorkingDir(Path dir);

Map<String, List<String>> getEntryEditorTabList();

Boolean getEnforceLegalKeys();

Map<String, String> getCustomTabsNamesAndFields();

void setCustomTabsNameAndFields(String name, String fields, int defNumber);

public void purgeSeries(String prefix, int number);

public void updateEntryEditorTabList();

}