Java Code Examples for javax.swing.JTree#getSelectionRows()

The following examples show how to use javax.swing.JTree#getSelectionRows() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RTree.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
@Override
public void focusLost(RComponent next) {
    JTree tree = (JTree) component;
    String currentText = getText();
    String currentCellValue = getTreeCellValue(tree, row);
    if (currentCellValue != null && !currentCellValue.equals(cellValue)) {
        recorder.recordSelect2(this, currentCellValue, true);
        return;
    }
    if (currentText != null && !currentText.equals(text)) {
        recorder.recordSelect2(this, currentText, true);
    }
    if ((next == null || next.getComponent() != component) && tree.getSelectionCount() > 1) {
        int[] selectionRows = tree.getSelectionRows();
        if (selectionRows == null) {
            selectionRows = new int[0];
        }
        List<Properties> pa = new ArrayList<Properties>();
        for (int selectionRow : selectionRows) {
            Properties p = new Properties();
            p.put("Path", getTextForNode(tree, selectionRow));
            pa.add(p);
        }
        recorder.recordSelect(this, PropertyHelper.toString(pa.toArray(new Properties[pa.size()]), new String[] { "Path" }));
    }
}
 
Example 2
Source File: LastNodeLowerHalfDrop.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private boolean haveCompleteNode(JTree tree) {
    int[] selRows = tree.getSelectionRows();
    TreePath path = tree.getPathForRow(selRows[0]);
    DefaultMutableTreeNode first = (DefaultMutableTreeNode)
            path.getLastPathComponent();
    int childCount = first.getChildCount();
    // first has children and no children are selected.
    if (childCount > 0 && selRows.length == 1) {
        return false;
    }
    // first may have children.
    for (int i = 1; i < selRows.length; i++) {
        path = tree.getPathForRow(selRows[i]);
        DefaultMutableTreeNode next = (DefaultMutableTreeNode)
                path.getLastPathComponent();
        if (first.isNodeChild(next)) {
            // Found a child of first.
            if (childCount > selRows.length - 1) {
                // Not all children of first are selected.
                return false;
            }
        }
    }
    return true;
}
 
Example 3
Source File: CheckNodeListener.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static void selectNextPrev(final boolean next, boolean isQuery, JTree tree) {
    int[] rows = tree.getSelectionRows();
    int newRow = rows == null || rows.length == 0 ? 0 : rows[0];
    int maxcount = tree.getRowCount();
    CheckNode node;
    do {
        if (next) {
            newRow++;
            if (newRow >= maxcount) {
                newRow = 0;
            }
        } else {
            newRow--;
            if (newRow < 0) {
                newRow = maxcount - 1;
            }
        }
        TreePath path = tree.getPathForRow(newRow);
        node = (CheckNode) path.getLastPathComponent();
        if (!node.isLeaf()) {
            tree.expandRow(newRow);
            maxcount = tree.getRowCount();
        }
    } while (!node.isLeaf());
    tree.setSelectionRow(newRow);
    tree.scrollRowToVisible(newRow);
}
 
Example 4
Source File: CheckNodeListener.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN) {
            JTree tree = (JTree) e.getSource();
            int row = tree.getSelectionRows()[0];
            TreePath path = tree.getSelectionPath();
            if (path != null) {
                CheckNode node = (CheckNode) path.getLastPathComponent();

                Object o = node.getUserObject();
                if (o instanceof TreeElement) {
                    o = ((TreeElement) o).getUserObject();
                    if (o instanceof RefactoringElement) {
                        openDiff(node);
                    }
//                    else if (o instanceof FileObject) {
//                        tree.expandPath(path);
//                        TreePath pathForRow = tree.getPathForRow(row + 1);
//                        CheckNode lastPathComponent = (CheckNode) pathForRow.getLastPathComponent();
//                        Object userObject = lastPathComponent.getUserObject();
//                        if (userObject instanceof TreeElement) {
//                            Object refElement = ((TreeElement) userObject).getUserObject();
//                            if (refElement instanceof RefactoringElement) {
//                                openDiff(lastPathComponent);
//                            }
//                        }
//                    }
                }
            }
        }
    }
 
Example 5
Source File: CheckNodeListener.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static void selectNextPrev(final boolean next, boolean isQuery, JTree tree) {
    int[] rows = tree.getSelectionRows();
    int newRow = rows == null || rows.length == 0 ? 0 : rows[0];
    int maxcount = tree.getRowCount();
    CheckNode node;
    do {
        if (next) {
            newRow++;
            if (newRow >= maxcount) {
                newRow = 0;
            }
        } else {
            newRow--;
            if (newRow < 0) {
                newRow = maxcount - 1;
            }
        }
        TreePath path = tree.getPathForRow(newRow);
        node = (CheckNode) path.getLastPathComponent();
        if (!node.isLeaf()) {
            tree.expandRow(newRow);
            maxcount = tree.getRowCount();
        }
    } while (!node.isLeaf());
    tree.setSelectionRow(newRow);
    tree.scrollRowToVisible(newRow);
    if (isQuery) {
        CheckNodeListener.findInSource(node);
    } else {
        CheckNodeListener.openDiff(node);
    }
}
 
Example 6
Source File: LastNodeLowerHalfDrop.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
    if (!support.isDrop()) {
        return false;
    }
    support.setShowDropLocation(true);
    if (!support.isDataFlavorSupported(nodesFlavor)) {
        return false;
    }
    // Do not allow a drop on the drag source selections.
    JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
    JTree tree = (JTree) support.getComponent();
    int dropRow = tree.getRowForPath(dl.getPath());
    int[] selRows = tree.getSelectionRows();
    for (int i = 0; i < selRows.length; i++) {
        if (selRows[i] == dropRow) {
            return false;
        }
    }
    // Do not allow MOVE-action drops if a non-leaf node is
    // selected unless all of its children are also selected.
    int action = support.getDropAction();
    if (action == MOVE) {
        return haveCompleteNode(tree);
    }
    // Do not allow a non-leaf node to be copied to a level
    // which is less than its source level.
    TreePath dest = dl.getPath();
    DefaultMutableTreeNode target = (DefaultMutableTreeNode)
            dest.getLastPathComponent();
    TreePath path = tree.getPathForRow(selRows[0]);
    DefaultMutableTreeNode firstNode = (DefaultMutableTreeNode)
            path.getLastPathComponent();
    if (firstNode.getChildCount() > 0
            && target.getLevel() < firstNode.getLevel()) {
        return false;
    }
    return true;
}
 
Example 7
Source File: TreeKeyListener.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
protected boolean changeSelectionUsingText(KeyEvent e, String inputStr) {
	inputStr = inputStr.toUpperCase();
	JTree tree = (JTree) e.getComponent();

	int[] selection = tree.getSelectionRows();
	Arrays.sort(selection);
	int i = selection.length > 0 ? selection[selection.length - 1] : 0;
	int rowCount = tree.getRowCount();
	for (int offset = 0; offset < rowCount; offset++) {
		int row = (i + offset) % rowCount;
		TreePath path = tree.getPathForRow(row);
		TreeNode node = (TreeNode) path.getLastPathComponent();
		Component renderer = tree.getCellRenderer()
				.getTreeCellRendererComponent(tree, node, false,
						tree.isExpanded(path), node.isLeaf(), row,
						tree.isFocusOwner());
		String str = getText(renderer);
		if (str != null && str.length() >= 0
				&& str.toUpperCase().startsWith(inputStr)) {
			tree.setSelectionPath(path);
			return true;
		}
	}

	return false;
}