Search in sources :

Example 1 with Searchable

use of org.apache.jmeter.gui.Searchable in project jmeter by apache.

the class SearchTreePanel method searchInNode.

/**
     * @param searcher
     * @param node
     */
private boolean searchInNode(Searcher searcher, SearchableTreeNode node) {
    node.reset();
    Object userObject = node.getUserObject();
    try {
        Searchable searchable;
        if (userObject instanceof Searchable) {
            searchable = (Searchable) userObject;
        } else {
            return false;
        }
        if (searcher.search(searchable.getSearchableTokens())) {
            node.setNodeHasMatched(true);
        }
        boolean foundInChildren = false;
        for (int i = 0; i < node.getChildCount(); i++) {
            searchInNode(searcher, (SearchableTreeNode) node.getChildAt(i));
            foundInChildren = searchInNode(searcher, (SearchableTreeNode) node.getChildAt(i)) || // Must be the last in condition
            foundInChildren;
        }
        if (!node.isNodeHasMatched()) {
            node.setChildrenNodesHaveMatched(foundInChildren);
        }
        node.updateState();
        return node.isNodeHasMatched() || node.isChildrenNodesHaveMatched();
    } catch (Exception e) {
        log.error("Error extracting data from tree node");
        return false;
    }
}
Also used : Searchable(org.apache.jmeter.gui.Searchable)

Example 2 with Searchable

use of org.apache.jmeter.gui.Searchable in project jmeter by apache.

the class SearchTreeDialog method doSearch.

/**
     * @param e {@link ActionEvent}
     */
private void doSearch(ActionEvent e) {
    boolean expand = e.getSource() == searchAndExpandButton;
    String wordToSearch = searchTF.getText();
    if (StringUtils.isEmpty(wordToSearch)) {
        return;
    } else {
        this.lastSearch = wordToSearch;
    }
    // reset previous result
    ActionRouter.getInstance().doActionNow(new ActionEvent(e.getSource(), e.getID(), ActionNames.SEARCH_RESET));
    // do search
    Searcher searcher = null;
    if (isRegexpCB.isSelected()) {
        searcher = new RegexpSearcher(isCaseSensitiveCB.isSelected(), searchTF.getText());
    } else {
        searcher = new RawTextSearcher(isCaseSensitiveCB.isSelected(), searchTF.getText());
    }
    GuiPackage guiPackage = GuiPackage.getInstance();
    JMeterTreeModel jMeterTreeModel = guiPackage.getTreeModel();
    Set<JMeterTreeNode> nodes = new HashSet<>();
    int numberOfMatches = 0;
    for (JMeterTreeNode jMeterTreeNode : jMeterTreeModel.getNodesOfType(Searchable.class)) {
        try {
            Searchable searchable = (Searchable) jMeterTreeNode.getUserObject();
            List<JMeterTreeNode> matchingNodes = jMeterTreeNode.getPathToThreadGroup();
            List<String> searchableTokens = searchable.getSearchableTokens();
            boolean result = searcher.search(searchableTokens);
            if (result) {
                numberOfMatches++;
                nodes.addAll(matchingNodes);
            }
        } catch (Exception ex) {
            logger.error("Error occurred searching for word:" + wordToSearch + " in node:" + jMeterTreeNode.getName(), ex);
        }
    }
    GuiPackage guiInstance = GuiPackage.getInstance();
    JTree jTree = guiInstance.getMainFrame().getTree();
    for (JMeterTreeNode jMeterTreeNode : nodes) {
        jMeterTreeNode.setMarkedBySearch(true);
        if (expand) {
            jTree.expandPath(new TreePath(jMeterTreeNode.getPath()));
        }
    }
    GuiPackage.getInstance().getMainFrame().repaint();
    searchTF.requestFocusInWindow();
    statusLabel.setText(MessageFormat.format(JMeterUtils.getResString("search_tree_matches"), new Object[] { numberOfMatches }));
}
Also used : ActionEvent(java.awt.event.ActionEvent) JMeterTreeModel(org.apache.jmeter.gui.tree.JMeterTreeModel) JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) GuiPackage(org.apache.jmeter.gui.GuiPackage) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) Searchable(org.apache.jmeter.gui.Searchable) HashSet(java.util.HashSet)

Example 3 with Searchable

use of org.apache.jmeter.gui.Searchable in project jmeter by apache.

the class ResetSearchCommand method doAction.

/**
     * @see Command#doAction(ActionEvent)
     */
@Override
public void doAction(ActionEvent e) {
    GuiPackage guiPackage = GuiPackage.getInstance();
    JMeterTreeModel jMeterTreeModel = guiPackage.getTreeModel();
    for (JMeterTreeNode jMeterTreeNode : jMeterTreeModel.getNodesOfType(Searchable.class)) {
        if (jMeterTreeNode.getUserObject() instanceof Searchable) {
            List<JMeterTreeNode> matchingNodes = jMeterTreeNode.getPathToThreadGroup();
            for (JMeterTreeNode jMeterTreeNode2 : matchingNodes) {
                jMeterTreeNode2.setMarkedBySearch(false);
            }
        }
    }
    GuiPackage.getInstance().getMainFrame().repaint();
}
Also used : JMeterTreeModel(org.apache.jmeter.gui.tree.JMeterTreeModel) GuiPackage(org.apache.jmeter.gui.GuiPackage) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) Searchable(org.apache.jmeter.gui.Searchable)

