Search in sources :

Example 6 with TreeExpansionEvent

use of javax.swing.event.TreeExpansionEvent in project sonarlint-intellij by SonarSource.

the class FlowsTree method init.

private void init() {
    setRootVisible(false);
    setShowsRootHandles(false);
    setCellRenderer(new TreeCellRenderer());
    this.selectionModel.addTreeSelectionListener(e -> {
        if (e.getSource() != null) {
            TreePath newPath = e.getNewLeadSelectionPath();
            if (newPath != null) {
                Object o = newPath.getLastPathComponent();
                if (!(o instanceof LocationNode)) {
                    FlowsTree.this.setSelectionPath(e.getOldLeadSelectionPath());
                } else {
                    navigateToSelected();
                }
            }
        }
    });
    TreeWillExpandListener l = new TreeWillExpandListener() {

        @Override
        public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
        // nothing to do
        }

        @Override
        public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
            throw new ExpandVetoException(event);
        }
    };
    addTreeWillExpandListener(l);
}
Also used : TreeWillExpandListener(javax.swing.event.TreeWillExpandListener) TreePath(javax.swing.tree.TreePath) LocationNode(org.sonarlint.intellij.ui.nodes.LocationNode) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent) ExpandVetoException(javax.swing.tree.ExpandVetoException)

Example 7 with TreeExpansionEvent

use of javax.swing.event.TreeExpansionEvent in project WorldPainter by Captain-Chaos.

the class MapExplorer method main.

public static void main(String[] args) throws IOException, ClassNotFoundException {
    // Load or initialise configuration
    File configDir = Configuration.getConfigDir();
    if (!configDir.isDirectory()) {
        configDir.mkdirs();
    }
    // This will migrate the configuration directory if necessary
    Configuration config = Configuration.load();
    if (config == null) {
        if (!logger.isDebugEnabled()) {
            // If debug logging is on, the Configuration constructor will
            // already log this
            logger.info("Creating new configuration");
        }
        config = new Configuration();
    }
    Configuration.setInstance(config);
    logger.info("Installation ID: " + config.getUuid());
    // Load and install trusted WorldPainter root certificate
    X509Certificate trustedCert = null;
    try {
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        trustedCert = (X509Certificate) certificateFactory.generateCertificate(Main.class.getResourceAsStream("/wproot.pem"));
        WPTrustManager trustManager = new WPTrustManager(trustedCert);
        SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, new TrustManager[] { trustManager }, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    } catch (CertificateException e) {
        logger.error("Certificate exception while loading trusted root certificate", e);
    } catch (NoSuchAlgorithmException e) {
        logger.error("No such algorithm exception while loading trusted root certificate", e);
    } catch (KeyManagementException e) {
        logger.error("Key management exception while loading trusted root certificate", e);
    }
    // Load the plugins
    if (trustedCert != null) {
        PluginManager.loadPlugins(new File(configDir, "plugins"), trustedCert.getPublicKey());
    } else {
        logger.error("Trusted root certificate not available; not loading plugins");
    }
    WPPluginManager.initialise(config.getUuid());
    SwingUtilities.invokeLater(() -> {
        JFrame frame = new JFrame("Minecraft Map Explorer");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        MapTreeModel treeModel = new MapTreeModel();
        // File minecraftDir = MinecraftUtil.findMinecraftDir();
        // File defaultDir;
        // if (minecraftDir != null) {
        // defaultDir = new File(minecraftDir, "saves");
        // } else {
        // defaultDir = new File(System.getProperty("user.home"));
        // }
        JTree tree = new JTree(treeModel);
        tree.setRootVisible(false);
        tree.setShowsRootHandles(true);
        tree.setCellRenderer(new MapTreeCellRenderer());
        JScrollPane scrollPane = new JScrollPane(tree);
        // tree.expandPath(treeModel.getPath(defaultDir));
        // tree.scrollPathToVisible(treeModel.getPath(defaultDir));
        // Automatically expand any nodes if they only have one child
        tree.addTreeExpansionListener(new TreeExpansionListener() {

            @Override
            public void treeExpanded(TreeExpansionEvent event) {
                Object node = event.getPath().getLastPathComponent();
                if ((!treeModel.isLeaf(node)) && (treeModel.getChildCount(node) == 1)) {
                    tree.expandPath(event.getPath().pathByAddingChild(treeModel.getChild(node, 0)));
                }
            }

            @Override
            public void treeCollapsed(TreeExpansionEvent event) {
            }
        });
        tree.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    TreePath path = tree.getPathForLocation(e.getX(), e.getY());
                    if (path != null) {
                        ((Node) path.getLastPathComponent()).doubleClicked();
                    }
                }
            }
        });
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.setSize(1024, 768);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
}
Also used : TreeExpansionListener(javax.swing.event.TreeExpansionListener) MouseEvent(java.awt.event.MouseEvent) Configuration(org.pepsoft.worldpainter.Configuration) WPTrustManager(org.pepsoft.worldpainter.browser.WPTrustManager) MouseAdapter(org.pepsoft.worldpainter.MouseAdapter) SecureRandom(java.security.SecureRandom) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException) TreePath(javax.swing.tree.TreePath) File(java.io.File) Main(org.pepsoft.worldpainter.Main) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent)

