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

Fix options databinding by adding elements if they are not in the initial map #3539

Merged
merged 6 commits into from
Dec 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -63,6 +63,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We fixed an issue where dropping a pdf on the entry table and renaming it triggered an exception [#3490](https://github.com/JabRef/jabref/issues/3490)
- We fixed an issue where integrating external changes to a bib file caused instability [#3498](https://github.com/JabRef/jabref/issues/3498)
- We fixed an issue where fetched entries from the ACM fetcher could not be imported [#3500](https://github.com/JabRef/jabref/issues/3500)
- We fixed an issue where custom data in combobox fields in the entry editor was not saved [#3538](https://github.com/JabRef/jabref/issues/3538)

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
import org.jabref.logic.integrity.FieldCheckers;

import com.google.common.collect.BiMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* View model for a field editor that shows various options backed by a map.
*/
public abstract class MapBasedEditorViewModel<T> extends OptionEditorViewModel<T> {

private static final Log LOGGER = LogFactory.getLog(MapBasedEditorViewModel.class);

public MapBasedEditorViewModel(String fieldName, AutoCompleteSuggestionProvider<?> suggestionProvider, FieldCheckers fieldCheckers) {
super(fieldName, suggestionProvider, fieldCheckers);
}
Expand All @@ -24,12 +28,13 @@ public MapBasedEditorViewModel(String fieldName, AutoCompleteSuggestionProvider<
@Override
public StringConverter<T> getStringConverter() {
return new StringConverter<T>() {

@Override
public String toString(T object) {
if (object == null) {
return null;
} else {
return getItemMap().inverse().getOrDefault(object, "");
return getItemMap().inverse().getOrDefault(object, object.toString()); //if the object is not found we simply return itself as string
}
}

Expand All @@ -38,12 +43,28 @@ public T fromString(String string) {
if (string == null) {
return null;
} else {
return getItemMap().getOrDefault(string, null);
return getItemMap().getOrDefault(string, getValueFromString(string));
}
}
};
}

/**
* Converts a String value to the Type T. If the type cannot be directly casted to T, this method must be overriden in a subclass
* @param string The input value to convert
* @return The value or null if the value could not be casted
*/
@SuppressWarnings("unchecked")
protected T getValueFromString(String string) {
try {
return (T) string;
} catch (ClassCastException ex) {
LOGGER.error(String.format("Could not cast string to type %1$s. Try overriding the method in a subclass and provide a conversion from string to the concrete type %1$s", string.getClass()), ex);
}
return null;

}

@Override
public List<T> getItems() {
return new ArrayList<>(getItemMap().values());
Expand Down