Skip to content

Commit

Permalink
Merge pull request #32 from evolvedbinary/fix/multi-context
Browse files Browse the repository at this point in the history
[bugfix] prevent auto complete search outside the given context
  • Loading branch information
adamretter authored Nov 23, 2023
2 parents 217bb3a + eacb784 commit 31afb68
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
*/
package org.humanistika.oxygen.tei.completer.GUI;

import com.evolvedbinary.xpath.parser.ast.Expr;
import org.humanistika.oxygen.tei.completer.SuggestedAutocomplete;
import org.humanistika.oxygen.tei.completer.TeiCompleter;
import org.humanistika.oxygen.tei.completer.configuration.beans.AutoComplete;
import ro.sync.contentcompletion.xml.CIValue;
import ro.sync.contentcompletion.xml.WhatPossibleValuesHasAttributeContext;

import javax.annotation.Nullable;
import javax.swing.*;
Expand All @@ -34,24 +36,29 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.humanistika.oxygen.tei.completer.XPathUtil.isSubset;
import static org.humanistika.oxygen.tei.completer.XPathUtil.parseXPath;

/**
*
* @author younes
*/
public class newSuggestionForm extends javax.swing.JDialog {
private TeiCompleter teiCompleter;
private TeiCompleter.AutoCompleteContext autoCompleteContext;
private WhatPossibleValuesHasAttributeContext context;
private SuggestedAutocomplete suggestedAutocomplete = null;

private ArrayList<SuggestedAutocomplete> results = new ArrayList<>();

/**
* Creates new form JDialogForm
*/
public newSuggestionForm(java.awt.Frame parent, final TeiCompleter teiCompleter, final TeiCompleter.AutoCompleteContext autoCompleteContext) {
public newSuggestionForm(java.awt.Frame parent, final TeiCompleter teiCompleter, final TeiCompleter.AutoCompleteContext autoCompleteContext, final WhatPossibleValuesHasAttributeContext context) {
super(parent, ModalityType.DOCUMENT_MODAL);
this.teiCompleter = teiCompleter;
this.autoCompleteContext = autoCompleteContext;
this.context = context;
initComponents();
customLabels();
}
Expand Down Expand Up @@ -244,8 +251,23 @@ private void fetchjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
// get the auto complete suggestions based on the user input
final List<CIValue> suggestions = new ArrayList<>();

final String elemXPath = context.computeContextXPathExpression();
final String attrXPath = elemXPath + "/@" + context.getAttributeName();
Expr attributeExpr;
try {
attributeExpr = parseXPath(attrXPath);
} catch (Exception e) {
return;
}


for (final AutoComplete autoComplete : teiCompleter.getConfiguration().getAutoCompletes()) {
suggestions.addAll(teiCompleter.requestAutoComplete(autoComplete, selection, dependent));
//check if attributeExpr addresses a subset of autoCompleteXPaths.attributeXPath
// only request auto completer suggestions if it matches
final TeiCompleter.AutoCompleteXPaths autoCompleteXPaths = teiCompleter.getXPaths(autoComplete);
if (isSubset(attributeExpr, autoCompleteXPaths.getAttributeXPath())) {
suggestions.addAll(teiCompleter.requestAutoComplete(autoComplete, selection, dependent));
}
}

//get the model to populate the table
Expand Down Expand Up @@ -391,8 +413,24 @@ protected Object doInBackground() throws Exception {
// get the auto complete suggestions based on the user input
final List<CIValue> suggestions = new ArrayList<>();

final String elemXPath = context.computeContextXPathExpression();
final String attrXPath = elemXPath + "/@" + context.getAttributeName();
Expr attributeExpr;
try {
attributeExpr = parseXPath(attrXPath);
} catch (Exception e) {
throw new Exception(e);
}


for (final AutoComplete autoComplete : teiCompleter.getConfiguration().getAutoCompletes()) {
suggestions.addAll(teiCompleter.requestAutoComplete(autoComplete, selection, dependent));
//check if attributeExpr addresses a subset of autoCompleteXPaths.attributeXPath
// only request auto complete suggestions if it matches
final TeiCompleter.AutoCompleteXPaths autoCompleteXPaths = teiCompleter.getXPaths(autoComplete);
if (isSubset(attributeExpr, autoCompleteXPaths.getAttributeXPath())) {
suggestions.addAll(teiCompleter.requestAutoComplete(autoComplete, selection, dependent));

}
}

this.suggestions.addAll(suggestions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public List<CIValue> filterAttributeValues(final List<CIValue> list, final WhatP

if(autoCompleteSuggestions != null) {
// the value needs to be prefixed with a space character to bump it to the top of the list
list.add(new CustomCIValue("\uD83D\uDC49 Custom lookup...", this, autoCompleteSuggestions.autoCompleteContext));
list.add(new CustomCIValue("\uD83D\uDC49 Custom lookup...", this, autoCompleteSuggestions.autoCompleteContext, context));
}

}
Expand Down Expand Up @@ -235,7 +235,7 @@ private AuthenticationType asClientFactoryAuthenticationType(@Nullable final Aut
}
}

protected class AutoCompleteXPaths {
public class AutoCompleteXPaths {
private final Expr attributeXPath;

public AutoCompleteXPaths(final Expr attributeXPath) {
Expand All @@ -247,7 +247,7 @@ public Expr getAttributeXPath() {
}
}

protected AutoCompleteXPaths getXPaths(final AutoComplete autoComplete) {
public AutoCompleteXPaths getXPaths(final AutoComplete autoComplete) {
synchronized(cachedAutoCompleteXPaths) {
AutoCompleteXPaths autoCompleteXPaths = cachedAutoCompleteXPaths.get(autoComplete);
if(autoCompleteXPaths == null) {
Expand Down Expand Up @@ -414,11 +414,14 @@ public List<CIValue> filterElementValues(final List<CIValue> list, final Context
public class CustomCIValue extends CIValue {
private TeiCompleter teiCompleter;
private AutoCompleteContext autoCompleteContext;

private WhatPossibleValuesHasAttributeContext context;
private String suggestion;
public CustomCIValue(String s, final TeiCompleter teiCompleter, final AutoCompleteContext autoCompleteContext) {
public CustomCIValue(String s, final TeiCompleter teiCompleter, final AutoCompleteContext autoCompleteContext, final WhatPossibleValuesHasAttributeContext context) {
super(s);
this.teiCompleter = teiCompleter;
this.autoCompleteContext = autoCompleteContext;
this.context = context;
}

@Override
Expand All @@ -436,7 +439,7 @@ private SuggestedAutocomplete promptUserForNewSuggestion() {
final KeyboardFocusManager keyboardFocusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
final Component comp = keyboardFocusManager.getFocusOwner();
final Frame parentFrame = getParentFrame(comp);
final newSuggestionForm newSuggestionForm = new newSuggestionForm(parentFrame, teiCompleter, autoCompleteContext);
final newSuggestionForm newSuggestionForm = new newSuggestionForm(parentFrame, teiCompleter, autoCompleteContext, context);



Expand Down

0 comments on commit 31afb68

Please sign in to comment.