Search in sources :

Example 11 with TreeSelectionListener

use of javax.swing.event.TreeSelectionListener in project felix by apache.

the class OBRPlugin method createEventListeners.

private void createEventListeners() {
    // Create action listeners.
    m_addRepoButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            synchronized (OBRPlugin.this) {
                if (m_repoAdmin == null) {
                    return;
                }
                String s = JOptionPane.showInputDialog(OBRPlugin.this, "Enter repository URL:");
                if (s != null) {
                    try {
                        m_repoAdmin.addRepository(new URL(s));
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
                // Update the table.
                initializeRootNode();
            }
        }
    });
    m_removeRepoButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            synchronized (OBRPlugin.this) {
                if (m_repoAdmin == null) {
                    return;
                }
                TreePath[] paths = m_bundleTree.getSelectionPaths();
                for (int i = 0; i < paths.length; i++) {
                    SimpleTreeNode node = (SimpleTreeNode) paths[i].getLastPathComponent();
                    if (node.getObject() instanceof Repository) {
                        m_repoAdmin.removeRepository(((Repository) ((SimpleTreeNode) paths[i].getLastPathComponent()).getObject()).getURL());
                    }
                }
                // Update the table.
                initializeRootNode();
            }
        }
    });
    m_refreshRepoButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            synchronized (OBRPlugin.this) {
                if (m_repoAdmin == null) {
                    return;
                }
                TreePath[] paths = m_bundleTree.getSelectionPaths();
                for (int i = 0; i < paths.length; i++) {
                    SimpleTreeNode node = (SimpleTreeNode) paths[i].getLastPathComponent();
                    if (node.getObject() instanceof Repository) {
                        try {
                            // Adding the repository again causes it to be reparsed.
                            m_repoAdmin.addRepository(((Repository) node.getObject()).getURL());
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
                // Update the table.
                initializeRootNode();
            }
        }
    });
    ActionListener al = new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            boolean start = event.getActionCommand().equals(START_BUTTON);
            synchronized (OBRPlugin.this) {
                if (m_repoAdmin == null) {
                    return;
                }
                Resolver resolver = m_repoAdmin.resolver();
                TreePath[] paths = m_bundleTree.getSelectionPaths();
                for (int i = 0; i < paths.length; i++) {
                    SimpleTreeNode node = (SimpleTreeNode) paths[i].getLastPathComponent();
                    if (node.getObject() instanceof Resource) {
                        resolver.add((Resource) node.getObject());
                    }
                }
                if ((resolver.getAddedResources() != null) && (resolver.getAddedResources().length > 0)) {
                    if (resolver.resolve()) {
                        m_out.println("Target resource(s):");
                        printUnderline(m_out, 19);
                        Resource[] resources = resolver.getAddedResources();
                        for (int resIdx = 0; (resources != null) && (resIdx < resources.length); resIdx++) {
                            m_out.println("   " + resources[resIdx].getPresentationName() + " (" + resources[resIdx].getVersion() + ")");
                        }
                        resources = resolver.getRequiredResources();
                        if ((resources != null) && (resources.length > 0)) {
                            m_out.println("\nRequired resource(s):");
                            printUnderline(m_out, 21);
                            for (int resIdx = 0; resIdx < resources.length; resIdx++) {
                                m_out.println("   " + resources[resIdx].getPresentationName() + " (" + resources[resIdx].getVersion() + ")");
                            }
                        }
                        resources = resolver.getOptionalResources();
                        if ((resources != null) && (resources.length > 0)) {
                            m_out.println("\nOptional resource(s):");
                            printUnderline(m_out, 21);
                            for (int resIdx = 0; resIdx < resources.length; resIdx++) {
                                m_out.println("   " + resources[resIdx].getPresentationName() + " (" + resources[resIdx].getVersion() + ")");
                            }
                        }
                        try {
                            m_out.print("\nDeploying...");
                            resolver.deploy(start);
                            m_out.println("done.");
                        } catch (IllegalStateException ex) {
                            m_out.println(ex);
                        }
                    } else {
                        Requirement[] reqs = resolver.getUnsatisfiedRequirements();
                        if ((reqs != null) && (reqs.length > 0)) {
                            m_out.println("Unsatisfied requirement(s):");
                            printUnderline(m_out, 27);
                            for (int reqIdx = 0; reqIdx < reqs.length; reqIdx++) {
                                m_out.println("   " + reqs[reqIdx].getFilter());
                                Resource[] resources = resolver.getResources(reqs[reqIdx]);
                                for (int resIdx = 0; resIdx < resources.length; resIdx++) {
                                    m_out.println("      " + resources[resIdx].getPresentationName());
                                }
                            }
                        } else {
                            m_out.println("Could not resolve targets.");
                        }
                    }
                    m_out.println("");
                }
            }
        }
    };
    m_deployButton.addActionListener(al);
    m_startButton.addActionListener(al);
    m_infoButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            synchronized (OBRPlugin.this) {
                if (m_repoAdmin == null) {
                    return;
                }
                TreePath[] paths = m_bundleTree.getSelectionPaths();
                for (int i = 0; i < paths.length; i++) {
                    if (i != 0) {
                        m_out.println("");
                    }
                    printInfo(m_out, ((SimpleTreeNode) paths[i].getLastPathComponent()).getObject());
                }
                m_out.println("");
            }
        }
    });
    m_clearButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            synchronized (OBRPlugin.this) {
                m_soa.setText("");
            }
        }
    });
    m_bundleTree.addTreeSelectionListener(new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            if (m_repoAdmin == null) {
                return;
            }
            TreePath[] paths = m_bundleTree.getSelectionPaths();
            boolean repoOnly = true;
            if (paths != null) {
                for (int i = 0; repoOnly && (i < paths.length); i++) {
                    SimpleTreeNode node = (SimpleTreeNode) paths[i].getLastPathComponent();
                    if (!(node.getObject() instanceof Repository)) {
                        repoOnly = false;
                    }
                }
            }
            m_removeRepoButton.setEnabled((paths != null) && repoOnly);
            m_refreshRepoButton.setEnabled((paths != null) && repoOnly);
            m_infoButton.setEnabled((paths != null) && (paths.length > 0));
        }
    });
}
Also used : ActionEvent(java.awt.event.ActionEvent) TreeSelectionListener(javax.swing.event.TreeSelectionListener) URL(java.net.URL) ActionListener(java.awt.event.ActionListener) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent)

