Search in sources :

Example 11 with BasicFindReplaceParameters

use of au.gov.asd.tac.constellation.views.find2.utilities.BasicFindReplaceParameters in project constellation by constellation-app.

the class FindViewStateIoProvider method writeObject.

@Override
public void writeObject(final Attribute attribute, final int elementId, final JsonGenerator jsonGenerator, final GraphReadMethods graph, final GraphByteWriter byteWriter, boolean verbose) throws IOException {
    if (verbose || !graph.isDefaultValue(attribute.getId(), elementId)) {
        final FindResultsList originalResultsList = graph.getObjectValue(attribute.getId(), elementId);
        if (originalResultsList == null) {
            jsonGenerator.writeNullField(attribute.getName());
        } else {
            final FindResultsList resultsList = new FindResultsList(originalResultsList);
            // Store the current index and the graph ID
            jsonGenerator.writeObjectFieldStart(attribute.getName());
            jsonGenerator.writeNumberField("currentIndex", resultsList.getCurrentIndex());
            jsonGenerator.writeStringField("graphID", resultsList.getGraphId());
            // Stores a list of the Find Results, ID, UID and Graph element type label
            jsonGenerator.writeArrayFieldStart("findResults");
            for (final FindResult fr : resultsList) {
                jsonGenerator.writeNumber(fr.getID());
                jsonGenerator.writeNumber(fr.getUID());
                jsonGenerator.writeString(fr.getType().getShortLabel());
            }
            jsonGenerator.writeEndArray();
            // Store all the basic find replace parameters
            final BasicFindReplaceParameters parameters = resultsList.getSearchParameters();
            jsonGenerator.writeStringField("findString", parameters.getFindString());
            jsonGenerator.writeStringField("replaceString", parameters.getReplaceString());
            jsonGenerator.writeStringField("graphElement", parameters.getGraphElement().getShortLabel());
            jsonGenerator.writeBooleanField("standardText", parameters.isStandardText());
            jsonGenerator.writeBooleanField("regEx", parameters.isRegEx());
            jsonGenerator.writeBooleanField("ignoreCase", parameters.isIgnoreCase());
            jsonGenerator.writeBooleanField("exactMatch", parameters.isExactMatch());
            jsonGenerator.writeBooleanField("searchAllGraphs", parameters.isSearchAllGraphs());
            jsonGenerator.writeBooleanField("addToSelection", parameters.isAddTo());
            jsonGenerator.writeBooleanField("findInSelection", parameters.isFindIn());
            jsonGenerator.writeBooleanField("removeFromSelection", parameters.isRemoveFrom());
            jsonGenerator.writeBooleanField("replaceInSelected", parameters.isReplaceIn());
            // Store all the selected attributes of the search
            jsonGenerator.writeArrayFieldStart("selectedAttributes");
            for (final Attribute a : parameters.getAttributeList()) {
                jsonGenerator.writeObject(a.getName());
            }
            jsonGenerator.writeEndArray();
            jsonGenerator.writeEndObject();
        }
    }
}
Also used : GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) Attribute(au.gov.asd.tac.constellation.graph.Attribute) BasicFindReplaceParameters(au.gov.asd.tac.constellation.views.find2.utilities.BasicFindReplaceParameters) FindResultsList(au.gov.asd.tac.constellation.views.find2.utilities.FindResultsList) FindResult(au.gov.asd.tac.constellation.views.find2.utilities.FindResult)

Example 12 with BasicFindReplaceParameters

use of au.gov.asd.tac.constellation.views.find2.utilities.BasicFindReplaceParameters in project constellation by constellation-app.

the class FindViewStateIoProvider method readObject.

