/
FieldsEditorTab.java
225 lines (189 loc) · 8.39 KB
/
FieldsEditorTab.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package org.jabref.gui.entryeditor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.RowConstraints;
import org.jabref.Globals;
import org.jabref.gui.FXDialogService;
import org.jabref.gui.GUIGlobals;
import org.jabref.gui.autocompleter.SuggestionProviders;
import org.jabref.gui.fieldeditors.FieldEditorFX;
import org.jabref.gui.fieldeditors.FieldEditors;
import org.jabref.gui.fieldeditors.FieldNameLabel;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.EntryTypes;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.EntryType;
import org.jabref.model.entry.FieldName;
import org.jabref.model.entry.FieldProperty;
import org.jabref.model.entry.InternalBibtexFields;
/**
* A single tab displayed in the EntryEditor holding several FieldEditors.
*/
abstract class FieldsEditorTab extends EntryEditorTab {
private final Map<String, FieldEditorFX> editors = new LinkedHashMap<>();
private final boolean isCompressed;
private final SuggestionProviders suggestionProviders;
private FieldEditorFX activeField;
private final BibDatabaseContext databaseContext;
public FieldsEditorTab(boolean compressed, BibDatabaseContext databaseContext, SuggestionProviders suggestionProviders) {
this.isCompressed = compressed;
this.databaseContext = databaseContext;
this.suggestionProviders = suggestionProviders;
}
private static void addColumn(GridPane gridPane, int columnIndex, List<Label> nodes) {
gridPane.addColumn(columnIndex, nodes.toArray(new Node[nodes.size()]));
}
private static void addColumn(GridPane gridPane, int columnIndex, Stream<Parent> nodes) {
gridPane.addColumn(columnIndex, nodes.toArray(Node[]::new));
}
private String convertToHex(java.awt.Color color) {
return String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue());
}
private Region setupPanel(BibEntry entry, boolean compressed, SuggestionProviders suggestionProviders) {
editors.clear();
List<Label> labels = new ArrayList<>();
EntryType entryType = EntryTypes.getTypeOrDefault(entry.getType(), databaseContext.getMode());
Collection<String> fields = determineFieldsToShow(entry, entryType);
for (String fieldName : fields) {
FieldEditorFX fieldEditor = FieldEditors.getForField(fieldName, Globals.TASK_EXECUTOR, new FXDialogService(),
Globals.journalAbbreviationLoader, Globals.prefs.getJournalAbbreviationPreferences(), Globals.prefs,
databaseContext, entry.getType(),
suggestionProviders);
fieldEditor.bindToEntry(entry);
editors.put(fieldName, fieldEditor);
/*
// TODO: Reenable this
if (i == 0) {
activeField = fieldEditor;
}
*/
labels.add(new FieldNameLabel(fieldName));
}
GridPane gridPane = new GridPane();
gridPane.getStyleClass().add("editorPane");
ColumnConstraints columnExpand = new ColumnConstraints();
columnExpand.setHgrow(Priority.ALWAYS);
ColumnConstraints columnDoNotContract = new ColumnConstraints();
columnDoNotContract.setMinWidth(Region.USE_PREF_SIZE);
int rows;
if (compressed) {
rows = (int) Math.ceil((double) fields.size() / 2);
addColumn(gridPane, 0, labels.subList(0, rows));
addColumn(gridPane, 3, labels.subList(rows, labels.size()));
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);
setCompressedRowLayout(gridPane, rows);
} else {
rows = fields.size();
addColumn(gridPane, 0, labels);
addColumn(gridPane, 1, editors.values().stream().map(FieldEditorFX::getNode));
gridPane.getColumnConstraints().addAll(columnDoNotContract, columnExpand);
setRegularRowLayout(gridPane, fields, rows);
}
if (GUIGlobals.currentFont != null) {
gridPane.setStyle(
"text-area-background: " + convertToHex(GUIGlobals.validFieldBackgroundColor) + ";"
+ "text-area-foreground: " + convertToHex(GUIGlobals.editorTextColor) + ";"
+ "text-area-highlight: " + convertToHex(GUIGlobals.activeBackgroundColor) + ";");
}
gridPane.getStylesheets().add("org/jabref/gui/entryeditor/EntryEditor.css");
// Warp everything in a scroll-pane
ScrollPane scrollPane = new ScrollPane();
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
scrollPane.setContent(gridPane);
scrollPane.setFitToWidth(true);
scrollPane.setFitToHeight(true);
return scrollPane;
}
private void setRegularRowLayout(GridPane gridPane, Collection<String> fields, int rows) {
List<RowConstraints> constraints = new ArrayList<>(rows);
for (String field : fields) {
RowConstraints rowExpand = new RowConstraints();
rowExpand.setVgrow(Priority.ALWAYS);
rowExpand.setValignment(VPos.TOP);
if (rows == 0) {
rowExpand.setPercentHeight(100);
} else {
rowExpand.setPercentHeight(100 / rows * editors.get(field).getWeight());
}
constraints.add(rowExpand);
}
gridPane.getRowConstraints().addAll(constraints);
}
private void setCompressedRowLayout(GridPane gridPane, int rows) {
RowConstraints rowExpand = new RowConstraints();
rowExpand.setVgrow(Priority.ALWAYS);
rowExpand.setValignment(VPos.TOP);
if (rows == 0) {
rowExpand.setPercentHeight(100);
} else {
rowExpand.setPercentHeight(100 / rows);
}
for (int i = 0; i < rows; i++) {
gridPane.getRowConstraints().add(rowExpand);
}
}
private String getPrompt(String field) {
Set<FieldProperty> fieldProperties = InternalBibtexFields.getFieldProperties(field);
if (fieldProperties.contains(FieldProperty.PERSON_NAMES)) {
return String.format("%1$s and %1$s and others", Localization.lang("Firstname Lastname"));
} else if (fieldProperties.contains(FieldProperty.DOI)) {
return "10.ORGANISATION/ID";
} else if (fieldProperties.contains(FieldProperty.DATE)) {
return "YYYY-MM-DD";
}
switch (field) {
case FieldName.YEAR:
return "YYYY";
case FieldName.MONTH:
return "MM or #mmm#";
case FieldName.URL:
return "https://";
}
return "";
}
/**
* Focuses the given field.
*/
public void requestFocus(String fieldName) {
if (editors.containsKey(fieldName)) {
activeField = editors.get(fieldName);
activeField.requestFocus();
}
}
@Override
public boolean shouldShow(BibEntry entry) {
EntryType entryType = EntryTypes.getTypeOrDefault(entry.getType(), databaseContext.getMode());
return !determineFieldsToShow(entry, entryType).isEmpty();
}
@Override
public void handleFocus() {
if (activeField != null) {
activeField.requestFocus();
}
}
@Override
protected void bindToEntry(BibEntry entry) {
Region panel = setupPanel(entry, isCompressed, suggestionProviders);
setContent(panel);
}
protected abstract Collection<String> determineFieldsToShow(BibEntry entry, EntryType entryType);
}