Search in sources :

Example 96 with TreePath

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 }));
}
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 97 with TreePath

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;
}
Also used : HashTree(org.apache.jorphan.collections.HashTree) TestPlan(org.apache.jmeter.testelement.TestPlan) ActionEvent(java.awt.event.ActionEvent) TestElement(org.apache.jmeter.testelement.TestElement) WorkBench(org.apache.jmeter.testelement.WorkBench) JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) GuiPackage(org.apache.jmeter.gui.GuiPackage) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode)

Example 98 with TreePath

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));
}
Also used : JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) GuiPackage(org.apache.jmeter.gui.GuiPackage) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) TreeNode(javax.swing.tree.TreeNode)

Example 99 with TreePath

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;
}
Also used : Transferable(java.awt.datatransfer.Transferable) ArrayList(java.util.ArrayList) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) DataFlavor(java.awt.datatransfer.DataFlavor) JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) GuiPackage(org.apache.jmeter.gui.GuiPackage)

Example 100 with TreePath

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);
}
Also used : JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) DataFlavor(java.awt.datatransfer.DataFlavor)

Aggregations

TreePath (javax.swing.tree.TreePath)539 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)177 ArrayList (java.util.ArrayList)58 TreeNode (javax.swing.tree.TreeNode)56 Nullable (org.jetbrains.annotations.Nullable)49 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)46 JTree (javax.swing.JTree)34 NotNull (org.jetbrains.annotations.NotNull)32 Test (org.junit.Test)22 Tree (com.intellij.ui.treeStructure.Tree)21 TreeSelectionEvent (javax.swing.event.TreeSelectionEvent)21 TreeSelectionListener (javax.swing.event.TreeSelectionListener)21 MouseEvent (java.awt.event.MouseEvent)18 Point (java.awt.Point)17 ActionEvent (java.awt.event.ActionEvent)15 KeyEvent (java.awt.event.KeyEvent)12 EVTask (net.sourceforge.processdash.ev.EVTask)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 KeyAdapter (java.awt.event.KeyAdapter)11 List (java.util.List)11