Skip to content

Commit

Permalink
Replace getField with getFieldOptional in all of the tests and in som… (
Browse files Browse the repository at this point in the history
#1591)

* Replace getField with getFieldOptional in all of the tests and in some more code

* Some more conversions
  • Loading branch information
oscargus authored Jul 20, 2016
1 parent d66e3eb commit 82b8f6e
Show file tree
Hide file tree
Showing 53 changed files with 536 additions and 495 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/external/AttachFileAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void action() {
String newVal = model.getStringRepresentation();

UndoableFieldChange ce = new UndoableFieldChange(entry, Globals.FILE_FIELD,
entry.getField(Globals.FILE_FIELD), newVal);
entry.getFieldOptional(Globals.FILE_FIELD).orElse(null), newVal);
entry.setField(Globals.FILE_FIELD, newVal);
panel.getUndoManager().addEdit(ce);
panel.markBaseChanged();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/sf/jabref/external/FindFullTextAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ public void update() {
try {
def.download(result.get(), file -> {
FileListTableModel tm = new FileListTableModel();
String oldValue = entry.getField(Globals.FILE_FIELD);
tm.setContent(oldValue);
entry.getFieldOptional(Globals.FILE_FIELD).ifPresent(tm::setContent);
tm.addEntry(tm.getRowCount(), file);
String newValue = tm.getStringRepresentation();
UndoableFieldChange edit = new UndoableFieldChange(entry, Globals.FILE_FIELD, oldValue, newValue);
UndoableFieldChange edit = new UndoableFieldChange(entry, Globals.FILE_FIELD,
entry.getFieldOptional(Globals.FILE_FIELD).orElse(null), newValue);
entry.setField(Globals.FILE_FIELD, newValue);
basePanel.getUndoManager().addEdit(edit);
basePanel.markBaseChanged();
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/net/sf/jabref/external/WriteXMPAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,14 @@ public void run() {
List<File> files = new ArrayList<>();

// First check the (legacy) "pdf" field:
String pdf = entry.getField("pdf");
List<String> dirs = panel.getBibDatabaseContext().getFileDirectory("pdf");
FileUtil.expandFilename(pdf, dirs).ifPresent(files::add);

entry.getFieldOptional("pdf").ifPresent(pdf ->
FileUtil.expandFilename(pdf, panel.getBibDatabaseContext().getFileDirectory("pdf"))
.ifPresent(files::add));
// Then check the "file" field:
dirs = panel.getBibDatabaseContext().getFileDirectory();
List<String> dirs = panel.getBibDatabaseContext().getFileDirectory();
if (entry.hasField(Globals.FILE_FIELD)) {
FileListTableModel tm = new FileListTableModel();
tm.setContent(entry.getField(Globals.FILE_FIELD));
entry.getFieldOptional(Globals.FILE_FIELD).ifPresent(tm::setContent);
for (int j = 0; j < tm.getRowCount(); j++) {
FileListEntry flEntry = tm.getEntry(j);
if ((flEntry.type.isPresent()) && "pdf".equalsIgnoreCase(flEntry.type.get().getName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,14 @@ public void actionPerformed(ActionEvent actionEvent) {
List<File> files = new ArrayList<>();

// First check the (legacy) "pdf" field:
String pdf = entry.getField("pdf");
List<String> dirs = panel.getBibDatabaseContext().getFileDirectory("pdf");
FileUtil.expandFilename(pdf, dirs).ifPresent(files::add);
entry.getFieldOptional("pdf").ifPresent(pdf -> FileUtil
.expandFilename(pdf, panel.getBibDatabaseContext().getFileDirectory("pdf")).ifPresent(files::add));

// Then check the "file" field:
dirs = panel.getBibDatabaseContext().getFileDirectory();
List<String> dirs = panel.getBibDatabaseContext().getFileDirectory();
if (entry.hasField(Globals.FILE_FIELD)) {
FileListTableModel tm = new FileListTableModel();
tm.setContent(entry.getField(Globals.FILE_FIELD));
entry.getFieldOptional(Globals.FILE_FIELD).ifPresent(tm::setContent);
for (int j = 0; j < tm.getRowCount(); j++) {
FileListEntry flEntry = tm.getEntry(j);
if ((flEntry.type.isPresent()) && "pdf".equalsIgnoreCase(flEntry.type.get().getName())) {
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ private void openExternalFile() {
return;
}
FileListTableModel tableModel = new FileListTableModel();
tableModel.setContent(entry.getField(Globals.FILE_FIELD));
entry.getFieldOptional(Globals.FILE_FIELD).ifPresent(tableModel::setContent);
if (tableModel.getRowCount() == 0) {
// content in bibtex field is not readable
new SearchAndOpenFile(entry, BasePanel.this).searchAndOpen();
Expand Down Expand Up @@ -2064,19 +2064,26 @@ private class OpenURLAction implements BaseAction {
@Override
public void action() {
final List<BibEntry> bes = mainTable.getSelectedEntries();
String field = DOI_FIELD;
if (bes.size() == 1) {
Object link = bes.get(0).getField(DOI_FIELD);
String field = DOI_FIELD;
Optional<String> link = bes.get(0).getFieldOptional(DOI_FIELD);
if (bes.get(0).hasField(URL_FIELD)) {
link = bes.get(0).getField(URL_FIELD);
link = bes.get(0).getFieldOptional(URL_FIELD);
field = URL_FIELD;
}
if (link == null) {
if (link.isPresent()) {
try {
JabRefDesktop.openExternalViewer(bibDatabaseContext, link.get(), field);
output(Localization.lang("External viewer called") + '.');
} catch (IOException ex) {
output(Localization.lang("Error") + ": " + ex.getMessage());
}
} else {
// No URL or DOI found in the "url" and "doi" fields.
// Look for web links in the "file" field as a fallback:
FileListEntry entry = null;
FileListTableModel tm = new FileListTableModel();
tm.setContent(bes.get(0).getField(Globals.FILE_FIELD));
bes.get(0).getFieldOptional(Globals.FILE_FIELD).ifPresent(tm::setContent);
for (int i = 0; i < tm.getRowCount(); i++) {
FileListEntry flEntry = tm.getEntry(i);
if (URL_FIELD.equalsIgnoreCase(flEntry.type.get().getName())
Expand All @@ -2097,13 +2104,6 @@ public void action() {
LOGGER.info("Could not open link", e);
}
}
} else {
try {
JabRefDesktop.openExternalViewer(bibDatabaseContext, link.toString(), field);
output(Localization.lang("External viewer called") + '.');
} catch (IOException ex) {
output(Localization.lang("Error") + ": " + ex.getMessage());
}
}
} else {
output(Localization.lang("This operation requires exactly one item to be selected."));
Expand Down
40 changes: 22 additions & 18 deletions src/main/java/net/sf/jabref/gui/EntryMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ public static void markEntry(BibEntry be, int markIncrement, boolean increment,
int prevMarkLevel;
String newValue = null;
if (be.hasField(InternalBibtexFields.MARKED)) {
String s = be.getField(InternalBibtexFields.MARKED);
int index = s.indexOf(Globals.prefs.WRAPPED_USERNAME);
String markerString = be.getFieldOptional(InternalBibtexFields.MARKED).get();
int index = markerString.indexOf(Globals.prefs.WRAPPED_USERNAME);
if (index >= 0) {
// Already marked 1 for this user.
prevMarkLevel = 1;
newValue = s.substring(0, index) + s.substring(index + Globals.prefs.WRAPPED_USERNAME.length()) + Globals.prefs.WRAPPED_USERNAME.substring(0, Globals.prefs.WRAPPED_USERNAME.length() - 1) + ":" + (increment ? Math.min(MAX_MARKING_LEVEL, prevMarkLevel + markIncrement) : markIncrement) + "]";
newValue = markerString.substring(0, index) + markerString.substring(index + Globals.prefs.WRAPPED_USERNAME.length()) + Globals.prefs.WRAPPED_USERNAME.substring(0, Globals.prefs.WRAPPED_USERNAME.length() - 1) + ":" + (increment ? Math.min(MAX_MARKING_LEVEL, prevMarkLevel + markIncrement) : markIncrement) + "]";
} else {
Matcher m = MARK_NUMBER_PATTERN.matcher(s);
Matcher m = MARK_NUMBER_PATTERN.matcher(markerString);
if (m.find()) {
try {
prevMarkLevel = Integer.parseInt(m.group(1));
newValue = s.substring(0, m.start(1)) + (increment ? Math.min(MAX_MARKING_LEVEL, prevMarkLevel + markIncrement) : markIncrement) + s.substring(m.end(1));
newValue = markerString.substring(0, m.start(1)) + (increment ? Math.min(MAX_MARKING_LEVEL, prevMarkLevel + markIncrement) : markIncrement) + markerString.substring(m.end(1));
} catch (NumberFormatException ex) {
// Do nothing.
}
Expand All @@ -66,7 +66,8 @@ public static void markEntry(BibEntry be, int markIncrement, boolean increment,
newValue = Globals.prefs.WRAPPED_USERNAME.substring(0, Globals.prefs.WRAPPED_USERNAME.length() - 1) + ":" + markIncrement + "]";
}

ce.addEdit(new UndoableFieldChange(be, InternalBibtexFields.MARKED, be.getField(InternalBibtexFields.MARKED), newValue));
ce.addEdit(new UndoableFieldChange(be, InternalBibtexFields.MARKED,
be.getFieldOptional(InternalBibtexFields.MARKED).orElse(null), newValue));
be.setField(InternalBibtexFields.MARKED, newValue);
}

Expand All @@ -75,35 +76,35 @@ public static void markEntry(BibEntry be, int markIncrement, boolean increment,
*/
public static void unmarkEntry(BibEntry be, boolean onlyMaxLevel, BibDatabase database, NamedCompound ce) {
if (be.hasField(InternalBibtexFields.MARKED)) {
String s = be.getField(InternalBibtexFields.MARKED);
if ("0".equals(s)) {
String markerString = be.getFieldOptional(InternalBibtexFields.MARKED).get();
if ("0".equals(markerString)) {
if (!onlyMaxLevel) {
unmarkOldStyle(be, database, ce);
}
return;
}
String newValue = null;
int index = s.indexOf(Globals.prefs.WRAPPED_USERNAME);
int index = markerString.indexOf(Globals.prefs.WRAPPED_USERNAME);
if (index >= 0) {
// Marked 1 for this user.
if (onlyMaxLevel) {
return;
} else {
newValue = s.substring(0, index) + s.substring(index + Globals.prefs.WRAPPED_USERNAME.length());
newValue = markerString.substring(0, index) + markerString.substring(index + Globals.prefs.WRAPPED_USERNAME.length());
}
} else {
Matcher m = MARK_NUMBER_PATTERN.matcher(s);
Matcher m = MARK_NUMBER_PATTERN.matcher(markerString);
if (m.find()) {
try {
int prevMarkLevel = Integer.parseInt(m.group(1));
if (!onlyMaxLevel || (prevMarkLevel == MARK_COLOR_LEVELS)) {
if (prevMarkLevel > 1) {
newValue = s.substring(0, m.start(1)) + s.substring(m.end(1));
newValue = markerString.substring(0, m.start(1)) + markerString.substring(m.end(1));
} else {
String toRemove = Globals.prefs.WRAPPED_USERNAME.substring(0, Globals.prefs.WRAPPED_USERNAME.length() - 1) + ":1]";
index = s.indexOf(toRemove);
index = markerString.indexOf(toRemove);
if (index >= 0) {
newValue = s.substring(0, index) + s.substring(index + toRemove.length());
newValue = markerString.substring(0, index) + markerString.substring(index + toRemove.length());
}
}
} else {
Expand All @@ -127,7 +128,8 @@ public static void unmarkEntry(BibEntry be, boolean onlyMaxLevel, BibDatabase da
sb.append(s.substring(piv));
}
String newVal = sb.length() > 0 ? sb.toString() : null;*/
ce.addEdit(new UndoableFieldChange(be, InternalBibtexFields.MARKED, be.getField(InternalBibtexFields.MARKED), newValue));
ce.addEdit(new UndoableFieldChange(be, InternalBibtexFields.MARKED,
be.getFieldOptional(InternalBibtexFields.MARKED).get(), newValue));
if (newValue == null) {
be.clearField(InternalBibtexFields.MARKED);
} else {
Expand Down Expand Up @@ -161,10 +163,12 @@ private static void unmarkOldStyle(BibEntry be, BibDatabase database, NamedCompo
}
String newVal = sb.toString();
if (newVal.isEmpty()) {
ce.addEdit(new UndoableFieldChange(be, InternalBibtexFields.MARKED, be.getField(InternalBibtexFields.MARKED), null));
ce.addEdit(new UndoableFieldChange(be, InternalBibtexFields.MARKED,
be.getFieldOptional(InternalBibtexFields.MARKED).orElse(null), null));
be.clearField(InternalBibtexFields.MARKED);
} else {
ce.addEdit(new UndoableFieldChange(be, InternalBibtexFields.MARKED, be.getField(InternalBibtexFields.MARKED), newVal));
ce.addEdit(new UndoableFieldChange(be, InternalBibtexFields.MARKED,
be.getFieldOptional(InternalBibtexFields.MARKED).orElse(null), newVal));
be.setField(InternalBibtexFields.MARKED, newVal);
}
}
Expand All @@ -173,7 +177,7 @@ public static int isMarked(BibEntry be) {
if (!be.hasField(InternalBibtexFields.MARKED)) {
return 0;
}
String s = be.getField(InternalBibtexFields.MARKED);
String s = be.getFieldOptional(InternalBibtexFields.MARKED).get();
if ("0".equals(s)) {
return 1;
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/sf/jabref/gui/ImportInspectionDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ public void mouseClicked(MouseEvent e) {
if (col == FILE_COL) {
if (entry.hasField(Globals.FILE_FIELD)) {
FileListTableModel tableModel = new FileListTableModel();
tableModel.setContent(entry.getField(Globals.FILE_FIELD));
entry.getFieldOptional(Globals.FILE_FIELD).ifPresent(tableModel::setContent);
if (tableModel.getRowCount() == 0) {
return;
}
Expand Down Expand Up @@ -1129,7 +1129,7 @@ public void actionPerformed(ActionEvent event) {
}
BibEntry entry = selectionModel.getSelected().get(0);
String result = JOptionPane.showInputDialog(ImportInspectionDialog.this, Localization.lang("Enter URL"),
entry.getField(URL_FIELD));
entry.getFieldOptional(URL_FIELD).orElse(""));
entries.getReadWriteLock().writeLock().lock();
try {
if (result != null) {
Expand Down Expand Up @@ -1425,7 +1425,7 @@ public Object getColumnValue(BibEntry entry, int i) {
case FILE_COL:
if (entry.hasField(Globals.FILE_FIELD)) {
FileListTableModel model = new FileListTableModel();
model.setContent(entry.getField(Globals.FILE_FIELD));
entry.getFieldOptional(Globals.FILE_FIELD).ifPresent(model::setContent);
fileLabel.setToolTipText(model.getToolTipHTMLRepresentation());
if (model.getRowCount() > 0) {
fileLabel.setIcon(model.getEntry(0).type.get().getIcon());
Expand All @@ -1436,7 +1436,7 @@ public Object getColumnValue(BibEntry entry, int i) {
}
case URL_COL:
if (entry.hasField(URL_FIELD)) {
urlLabel.setToolTipText(entry.getField(URL_FIELD));
urlLabel.setToolTipText(entry.getFieldOptional(URL_FIELD).orElse(""));
return urlLabel;
} else {
return null;
Expand All @@ -1449,7 +1449,7 @@ public Object getColumnValue(BibEntry entry, int i) {
if (InternalBibtexFields.getFieldExtras(field).contains(FieldProperties.PERSON_NAMES)) {
return entry.getFieldOptional(field).map(AuthorList::fixAuthorNatbib).orElse("");
} else {
return entry.getField(field);
return entry.getFieldOptional(field).orElse(null);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/gui/ReplaceStringDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private int replaceField(BibEntry be, String fieldname, NamedCompound ce) {
if (!be.hasField(fieldname)) {
return 0;
}
String txt = be.getField(fieldname);
String txt = be.getFieldOptional(fieldname).get();
StringBuilder sb = new StringBuilder();
int ind;
int piv = 0;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/sf/jabref/gui/desktop/JabRefDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ public static boolean openExternalFileUnknown(JabRefFrame frame, BibEntry entry,
// User wants to change the type of this link.
// First get a model of all file links for this entry:
FileListTableModel tModel = new FileListTableModel();
String oldValue = entry.getField(Globals.FILE_FIELD);
tModel.setContent(oldValue);
Optional<String> oldValue = entry.getFieldOptional(Globals.FILE_FIELD);
oldValue.ifPresent(tModel::setContent);
FileListEntry flEntry = null;
// Then find which one we are looking at:
for (int i = 0; i < tModel.getRowCount(); i++) {
Expand All @@ -249,7 +249,8 @@ public static boolean openExternalFileUnknown(JabRefFrame frame, BibEntry entry,
if (editor.okPressed()) {
// Store the changes and add an undo edit:
String newValue = tModel.getStringRepresentation();
UndoableFieldChange ce = new UndoableFieldChange(entry, Globals.FILE_FIELD, oldValue, newValue);
UndoableFieldChange ce = new UndoableFieldChange(entry, Globals.FILE_FIELD, oldValue.orElse(null),
newValue);
entry.setField(Globals.FILE_FIELD, newValue);
frame.getCurrentBasePanel().getUndoManager().addEdit(ce);
frame.getCurrentBasePanel().markBaseChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

Expand Down Expand Up @@ -225,8 +224,7 @@ private boolean isFieldModified(FieldEditor fieldEditor) {
if (text.isEmpty()) {
return getEntry().hasField(fieldEditor.getFieldName());
} else {
String entryValue = getEntry().getField(fieldEditor.getFieldName());
return !Objects.equals(entryValue, text);
return !Optional.of(text).equals(getEntry().getFieldOptional(fieldEditor.getFieldName()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public boolean abbreviate(BibDatabase database, BibEntry entry, String fieldName
if (!entry.hasField(fieldName)) {
return false;
}
String text = entry.getField(fieldName);
String text = entry.getFieldOptional(fieldName).get();
String origText = text;
if (database != null) {
text = database.resolveForStrings(text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public boolean unabbreviate(BibDatabase database, BibEntry entry, String fieldNa
if (!entry.hasField(fieldName)) {
return false;
}
String text = entry.getField(fieldName);
String text = entry.getFieldOptional(fieldName).get();
String origText = text;
if (database != null) {
text = database.resolveForStrings(text);
Expand Down
Loading

0 comments on commit 82b8f6e

Please sign in to comment.