Example 8 with TreeExpansionEvent

use of javax.swing.event.TreeExpansionEvent in project jadx by skylot.

the class MainWindow method initUI.

private void initUI() {
    setMinimumSize(new Dimension(200, 150));
    mainPanel = new JPanel(new BorderLayout());
    splitPane = new JSplitPane();
    splitPane.setResizeWeight(SPLIT_PANE_RESIZE_WEIGHT);
    mainPanel.add(splitPane);
    DefaultMutableTreeNode treeRootNode = new DefaultMutableTreeNode(NLS.str("msg.open_file"));
    treeModel = new DefaultTreeModel(treeRootNode);
    tree = new JTree(treeModel);
    ToolTipManager.sharedInstance().registerComponent(tree);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setFocusable(false);
    tree.addFocusListener(new FocusAdapter() {

        @Override
        public void focusLost(FocusEvent e) {
            tree.setFocusable(false);
        }
    });
    tree.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e)) {
                if (!nodeClickAction(getJNodeUnderMouse(e))) {
                    // click ignored -> switch to focusable mode
                    tree.setFocusable(true);
                    tree.requestFocus();
                }
            } else if (SwingUtilities.isRightMouseButton(e)) {
                treeRightClickAction(e);
            }
        }
    });
    tree.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                nodeClickAction(tree.getLastSelectedPathComponent());
            }
        }
    });
    tree.setCellRenderer(new DefaultTreeCellRenderer() {

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean isLeaf, int row, boolean focused) {
            Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, isLeaf, row, focused);
            if (value instanceof JNode) {
                JNode jNode = (JNode) value;
                setText(jNode.makeStringHtml());
                setIcon(jNode.getIcon());
                setToolTipText(jNode.getTooltip());
            } else {
                setToolTipText(null);
            }
            if (value instanceof JPackage) {
                setEnabled(((JPackage) value).isEnabled());
            }
            return c;
        }
    });
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {

        @Override
        public void treeWillExpand(TreeExpansionEvent event) {
            TreePath path = event.getPath();
            Object node = path.getLastPathComponent();
            if (node instanceof JLoadableNode) {
                ((JLoadableNode) node).loadNode();
            }
            if (!treeReloading) {
                project.addTreeExpansion(getPathExpansion(event.getPath()));
                update();
            }
        }

        @Override
        public void treeWillCollapse(TreeExpansionEvent event) {
            if (!treeReloading) {
                project.removeTreeExpansion(getPathExpansion(event.getPath()));
                update();
            }
        }
    });
    progressPane = new ProgressPanel(this, true);
    IssuesPanel issuesPanel = new IssuesPanel(this);
    JPanel leftPane = new JPanel(new BorderLayout());
    JScrollPane treeScrollPane = new JScrollPane(tree);
    treeScrollPane.setMinimumSize(new Dimension(100, 150));
    JPanel bottomPane = new JPanel(new BorderLayout());
    bottomPane.add(issuesPanel, BorderLayout.PAGE_START);
    bottomPane.add(progressPane, BorderLayout.PAGE_END);
    leftPane.add(treeScrollPane, BorderLayout.CENTER);
    leftPane.add(bottomPane, BorderLayout.PAGE_END);
    splitPane.setLeftComponent(leftPane);
    tabbedPane = new TabbedPane(this);
    tabbedPane.setMinimumSize(new Dimension(150, 150));
    splitPane.setRightComponent(tabbedPane);
    new DropTarget(this, DnDConstants.ACTION_COPY, new MainDropTarget(this));
    heapUsageBar = new HeapUsageBar();
    mainPanel.add(heapUsageBar, BorderLayout.SOUTH);
    verticalSplitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    verticalSplitter.setTopComponent(splitPane);
    verticalSplitter.setResizeWeight(SPLIT_PANE_RESIZE_WEIGHT);
    mainPanel.add(verticalSplitter, BorderLayout.CENTER);
    setContentPane(mainPanel);
    setTitle(DEFAULT_TITLE);
}
Also used : JPanel(javax.swing.JPanel) FocusAdapter(java.awt.event.FocusAdapter) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) JLoadableNode(jadx.gui.treemodel.JLoadableNode) KeyAdapter(java.awt.event.KeyAdapter) IssuesPanel(jadx.gui.ui.panel.IssuesPanel) FocusEvent(java.awt.event.FocusEvent) DefaultTreeCellRenderer(javax.swing.tree.DefaultTreeCellRenderer) KeyEvent(java.awt.event.KeyEvent) BorderLayout(java.awt.BorderLayout) JNode(jadx.gui.treemodel.JNode) Component(java.awt.Component) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent) TreeWillExpandListener(javax.swing.event.TreeWillExpandListener) JScrollPane(javax.swing.JScrollPane) MouseEvent(java.awt.event.MouseEvent) ProgressPanel(jadx.gui.ui.panel.ProgressPanel) MouseAdapter(java.awt.event.MouseAdapter) JPackage(jadx.gui.treemodel.JPackage) Dimension(java.awt.Dimension) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) CacheObject(jadx.gui.utils.CacheObject) DropTarget(java.awt.dnd.DropTarget) JSplitPane(javax.swing.JSplitPane)

