Example usage for javax.swing.event TreeSelectionListener TreeSelectionListener

List of usage examples for javax.swing.event TreeSelectionListener TreeSelectionListener

Introduction

In this page you can find the example usage for javax.swing.event TreeSelectionListener TreeSelectionListener.

Prototype

TreeSelectionListener

Source Link

Usage

From source file:org.objectpocket.viewer.Viewer.java

/**
 * Initialize the contents of the frame.
 *//*from w  ww  . ja  v  a2 s  . com*/
private void initialize() {
    viewerFrame = new JFrame();
    viewerFrame.setTitle("ObjectPocketViewer");
    viewerFrame.setBounds(100, 100, 1427, 524);
    viewerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    splitPane = new JSplitPane();
    splitPane.setResizeWeight(0.2);
    viewerFrame.getContentPane().add(splitPane, BorderLayout.CENTER);

    classTree = new JTree();
    classTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    classTree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            updateTable();
        }
    });
    splitPane.setLeftComponent(classTree);

    panel = new JPanel();
    splitPane.setRightComponent(panel);
    panel.setLayout(new BorderLayout(0, 0));

    scrollPane = new JScrollPane();
    panel.add(scrollPane);
    objectTable = new JTable();
    objectTable.setAutoCreateRowSorter(true);
    objectTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    scrollPane.setViewportView(objectTable);
    panel.add(scrollPane, BorderLayout.CENTER);

    filterPanel = new JPanel();
    panel.add(filterPanel, BorderLayout.NORTH);
    filterPanel.setLayout(new BorderLayout(0, 0));

    filterTextField = new JTextField();
    filterTextField.addKeyListener(new KeyAdapter() {
        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyChar() == KeyEvent.VK_ENTER) {
                updateTable();
            }
        }
    });
    filterPanel.add(filterTextField, BorderLayout.CENTER);
    filterTextField.setColumns(10);

    filterButton = new JButton("apply filter");
    filterButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            updateTable();
        }
    });
    filterPanel.add(filterButton, BorderLayout.EAST);

    statusLabel = new JLabel("");
    panel.add(statusLabel, BorderLayout.SOUTH);

}

From source file:org.objectstyle.cayenne.modeler.ProjectTreeView.java

private void initController() {
    treeSelectionListener = new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            processSelection(e.getPath());
        }//  www .  ja v a2  s  . com
    };

    addTreeSelectionListener(treeSelectionListener);

    mediator.addDomainListener(this);
    mediator.addDomainDisplayListener(this);
    mediator.addDataNodeListener(this);
    mediator.addDataNodeDisplayListener(this);
    mediator.addDataMapListener(this);
    mediator.addDataMapDisplayListener(this);
    mediator.addObjEntityListener(this);
    mediator.addObjEntityDisplayListener(this);
    mediator.addDbEntityListener(this);
    mediator.addDbEntityDisplayListener(this);
    mediator.addProcedureListener(this);
    mediator.addProcedureDisplayListener(this);
    mediator.addQueryListener(this);
    mediator.addQueryDisplayListener(this);
}

From source file:org.openconcerto.erp.panel.ITreeSelection.java

public ITreeSelection(SQLElement element) {
    super();/*w  w w  . ja  v  a 2  s . c  om*/
    DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
    renderer.setOpenIcon(null);
    renderer.setClosedIcon(null);
    renderer.setLeafIcon(null);
    this.setCellRenderer(renderer);

    this.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    this.element = element;

    this.supp = new PropertyChangeSupport(this);

    // Value changed
    this.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            ITreeSelection.this.supp.firePropertyChange("value", null, getUncheckedValue());
        }
    });

}

From source file:org.openmicroscopy.shoola.agents.measurement.view.ObjectManager.java

