use of javax.swing.tree.TreePath 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 }));
}
use of javax.swing.tree.TreePath in project jmeter by apache.
the class Load method insertLoadedTree.
/**
* Inserts (or merges) the tree into the GUI.
* Does not check if the previous tree has been saved.
* Clears the existing GUI test plan if we are inserting a complete plan.
* @param id the id for the ActionEvent that is created
* @param tree the tree to load
* @param merging true if the tree is to be merged; false if it is to replace the existing tree
* @return true if the loaded tree was a full test plan
* @throws IllegalUserActionException if the tree cannot be merged at the selected position or the tree is empty
*/
// Does not appear to be used externally; called by #loadProjectFile()
public static boolean insertLoadedTree(final int id, final HashTree tree, final boolean merging) throws IllegalUserActionException {
if (tree == null) {
throw new IllegalUserActionException("Empty TestPlan or error reading test plan - see log file");
}
final boolean isTestPlan = tree.getArray()[0] instanceof TestPlan;
// If we are loading a new test plan, initialize the tree with the testplan node we are loading
final GuiPackage guiInstance = GuiPackage.getInstance();
if (isTestPlan && !merging) {
// Why does this not call guiInstance.clearTestPlan() ?
// Is there a reason for not clearing everything?
guiInstance.clearTestPlan((TestElement) tree.getArray()[0]);
}
if (merging) {
// Check if target of merge is reasonable
final TestElement te = (TestElement) tree.getArray()[0];
if (!(te instanceof WorkBench || te instanceof TestPlan)) {
// These are handled specially by addToTree
final boolean ok = MenuFactory.canAddTo(guiInstance.getCurrentNode(), te);
if (!ok) {
String name = te.getName();
String className = te.getClass().getName();
className = className.substring(className.lastIndexOf('.') + 1);
throw new IllegalUserActionException("Can't merge " + name + " (" + className + ") here");
}
}
}
final HashTree newTree = guiInstance.addSubTree(tree);
guiInstance.updateCurrentGui();
guiInstance.getMainFrame().getTree().setSelectionPath(new TreePath(((JMeterTreeNode) newTree.getArray()[0]).getPath()));
final HashTree subTree = guiInstance.getCurrentSubTree();
// Send different event wether we are merging a test plan into another test plan,
// or loading a testplan from scratch
ActionEvent actionEvent = new ActionEvent(subTree.get(subTree.getArray()[subTree.size() - 1]), id, merging ? ActionNames.SUB_TREE_MERGED : ActionNames.SUB_TREE_LOADED);
ActionRouter.getInstance().actionPerformed(actionEvent);
final JTree jTree = guiInstance.getMainFrame().getTree();
if (EXPAND_TREE && !merging) {
// don't automatically expand when merging
for (int i = 0; i < jTree.getRowCount(); i++) {
jTree.expandRow(i);
}
} else {
jTree.expandRow(0);
}
jTree.setSelectionPath(jTree.getPathForRow(1));
FocusRequester.requestFocus(jTree);
return isTestPlan;
}
use of javax.swing.tree.TreePath in project jmeter by apache.
the class Move method moveAndSelectNode.
private static void moveAndSelectNode(JMeterTreeNode currentNode, JMeterTreeNode parentNode, int newIndx) {
GuiPackage guiInstance = GuiPackage.getInstance();
guiInstance.getTreeModel().removeNodeFromParent(currentNode);
guiInstance.getTreeModel().insertNodeInto(currentNode, parentNode, newIndx);
// select the node
TreeNode[] nodes = guiInstance.getTreeModel().getPathToRoot(currentNode);
JTree jTree = guiInstance.getMainFrame().getTree();
jTree.setSelectionPath(new TreePath(nodes));
}
use of javax.swing.tree.TreePath in project jmeter by apache.
the class JMeterTreeTransferHandler method importData.
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
// deal with the jmx files
GuiPackage guiInstance = GuiPackage.getInstance();
DataFlavor[] flavors = support.getDataFlavors();
Transferable t = support.getTransferable();
for (DataFlavor flavor : flavors) {
// Check for file lists specifically
if (flavor.isFlavorJavaFileListType()) {
try {
return guiInstance.getMainFrame().openJmxFilesFromDragAndDrop(t);
} catch (Exception e) {
log.error("Drop file failed", e);
}
return false;
}
}
// Extract transfer data.
JMeterTreeNode[] nodes = getDraggedNodes(t);
if (nodes == null || nodes.length == 0) {
return false;
}
// Get drop location and mode
JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
TreePath dest = dl.getPath();
JMeterTreeNode target = (JMeterTreeNode) dest.getLastPathComponent();
nodesForRemoval = new ArrayList<>();
int index = dl.getChildIndex();
TreePath[] pathsToSelect = new TreePath[nodes.length];
int pathPosition = 0;
JMeterTreeModel treeModel = guiInstance.getTreeModel();
for (JMeterTreeNode node : nodes) {
if (index == -1) {
// drop mode == DropMode.ON
index = target.getChildCount();
}
// Insert a clone of the node, the original one will be removed by the exportDone method
// the children are not cloned but moved to the cloned node
// working on the original node would be harder as
// you'll have to deal with the insertion index offset if you re-order a node inside a parent
JMeterTreeNode copy = (JMeterTreeNode) node.clone();
// first copy the children as the call to copy.add will modify the collection we're iterating on
Enumeration<?> enumFrom = node.children();
List<JMeterTreeNode> tmp = new ArrayList<>();
while (enumFrom.hasMoreElements()) {
JMeterTreeNode child = (JMeterTreeNode) enumFrom.nextElement();
tmp.add(child);
}
for (JMeterTreeNode jMeterTreeNode : tmp) {
copy.add(jMeterTreeNode);
}
treeModel.insertNodeInto(copy, target, index++);
nodesForRemoval.add(node);
pathsToSelect[pathPosition++] = new TreePath(treeModel.getPathToRoot(copy));
}
TreePath treePath = new TreePath(target.getPath());
// expand the destination node
JTree tree = (JTree) support.getComponent();
tree.expandPath(treePath);
tree.setSelectionPaths(pathsToSelect);
return true;
}
use of javax.swing.tree.TreePath in project jmeter by apache.
the class JMeterTreeTransferHandler method canImport.
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
if (!support.isDrop()) {
return false;
}
// the tree accepts a jmx file
DataFlavor[] flavors = support.getDataFlavors();
for (DataFlavor flavor : flavors) {
// Check for file lists specifically
if (flavor.isFlavorJavaFileListType()) {
return true;
}
}
// or a treenode from the same tree
if (!support.isDataFlavorSupported(nodeFlavor)) {
return false;
}
// the copy is disabled
int action = support.getDropAction();
if (action != MOVE) {
return false;
}
support.setShowDropLocation(true);
// Do not allow a drop on the drag source selections.
JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
TreePath dest = dl.getPath();
JMeterTreeNode target = (JMeterTreeNode) dest.getLastPathComponent();
// TestPlan and WorkBench are the only children of the root
if (target.isRoot()) {
return false;
}
JMeterTreeNode[] nodes = getDraggedNodes(support.getTransferable());
if (nodes == null || nodes.length == 0) {
return false;
}
for (JMeterTreeNode node : nodes) {
if (target == node) {
return false;
}
// Do not allow a non-leaf node to be moved into one of its children
if (node.getChildCount() > 0 && target.isNodeAncestor(node)) {
return false;
}
}
// re-use node association logic
return MenuFactory.canAddTo(target, nodes);
}
Aggregations