Search in sources :

Example 76 with TreeSelectionListener

use of javax.swing.event.TreeSelectionListener in project tetrad by cmu-phil.

the class CategorizingModelChooser method setModelConfigs.

public void setModelConfigs(List<SessionNodeModelConfig> configs) {
    ChooserTreeModel model = new ChooserTreeModel(configs);
    this.tree = new JTree(model);
    this.tree.setCellRenderer(new ChooserRenderer());
    this.tree.setRootVisible(false);
    this.tree.setEditable(false);
    this.tree.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    for (int i = 0; i < this.tree.getRowCount(); i++) {
        this.tree.expandRow(i);
    }
    this.tree.addTreeSelectionListener(new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            TreePath path = e.getPath();
            Object selected = path.getLastPathComponent();
            if (selected instanceof ModelWrapper) {
                String name = ((ModelWrapper) selected).name;
                Preferences.userRoot().put(nodeId, name);
            }
        }
    });
    // select a default value, if one exists
    String storedModelType = getModelTypeFromSessionNode(sessionNode, model);
    if (storedModelType == null) {
        storedModelType = Preferences.userRoot().get(this.nodeId, "");
    }
    System.out.println("Stored model type = " + storedModelType);
    if (storedModelType.length() != 0) {
        for (Map.Entry<String, List<ModelWrapper>> entry : model.map.entrySet()) {
            for (ModelWrapper wrapper : entry.getValue()) {
                if (storedModelType.equals(wrapper.name)) {
                    Object[] path = new Object[] { ChooserTreeModel.ROOT, entry.getKey(), wrapper };
                    this.tree.setSelectionPath(new TreePath(path));
                    break;
                }
            }
        }
    }
}
Also used : TreeSelectionListener(javax.swing.event.TreeSelectionListener) TreePath(javax.swing.tree.TreePath) List(java.util.List) LinkedList(java.util.LinkedList) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) HashMap(java.util.HashMap) Map(java.util.Map)

Example 77 with TreeSelectionListener

use of javax.swing.event.TreeSelectionListener in project antlr4 by tunnelvisionlabs.

the class TreeViewer method showInDialog.