/** Initializes the components composing the display. */
private void initComponents() {
    ROINode root = new ROINode("root");
    objectsTable = new ROITable(new ROITableModel(root, COLUMN_NAMES), COLUMN_NAMES, this);
    objectsTable.setRootVisible(false);/*  w  w  w. j a  va2 s . co m*/
    objectsTable.setColumnSelectionAllowed(true);
    objectsTable.setRowSelectionAllowed(true);
    treeSelectionListener = new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            TreeSelectionModel tsm = objectsTable.getTreeSelectionModel();
            if (tsm.isSelectionEmpty())
                return;
            int[] index = tsm.getSelectionRows();
            if (index.length == 0)
                return;
            if (index.length == 1) {
                ROINode node = (ROINode) objectsTable.getNodeAtRow(objectsTable.getSelectedRow());
                if (node == null)
                    return;
                Object nodeValue = node.getUserObject();
                view.clearInspector();
                if (nodeValue instanceof ROIShape) {
                    view.selectFigure(((ROIShape) nodeValue).getFigure());
                }
                int col = objectsTable.getSelectedColumn();
                int row = objectsTable.getSelectedRow();

                if (row < 0 || col < 0)
                    return;
            } else {
                ROIShape shape;
                for (int i = 0; i < index.length; i++) {
                    shape = objectsTable.getROIShapeAtRow(index[i]);
                    if (shape != null) {
                        view.selectFigure(shape.getFigure());
                        requestFocus();
                    }
                }
            }
        }
    };

    objectsTable.addTreeSelectionListener(treeSelectionListener);

    ColumnFactory columnFactory = new ColumnFactory() {

        public void configureColumnWidths(JXTable table, TableColumnExt columnExt) {
            columnExt.setPreferredWidth(COLUMN_WIDTHS.get(columnExt.getHeaderValue()));
        }
    };
    objectsTable.setHorizontalScrollEnabled(true);
    objectsTable.setColumnControlVisible(true);
    objectsTable.setColumnFactory(columnFactory);
}

From source file:org.openmicroscopy.shoola.agents.treeviewer.browser.BrowserUI.java

/** 
 * Helper method to create the trees hosting the display. 
 * // w ww  .j  a v  a  2 s  . c o  m
 * @param exp The logged in experimenter.
 */
private void createTrees(ExperimenterData exp) {
    treeDisplay = new DnDTree(model.getUserID(), TreeViewerAgent.isAdministrator());
    treeDisplay.addPropertyChangeListener(this);
    String key = "meta pressed A";
    if (UIUtilities.isWindowsOS())
        key = "ctrl pressed A";
    KeyStroke ks = KeyStroke.getKeyStroke(key);
    treeDisplay.getInputMap().put(ks, "none");
    treeDisplay.setVisible(true);
    treeDisplay.setRootVisible(false);
    ToolTipManager.sharedInstance().registerComponent(treeDisplay);
    treeDisplay.setCellRenderer(new TreeCellRenderer(model.getUserID()));
    treeDisplay.setShowsRootHandles(true);
    TreeImageSet root = new TreeImageSet("");
    treeDisplay.setModel(new DefaultTreeModel(root));
    if (model.getBrowserType() != Browser.ADMIN_EXPLORER) {
        TreeImageDisplay node = buildTreeNodes(exp);
        if (node != null)
            treeDisplay.collapsePath(new TreePath(node.getPath()));
    }
    //Add Listeners
    //treeDisplay.requestFocus();
    treeDisplay.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            mousePressedTime = e.getWhen();
            rightClickPad = UIUtilities.isMacOS() && SwingUtilities.isLeftMouseButton(e) && e.isControlDown();
            rightClickButton = SwingUtilities.isRightMouseButton(e);
            ctrl = e.isControlDown();
            if (UIUtilities.isMacOS())
                ctrl = e.isMetaDown();
            leftMouseButton = SwingUtilities.isLeftMouseButton(e);
            if (UIUtilities.isMacOS() || UIUtilities.isLinuxOS())
                onClick(e, false);
        }

        public void mouseReleased(MouseEvent e) {
            leftMouseButton = SwingUtilities.isLeftMouseButton(e);
            if (UIUtilities.isWindowsOS())
                onClick(e, true);
        }

        // public void mouseMoved(MouseEvent e) { rollOver(e); }
    });
    treeDisplay.addMouseMotionListener(new MouseMotionAdapter() {

        public void mouseMoved(MouseEvent e) {
            rollOver(e);
        }
    });
    treeDisplay.addTreeExpansionListener(listener);
    selectionListener = new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            event = e;
            eventHandledTime = System.currentTimeMillis();

            if (delayedHandlingTreeSelection)
                /* mouse click delayed handling until this event occurred */
                handleTreeSelection();

            switch (keyEvent) {
            case KeyEvent.VK_DOWN:
            case KeyEvent.VK_UP:
                TreePath[] paths = treeDisplay.getSelectionPaths();
                if (paths != null)
                    controller.onClick(Arrays.asList(paths));
                else
                    controller.onClick(new ArrayList<TreePath>());
                break;
            }
        }
    };
    treeDisplay.addTreeSelectionListener(selectionListener);
    //remove standard behaviour
    treeDisplay.addKeyListener(new KeyAdapter() {

        public void keyPressed(KeyEvent e) {
            ctrl = false;
            switch (e.getKeyCode()) {
            case KeyEvent.VK_ENTER:
                ViewCmd cmd = new ViewCmd(model.getParentModel(), true);
                cmd.execute();
                break;
            case KeyEvent.VK_DELETE:
                switch (model.getState()) {
                case Browser.LOADING_DATA:
                case Browser.LOADING_LEAVES:
                    //case Browser.COUNTING_ITEMS:  
                    break;
                default:
                    model.delete();
                }
                break;
            case KeyEvent.VK_CONTROL:
                if (!UIUtilities.isMacOS())
                    ctrl = true;
                break;
            case KeyEvent.VK_META:
                if (UIUtilities.isMacOS())
                    ctrl = true;
                break;
            case KeyEvent.VK_A:
                if (UIUtilities.isWindowsOS() && e.isControlDown()
                        || !UIUtilities.isWindowsOS() && e.isMetaDown()) {
                    handleMultiSelection();
                }
                break;
            case KeyEvent.VK_DOWN:
            case KeyEvent.VK_UP:
            case KeyEvent.VK_RIGHT:
                keyEvent = e.getKeyCode();
                break;
            case KeyEvent.VK_LEFT:
                TreePath[] paths = treeDisplay.getSelectionPaths();
                TreeImageDisplay node;
                Object o;
                for (int i = 0; i < paths.length; i++) {
                    o = paths[i].getLastPathComponent();
                    if (o instanceof TreeImageDisplay) {
                        node = (TreeImageDisplay) o;
                        if (node.isExpanded())
                            node.setExpanded(false);
                    }
                }
            }
        }

        public void keyReleased(KeyEvent e) {
            ctrl = false;
            keyEvent = -1;
        }

    });
}

