Skip to content

Commit

Permalink
Reimplement content selectors (#3003)
Browse files Browse the repository at this point in the history
* Fix #2897: readd content selectors

* Add content selectors to bib file

* Fix order

* Update localization
  • Loading branch information
tobiasdiez authored Jul 13, 2017
1 parent aa0e98f commit 983ea91
Show file tree
Hide file tree
Showing 29 changed files with 102 additions and 363 deletions.
9 changes: 0 additions & 9 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -1996,15 +1996,6 @@ private String formatOutputMessage(String start, int count) {
(count > 1 ? Localization.lang("entries") : Localization.lang("entry")));
}

/**
* This method iterates through all existing entry editors in this BasePanel, telling each to update all its
* instances of FieldContentSelector. This is done to ensure that the list of words in each selector is up-to-date
* after the user has made changes in the Manage dialog.
*/
public void updateAllContentSelectors() {
currentEditor.updateAllContentSelectors();
}

/**
* Set the preview active state for all BasePanel instances.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,26 @@
*/
public class AutoCompletionTextInputBinding<T> extends AutoCompletionBinding<T> {

private final ChangeListener<Boolean> focusChangedListener = (obs, oldFocused, newFocused) -> {
if (!newFocused) {
hidePopup();
}
};
/**
* String converter to be used to convert suggestions to strings.
*/
private StringConverter<T> converter;
private AutoCompletionStrategy inputAnalyzer;
private final ChangeListener<String> textChangeListener = (obs, oldText, newText) -> {
if (getCompletionTarget().isFocused()) {
AutoCompletionInput input = inputAnalyzer.analyze(newText);
setUserInput(input.getUnfinishedPart());
setUserInputText(newText);
}
};
private boolean showOnFocus;
private final ChangeListener<Boolean> focusChangedListener = (obs, oldFocused, newFocused) -> {
if (newFocused) {
if (showOnFocus) {
setUserInputText(getCompletionTarget().getText());
}
} else {
hidePopup();
}
};


/**
* Creates a new auto-completion binding between the given textInputControl
Expand Down Expand Up @@ -117,12 +120,20 @@ public static <T> void autoComplete(TextInputControl textArea, Callback<ISuggest
new AutoCompletionTextInputBinding<>(textArea, suggestionProvider, converter);
}

public static <T> void autoComplete(TextInputControl textArea, Callback<ISuggestionRequest, Collection<T>> suggestionProvider, StringConverter<T> converter, AutoCompletionStrategy inputAnalyzer) {
new AutoCompletionTextInputBinding<>(textArea, suggestionProvider, converter, inputAnalyzer);
public static <T> AutoCompletionTextInputBinding<T> autoComplete(TextInputControl textArea, Callback<ISuggestionRequest, Collection<T>> suggestionProvider, StringConverter<T> converter, AutoCompletionStrategy inputAnalyzer) {
return new AutoCompletionTextInputBinding<>(textArea, suggestionProvider, converter, inputAnalyzer);
}

public static <T> AutoCompletionTextInputBinding<T> autoComplete(TextInputControl textArea, Callback<ISuggestionRequest, Collection<T>> suggestionProvider, AutoCompletionStrategy inputAnalyzer) {
return autoComplete(textArea, suggestionProvider, AutoCompletionTextInputBinding.defaultStringConverter(), inputAnalyzer);
}

public static <T> void autoComplete(TextInputControl textArea, Callback<ISuggestionRequest, Collection<T>> suggestionProvider, AutoCompletionStrategy inputAnalyzer) {
autoComplete(textArea, suggestionProvider, AutoCompletionTextInputBinding.defaultStringConverter(), inputAnalyzer);
private void setUserInputText(String newText) {
if (newText == null) {
newText = "";
}
AutoCompletionInput input = inputAnalyzer.analyze(newText);
setUserInput(input.getUnfinishedPart());
}

@Override
Expand All @@ -139,9 +150,17 @@ public void dispose() {
@Override
protected void completeUserInput(T completion) {
String completionText = converter.toString(completion);
AutoCompletionInput input = inputAnalyzer.analyze(getCompletionTarget().getText());
String inputText = getCompletionTarget().getText();
if (inputText == null) {
inputText = "";
}
AutoCompletionInput input = inputAnalyzer.analyze(inputText);
String newText = input.getPrefix() + completionText;
getCompletionTarget().setText(newText);
getCompletionTarget().positionCaret(newText.length());
}

public void setShowOnFocus(boolean showOnFocus) {
this.showOnFocus = showOnFocus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import java.util.Collection;
import java.util.List;

import javafx.util.Callback;
import org.jabref.model.entry.BibEntry;

import org.controlsfx.control.textfield.AutoCompletionBinding;

/**
* Enriches a suggestion provider by a given set of content selector values.
*/
public class ContentSelectorSuggestionProvider implements Callback<AutoCompletionBinding.ISuggestionRequest, Collection<String>> {
public class ContentSelectorSuggestionProvider implements AutoCompleteSuggestionProvider<String> {

private final AutoCompleteSuggestionProvider<String> suggestionProvider;
private final List<String> contentSelectorValues;
Expand All @@ -26,8 +26,15 @@ public ContentSelectorSuggestionProvider(AutoCompleteSuggestionProvider<String>
@Override
public Collection<String> call(AutoCompletionBinding.ISuggestionRequest request) {
List<String> suggestions = new ArrayList<>();
suggestions.addAll(suggestionProvider.call(request));
if (suggestionProvider != null) {
suggestions.addAll(suggestionProvider.call(request));
}
suggestions.addAll(contentSelectorValues);
return suggestions;
}

@Override
public void indexEntry(BibEntry entry) {
suggestionProvider.indexEntry(entry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public class ContentSelectorDialog extends JabRefDialog {
private final JTextField fieldNameField = new JTextField("", 20);
private final JTextField wordEditField = new JTextField("", 20);
private final JScrollPane fPane = new JScrollPane(fieldList);
private DefaultListModel<String> wordListModel = new DefaultListModel<>();
private final Map<String, DefaultListModel<String>> wordListModels = new HashMap<>();
private final JList<String> wordList = new JList<>(wordListModel);
private final JScrollPane wPane = new JScrollPane(wordList);
private final List<String> removedFields = new ArrayList<>();
private DefaultListModel<String> wordListModel = new DefaultListModel<>();
private JList<String> wordList = new JList<>(wordListModel);
private JScrollPane wPane = new JScrollPane(wordList);
private String currentField;


Expand Down Expand Up @@ -262,15 +262,13 @@ private void newWordAction() {
}

private void applyChanges() {
boolean changedFieldSet = false; // Watch if we need to rebuild entry editors
boolean anythingChanged = false; // Watch if we should mark as there is data changed

// First remove the mappings for fields that have been deleted.
// If these were re-added, they will be added below, so it doesn't
// cause any harm to remove them here.
for (String fieldName : removedFields) {
metaData.clearContentSelectors(fieldName);
changedFieldSet = true;
anythingChanged = true;
}

Expand Down Expand Up @@ -302,21 +300,16 @@ private void applyChanges() {

// Check if there are words to be added and previously there were no content selector for the field
if (!data.isEmpty() && metaData.getContentSelectorValuesForField(entry.getKey()).isEmpty()) {
changedFieldSet = true;
anythingChanged = true;
}

metaData.addContentSelector(new ContentSelector(entry.getKey(), new ArrayList<>(data)));
}

// Update all selectors in the current BasePanel.
if (changedFieldSet) {
// TODO: We have added or removed content selectors, update the entry editor
} else if (anythingChanged) {
// Enough to update the content selectors, if anything changed
panel.updateAllContentSelectors();
}

if (anythingChanged) {
// Update all selectors in the current BasePanel.
panel.setupMainPanel();

// Mark the database updated so changes are not lost
panel.markNonUndoableBaseChanged();
}
Expand Down
200 changes: 0 additions & 200 deletions src/main/java/org/jabref/gui/contentselector/FieldContentSelector.java

This file was deleted.

Loading

0 comments on commit 983ea91

Please sign in to comment.