diff --git a/src/main/java/org/humanistika/oxygen/tei/completer/GUI/newSuggestionForm.java b/src/main/java/org/humanistika/oxygen/tei/completer/GUI/newSuggestionForm.java index 8c004d0..92a06e0 100644 --- a/src/main/java/org/humanistika/oxygen/tei/completer/GUI/newSuggestionForm.java +++ b/src/main/java/org/humanistika/oxygen/tei/completer/GUI/newSuggestionForm.java @@ -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.*; @@ -34,6 +36,9 @@ 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 @@ -41,6 +46,7 @@ public class newSuggestionForm extends javax.swing.JDialog { private TeiCompleter teiCompleter; private TeiCompleter.AutoCompleteContext autoCompleteContext; + private WhatPossibleValuesHasAttributeContext context; private SuggestedAutocomplete suggestedAutocomplete = null; private ArrayList results = new ArrayList<>(); @@ -48,10 +54,11 @@ public class newSuggestionForm extends javax.swing.JDialog { /** * 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(); } @@ -244,7 +251,21 @@ private void fetchjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN- // get the auto complete suggestions based on the user input final List 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()) { + //check if attributeExpr addresses a subset of autoCompleteXPaths.attributeXPath + final TeiCompleter.AutoCompleteXPaths autoCompleteXPaths = teiCompleter.getXPaths(autoComplete); + if (!isSubset(attributeExpr, autoCompleteXPaths.getAttributeXPath())) continue; + suggestions.addAll(teiCompleter.requestAutoComplete(autoComplete, selection, dependent)); } @@ -391,7 +412,21 @@ protected Object doInBackground() throws Exception { // get the auto complete suggestions based on the user input final List 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()) { + //check if attributeExpr addresses a subset of autoCompleteXPaths.attributeXPath + final TeiCompleter.AutoCompleteXPaths autoCompleteXPaths = teiCompleter.getXPaths(autoComplete); + if (!isSubset(attributeExpr, autoCompleteXPaths.getAttributeXPath())) continue; + suggestions.addAll(teiCompleter.requestAutoComplete(autoComplete, selection, dependent)); } diff --git a/src/main/java/org/humanistika/oxygen/tei/completer/TeiCompleter.java b/src/main/java/org/humanistika/oxygen/tei/completer/TeiCompleter.java index 8f07a30..32feeff 100755 --- a/src/main/java/org/humanistika/oxygen/tei/completer/TeiCompleter.java +++ b/src/main/java/org/humanistika/oxygen/tei/completer/TeiCompleter.java @@ -103,7 +103,7 @@ public List filterAttributeValues(final List 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)); } } @@ -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) { @@ -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) { @@ -414,11 +414,14 @@ public List filterElementValues(final List 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 @@ -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);