@NotNull
protected static JDialog showInDialog(final TreeViewer viewer) {
    final JDialog dialog = new JDialog();
    dialog.setTitle("Parse Tree Inspector");
    final Preferences prefs = Preferences.userNodeForPackage(TreeViewer.class);
    // Make new content panes
    final Container mainPane = new JPanel(new BorderLayout(5, 5));
    final Container contentPane = new JPanel(new BorderLayout(0, 0));
    contentPane.setBackground(Color.white);
    // Wrap viewer in scroll pane
    JScrollPane scrollPane = new JScrollPane(viewer);
    // Make the scrollpane (containing the viewer) the center component
    contentPane.add(scrollPane, BorderLayout.CENTER);
    JPanel wrapper = new JPanel(new FlowLayout());
    // Add button to bottom
    JPanel bottomPanel = new JPanel(new BorderLayout(0, 0));
    contentPane.add(bottomPanel, BorderLayout.SOUTH);
    JButton ok = new JButton("OK");
    ok.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
        }
    });
    wrapper.add(ok);
    // Add an export-to-png button right of the "OK" button
    JButton png = new JButton("Export as PNG");
    png.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            generatePNGFile(viewer, dialog);
        }
    });
    wrapper.add(png);
    // Add an export-to-png button right of the "OK" button
    JButton svg = new JButton("Export as SVG");
    svg.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            generateSVGFile(viewer, dialog);
        }
    });
    wrapper.add(svg);
    bottomPanel.add(wrapper, BorderLayout.SOUTH);
    // Add scale slider
    double lastKnownViewerScale = prefs.getDouble(DIALOG_VIEWER_SCALE_PREFS_KEY, viewer.getScale());
    viewer.setScale(lastKnownViewerScale);
    int sliderValue = (int) ((lastKnownViewerScale - 1.0) * 1000);
    final JSlider scaleSlider = new JSlider(JSlider.HORIZONTAL, -999, 1000, sliderValue);
    scaleSlider.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            int v = scaleSlider.getValue();
            viewer.setScale(v / 1000.0 + 1.0);
        }
    });
    bottomPanel.add(scaleSlider, BorderLayout.CENTER);
    // Add a JTree representing the parser tree of the input.
    JPanel treePanel = new JPanel(new BorderLayout(5, 5));
    // An "empty" icon that will be used for the JTree's nodes.
    Icon empty = new EmptyIcon();
    UIManager.put("Tree.closedIcon", empty);
    UIManager.put("Tree.openIcon", empty);
    UIManager.put("Tree.leafIcon", empty);
    Tree parseTreeRoot = viewer.getTree().getRoot();
    TreeNodeWrapper nodeRoot = new TreeNodeWrapper(parseTreeRoot, viewer);
    fillTree(nodeRoot, parseTreeRoot, viewer);
    final JTree tree = new JTree(nodeRoot);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            JTree selectedTree = (JTree) e.getSource();
            TreePath path = selectedTree.getSelectionPath();
            if (path != null) {
                TreeNodeWrapper treeNode = (TreeNodeWrapper) path.getLastPathComponent();
                // Set the clicked AST.
                viewer.setTree((Tree) treeNode.getUserObject());
            }
        }
    });
    treePanel.add(new JScrollPane(tree));
    // Create the pane for both the JTree and the AST
    final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treePanel, contentPane);
    mainPane.add(splitPane, BorderLayout.CENTER);
    dialog.setContentPane(mainPane);
    // make viz
    WindowListener exitListener = new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            prefs.putInt(DIALOG_WIDTH_PREFS_KEY, (int) dialog.getSize().getWidth());
            prefs.putInt(DIALOG_HEIGHT_PREFS_KEY, (int) dialog.getSize().getHeight());
            prefs.putDouble(DIALOG_X_PREFS_KEY, dialog.getLocationOnScreen().getX());
            prefs.putDouble(DIALOG_Y_PREFS_KEY, dialog.getLocationOnScreen().getY());
            prefs.putInt(DIALOG_DIVIDER_LOC_PREFS_KEY, splitPane.getDividerLocation());
            prefs.putDouble(DIALOG_VIEWER_SCALE_PREFS_KEY, viewer.getScale());
            dialog.setVisible(false);
            dialog.dispose();
        }
    };
    dialog.addWindowListener(exitListener);
    dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    int width = prefs.getInt(DIALOG_WIDTH_PREFS_KEY, 600);
    int height = prefs.getInt(DIALOG_HEIGHT_PREFS_KEY, 500);
    dialog.setPreferredSize(new Dimension(width, height));
    dialog.pack();
    // After pack(): set the divider at 1/3 (200/600) of the frame.
    int dividerLocation = prefs.getInt(DIALOG_DIVIDER_LOC_PREFS_KEY, 200);
    splitPane.setDividerLocation(dividerLocation);
    if (prefs.getDouble(DIALOG_X_PREFS_KEY, -1) != -1) {
        dialog.setLocation((int) prefs.getDouble(DIALOG_X_PREFS_KEY, 100), (int) prefs.getDouble(DIALOG_Y_PREFS_KEY, 100));
    } else {
        dialog.setLocationRelativeTo(null);
    }
    dialog.setVisible(true);
    return dialog;
}
Also used : JPanel(javax.swing.JPanel) FlowLayout(java.awt.FlowLayout) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) WindowAdapter(java.awt.event.WindowAdapter) TreeSelectionListener(javax.swing.event.TreeSelectionListener) Container(java.awt.Container) BorderLayout(java.awt.BorderLayout) JSlider(javax.swing.JSlider) Tree(org.antlr.v4.runtime.tree.Tree) JTree(javax.swing.JTree) ChangeListener(javax.swing.event.ChangeListener) Preferences(java.util.prefs.Preferences) JScrollPane(javax.swing.JScrollPane) WindowListener(java.awt.event.WindowListener) Dimension(java.awt.Dimension) JTree(javax.swing.JTree) ActionListener(java.awt.event.ActionListener) ChangeEvent(javax.swing.event.ChangeEvent) TreePath(javax.swing.tree.TreePath) WindowEvent(java.awt.event.WindowEvent) Icon(javax.swing.Icon) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) JSplitPane(javax.swing.JSplitPane) JDialog(javax.swing.JDialog) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 78 with TreeSelectionListener

use of javax.swing.event.TreeSelectionListener in project android-classyshark by google.

the class FilesTree method configureJTree.

private void configureJTree(final JTree jTree) {
    jTree.setRootVisible(false);
    DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer) jTree.getCellRenderer();
    cellRenderer.setFont(new Font("Monospaced", Font.PLAIN, 20));
    jTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    jTree.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            Object selection = jTree.getLastSelectedPathComponent();
            if (selection == null)
                return;
            DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode) selection;
            if (selection.toString().endsWith(".dex")) {
                FilesTree.this.viewerController.onSelectedClassName((String) defaultMutableTreeNode.getUserObject());
                return;
            }
            if (selection.toString().endsWith(".jar")) {
                FilesTree.this.viewerController.onSelectedClassName((String) defaultMutableTreeNode.getUserObject());
                return;
            }
            if (selection.toString().endsWith(".apk")) {
                FilesTree.this.viewerController.onSelectedClassName((String) defaultMutableTreeNode.getUserObject());
                return;
            }
            if (selection.toString().endsWith(".so")) {
                FilesTree.this.viewerController.onSelectedClassName((String) defaultMutableTreeNode.getUserObject());
                return;
            }
            if (!defaultMutableTreeNode.isLeaf())
                return;
            if (FilesTree.this.viewerController != null) {
                if (defaultMutableTreeNode.getUserObject() instanceof String) {
                    FilesTree.this.viewerController.onSelectedClassName((String) defaultMutableTreeNode.getUserObject());
                } else {
                    FilesTree.this.viewerController.onSelectedClassName(((NodeInfo) defaultMutableTreeNode.getUserObject()).fullname);
                }
            }
        }
    });
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeSelectionListener(javax.swing.event.TreeSelectionListener) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) DefaultTreeCellRenderer(javax.swing.tree.DefaultTreeCellRenderer) Font(java.awt.Font)