@Override
public void readObject(final int attributeId, final int elementId, final JsonNode jnode, final GraphWriteMethods writableGraph, final Map<Integer, Integer> vertexMap, final Map<Integer, Integer> transactionMap, final GraphByteReader byteReader, final ImmutableObjectCache cache) throws IOException {
    if (!jnode.isNull()) {
        // Get the findResultsList variables
        final int currentIndex = jnode.get("currentIndex").asInt();
        final String graphID = jnode.get("graphID").asText();
        // Get the basic find replace parameters
        final String findString = jnode.get("findString").asText();
        final String replaceString = jnode.get("replaceString").asText();
        final String graphElementString = jnode.get("graphElement").asText();
        final GraphElementType graphElement = GraphElementType.getValue(graphElementString);
        final Boolean standardText = jnode.get("standardText").asBoolean();
        final Boolean regEx = jnode.get("regEx").asBoolean();
        final Boolean ignoreCase = jnode.get("ignoreCase").asBoolean();
        final Boolean exactMatch = jnode.get("exactMatch").asBoolean();
        final Boolean findInSelection = jnode.get("findInSelection").asBoolean();
        final Boolean addToSelection = jnode.get("addToSelection").asBoolean();
        final Boolean removeFromSelection = jnode.get("removeFromSelection").asBoolean();
        final Boolean replaceInSelected = jnode.get("replaceInSelected").asBoolean();
        final Boolean searchAllGraphs = jnode.get("searchAllGraphs").asBoolean();
        // Get the selected attributes
        final List<Attribute> selectedAttributes = new ArrayList<>();
        final ArrayNode selectedAttributesArray = (ArrayNode) jnode.withArray("selectedAttributes");
        for (int i = 0; i < selectedAttributesArray.size(); i++) {
            if (selectedAttributesArray.get(i).isNull()) {
                selectedAttributes.add(null);
            } else {
                int attributeInt = writableGraph.getAttribute(graphElement, selectedAttributesArray.get(i).asText());
                selectedAttributes.add(new GraphAttribute(writableGraph, attributeInt));
            }
        }
        // Create the basic find replace parameter object with the variables
        final BasicFindReplaceParameters parameters = new BasicFindReplaceParameters(findString, replaceString, graphElement, selectedAttributes, standardText, regEx, ignoreCase, exactMatch, findInSelection, addToSelection, removeFromSelection, replaceInSelected, searchAllGraphs);
        // Get the find results
        final List<FindResult> findResults = new ArrayList<>();
        final ArrayNode findResultsArray = (ArrayNode) jnode.withArray("findResults");
        for (int i = 0; i < findResultsArray.size(); i = i + 3) {
            findResults.add(findResultsArray.get(i).isNull() ? null : new FindResult(findResultsArray.get(i).asInt(), findResultsArray.get(i + 1).asInt(), GraphElementType.getValue(findResultsArray.get(i + 2).asText())));
        }
        // Create the findResultList object
        final FindResultsList findResultList = new FindResultsList(currentIndex, parameters, graphID);
        // Add the find results to the findResultsList
        findResultList.addAll(findResults);
        writableGraph.setObjectValue(attributeId, elementId, findResultList);
    }
}
Also used : GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) Attribute(au.gov.asd.tac.constellation.graph.Attribute) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) ArrayList(java.util.ArrayList) BasicFindReplaceParameters(au.gov.asd.tac.constellation.views.find2.utilities.BasicFindReplaceParameters) FindResultsList(au.gov.asd.tac.constellation.views.find2.utilities.FindResultsList) GraphElementType(au.gov.asd.tac.constellation.graph.GraphElementType) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) FindResult(au.gov.asd.tac.constellation.views.find2.utilities.FindResult)

Example 13 with BasicFindReplaceParameters

use of au.gov.asd.tac.constellation.views.find2.utilities.BasicFindReplaceParameters in project constellation by constellation-app.

the class BasicFindPlugin method edit.

