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

[bugfix] prevent auto complete search outside the given context #32

Merged
merged 2 commits into from
Nov 23, 2023
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
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 {
adamretter marked this conversation as resolved.
Show resolved Hide resolved
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) {
adamretter marked this conversation as resolved.
Show resolved Hide resolved
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