private DefaultMutableTreeNode createNode(@Nullable PatternDescriptor descriptor) { DefaultMutableTreeNode root = new DefaultMutableTreeNode(descriptor) { @NotNull @Override public String toString() { Object object = getUserObject(); return object == null ? "" : ((PatternDescriptor) object).getName(); } }; String id = descriptor == null ? PatternDescriptor.ROOT : descriptor.getId(); Collection<PatternDescriptor> collection = myMap.get(id); for (PatternDescriptor childDescriptor : collection) { root.add(createNode(childDescriptor)); } return root; }
public GenerateByPatternDialog(Project project, PatternDescriptor[] descriptors) { super(project); myProject = project; setTitle("Generate by Pattern"); setOKButtonText("Generate"); myMap = new MultiMap<>(); for (PatternDescriptor descriptor : descriptors) { myMap.putValue(descriptor.getParentId(), descriptor); } DefaultMutableTreeNode root = createNode(null); myTree = new SimpleTree() {}; myTree.setRootVisible(false); myTree.setCellRenderer( new DefaultTreeCellRenderer() { @NotNull @Override public Component getTreeCellRendererComponent( @NotNull JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { Component component = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); Object object = ((DefaultMutableTreeNode) value).getUserObject(); if (object instanceof PatternDescriptor) { setText(((PatternDescriptor) object).getName()); setIcon(((PatternDescriptor) object).getIcon()); } return component; } }); myTree.setModel(new DefaultTreeModel(root)); myTree .getSelectionModel() .addTreeSelectionListener( new TreeSelectionListener() { @Override public void valueChanged(@NotNull TreeSelectionEvent e) { update(); } }); myEditor = TemplateEditorUtil.createEditor(true, ""); mySplitter.setFirstComponent(ScrollPaneFactory.createScrollPane(myTree)); JPanel details = new JPanel(new BorderLayout()); details.add(myEditor.getComponent(), BorderLayout.CENTER); mySplitter.setSecondComponent(details); mySplitter.setHonorComponentsMinimumSize(true); mySplitter.setShowDividerControls(true); myTree.setSelectionRow(0); init(); }