From source file:org.openmicroscopy.shoola.agents.util.SelectionWizardUI.java

/** Initializes the components composing the display.*/
private void initComponents() {
    filterAnywhere = true;/*from w w  w  .j  a  va  2  s.  c o  m*/
    filterArea = new JTextField();
    originalColor = filterArea.getForeground();
    setTextFieldDefault(DEFAULT_FILTER_TEXT);
    StringBuilder builder = new StringBuilder();
    builder.append("Filter");
    if (TagAnnotationData.class.equals(type)) {
        builder.append(" Tags.");
    } else if (FileAnnotationData.class.equals(type)) {
        builder.append(" Attachments.");
    } else if (DatasetData.class.equals(type)) {
        builder.append(" Datasets.");
    } else
        builder.append(".");
    filterArea.setToolTipText(builder.toString());
    filterArea.getDocument().addDocumentListener(this);
    filterArea.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent evt) {
            String value = filterArea.getText();
            if (StringUtils.isBlank(value)) {
                setTextFieldDefault(DEFAULT_FILTER_TEXT);
            }
        }

        @Override
        public void focusGained(FocusEvent evt) {
        }
    });
    filterArea.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent evt) {
            String value = filterArea.getText();
            if (DEFAULT_FILTER_TEXT.equals(value)) {
                setTextFieldDefault(null);
            }
        }
    });
    filterArea.addKeyListener(new KeyAdapter() {

        /**
         * Adds the items to the selected list.
         * @see KeyListener#keyPressed(KeyEvent)
         */
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER && filterArea.isFocusOwner()) {
                addItem();
                //reset filter
                setTextFieldDefault(DEFAULT_FILTER_TEXT);
                availableItemsListbox.requestFocus();
            }
        }
    });
    sorter = new ViewerSorter();
    availableItemsListbox = new JTree();
    initializeTree(availableItemsListbox, user);
    availableItemsListbox.addKeyListener(new KeyAdapter() {

        /**
         * Adds the items to the selected list.
         * @see KeyListener#keyPressed(KeyEvent)
         */
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER && availableItemsListbox.isFocusOwner()) {
                addItem();
            }
        }
    });
    availableItemsListbox.addMouseListener(new MouseAdapter() {

        /**
         * Adds the items to the selected list.
         * @see MouseListener#mouseReleased(MouseEvent)
         */
        public void mouseReleased(MouseEvent e) {
            if (e.getClickCount() == 2) {
                if (availableItemsListbox.isFocusOwner())
                    addItem();
            }
        }
    });
    availableItemsListbox.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent evt) {
            firePropertyChange(AVAILABLE_SELECTION_CHANGE, Boolean.TRUE, Boolean.FALSE);
        }
    });
    selectedItemsListbox = new JTree();
    initializeTree(selectedItemsListbox, user);
    selectedItemsListbox.addKeyListener(new KeyAdapter() {

        /**
         * Removes the selected elements from the selected list.
         * @see KeyListener#keyPressed(KeyEvent)
         */
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER && selectedItemsListbox.isFocusOwner()) {
                removeItem();
            }
        }
    });
    selectedItemsListbox.addMouseListener(new MouseAdapter() {

        /**
         * Removes the selected elements from the selected list.
         * @see MouseListener#mouseReleased(MouseEvent)
         */
        public void mouseReleased(MouseEvent e) {
            if (e.getClickCount() == 2) {
                if (selectedItemsListbox.isFocusOwner())
                    removeItem();
            }
        }
    });
    IconManager icons = IconManager.getInstance();
    addButton = new JButton(icons.getIcon(IconManager.RIGHT_ARROW));
    removeButton = new JButton(icons.getIcon(IconManager.LEFT_ARROW));
    addAllButton = new JButton(icons.getIcon(IconManager.DOUBLE_RIGHT_ARROW));
    removeAllButton = new JButton(icons.getIcon(IconManager.DOUBLE_LEFT_ARROW));

    addButton.setActionCommand("" + ADD);
    addButton.addActionListener(this);
    addAllButton.setActionCommand("" + ADD_ALL);
    addAllButton.addActionListener(this);
    removeButton.setActionCommand("" + REMOVE);
    removeButton.addActionListener(this);
    removeAllButton.setActionCommand("" + REMOVE_ALL);
    removeAllButton.addActionListener(this);
    setImmutableElements(null);
}