Example 12 with TreeSelectionListener

use of javax.swing.event.TreeSelectionListener in project JMRI by JMRI.

the class TreePaneDemo method testWithSelect.

@Test
public void testWithSelect() {
    Assume.assumeFalse(GraphicsEnvironment.isHeadless());
    frame.setTitle("listener test");
    pane.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            JTree tree = (JTree) e.getSource();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
            if (node == null) {
                return;
            }
            System.out.print("Test prints selected treenode " + node);
            if (node.getUserObject() instanceof NodeTreeRep.SelectionKey) {
                System.out.println(" and invokes");
                ((NodeTreeRep.SelectionKey) node.getUserObject()).select(node);
            } else {
                System.out.println();
            }
        }
    });
    Message msg;
    msg = new ProducerIdentifiedMessage(nid2, eventA, EventState.Unknown);
    store.put(msg, null);
    store.put(pipmsg, null);
    msg = new SimpleNodeIdentInfoReplyMessage(nid2, nid2, new byte[] { 0x01, 0x31, 0x32, 0x33, 0x41, 0x42, (byte) 0xC2, (byte) 0xA2, 0x44, 0x00 });
    store.put(msg, null);
}
Also used : JTree(javax.swing.JTree) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) SimpleNodeIdentInfoReplyMessage(org.openlcb.SimpleNodeIdentInfoReplyMessage) Message(org.openlcb.Message) ProducerIdentifiedMessage(org.openlcb.ProducerIdentifiedMessage) ProtocolIdentificationReplyMessage(org.openlcb.ProtocolIdentificationReplyMessage) SimpleNodeIdentInfoReplyMessage(org.openlcb.SimpleNodeIdentInfoReplyMessage) NodeTreeRep(org.openlcb.swing.networktree.NodeTreeRep) TreeSelectionListener(javax.swing.event.TreeSelectionListener) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) ProducerIdentifiedMessage(org.openlcb.ProducerIdentifiedMessage) Test(org.junit.Test)

Example 13 with TreeSelectionListener

use of javax.swing.event.TreeSelectionListener in project JMRI by JMRI.

the class NetworkTreePane method initComponents.

@Override
public void initComponents(CanSystemConnectionMemo memo) {
    this.memo = memo;
    memo.getTrafficController().addCanListener(this);
    // add GUI components
    setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.Y_AXIS));
    treePane = new TreePane();
    treePane.setPreferredSize(new Dimension(300, 300));
    treePane.initComponents((MimicNodeStore) memo.get(MimicNodeStore.class), (Connection) memo.get(Connection.class), (NodeID) memo.get(NodeID.class), new ActionLoader(memo.get(OlcbInterface.class)));
    add(treePane);
    treePane.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            JTree tree = (JTree) e.getSource();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
            if (node == null) {
                return;
            }
            if (node.getUserObject() instanceof NodeTreeRep.SelectionKey) {
                ((NodeTreeRep.SelectionKey) node.getUserObject()).select(node);
            }
        }
    });
}
Also used : TreePane(org.openlcb.swing.networktree.TreePane) JTree(javax.swing.JTree) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) NodeTreeRep(org.openlcb.swing.networktree.NodeTreeRep) TreeSelectionListener(javax.swing.event.TreeSelectionListener) Dimension(java.awt.Dimension) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent)