Example 4 with Searchable

use of org.apache.jmeter.gui.Searchable in project jmeter by apache.

the class SearchTreeDialog method doReplaceAll.

/**
     * Replace all occurrences in nodes that contain {@link Replaceable} Test Elements
     * @param e {@link ActionEvent}
     */
private void doReplaceAll(ActionEvent e) {
    String wordToSearch = searchTF.getText();
    String wordToReplace = replaceTF.getText();
    if (StringUtils.isEmpty(wordToReplace)) {
        return;
    }
    // Save any change to current node
    GuiPackage.getInstance().updateCurrentNode();
    // reset previous result
    ActionRouter.getInstance().doActionNow(new ActionEvent(e.getSource(), e.getID(), ActionNames.SEARCH_RESET));
    Searcher searcher;
    String regex;
    if (isRegexpCB.isSelected()) {
        regex = wordToSearch;
        searcher = new RegexpSearcher(isCaseSensitiveCB.isSelected(), wordToSearch);
    } else {
        regex = Pattern.quote(wordToSearch);
        searcher = new RawTextSearcher(isCaseSensitiveCB.isSelected(), wordToSearch);
    }
    GuiPackage guiPackage = GuiPackage.getInstance();
    JMeterTreeModel jMeterTreeModel = guiPackage.getTreeModel();
    Set<JMeterTreeNode> nodes = new HashSet<>();
    boolean caseSensitiveReplacement = isCaseSensitiveCB.isSelected();
    int totalReplaced = 0;
    for (JMeterTreeNode jMeterTreeNode : jMeterTreeModel.getNodesOfType(Searchable.class)) {
        try {
            Searchable searchable = (Searchable) jMeterTreeNode.getUserObject();
            List<String> searchableTokens = searchable.getSearchableTokens();
            boolean result = searcher.search(searchableTokens);
            if (result && jMeterTreeNode.getUserObject() instanceof Replaceable) {
                Replaceable replaceable = (Replaceable) jMeterTreeNode.getUserObject();
                int numberOfReplacements = replaceable.replace(regex, wordToReplace, caseSensitiveReplacement);
                if (logger.isInfoEnabled()) {
                    logger.info("Replaced " + numberOfReplacements + " in element:" + ((TestElement) jMeterTreeNode.getUserObject()).getName());
                }
                totalReplaced += numberOfReplacements;
                if (numberOfReplacements > 0) {
                    List<JMeterTreeNode> matchingNodes = jMeterTreeNode.getPathToThreadGroup();
                    nodes.addAll(matchingNodes);
                }
            }
        } catch (Exception ex) {
            logger.error("Error occurred replacing data in node:" + jMeterTreeNode.getName(), ex);
        }
    }
    statusLabel.setText(MessageFormat.format("Replaced {0} occurrences", new Object[] { totalReplaced }));
    GuiPackage guiInstance = GuiPackage.getInstance();
    JTree jTree = guiInstance.getMainFrame().getTree();
    for (JMeterTreeNode jMeterTreeNode : nodes) {
        jMeterTreeNode.setMarkedBySearch(true);
        jTree.expandPath(new TreePath(jMeterTreeNode.getPath()));
    }
    // Update GUI as current node may be concerned by changes
    if (totalReplaced > 0) {
        GuiPackage.getInstance().refreshCurrentGui();
    }
    GuiPackage.getInstance().getMainFrame().repaint();
    searchTF.requestFocusInWindow();
}
Also used : ActionEvent(java.awt.event.ActionEvent) TestElement(org.apache.jmeter.testelement.TestElement) JMeterTreeModel(org.apache.jmeter.gui.tree.JMeterTreeModel) JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) GuiPackage(org.apache.jmeter.gui.GuiPackage) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) Searchable(org.apache.jmeter.gui.Searchable) Replaceable(org.apache.jmeter.gui.Replaceable) HashSet(java.util.HashSet)

Aggregations

Searchable (org.apache.jmeter.gui.Searchable)4 GuiPackage (org.apache.jmeter.gui.GuiPackage)3 JMeterTreeModel (org.apache.jmeter.gui.tree.JMeterTreeModel)3 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)3 ActionEvent (java.awt.event.ActionEvent)2 HashSet (java.util.HashSet)2 JTree (javax.swing.JTree)2 TreePath (javax.swing.tree.TreePath)2 Replaceable (org.apache.jmeter.gui.Replaceable)1 TestElement (org.apache.jmeter.testelement.TestElement)1