Example 79 with TreeSelectionListener

use of javax.swing.event.TreeSelectionListener in project android-classyshark by google.

the class MethodsCountPanel method setup.

private void setup() {
    this.setLayout(new BorderLayout());
    treeModel = new DefaultTreeModel(new DefaultMutableTreeNode(null));
    jTree = new JTree(treeModel);
    jTree.setRootVisible(false);
    jTree.setCellRenderer(new CellRenderer());
    theme.applyTo(jTree);
    DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer) jTree.getCellRenderer();
    cellRenderer.setFont(new Font("Monospaced", Font.PLAIN, 20));
    jTree.setCellRenderer(cellRenderer);
    jTree.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            Object selection = jTree.getLastSelectedPathComponent();
            DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode) selection;
            ClassNode node = (ClassNode) defaultMutableTreeNode.getUserObject();
            viewerController.onSelectedMethodCount(node);
        }
    });
    JScrollPane jScrollPane = new JScrollPane(jTree);
    this.setBorder(new EmptyBorder(0, 0, 0, 0));
    this.add(jScrollPane, BorderLayout.CENTER);
    theme.applyTo(jScrollPane);
    jTree.setDragEnabled(true);
    jTree.setTransferHandler(new FileTransferHandler(viewerController));
}
Also used : JScrollPane(javax.swing.JScrollPane) ClassNode(com.google.classyshark.silverghost.methodscounter.ClassNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) CellRenderer(com.google.classyshark.gui.panel.tree.CellRenderer) DefaultTreeCellRenderer(javax.swing.tree.DefaultTreeCellRenderer) TreeSelectionListener(javax.swing.event.TreeSelectionListener) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) FileTransferHandler(com.google.classyshark.gui.panel.FileTransferHandler) DefaultTreeCellRenderer(javax.swing.tree.DefaultTreeCellRenderer) Font(java.awt.Font) JTree(javax.swing.JTree) BorderLayout(java.awt.BorderLayout) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) EmptyBorder(javax.swing.border.EmptyBorder)

Example 80 with TreeSelectionListener

use of javax.swing.event.TreeSelectionListener in project qi4j-sdk by Qi4j.

the class EntityViewer method initUI.

private void initUI() {
    propertiesPanel = new PropertiesPanel();
    propertiesPanel.initializeQi4J(qi4jspi);
    propertiesAreaPane.add(propertiesPanel, BorderLayout.CENTER);
    treePanel = new TreePanel();
    treePanel.initializeQi4J(qi4jspi, model);
    treePanel.reload();
    splitPane.setLeftComponent(treePanel);
    splitPane.setDividerLocation(200);
    DefaultComboBoxModel entityComboModel = new DefaultComboBoxModel();
    entitiesCombo.setModel(entityComboModel);
    entitiesCombo.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent evt) {
            entitiesComboItemStateChanged(evt);
        }
    });
    treePanel.getTreeComponent().addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent evt) {
            treePanelValueChanged(evt);
        }
    });
    initEntityCombo(entityComboModel);
}
Also used : ItemEvent(java.awt.event.ItemEvent) ItemListener(java.awt.event.ItemListener) TreeSelectionListener(javax.swing.event.TreeSelectionListener) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent)

Aggregations

TreeSelectionListener (javax.swing.event.TreeSelectionListener)87 TreeSelectionEvent (javax.swing.event.TreeSelectionEvent)82 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)36 TreePath (javax.swing.tree.TreePath)33 JTree (javax.swing.JTree)27 JScrollPane (javax.swing.JScrollPane)25 ActionEvent (java.awt.event.ActionEvent)23 JPanel (javax.swing.JPanel)22 ActionListener (java.awt.event.ActionListener)20 MouseEvent (java.awt.event.MouseEvent)16 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)16 BorderLayout (java.awt.BorderLayout)14 Dimension (java.awt.Dimension)14 GridBagConstraints (java.awt.GridBagConstraints)12 GridBagLayout (java.awt.GridBagLayout)12 Insets (java.awt.Insets)12 JButton (javax.swing.JButton)11 Tree (com.intellij.ui.treeStructure.Tree)10 KeyAdapter (java.awt.event.KeyAdapter)9 KeyEvent (java.awt.event.KeyEvent)9