Example 9 with TreeExpansionEvent

use of javax.swing.event.TreeExpansionEvent in project java-swing-tips by aterai.

the class EmptyIcon method makeTree.

private static JTree makeTree() {
    Icon emptyIcon = new EmptyIcon();
    UIManager.put("Tree.expandedIcon", new IconUIResource(emptyIcon));
    UIManager.put("Tree.collapsedIcon", new IconUIResource(emptyIcon));
    JTree tree = new JTree();
    tree.setEditable(true);
    tree.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    int row = 0;
    while (row < tree.getRowCount()) {
        tree.expandRow(row++);
    }
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {

        @Override
        public void treeWillExpand(TreeExpansionEvent e) {
        // throws ExpandVetoException {
        // throw new ExpandVetoException(e, "Tree expansion cancelled");
        }

        @Override
        public void treeWillCollapse(TreeExpansionEvent e) throws ExpandVetoException {
            throw new ExpandVetoException(e, "Tree collapse cancelled");
        }
    });
    return tree;
}
Also used : TreeWillExpandListener(javax.swing.event.TreeWillExpandListener) IconUIResource(javax.swing.plaf.IconUIResource) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent) ExpandVetoException(javax.swing.tree.ExpandVetoException)

Example 10 with TreeExpansionEvent

use of javax.swing.event.TreeExpansionEvent in project java-swing-tips by aterai.

the class EmptyIcon method updateUI.

@Override
public void updateUI() {
    setCellRenderer(null);
    removeTreeWillExpandListener(listener);
    super.updateUI();
    setUI(new BasicTreeUI() {

        @Override
        public Rectangle getPathBounds(JTree tree, TreePath path) {
            if (Objects.nonNull(tree) && Objects.nonNull(treeState)) {
                return getTreePathBounds(path, tree.getInsets(), new Rectangle());
            }
            return null;
        }

        private Rectangle getTreePathBounds(TreePath path, Insets insets, Rectangle bounds) {
            Rectangle rect = treeState.getBounds(path, bounds);
            if (Objects.nonNull(rect)) {
                rect.width = tree.getWidth();
                rect.y += insets.top;
            }
            return rect;
        }
    });
    UIManager.put("Tree.repaintWholeRow", Boolean.TRUE);
    TreeCellRenderer r = getCellRenderer();
    setCellRenderer((tree, value, selected, expanded, leaf, row, hasFocus) -> {
        Component c = r.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        c.setBackground(selected ? SELECTED_COLOR : tree.getBackground());
        if (c instanceof JComponent) {
            ((JComponent) c).setOpaque(true);
        }
        return c;
    });
    setOpaque(false);
    setRootVisible(false);
    // https://ateraimemo.com/Swing/TreeNodeCollapseVeto.html
    listener = new TreeWillExpandListener() {

        @Override
        public void treeWillExpand(TreeExpansionEvent e) {
        // throw new ExpandVetoException(e, "Tree expansion cancelled");
        }

        @Override
        public void treeWillCollapse(TreeExpansionEvent e) throws ExpandVetoException {
            throw new ExpandVetoException(e, "Tree collapse cancelled");
        }
    };
    addTreeWillExpandListener(listener);
}
Also used : TreeCellRenderer(javax.swing.tree.TreeCellRenderer) TreeWillExpandListener(javax.swing.event.TreeWillExpandListener) TreePath(javax.swing.tree.TreePath) BasicTreeUI(javax.swing.plaf.basic.BasicTreeUI) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent) ExpandVetoException(javax.swing.tree.ExpandVetoException)

Aggregations

TreeExpansionEvent (javax.swing.event.TreeExpansionEvent)20 TreePath (javax.swing.tree.TreePath)14 TreeExpansionListener (javax.swing.event.TreeExpansionListener)11 TreeWillExpandListener (javax.swing.event.TreeWillExpandListener)10 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)9 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)7 MouseEvent (java.awt.event.MouseEvent)5 JPanel (javax.swing.JPanel)5 JScrollPane (javax.swing.JScrollPane)5 ExpandVetoException (javax.swing.tree.ExpandVetoException)5 TreeNode (javax.swing.tree.TreeNode)5 BorderLayout (java.awt.BorderLayout)4 JTree (javax.swing.JTree)4 Component (java.awt.Component)3 Dimension (java.awt.Dimension)3 MouseAdapter (java.awt.event.MouseAdapter)3 DefaultTreeCellRenderer (javax.swing.tree.DefaultTreeCellRenderer)3 Module (com.intellij.openapi.module.Module)2 Navigatable (com.intellij.pom.Navigatable)2 Tree (com.intellij.ui.treeStructure.Tree)2