Search in sources :

Example 1 with TreeExpansionListener

use of javax.swing.event.TreeExpansionListener in project jdk8u_jdk by JetBrains.

the class bug6505523 method createAndShowGUI.

private static void createAndShowGUI() {
    final DefaultMutableTreeNode root = new DefaultMutableTreeNode("Problem with NPE under JDK 1.6");
    final DefaultMutableTreeNode problematic = new DefaultMutableTreeNode("Expand me and behold a NPE in stderr");
    problematic.add(new DefaultMutableTreeNode("some content"));
    root.add(new DefaultMutableTreeNode("irrelevant..."));
    root.add(problematic);
    final DefaultTreeModel model = new DefaultTreeModel(root);
    tree = new JTree(model);
    tree.setRootVisible(true);
    tree.setShowsRootHandles(true);
    tree.expandRow(0);
    tree.collapseRow(2);
    // this is critical - without dragEnabled everything works
    tree.setDragEnabled(true);
    tree.addTreeExpansionListener(new TreeExpansionListener() {

        @Override
        public void treeExpanded(TreeExpansionEvent event) {
            TreeNode parent = problematic.getParent();
            if (parent instanceof DefaultMutableTreeNode) {
                model.removeNodeFromParent(problematic);
            }
        }

        @Override
        public void treeCollapsed(TreeExpansionEvent event) {
        }
    });
    JFrame frame = new JFrame("JTree Problem");
    frame.add(new JScrollPane(tree));
    frame.setSize(500, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
Also used : JScrollPane(javax.swing.JScrollPane) JTree(javax.swing.JTree) TreeExpansionListener(javax.swing.event.TreeExpansionListener) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) JFrame(javax.swing.JFrame) TreeNode(javax.swing.tree.TreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent)

Example 2 with TreeExpansionListener

use of javax.swing.event.TreeExpansionListener in project sldeditor by robward-scisys.

the class FileSystemExtension method initialise.

/**
 * Initialise.
 *
 * @param parent the parent
 * @param toolMgr the tool manager
 */
@Override
public void initialise(LoadSLDInterface parent, ToolSelectionInterface toolMgr) {
    this.toolMgr = toolMgr;
    this.parentObj = parent;
    if (toolMgr != null) {
        toolMgr.addRecursiveListener(this);
    }
    // Add extensions
    extensionList = FileSystemExtensionFactory.getFileExtensionList(toolMgr);
    extensionPanel = new JPanel();
    extensionPanel.setLayout(new BorderLayout());
    extensionPanel.setPreferredSize(new Dimension(450, 400));
    JPanel toolPanel = null;
    if (toolMgr != null) {
        toolPanel = toolMgr.getPanel();
    }
    if (toolPanel != null) {
        extensionPanel.add(toolPanel, BorderLayout.NORTH);
    }
    tree = new FSTree();
    tree.setRootVisible(true);
    DefaultMutableTreeNode rootNode;
    try {
        rootNode = new DefaultMutableTreeNode(ROOT_NODE);
        model = new DefaultTreeModel(rootNode);
        FileSystemNodeManager.create(tree, model);
        for (FileSystemInterface extension : extensionList) {
            extension.populate(tree, model, rootNode);
        }
    } catch (SecurityException e1) {
        ConsoleManager.getInstance().exception(this, e1);
    }
    tree.setModel(model);
    tree.setDragEnabled(true);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    tree.addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (SwingUtilities.isRightMouseButton(e)) {
                int row = tree.getClosestRowForLocation(e.getX(), e.getY());
                tree.setSelectionRow(row);
                Object selectedItem = tree.getLastSelectedPathComponent();
                Thread t1 = new Thread(new Runnable() {

                    public void run() {
                        JPopupMenu popupMenu = new JPopupMenu();
                        for (FileSystemInterface extension : extensionList) {
                            extension.rightMouseButton(popupMenu, selectedItem, e);
                        }
                        if ((popupMenu.getComponentCount() > 0) && (e != null)) {
                            popupMenu.show(e.getComponent(), e.getX(), e.getY());
                        }
                    }
                });
                t1.start();
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    });
    // Listen for Tree Selection Events
    tree.addTreeExpansionListener(new TreeExpansionListener() {

        /*
             * (non-Javadoc)
             * 
             * @see javax.swing.event.TreeExpansionListener#treeExpanded(javax.swing.event.TreeExpansionEvent)
             */
        public void treeExpanded(TreeExpansionEvent evt) {
            TreePath path = evt.getPath();
            Object selectedItem = path.getLastPathComponent();
            for (FileSystemInterface extension : extensionList) {
                if (extension.treeExpanded(selectedItem)) {
                    ((DefaultTreeModel) tree.getModel()).nodeStructureChanged((TreeNode) selectedItem);
                }
            }
        }

        /*
             * (non-Javadoc)
             * 
             * @see javax.swing.event.TreeExpansionListener#treeCollapsed(javax.swing.event.TreeExpansionEvent)
             */
        public void treeCollapsed(TreeExpansionEvent evt) {
        // Nothing to do
        }
    });
    // Tree selection listener
    tree.setTreeSelection(this);
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
    panel.add(new JScrollPane(tree));
    extensionPanel.add(panel, BorderLayout.CENTER);
    logger.debug("FileSystem initialise finished");
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) TreeExpansionListener(javax.swing.event.TreeExpansionListener) MouseEvent(java.awt.event.MouseEvent) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) FSTree(com.sldeditor.datasource.extension.filesystem.node.FSTree) BoxLayout(javax.swing.BoxLayout) Dimension(java.awt.Dimension) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) JPopupMenu(javax.swing.JPopupMenu) FileSystemInterface(com.sldeditor.common.filesystem.FileSystemInterface) MouseListener(java.awt.event.MouseListener) BorderLayout(java.awt.BorderLayout) TreePath(javax.swing.tree.TreePath) TreeNode(javax.swing.tree.TreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent)

