public static void unselect(JTree tree, final DefaultMutableTreeNode node) { final TreePath rootPath = new TreePath(node.getPath()); final TreePath[] selectionPaths = tree.getSelectionPaths(); if (selectionPaths != null) { for (TreePath selectionPath : selectionPaths) { if (selectionPath.getPathCount() > rootPath.getPathCount() && rootPath.isDescendant(selectionPath)) { tree.removeSelectionPath(selectionPath); } } } }
protected boolean haveSelectedFileType(final Object tag) { final TreePath[] paths = getSelectionPaths(); if (paths != null) { for (TreePath path : paths) { if (path.getPathCount() > 1) { ChangesBrowserNode firstNode = (ChangesBrowserNode) path.getPathComponent(1); if ((tag == null || firstNode.getUserObject() == tag) && path.getPathCount() > 2) { return true; } } } } return false; }
// {{{ removeSelectedNode() method private void removeSelectedNode() { TreePath path = resultTree.getSelectionPath(); if (path == null) return; MutableTreeNode value = (MutableTreeNode) path.getLastPathComponent(); if (path.getPathCount() > 1) { // Adjust selection so that repeating some removals // behave naturally. TreePath parentPath = path.getParentPath(); MutableTreeNode parent = (MutableTreeNode) parentPath.getLastPathComponent(); int removingIndex = parent.getIndex(value); int nextIndex = removingIndex + 1; if (nextIndex < parent.getChildCount()) { TreeNode next = parent.getChildAt(nextIndex); resultTree.setSelectionPath(parentPath.pathByAddingChild(next)); } else { resultTree.setSelectionPath(parentPath); } resultTreeModel.removeNodeFromParent(value); } HyperSearchOperationNode.removeNodeFromCache(value); if (resultTreeRoot.getChildCount() == 0) { hideDockable(); } } // }}}
@Override public void calcData(DataKey key, DataSink sink) { if (key == VcsDataKeys.CHANGES) { sink.put(VcsDataKeys.CHANGES, getSelectedChanges()); } else if (key == VcsDataKeys.CHANGE_LEAD_SELECTION) { sink.put(VcsDataKeys.CHANGE_LEAD_SELECTION, getLeadSelection()); } else if (key == VcsDataKeys.CHANGE_LISTS) { sink.put(VcsDataKeys.CHANGE_LISTS, getSelectedChangeLists()); } else if (key == PlatformDataKeys.VIRTUAL_FILE_ARRAY) { sink.put(PlatformDataKeys.VIRTUAL_FILE_ARRAY, getSelectedFiles()); } else if (key == PlatformDataKeys.NAVIGATABLE) { final VirtualFile[] files = getSelectedFiles(); if (files.length == 1 && !files[0].isDirectory()) { sink.put(PlatformDataKeys.NAVIGATABLE, new OpenFileDescriptor(myProject, files[0], 0)); } } else if (key == PlatformDataKeys.NAVIGATABLE_ARRAY) { sink.put( PlatformDataKeys.NAVIGATABLE_ARRAY, ChangesUtil.getNavigatableArray(myProject, getSelectedFiles())); } else if (key == PlatformDataKeys.DELETE_ELEMENT_PROVIDER) { final TreePath[] paths = getSelectionPaths(); if (paths != null) { for (TreePath path : paths) { ChangesBrowserNode node = (ChangesBrowserNode) path.getLastPathComponent(); if (!(node.getUserObject() instanceof ChangeList)) { sink.put(PlatformDataKeys.DELETE_ELEMENT_PROVIDER, new VirtualFileDeleteProvider()); break; } } } } else if (key == PlatformDataKeys.COPY_PROVIDER) { sink.put(PlatformDataKeys.COPY_PROVIDER, myCopyProvider); } else if (key == UNVERSIONED_FILES_DATA_KEY) { sink.put(UNVERSIONED_FILES_DATA_KEY, getSelectedUnversionedFiles()); } else if (key == VcsDataKeys.MODIFIED_WITHOUT_EDITING_DATA_KEY) { sink.put(VcsDataKeys.MODIFIED_WITHOUT_EDITING_DATA_KEY, getSelectedModifiedWithoutEditing()); } else if (key == LOCALLY_DELETED_CHANGES) { sink.put(LOCALLY_DELETED_CHANGES, getSelectedLocallyDeletedChanges()); } else if (key == MISSING_FILES_DATA_KEY) { sink.put(MISSING_FILES_DATA_KEY, getSelectedMissingFiles()); } else if (VcsDataKeys.HAVE_LOCALLY_DELETED == key) { sink.put(VcsDataKeys.HAVE_LOCALLY_DELETED, haveLocallyDeleted()); } else if (VcsDataKeys.HAVE_MODIFIED_WITHOUT_EDITING == key) { sink.put(VcsDataKeys.HAVE_MODIFIED_WITHOUT_EDITING, haveLocallyModified()); } else if (VcsDataKeys.HAVE_SELECTED_CHANGES == key) { sink.put(VcsDataKeys.HAVE_SELECTED_CHANGES, haveSelectedChanges()); } else if (key == HELP_ID_DATA_KEY) { sink.put(HELP_ID_DATA_KEY, ourHelpId); } else if (key == VcsDataKeys.CHANGES_IN_LIST_KEY) { final TreePath selectionPath = getSelectionPath(); if (selectionPath != null && selectionPath.getPathCount() > 1) { ChangesBrowserNode<?> firstNode = (ChangesBrowserNode) selectionPath.getPathComponent(1); if (firstNode instanceof ChangesBrowserChangeListNode) { final List<Change> list = firstNode.getAllChangesUnder(); sink.put(VcsDataKeys.CHANGES_IN_LIST_KEY, list); } } } }
private static boolean areComponentsEqual(final TreePath[] paths, final int componentIndex) { if (paths[0].getPathCount() <= componentIndex) return false; final Object pathComponent = paths[0].getPathComponent(componentIndex); for (final TreePath treePath : paths) { if (treePath.getPathCount() <= componentIndex) return false; if (!pathComponent.equals(treePath.getPathComponent(componentIndex))) return false; } return true; }
private List<FilePath> getSelectedFilePaths(final Object tag) { Set<FilePath> files = new HashSet<FilePath>(); final TreePath[] paths = getSelectionPaths(); if (paths != null) { for (TreePath path : paths) { if (path.getPathCount() > 1) { ChangesBrowserNode firstNode = (ChangesBrowserNode) path.getPathComponent(1); if (tag == null || firstNode.getUserObject() == tag) { ChangesBrowserNode<?> node = (ChangesBrowserNode) path.getLastPathComponent(); files.addAll(node.getAllFilePathsUnder()); } } } } return new ArrayList<FilePath>(files); }
// ====================================================== // ====================================================== private void treeMousePressed(java.awt.event.MouseEvent evt) { int mask = evt.getModifiers(); if ((mask & MouseEvent.BUTTON1_MASK) != 0) { TreePath selectedPath = getPathForLocation(evt.getX(), evt.getY()); if (selectedPath == null) return; DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectedPath.getPathComponent(selectedPath.getPathCount() - 1); Object o = node.getUserObject(); if (o instanceof String) { TransferHandler transfer = this.getTransferHandler(); transfer.exportAsDrag(this, evt, TransferHandler.COPY); dragged_node = node; parent.setCursor(renderer.getNodeCursor(node)); } } }
private List<LocallyDeletedChange> getSelectedLocallyDeletedChanges() { Set<LocallyDeletedChange> files = new HashSet<LocallyDeletedChange>(); final TreePath[] paths = getSelectionPaths(); if (paths != null) { for (TreePath path : paths) { if (path.getPathCount() > 1) { ChangesBrowserNode firstNode = (ChangesBrowserNode) path.getPathComponent(1); if (firstNode.getUserObject() == TreeModelBuilder.LOCALLY_DELETED_NODE) { ChangesBrowserNode<?> node = (ChangesBrowserNode) path.getLastPathComponent(); final List<LocallyDeletedChange> objectsUnder = node.getAllObjectsUnder(LocallyDeletedChange.class); files.addAll(objectsUnder); } } } } return new ArrayList<LocallyDeletedChange>(files); }
// ====================================================== // ====================================================== private void treeMouseReleased(java.awt.event.MouseEvent evt) { int mask = evt.getModifiers(); if ((mask & MouseEvent.BUTTON1_MASK) != 0) { if (dragged_node == null) return; TreePath selectedPath = getPathForLocation(evt.getX(), evt.getY()); if (selectedPath == null) if ((selectedPath = getUpperPath(evt.getX(), evt.getY())) == null) return; DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectedPath.getPathComponent(selectedPath.getPathCount() - 1); Object o = node.getUserObject(); int pos = 0; if (o instanceof String) { DefaultMutableTreeNode p_node = (DefaultMutableTreeNode) node.getParent(); pos = p_node.getIndex(node); node = p_node; } moveLeaf(node, dragged_node, pos); dragged_node = null; Cursor cursor = new Cursor(Cursor.DEFAULT_CURSOR); parent.setCursor(cursor); } }
public void valueChanged(TreeSelectionEvent evt) { TreePath path = evt.getPath(); String fqn = SEP; String component_name; HashMap data = null; for (int i = 0; i < path.getPathCount(); i++) { component_name = ((MyNode) path.getPathComponent(i)).name; if (component_name.equals(SEP)) continue; if (fqn.equals(SEP)) fqn += component_name; else fqn = fqn + SEP + component_name; } data = getData(tree, fqn); if (data != null) { getContentPane().add(tablePanel, BorderLayout.SOUTH); populateTable(data); validate(); } else { clearTable(); getContentPane().remove(tablePanel); validate(); } }
@Nullable public static Range<Integer> getExpandControlRange(final JTree aTree, final TreePath path) { TreeModel treeModel = aTree.getModel(); final BasicTreeUI basicTreeUI = (BasicTreeUI) aTree.getUI(); Icon expandedIcon = basicTreeUI.getExpandedIcon(); Range<Integer> box = null; if (path != null && !treeModel.isLeaf(path.getLastPathComponent())) { int boxWidth; Insets i = aTree.getInsets(); if (expandedIcon != null) { boxWidth = expandedIcon.getIconWidth(); } else { boxWidth = 8; } int boxLeftX = i != null ? i.left : 0; boolean leftToRight = aTree.getComponentOrientation().isLeftToRight(); int depthOffset = getDepthOffset(aTree); int totalChildIndent = basicTreeUI.getLeftChildIndent() + basicTreeUI.getRightChildIndent(); if (leftToRight) { boxLeftX += ((path.getPathCount() + depthOffset - 2) * totalChildIndent + basicTreeUI.getLeftChildIndent()) - boxWidth / 2; } int boxRightX = boxLeftX + boxWidth; box = new Range<Integer>(boxLeftX, boxRightX); } return box; }
private static boolean isAncestor(final TreePath ancestor, final TreePath path) { if (path.getPathCount() < ancestor.getPathCount()) return false; for (int i = 0; i < ancestor.getPathCount(); i++) if (!path.getPathComponent(i).equals(ancestor.getPathComponent(i))) return false; return true; }