From source file:org.optaplanner.benchmark.impl.aggregator.swingui.BenchmarkAggregatorFrame.java

private CheckBoxTree createCheckBoxTree() {
    final CheckBoxTree resultCheckBoxTree = new CheckBoxTree(initBenchmarkHierarchy(true));
    resultCheckBoxTree.addTreeSelectionListener(new TreeSelectionListener() {

        @Override/*ww  w .ja v  a 2s .  c  o m*/
        public void valueChanged(TreeSelectionEvent e) {
            TreePath treeSelectionPath = e.getNewLeadSelectionPath();
            if (treeSelectionPath != null) {
                DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treeSelectionPath
                        .getLastPathComponent();
                MixedCheckBox checkBox = (MixedCheckBox) treeNode.getUserObject();
                detailTextArea.setText(checkBox.getDetail());
                detailTextArea.setCaretPosition(0);
                renameNodeButton.setEnabled(checkBox.getBenchmarkResult() instanceof PlannerBenchmarkResult
                        || checkBox.getBenchmarkResult() instanceof SolverBenchmarkResult);
            }
        }
    });
    resultCheckBoxTree.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            // Enable button if checked singleBenchmarkResults exist
            generateReportButton.setEnabled(!resultCheckBoxTree.getSelectedSingleBenchmarkNodes().isEmpty());
        }
    });
    checkBoxTree = resultCheckBoxTree;
    return resultCheckBoxTree;
}

From source file:org.parosproxy.paros.view.SiteMapPanel.java

/**
 * This method initializes treeSite/* w w  w  .  ja v a  2  s .c o  m*/
 * 
 * @return JTree
 */
