use of javax.swing.tree.TreePath in project intellij-plugins by StepicOrg.
the class NavigationUtils method collapseNonSelected.
private static void collapseNonSelected(@NotNull PsiFileSystemItem file) {
ProjectView projectView = ProjectView.getInstance(file.getProject());
if (projectView == null) {
return;
}
AbstractProjectViewPane projectViewPane = projectView.getCurrentProjectViewPane();
if (projectViewPane == null) {
return;
}
JTree tree = projectViewPane.getTree();
Set<TreePath> paths = new HashSet<>(TreeUtil.collectExpandedPaths(tree));
DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
DefaultMutableTreeNode selectionNode = findNodeWithObject(root, file);
if (selectionNode != null) {
List<TreePath> toCollapse = new ArrayList<>();
TreePath selectedPath = getPathFromRoot(selectionNode);
for (TreePath treePath : paths) {
if (treePath.isDescendant(selectedPath)) {
continue;
}
TreePath currPath = treePath;
TreePath parent = treePath.getParentPath();
while (parent != null) {
if (parent.isDescendant(selectedPath)) {
toCollapse.add(currPath);
break;
}
currPath = parent;
parent = parent.getParentPath();
}
}
for (TreePath path : toCollapse) {
tree.collapsePath(path);
tree.fireTreeCollapsed(path);
}
}
}
use of javax.swing.tree.TreePath in project intellij-community by JetBrains.
the class MavenArtifactSearchPanel method initComponents.
private void initComponents(String initialText) {
myResultList = new Tree();
myResultList.setExpandableItemsEnabled(false);
myResultList.getEmptyText().setText("Loading...");
myResultList.setRootVisible(false);
myResultList.setShowsRootHandles(true);
myResultList.setModel(null);
MyArtifactCellRenderer renderer = myClassMode ? new MyClassCellRenderer(myResultList) : new MyArtifactCellRenderer(myResultList);
myResultList.setCellRenderer(renderer);
myResultList.setRowHeight(renderer.getPreferredSize().height);
mySearchField = new JTextField(initialText);
mySearchField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int d;
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
d = 1;
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
d = -1;
} else {
return;
}
int row = myResultList.getSelectionModel().getLeadSelectionRow();
row += d;
if (row >= 0 && row < myResultList.getRowCount()) {
myResultList.setSelectionRow(row);
}
}
});
setLayout(new BorderLayout());
add(mySearchField, BorderLayout.NORTH);
JScrollPane pane = ScrollPaneFactory.createScrollPane(myResultList);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// Don't remove this line.
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
// Without VERTICAL_SCROLLBAR_ALWAYS policy our custom layout
// works incorrectly, see https://youtrack.jetbrains.com/issue/IDEA-72986
add(pane, BorderLayout.CENTER);
mySearchField.getDocument().addDocumentListener(new DocumentAdapter() {
@Override
protected void textChanged(DocumentEvent e) {
scheduleSearch();
}
});
myResultList.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
if (!myAlarm.isEmpty())
return;
boolean hasSelection = !myResultList.isSelectionEmpty();
myListener.canSelectStateChanged(MavenArtifactSearchPanel.this, hasSelection);
}
});
myResultList.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER && myResultList.getLastSelectedPathComponent() != null) {
myListener.itemSelected();
e.consume();
}
}
});
new DoubleClickListener() {
@Override
protected boolean onDoubleClick(MouseEvent e) {
final TreePath path = myResultList.getPathForLocation(e.getX(), e.getY());
if (path != null && myResultList.isPathSelected(path)) {
Object sel = path.getLastPathComponent();
if (sel != null && myResultList.getModel().isLeaf(sel)) {
myListener.itemSelected();
return true;
}
}
return false;
}
}.installOn(myResultList);
}
use of javax.swing.tree.TreePath in project intellij-community by JetBrains.
the class MavenSelectProjectPopup method attachToButton.
public static void attachToButton(@NotNull final MavenProjectsManager projectsManager, @NotNull final JButton button, @NotNull final Consumer<MavenProject> callback) {
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
List<MavenProject> projectList = projectsManager.getProjects();
if (projectList.isEmpty()) {
JBPopupFactory.getInstance().createMessage("Maven projects not found").showUnderneathOf(button);
return;
}
DefaultMutableTreeNode root = buildTree(projectList);
final Map<MavenProject, String> projectsNameMap = MavenProjectNamer.generateNameMap(projectList);
final Tree projectTree = new Tree(root);
projectTree.setRootVisible(false);
projectTree.setCellRenderer(new NodeRenderer() {
@Override
public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
if (value instanceof DefaultMutableTreeNode) {
MavenProject mavenProject = (MavenProject) ((DefaultMutableTreeNode) value).getUserObject();
value = projectsNameMap.get(mavenProject);
setIcon(MavenIcons.MavenProject);
}
super.customizeCellRenderer(tree, value, selected, expanded, leaf, row, hasFocus);
}
});
new TreeSpeedSearch(projectTree, new Convertor<TreePath, String>() {
@Override
public String convert(TreePath o) {
Object lastPathComponent = o.getLastPathComponent();
if (!(lastPathComponent instanceof DefaultMutableTreeNode))
return null;
Object userObject = ((DefaultMutableTreeNode) lastPathComponent).getUserObject();
//noinspection SuspiciousMethodCalls
return projectsNameMap.get(userObject);
}
});
final Ref<JBPopup> popupRef = new Ref<>();
final Runnable clickCallBack = () -> {
TreePath path = projectTree.getSelectionPath();
if (path == null)
return;
Object lastPathComponent = path.getLastPathComponent();
if (!(lastPathComponent instanceof DefaultMutableTreeNode))
return;
Object object = ((DefaultMutableTreeNode) lastPathComponent).getUserObject();
// may be it's the root
if (object == null)
return;
callback.consume((MavenProject) object);
popupRef.get().closeOk(null);
};
projectTree.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
clickCallBack.run();
e.consume();
}
}
});
JBPopup popup = new PopupChooserBuilder(projectTree).setTitle("Select maven project").setResizable(true).setItemChoosenCallback(clickCallBack).setAutoselectOnMouseMove(true).setCloseOnEnter(false).createPopup();
popupRef.set(popup);
popup.showUnderneathOf(button);
}
private DefaultMutableTreeNode buildTree(List<MavenProject> projectList) {
MavenProject[] projects = projectList.toArray(new MavenProject[projectList.size()]);
Arrays.sort(projects, new MavenProjectNamer.MavenProjectComparator());
Map<MavenProject, DefaultMutableTreeNode> projectsToNode = new HashMap<>();
for (MavenProject mavenProject : projects) {
projectsToNode.put(mavenProject, new DefaultMutableTreeNode(mavenProject));
}
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
for (MavenProject mavenProject : projects) {
DefaultMutableTreeNode parent;
MavenProject aggregator = projectsManager.findAggregator(mavenProject);
if (aggregator != null) {
parent = projectsToNode.get(aggregator);
} else {
parent = root;
}
parent.add(projectsToNode.get(mavenProject));
}
return root;
}
});
}
use of javax.swing.tree.TreePath in project intellij-community by JetBrains.
the class RepositoryBrowserComponent method getExpandedSubTree.
public Collection<TreeNode> getExpandedSubTree(@NotNull final TreeNode treeNode) {
final TreeNode[] pathToNode = ((RepositoryTreeModel) myRepositoryTree.getModel()).getPathToRoot(treeNode);
final Enumeration<TreePath> expanded = myRepositoryTree.getExpandedDescendants(new TreePath(pathToNode));
final List<TreeNode> result = new ArrayList<>();
if (expanded != null) {
while (expanded.hasMoreElements()) {
final TreePath treePath = expanded.nextElement();
result.add((TreeNode) treePath.getLastPathComponent());
}
}
return result;
}
use of javax.swing.tree.TreePath in project intellij-community by JetBrains.
the class RepositoryBrowserComponent method getSelectedEntry.
@Nullable
public DirectoryEntry getSelectedEntry() {
TreePath selection = myRepositoryTree.getSelectionPath();
if (selection == null) {
return null;
}
Object element = selection.getLastPathComponent();
if (element instanceof RepositoryTreeNode) {
RepositoryTreeNode node = (RepositoryTreeNode) element;
return node.getSVNDirEntry();
}
return null;
}
Aggregations