Skip to content

Commit

Permalink
Fix Divide by zero exception if rows is zero in Entry Editor Tab (#2873)
Browse files Browse the repository at this point in the history
* Fix Divide by zero exception if rows is zero

* Update EntryEditorTab.java
  • Loading branch information
Siedlerchr authored and tobiasdiez committed May 28, 2017
1 parent 4c87dde commit 51cd8ba
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditorTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ class EntryEditorTab {
private BibEntry entry;
private boolean updating;


public EntryEditorTab(JabRefFrame frame, BasePanel basePanel, List<String> fields, EntryEditor parent,
boolean addKeyField, boolean compressed, String tabTitle, BibEntry entry) {
boolean addKeyField, boolean compressed, String tabTitle, BibEntry entry) {
this.entry = Objects.requireNonNull(entry);
if (fields == null) {
this.fields = new ArrayList<>();
Expand All @@ -84,8 +83,7 @@ public EntryEditorTab(JabRefFrame frame, BasePanel basePanel, List<String> field
root.setStyle(
"text-area-background: " + convertToHex(GUIGlobals.validFieldBackgroundColor) + ";"
+ "text-area-foreground: " + convertToHex(GUIGlobals.editorTextColor) + ";"
+ "text-area-highlight: " + convertToHex(GUIGlobals.activeBackgroundColor) + ";"
);
+ "text-area-highlight: " + convertToHex(GUIGlobals.activeBackgroundColor) + ";");
}

root.getStylesheets().add("org/jabref/gui/entryeditor/EntryEditor.css");
Expand All @@ -110,7 +108,7 @@ private String convertToHex(java.awt.Color color) {
}

private Region setupPanel(JabRefFrame frame, BasePanel bPanel, boolean addKeyField,
boolean compressed, String title) {
boolean compressed, String title) {

setupKeyBindings(panel.getInputMap(JComponent.WHEN_FOCUSED), panel.getActionMap());

Expand Down Expand Up @@ -140,9 +138,9 @@ private Region setupPanel(JabRefFrame frame, BasePanel bPanel, boolean addKeyFie
//parent.addSearchListener((TextArea) fieldEditor);
defaultHeight = fieldEditor.getPane().getPreferredSize().height;
}
Optional<JComponent> extra = parent.getExtra(fieldEditor);
// Add autocompleter listener, if required for this field:
/*
AutoCompleter<String> autoCompleter = bPanel.getAutoCompleters().get(field);
Expand All @@ -154,7 +152,9 @@ private Region setupPanel(JabRefFrame frame, BasePanel bPanel, boolean addKeyFie
fieldEditor.setAutoCompleteListener(autoCompleteListener);
*/

FieldEditorFX fieldEditor = FieldEditors.getForField(fieldName, Globals.taskExecutor, new FXDialogService(), Globals.journalAbbreviationLoader, Globals.prefs.getJournalAbbreviationPreferences(), Globals.prefs, bPanel.getBibDatabaseContext(), entry.getType());
FieldEditorFX fieldEditor = FieldEditors.getForField(fieldName, Globals.taskExecutor, new FXDialogService(),
Globals.journalAbbreviationLoader, Globals.prefs.getJournalAbbreviationPreferences(), Globals.prefs,
bPanel.getBibDatabaseContext(), entry.getType());
editors.put(fieldName, fieldEditor);
/*
// TODO: Reenable this
Expand Down Expand Up @@ -204,7 +204,8 @@ private Region setupPanel(JabRefFrame frame, BasePanel bPanel, boolean addKeyFie
addColumn(gridPane, 1, editors.values().stream().map(FieldEditorFX::getNode).limit(rows));
addColumn(gridPane, 4, editors.values().stream().map(FieldEditorFX::getNode).skip(rows));

gridPane.getColumnConstraints().addAll(columnDoNotContract, columnExpand, new ColumnConstraints(10), columnDoNotContract, columnExpand);
gridPane.getColumnConstraints().addAll(columnDoNotContract, columnExpand, new ColumnConstraints(10),
columnDoNotContract, columnExpand);
} else {
rows = fields.size();

Expand All @@ -216,7 +217,11 @@ private Region setupPanel(JabRefFrame frame, BasePanel bPanel, boolean addKeyFie

RowConstraints rowExpand = new RowConstraints();
rowExpand.setVgrow(Priority.ALWAYS);
rowExpand.setPercentHeight(100 / rows);
if (rows == 0) {
rowExpand.setPercentHeight(100);
} else {
rowExpand.setPercentHeight(100 / rows);
}
for (int i = 0; i < rows; i++) {
gridPane.getRowConstraints().add(rowExpand);
}
Expand All @@ -236,12 +241,12 @@ private String getPrompt(String field) {
}

switch (field) {
case FieldName.YEAR:
return "YYYY";
case FieldName.MONTH:
return "MM or #mmm#";
case FieldName.URL:
return "https://";
case FieldName.YEAR:
return "YYYY";
case FieldName.MONTH:
return "MM or #mmm#";
case FieldName.URL:
return "https://";
}

return "";
Expand Down Expand Up @@ -290,7 +295,7 @@ public boolean updateField(String field, String content) {
if (fieldEditor.getText().equals(content)) {
return true;
}
// trying to preserve current edit position (fixes SF bug #1285)
if (fieldEditor.getTextComponent() instanceof JTextComponent) {
int initialCaretPosition = ((JTextComponent) fieldEditor).getCaretPosition();
Expand Down

0 comments on commit 51cd8ba

Please sign in to comment.