Example 3 with TreeExpansionListener

use of javax.swing.event.TreeExpansionListener in project IBMiProgTool by vzupka.

the class MainWindow method createNewLeftSide.

/**
 * Create split panes containing the PC file tree and table on the left side
 * of the window.
 *
 * @param leftRoot
 */
protected void createNewLeftSide(String leftRoot) {
    // Create new left hand tree
    leftPathString = leftRoot;
    leftTopNode = new DefaultMutableTreeNode(leftPathString);
    leftTreeMap.put(leftRoot, 0);
    // Create left hand tree
    leftTree = new JTree(leftTopNode);
    // Create tree model
    leftTreeModel = (DefaultTreeModel) leftTree.getModel();
    // Mouse listener for left tree
    leftTreeMouseListener = new LeftTreeMouseAdapter();
    leftTree.addMouseListener(leftTreeMouseListener);
    // Tree expansion listener for left tree
    TreeExpansionListener leftTreeExpansionListener = new LeftTreeExpansionListener();
    leftTree.addTreeExpansionListener(leftTreeExpansionListener);
    leftTree.setDragEnabled(true);
    leftTree.setDropMode(DropMode.USE_SELECTION);
    leftTree.setTransferHandler(new LeftTreeTransferHandler(this));
    leftTreeSelModel = leftTree.getSelectionModel();
    leftTreeSelModel.setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    leftRowMapper = leftTreeSelModel.getRowMapper();
    leftTree.setRootVisible(true);
    leftTree.setShowsRootHandles(true);
    leftTree.setSelectionRow(0);
    // Add PC nodes to the left tree
    leftTreeMap.put(leftPathString, 0);
    addPCNodes(Paths.get(leftPathString), leftTopNode);
    leftTree.expandRow(0);
    // Place the left tree in scroll pane
    scrollPaneLeft.setViewportView(leftTree);
    // Change cursor to default
    // setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    // Remove setting last element of messages
    scrollMessagePane.getVerticalScrollBar().removeAdjustmentListener(messageScrollPaneAdjustmentListenerMax);
}
Also used : JTree(javax.swing.JTree) TreeExpansionListener(javax.swing.event.TreeExpansionListener) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode)

Example 4 with TreeExpansionListener

use of javax.swing.event.TreeExpansionListener 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 5 with TreeExpansionListener

use of javax.swing.event.TreeExpansionListener in project cayenne by apache.

the class DbImportTree method createTreeExpandListener.

private void createTreeExpandListener() {
    TreeExpansionListener treeExpansionListener = new TreeExpansionListener() {

        @Override
        public void treeExpanded(TreeExpansionEvent event) {
            TreePath path = event.getPath();
            Object lastPathComponent = path.getLastPathComponent();
            if (!(lastPathComponent instanceof TransferableNode)) {
                return;
            }
            DbImportTreeNode node = (DbImportTreeNode) lastPathComponent;
            if ((node.isIncludeTable() || node.isSchema() || node.isCatalog()) && !node.isLoaded()) {
                // reload tables and columns action.
                LoadDbSchemaAction action = Application.getInstance().getActionManager().getAction(LoadDbSchemaAction.class);
                action.performAction(null, path);
            }
        }

        @Override
        public void treeCollapsed(TreeExpansionEvent event) {
        }
    };
    this.addTreeExpansionListener(treeExpansionListener);
}
Also used : TreeExpansionListener(javax.swing.event.TreeExpansionListener) TreePath(javax.swing.tree.TreePath) DbImportTreeNode(org.apache.cayenne.modeler.dialog.db.load.DbImportTreeNode) LoadDbSchemaAction(org.apache.cayenne.modeler.action.LoadDbSchemaAction) TransferableNode(org.apache.cayenne.modeler.dialog.db.load.TransferableNode) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent)

Aggregations

TreeExpansionListener (javax.swing.event.TreeExpansionListener)12 TreeExpansionEvent (javax.swing.event.TreeExpansionEvent)11 TreePath (javax.swing.tree.TreePath)8 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)6 TreeNode (javax.swing.tree.TreeNode)5 JPanel (javax.swing.JPanel)4 JScrollPane (javax.swing.JScrollPane)4 BorderLayout (java.awt.BorderLayout)3 MouseEvent (java.awt.event.MouseEvent)3 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)3 Component (java.awt.Component)2 Dimension (java.awt.Dimension)2 Comparator (java.util.Comparator)2 Enumeration (java.util.Enumeration)2 JLabel (javax.swing.JLabel)2 JTree (javax.swing.JTree)2 TreeSelectionEvent (javax.swing.event.TreeSelectionEvent)2 TreeSelectionListener (javax.swing.event.TreeSelectionListener)2 ToolTipTreeNode (CCDD.CcddClassesComponent.ToolTipTreeNode)1 ColumnTreeBuilder (com.android.tools.adtui.common.ColumnTreeBuilder)1