Search in sources :

Example 1 with Replaceable

use of org.apache.jmeter.gui.Replaceable 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

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