@Override
protected void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException {
    // Retrieve the existing FindResultList Meta attribute
    final int stateId = FindViewConcept.MetaAttribute.FINDVIEW_STATE.ensure(graph);
    FindResultsList foundResult = graph.getObjectValue(stateId, 0);
    /**
     * If it doesn't exist or is null, create a new list with the starting
     * index and the current find parameters. If it does exist, create a
     * list with the correct index and the current find parameters
     */
    if (foundResult == null) {
        foundResult = new FindResultsList(STARTING_INDEX, this.parameters, graph.getId());
    } else {
        /**
         * This is delicate, so don't change. This process, captures the
         * users previous search and their current search, compares the 2 to
         * see if they are the same. If yes then get the previous index of
         * the last foundResult. If its different reset the index. The
         * parameters are instantiated as new variables as they were
         * manipulation issues elsewhere causing this process to fail.
         */
        final FindResultsList oldList = new FindResultsList(STARTING_INDEX, foundResult.getSearchParameters(), foundResult.getGraphId());
        final BasicFindReplaceParameters oldparameters = oldList.getSearchParameters();
        final BasicFindReplaceParameters newParamters = new BasicFindReplaceParameters(this.parameters);
        int newIndex = getIndex(newParamters, oldparameters, foundResult.getCurrentIndex());
        foundResult = new FindResultsList(newIndex, newParamters, oldList.getGraphId());
    }
    foundResult.clear();
    graph.setObjectValue(stateId, 0, foundResult);
    if (findString.isEmpty()) {
        findString = "^$";
        regex = true;
    }
    boolean found;
    final int selectedAttribute = graph.getAttribute(elementType, VisualConcept.VertexAttribute.SELECTED.getName());
    final int elementCount = elementType.getElementCount(graph);
    // do this if add to selection
    if (!addToSelection && !removeFromCurrentSelection && !findInCurrentSelection) {
        clearSelection(graph);
    }
    final FindResultsList findInCurrentSelectionList = new FindResultsList(graph.getId());
    final FindResultsList removeFromCurrentSelectionList = new FindResultsList(graph.getId());
    final String searchString = regex ? findString : Pattern.quote(findString);
    final int caseSensitivity = ignorecase ? Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE : 0;
    final Pattern searchPattern = Pattern.compile(searchString, caseSensitivity);
    /**
     * Loop through all selected attributes, get the current element of the
     * selected type and its value, check the value isn't null, then compare
     * the value with the find string based on the search preferences. If
     * that element matches the search criteria, change its selected value
     * to true if selecting all. Otherwise create a FindResult and add that
     * find result to the foundResults list
     */
    for (final Attribute a : selectedAttributes) {
        // if the attribute exists on the current graph
        if (graph.getAttribute(elementType, a.getName()) >= 0) {
            // for all elements on the graph of the given type
            for (int i = 0; i < elementCount; i++) {
                // get the current element
                final int currElement = elementType.getElement(graph, i);
                // get string value of it graph elements attribute
                final String value = graph.getStringValue(graph.getAttribute(elementType, a.getName()), currElement);
                // if the value isnt null
                if (value != null) {
                    // Determine if the find string matches the attribute
                    // string
                    Matcher match = searchPattern.matcher(value);
                    found = matchWholeWord ? match.matches() : match.find();
                    if (found) {
                        // get the UID of the element and the graph
                        final long uid = elementType.getUID(graph, currElement);
                        // their current selection
                        if (findInCurrentSelection || removeFromCurrentSelection) {
                            // if the element is selected
                            if (graph.getBooleanValue(selectedAttribute, currElement)) {
                                // Add the element to the find in and
                                // remove from list
                                findInCurrentSelectionList.add(new FindResult(currElement, uid, elementType));
                                removeFromCurrentSelectionList.add(new FindResult(currElement, uid, elementType));
                            }
                        }
                        // matching element
                        if (selectAll && !findInCurrentSelection && !removeFromCurrentSelection) {
                            graph.setBooleanValue(selectedAttribute, currElement, true);
                        }
                        // add the graph element to the foundResult list
                        foundResult.add(new FindResult(currElement, uid, elementType));
                    }
                }
            }
        }
    }
    /**
     * If findIncurrentSelection is true, clear the current selection and
     * loop through the list of found elements and set them to selected.
     */
    selectFindInResults(findInCurrentSelection, findInCurrentSelectionList, foundResult, graph, selectedAttribute);
    /**
     * If removeFromCurrentlySelection is true, loop through the list of
     * found elements and set their selection status to false.
     */
    selectRemoveFromResults(removeFromCurrentSelection, removeFromCurrentSelectionList, foundResult, graph, selectedAttribute);
    /**
     * If the user clicked find next or find previous
     */
    if (!selectAll) {
        // Clean the find results list to only contain unique graph elements
        final List<FindResult> distinctValues = foundResult.stream().distinct().collect(Collectors.toList());
        foundResult.clear();
        foundResult.addAll(distinctValues);
        /**
         * If the list isn't empty, and the user clicked find next,
         * increment the found lists index by 1, otherwise decrement it by
         * 1. Set the element at the specified index to selected.
         */
        if (!foundResult.isEmpty()) {
            if (getNext) {
                foundResult.incrementCurrentIndex();
            } else {
                foundResult.decrementCurrentIndex();
            }
            final int elementId = foundResult.get(foundResult.getCurrentIndex()).getID();
            graph.setBooleanValue(selectedAttribute, elementId, !removeFromCurrentSelection);
        }
        graph.setObjectValue(stateId, 0, foundResult);
    }
    // If no results are found, set the meta attribute to null
    graph.setObjectValue(stateId, 0, foundResult.isEmpty() ? null : foundResult);
}
Also used : Pattern(java.util.regex.Pattern) Attribute(au.gov.asd.tac.constellation.graph.Attribute) Matcher(java.util.regex.Matcher) BasicFindReplaceParameters(au.gov.asd.tac.constellation.views.find2.utilities.BasicFindReplaceParameters) FindResultsList(au.gov.asd.tac.constellation.views.find2.utilities.FindResultsList) FindResult(au.gov.asd.tac.constellation.views.find2.utilities.FindResult)

Aggregations

BasicFindReplaceParameters (au.gov.asd.tac.constellation.views.find2.utilities.BasicFindReplaceParameters)13 Attribute (au.gov.asd.tac.constellation.graph.Attribute)9 ArrayList (java.util.ArrayList)7 Test (org.testng.annotations.Test)6 GraphElementType (au.gov.asd.tac.constellation.graph.GraphElementType)5 FindResultsList (au.gov.asd.tac.constellation.views.find2.utilities.FindResultsList)5 FindResult (au.gov.asd.tac.constellation.views.find2.utilities.FindResult)3 GraphAttribute (au.gov.asd.tac.constellation.graph.GraphAttribute)2 WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)2 DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1