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();
}
Aggregations