Example 14 with TreeSelectionListener

use of javax.swing.event.TreeSelectionListener in project intellij-plugins by JetBrains.

the class ChooseBuildConfigurationDialog method createCenterPanel.

@Override
protected JComponent createCenterPanel() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    List<Module> modules = new ArrayList<>(myTreeItems.keySet());
    Collections.sort(modules, (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
    for (Module module : modules) {
        DefaultMutableTreeNode moduleNode = new DefaultMutableTreeNode(module, true);
        root.add(moduleNode);
        List<FlexBCConfigurable> bcs = myTreeItems.get(module);
        Collections.sort(bcs, (o1, o2) -> o1.getDisplayName().compareToIgnoreCase(o2.getDisplayName()));
        for (FlexBCConfigurable bc : bcs) {
            DefaultMutableTreeNode bcNode = new DefaultMutableTreeNode(bc, false);
            moduleNode.add(bcNode);
        }
    }
    myTree.setModel(new DefaultTreeModel(root));
    myTree.setRootVisible(false);
    new TreeSpeedSearch(myTree, o -> {
        Object lastPathComponent = o.getLastPathComponent();
        return getText((DefaultMutableTreeNode) lastPathComponent);
    }, true).setComparator(new SpeedSearchComparator(false));
    TreeUIHelper.getInstance().installTreeSpeedSearch(myTree);
    myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    myTree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            updateOnSelectionChange();
        }
    });
    myTree.setCellRenderer(new ColoredTreeCellRenderer() {

        @Override
        public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
            Object object = treeNode.getUserObject();
            if (object instanceof Module) {
                Module module = (Module) object;
                setIcon(ModuleType.get(module).getIcon());
                append(module.getName());
            } else if (object instanceof FlexBCConfigurable) {
                FlexBCConfigurable configurable = (FlexBCConfigurable) object;
                setIcon(configurable.getIcon());
                BCUtils.renderBuildConfiguration(configurable.getEditableObject(), null).appendToComponent(this);
            }
        }
    });
    TreeUtil.expandAll(myTree);
    new DoubleClickListener() {

        @Override
        protected boolean onDoubleClick(MouseEvent e) {
            if (mySelection != null) {
                doOKAction();
                return true;
            }
            return false;
        }
    }.installOn(myTree);
    myTree.addKeyListener(new KeyAdapter() {

        @Override
        public void keyTyped(KeyEvent e) {
            if (e.getKeyChar() == KeyEvent.VK_ENTER) {
                doOKAction();
            }
        }
    });
    updateOnSelectionChange();
    return myContentPane;
}
Also used : FlexModuleType(com.intellij.lang.javascript.flex.FlexModuleType) HashMap(com.intellij.util.containers.HashMap) ContainerUtil(com.intellij.util.containers.ContainerUtil) KeyAdapter(java.awt.event.KeyAdapter) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) ArrayList(java.util.ArrayList) BCUtils(com.intellij.lang.javascript.flex.projectStructure.options.BCUtils) TreeSelectionListener(javax.swing.event.TreeSelectionListener) DialogWrapper(com.intellij.openapi.ui.DialogWrapper) Map(java.util.Map) Project(com.intellij.openapi.project.Project) Tree(com.intellij.ui.treeStructure.Tree) ModuleType(com.intellij.openapi.module.ModuleType) Module(com.intellij.openapi.module.Module) FlexBuildConfigurationsExtension(com.intellij.lang.javascript.flex.projectStructure.FlexBuildConfigurationsExtension) ModuleStructureConfigurable(com.intellij.openapi.roots.ui.configuration.projectRoot.ModuleStructureConfigurable) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) TreeUtil(com.intellij.util.ui.tree.TreeUtil) FlexBCConfigurator(com.intellij.lang.javascript.flex.projectStructure.FlexBCConfigurator) TreeSelectionModel(javax.swing.tree.TreeSelectionModel) KeyEvent(java.awt.event.KeyEvent) com.intellij.ui(com.intellij.ui) MouseEvent(java.awt.event.MouseEvent) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Collections(java.util.Collections) Condition(com.intellij.openapi.util.Condition) javax.swing(javax.swing) MouseEvent(java.awt.event.MouseEvent) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) KeyAdapter(java.awt.event.KeyAdapter) ArrayList(java.util.ArrayList) TreeSelectionListener(javax.swing.event.TreeSelectionListener) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) KeyEvent(java.awt.event.KeyEvent) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) Module(com.intellij.openapi.module.Module)

