use of com.intellij.ide.util.treeView.NodeRenderer 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 com.intellij.ide.util.treeView.NodeRenderer in project intellij-community by JetBrains.
the class FileChooserDialogImpl method createTree.
protected JTree createTree() {
Tree internalTree = createInternalTree();
myFileSystemTree = new FileSystemTreeImpl(myProject, myChooserDescriptor, internalTree, null, null, null);
internalTree.setRootVisible(myChooserDescriptor.isTreeRootVisible());
internalTree.setShowsRootHandles(true);
Disposer.register(myDisposable, myFileSystemTree);
myFileSystemTree.addOkAction(this::doOKAction);
JTree tree = myFileSystemTree.getTree();
tree.setCellRenderer(new NodeRenderer());
tree.getSelectionModel().addTreeSelectionListener(new FileTreeSelectionListener());
tree.addTreeExpansionListener(new FileTreeExpansionListener());
setOKActionEnabled(false);
myFileSystemTree.addListener(new FileSystemTree.Listener() {
public void selectionChanged(final List<VirtualFile> selection) {
updatePathFromTree(selection, false);
}
}, myDisposable);
new FileDrop(tree, new FileDrop.Target() {
public FileChooserDescriptor getDescriptor() {
return myChooserDescriptor;
}
public boolean isHiddenShown() {
return myFileSystemTree.areHiddensShown();
}
public void dropFiles(final List<VirtualFile> files) {
if (!myChooserDescriptor.isChooseMultiple() && files.size() > 0) {
selectInTree(new VirtualFile[] { files.get(0) }, true, true);
} else {
selectInTree(VfsUtilCore.toVirtualFileArray(files), true, true);
}
}
});
return tree;
}
use of com.intellij.ide.util.treeView.NodeRenderer in project intellij-community by JetBrains.
the class AbstractTreeClassChooserDialog method createCenterPanel.
@Override
protected JComponent createCenterPanel() {
final DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode());
myTree = new Tree(model);
ProjectAbstractTreeStructureBase treeStructure = new AbstractProjectTreeStructure(myProject) {
@Override
public boolean isFlattenPackages() {
return false;
}
@Override
public boolean isShowMembers() {
return myIsShowMembers;
}
@Override
public boolean isHideEmptyMiddlePackages() {
return true;
}
@Override
public boolean isAbbreviatePackageNames() {
return false;
}
@Override
public boolean isShowLibraryContents() {
return myIsShowLibraryContents;
}
@Override
public boolean isShowModules() {
return false;
}
};
myBuilder = new ProjectTreeBuilder(myProject, myTree, model, AlphaComparator.INSTANCE, treeStructure);
myTree.setRootVisible(false);
myTree.setShowsRootHandles(true);
myTree.expandRow(0);
myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
myTree.setCellRenderer(new NodeRenderer());
UIUtil.setLineStyleAngled(myTree);
JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(myTree);
scrollPane.setPreferredSize(JBUI.size(500, 300));
myTree.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (KeyEvent.VK_ENTER == e.getKeyCode()) {
doOKAction();
}
}
});
new DoubleClickListener() {
@Override
protected boolean onDoubleClick(MouseEvent event) {
TreePath path = myTree.getPathForLocation(event.getX(), event.getY());
if (path != null && myTree.isPathSelected(path)) {
doOKAction();
return true;
}
return false;
}
}.installOn(myTree);
myTree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
handleSelectionChanged();
}
});
new TreeSpeedSearch(myTree);
myTabbedPane = new TabbedPaneWrapper(getDisposable());
final JPanel dummyPanel = new JPanel(new BorderLayout());
String name = null;
/*
if (myInitialClass != null) {
name = myInitialClass.getName();
}
*/
myGotoByNamePanel = new ChooseByNamePanel(myProject, createChooseByNameModel(), name, myScope.isSearchInLibraries(), getContext()) {
@Override
protected void showTextFieldPanel() {
}
@Override
protected void close(boolean isOk) {
super.close(isOk);
if (isOk) {
doOKAction();
} else {
doCancelAction();
}
}
@NotNull
@Override
protected Set<Object> filter(@NotNull Set<Object> elements) {
return doFilter(elements);
}
@Override
protected void initUI(ChooseByNamePopupComponent.Callback callback, ModalityState modalityState, boolean allowMultipleSelection) {
super.initUI(callback, modalityState, allowMultipleSelection);
dummyPanel.add(myGotoByNamePanel.getPanel(), BorderLayout.CENTER);
IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
IdeFocusManager.getGlobalInstance().requestFocus(IdeFocusTraversalPolicy.getPreferredFocusedComponent(myGotoByNamePanel.getPanel()), true);
});
}
@Override
protected void showList() {
super.showList();
if (myInitialClass != null && myList.getModel().getSize() > 0) {
myList.setSelectedValue(myInitialClass, true);
myInitialClass = null;
}
}
@Override
protected void chosenElementMightChange() {
handleSelectionChanged();
}
};
Disposer.register(myDisposable, myGotoByNamePanel);
myTabbedPane.addTab(IdeBundle.message("tab.chooser.search.by.name"), dummyPanel);
myTabbedPane.addTab(IdeBundle.message("tab.chooser.project"), scrollPane);
myGotoByNamePanel.invoke(new MyCallback(), getModalityState(), false);
myTabbedPane.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
handleSelectionChanged();
}
});
return myTabbedPane.getComponent();
}
use of com.intellij.ide.util.treeView.NodeRenderer in project android by JetBrains.
the class NodeHyperlinkSupport method addHyperlinkBehaviorToSupportedNodes.
private void addHyperlinkBehaviorToSupportedNodes() {
myTree.setCellRenderer(new NodeRenderer() {
@NotNull
@Override
protected SimpleTextAttributes getSimpleTextAttributes(@NotNull PresentationData presentation, Color color, @NotNull Object node) {
List<PsIssue> issues = Collections.emptyList();
if (myShowIssues && node instanceof AbstractPsModelNode) {
AbstractPsModelNode<? extends PsModel> modelNode = (AbstractPsModelNode<? extends PsModel>) node;
issues = findIssues(modelNode, IssuesByTypeAndTextComparator.INSTANCE);
presentation.setTooltip(getTooltipText(issues, false));
}
if (myHoveredNode != null && myHoveredNode == node) {
return LINK_ATTRIBUTES;
}
SimpleTextAttributes textAttributes = super.getSimpleTextAttributes(presentation, color, node);
if (!issues.isEmpty()) {
PsIssue issue = issues.get(0);
Color waveColor = issue.getSeverity().getColor();
textAttributes = textAttributes.derive(STYLE_WAVED, null, null, waveColor);
}
return textAttributes;
}
@Override
public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
if (myShowIssues) {
AbstractPsModelNode<? extends PsModel> modelNode = findModelNode(value);
if (modelNode != null) {
List<PsIssue> issues = findIssues(modelNode, null);
if (!issues.isEmpty()) {
// Force color change of the node.
modelNode.getPresentation().clearText();
}
}
}
super.customizeCellRenderer(tree, value, selected, expanded, leaf, row, hasFocus);
Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
if (userObject instanceof CellAppearanceEx) {
((CellAppearanceEx) userObject).customize(this);
}
}
@Nullable
private AbstractPsModelNode<? extends PsModel> findModelNode(@Nullable Object value) {
if (value instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
Object userObject = node.getUserObject();
if (userObject instanceof AbstractPsModelNode) {
return (AbstractPsModelNode<? extends PsModel>) userObject;
}
}
return null;
}
});
MouseAdapter mouseListener = new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
Cursor cursor = getDefaultCursor();
T node = getIfHyperlink(e);
if (node != null) {
cursor = getPredefinedCursor(HAND_CURSOR);
}
setHoveredNode(node);
myTree.setCursor(cursor);
}
};
myTree.addMouseMotionListener(mouseListener);
// Make the cursor change to 'hand' if the mouse pointer is over a supported node and the user presses Ctrl or Cmd.
myKeyEventDispatcher = e -> {
T node = null;
if (e.getID() == KEY_PRESSED) {
Cursor cursor = getDefaultCursor();
if (isMetaOrCtrlKeyPressed(e)) {
node = getNodeUnderMousePointer();
if (node != null) {
cursor = getPredefinedCursor(HAND_CURSOR);
}
}
setHoveredNode(node);
myTree.setCursor(cursor);
} else if (e.getID() == KEY_RELEASED) {
if (isMetaOrCtrlKeyPressed(e)) {
setHoveredNode(null);
}
myTree.setCursor(getDefaultCursor());
}
return false;
};
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(myKeyEventDispatcher);
}
use of com.intellij.ide.util.treeView.NodeRenderer in project azure-tools-for-java by Microsoft.
the class AzureSdkTreePanel method initTree.
private Tree initTree() {
final DefaultMutableTreeNode root = new DefaultMutableTreeNode("Azure SDK Libraries");
this.model = new DefaultTreeModel(root);
final SimpleTree tree = new SimpleTree(model);
tree.putClientProperty(RenderingUtil.ALWAYS_PAINT_SELECTION_AS_FOCUSED, true);
tree.setCellRenderer(new NodeRenderer());
tree.setRootVisible(false);
tree.setShowsRootHandles(true);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
TreeUtil.installActions(tree);
RelativeFont.BOLD.install(tree);
return tree;
}
Aggregations