Search in sources :

Example 1 with SiteMapTreeCellRenderer

use of org.zaproxy.zap.view.SiteMapTreeCellRenderer in project zaproxy by zaproxy.

the class SiteMapPanel method getTreeSite.

/**
 * This method initializes treeSite
 *
 * @return javax.swing.JTree
 */
public JTree getTreeSite() {
    if (treeSite == null) {
        treeSite = new JTree(new DefaultTreeModel(new DefaultMutableTreeNode()));
        treeSite.setShowsRootHandles(true);
        treeSite.setName("treeSite");
        treeSite.setToggleClickCount(1);
        // Force macOS L&F to query the row height from SiteMapTreeCellRenderer to hide the
        // filtered nodes.
        // Other L&Fs hide the filtered nodes by default.
        LookAndFeel laf = UIManager.getLookAndFeel();
        if (laf != null && Constant.isMacOsX() && UIManager.getSystemLookAndFeelClassName().equals(laf.getClass().getName())) {
            treeSite.setRowHeight(0);
        }
        treeSite.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {

            @Override
            public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
                SiteNode node = (SiteNode) treeSite.getLastSelectedPathComponent();
                if (node == null) {
                    return;
                }
                if (!node.isRoot()) {
                    HttpMessage msg = null;
                    try {
                        msg = node.getHistoryReference().getHttpMessage();
                    } catch (Exception e1) {
                        // ZAP: Log exceptions
                        log.warn(e1.getMessage(), e1);
                        return;
                    }
                    getView().displayMessage(msg);
                    // ZAP: Call SiteMapListenners
                    for (SiteMapListener listener : listeners) {
                        listener.nodeSelected(node);
                    }
                } else {
                    // ZAP: clear the views when the root is selected
                    getView().displayMessage(null);
                }
            }
        });
        treeSite.setComponentPopupMenu(new SitesCustomPopupMenu());
        // ZAP: Add custom tree cell renderer.
        DefaultTreeCellRenderer renderer = new SiteMapTreeCellRenderer(listeners);
        treeSite.setCellRenderer(renderer);
        String deleteSiteNode = "zap.delete.sitenode";
        treeSite.getInputMap().put(getView().getDefaultDeleteKeyStroke(), deleteSiteNode);
        treeSite.getActionMap().put(deleteSiteNode, new AbstractAction() {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                ExtensionHistory extHistory = Control.getSingleton().getExtensionLoader().getExtension(ExtensionHistory.class);
                if (extHistory == null || treeSite.getSelectionCount() == 0) {
                    return;
                }
                int result = View.getSingleton().showConfirmDialog(Constant.messages.getString("sites.purge.warning"));
                if (result != JOptionPane.YES_OPTION) {
                    return;
                }
                SiteMap siteMap = Model.getSingleton().getSession().getSiteTree();
                for (TreePath path : treeSite.getSelectionPaths()) {
                    extHistory.purge(siteMap, (SiteNode) path.getLastPathComponent());
                }
            }
        });
    }
    return treeSite;
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ActionEvent(java.awt.event.ActionEvent) ExtensionHistory(org.parosproxy.paros.extension.history.ExtensionHistory) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) LookAndFeel(javax.swing.LookAndFeel) DefaultTreeCellRenderer(javax.swing.tree.DefaultTreeCellRenderer) DatabaseException(org.parosproxy.paros.db.DatabaseException) JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) SiteMap(org.parosproxy.paros.model.SiteMap) SiteMapTreeCellRenderer(org.zaproxy.zap.view.SiteMapTreeCellRenderer) HttpMessage(org.parosproxy.paros.network.HttpMessage) SiteMapListener(org.zaproxy.zap.view.SiteMapListener) AbstractAction(javax.swing.AbstractAction) SiteNode(org.parosproxy.paros.model.SiteNode)

Aggregations

ActionEvent (java.awt.event.ActionEvent)1 AbstractAction (javax.swing.AbstractAction)1 JTree (javax.swing.JTree)1 LookAndFeel (javax.swing.LookAndFeel)1 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)1 DefaultTreeCellRenderer (javax.swing.tree.DefaultTreeCellRenderer)1 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)1 TreePath (javax.swing.tree.TreePath)1 DatabaseException (org.parosproxy.paros.db.DatabaseException)1 ExtensionHistory (org.parosproxy.paros.extension.history.ExtensionHistory)1 SiteMap (org.parosproxy.paros.model.SiteMap)1 SiteNode (org.parosproxy.paros.model.SiteNode)1 HttpMessage (org.parosproxy.paros.network.HttpMessage)1 SiteMapListener (org.zaproxy.zap.view.SiteMapListener)1 SiteMapTreeCellRenderer (org.zaproxy.zap.view.SiteMapTreeCellRenderer)1