public JTree getTreeSite() {
    if (treeSite == null) {
        treeSite = new JTree();
        treeSite.setShowsRootHandles(true);
        treeSite.setName("treeSite");
        treeSite.setToggleClickCount(1);
        treeSite.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {
                    // ZAP: Select site list item on right click
                    TreePath tp = treeSite.getPathForLocation(e.getPoint().x, e.getPoint().y);
                    if (tp != null) {
                        boolean select = true;
                        // Only select a new item if the current item is not
                        // already selected - this is to allow multiple items
                        // to be selected
                        if (treeSite.getSelectionPaths() != null) {
                            for (TreePath t : treeSite.getSelectionPaths()) {
                                if (t.equals(tp)) {
                                    select = false;
                                    break;
                                }
                            }
                        }
                        if (select) {
                            treeSite.getSelectionModel().setSelectionPath(tp);
                        }
                    }

                    View.getSingleton().getPopupMenu().show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });

        treeSite.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                HttpMessage msg = null;
                SiteNode node = (SiteNode) treeSite.getLastSelectedPathComponent();
                if (node == null) {
                    return;
                }
                if (!node.isRoot()) {
                    try {
                        msg = node.getHistoryReference().getHttpMessage();
                    } catch (Exception e1) {
                        // ZAP: Log exceptions
                        log.warn(e1.getMessage(), e1);
                        return;
                    }

                    HttpPanel reqPanel = getView().getRequestPanel();
                    HttpPanel resPanel = getView().getResponsePanel();
                    reqPanel.setMessage(msg, true);
                    resPanel.setMessage(msg, false);

                    // ZAP: Call SiteMapListenners
                    for (SiteMapListener listener : listenners) {
                        listener.nodeSelected(node);
                    }
                }
            }
        });
    }
    return treeSite;
}

From source file:org.pentaho.ui.xul.swing.tags.SwingTree.java

public void setOnselect(final String select) {
    selectionListener = new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent event) {
            if (event.getValueIsAdjusting() == true) {
                return;
            }/*from  w  ww  .ja  v a  2s  .com*/
            invoke(select, new Object[] { new Integer(table.getSelectedRow()) });
        }
    };

    if (table != null) {
        table.getSelectionModel().addListSelectionListener(selectionListener);
    }

    treeSelectionListener = new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            if (tree != null) {
                invoke(select, new Object[] { tree.getSelectionRows()[0] });
            }
        }
    };

    if (tree != null) {
        tree.getSelectionModel().addTreeSelectionListener(treeSelectionListener);
    }
}

From source file:org.pentaho.ui.xul.swing.tags.SwingTree.java

@Override
public void layout() {

    XulComponent primaryColumn = this.getElementByXPath("//treecol[@primary='true']"); //$NON-NLS-1$
    XulComponent isaContainer = this.getElementByXPath("treechildren/treeitem[@container='true']"); //$NON-NLS-1$

    isHierarchical = (primaryColumn != null) || (isaContainer != null);

    if (isHierarchical) {
        tree = new JTree();
        if (treeSelectionListener != null) {
            tree.getSelectionModel().addTreeSelectionListener(treeSelectionListener);
        }/* w w w.  j ava2 s  .  c o  m*/
        tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                SwingTree.this.changeSupport.firePropertyChange("selectedRows", null, //$NON-NLS-1$
                        SwingTree.this.getSelectedRows());
                SwingTree.this.changeSupport.firePropertyChange("absoluteSelectedRows", null, //$NON-NLS-1$
                        SwingTree.this.getAbsoluteSelectedRows());
                SwingTree.this.fireSelectedItem();
            }
        });
    } else {
        table = new ScrollableJTable();
        if (selectionListener != null) {
            table.getSelectionModel().addListSelectionListener(selectionListener);
        }
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent event) {
                if (event.getValueIsAdjusting() == true) {
                    return;
                }
                SwingTree.this.changeSupport.firePropertyChange("selectedRows", null, //$NON-NLS-1$
                        SwingTree.this.getSelectedRows());
                SwingTree.this.fireSelectedItem();

            }
        });
    }

    JComponent comp = (table != null ? table : tree);
    ToolTipManager.sharedInstance().unregisterComponent(comp);
    if (table != null) {
        ToolTipManager.sharedInstance().unregisterComponent(table.getTableHeader());

        table.setRowHeight(18);
        scrollpane = new JScrollPane(table);
        setupTable();

    } else {
        setupTree();
        scrollpane = new JScrollPane(tree);
    }

    setManagedObject(scrollpane.getViewport());
}