Example 15 with TreeSelectionListener

use of javax.swing.event.TreeSelectionListener in project JMRI by JMRI.

the class EnumVariableValue method getNewRep.

@Override
public Component getNewRep(String format) {
    // sort on format type
    if (format.equals("checkbox")) {
        // this only makes sense if there are exactly two options
        ComboCheckBox b = new ComboCheckBox(_value, this);
        comboCBs.add(b);
        if (getReadOnly()) {
            b.setEnabled(false);
        }
        updateRepresentation(b);
        return b;
    } else if (format.equals("radiobuttons")) {
        ComboRadioButtons b = new ComboRadioButtons(_value, this);
        comboRBs.add(b);
        if (getReadOnly()) {
            b.setEnabled(false);
        }
        updateRepresentation(b);
        return b;
    } else if (format.equals("onradiobutton")) {
        ComboRadioButtons b = new ComboOnRadioButton(_value, this);
        comboRBs.add(b);
        if (getReadOnly()) {
            b.setEnabled(false);
        }
        updateRepresentation(b);
        return b;
    } else if (format.equals("offradiobutton")) {
        ComboRadioButtons b = new ComboOffRadioButton(_value, this);
        comboRBs.add(b);
        if (getReadOnly()) {
            b.setEnabled(false);
        }
        updateRepresentation(b);
        return b;
    } else if (format.equals("tree")) {
        DefaultTreeModel dModel = new DefaultTreeModel(treeNodes.getFirst());
        JTree dTree = new JTree(dModel);
        trees.add(dTree);
        JScrollPane dScroll = new JScrollPane(dTree);
        dTree.setRootVisible(false);
        dTree.setShowsRootHandles(true);
        dTree.setScrollsOnExpand(true);
        dTree.setExpandsSelectedPaths(true);
        dTree.getSelectionModel().setSelectionMode(DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
        // arrange for only leaf nodes can be selected
        dTree.addTreeSelectionListener(new TreeSelectionListener() {

            @Override
            public void valueChanged(TreeSelectionEvent e) {
                TreePath[] paths = e.getPaths();
                for (int i = 0; i < paths.length; i++) {
                    DefaultMutableTreeNode o = (DefaultMutableTreeNode) paths[i].getLastPathComponent();
                    if (o.getChildCount() > 0) {
                        ((JTree) e.getSource()).removeSelectionPath(paths[i]);
                    }
                }
                // now record selection
                if (paths.length >= 1) {
                    if (paths[0].getLastPathComponent() instanceof TreeLeafNode) {
                        // update value of Variable
                        setValue(_valueArray[((TreeLeafNode) paths[0].getLastPathComponent()).index]);
                    }
                }
            }
        });
        // select initial value
        TreePath path = _pathArray[_value.getSelectedIndex()];
        dTree.setSelectionPath(path);
        // ensure selection is in visible portion of JScrollPane
        dTree.scrollPathToVisible(path);
        if (getReadOnly()) {
            log.error("read only variables cannot use tree format: {}", item());
        }
        updateRepresentation(dScroll);
        return dScroll;
    } else {
        // return a new JComboBox representing the same model
        VarComboBox b = new VarComboBox(_value.getModel(), this);
        comboVars.add(b);
        if (getReadOnly()) {
            b.setEnabled(false);
        }
        updateRepresentation(b);
        return b;
    }
}
Also used : JScrollPane(javax.swing.JScrollPane) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeSelectionListener(javax.swing.event.TreeSelectionListener) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent)

Aggregations

TreeSelectionListener (javax.swing.event.TreeSelectionListener)86 TreeSelectionEvent (javax.swing.event.TreeSelectionEvent)81 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)36 TreePath (javax.swing.tree.TreePath)32 JTree (javax.swing.JTree)28 JScrollPane (javax.swing.JScrollPane)26 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 Dimension (java.awt.Dimension)15 BorderLayout (java.awt.BorderLayout)14 GridBagConstraints (java.awt.GridBagConstraints)11 GridBagLayout (java.awt.GridBagLayout)11 Insets (java.awt.Insets)11 JButton (javax.swing.JButton)11 Tree (com.intellij.ui.treeStructure.Tree)10 KeyAdapter (java.awt.event.KeyAdapter)9 KeyEvent (java.awt.event.KeyEvent)9