Search in sources :

Example 56 with DefaultTreeModel

use of javax.swing.tree.DefaultTreeModel in project intellij-community by JetBrains.

the class LiveTemplateSettingsEditor method createPopupContextPanel.

static JPanel createPopupContextPanel(final Runnable onChange, final TemplateContext context) {
    JPanel panel = new JPanel(new BorderLayout());
    MultiMap<TemplateContextType, TemplateContextType> hierarchy = MultiMap.createLinked();
    for (TemplateContextType type : TemplateManagerImpl.getAllContextTypes()) {
        hierarchy.putValue(type.getBaseContextType(), type);
    }
    final CheckedTreeNode root = new CheckedTreeNode(Pair.create(null, "Hi"));
    final CheckboxTree checkboxTree = new CheckboxTree(new CheckboxTree.CheckboxTreeCellRenderer() {

        @Override
        public void customizeRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            final Object o = ((DefaultMutableTreeNode) value).getUserObject();
            if (o instanceof Pair) {
                getTextRenderer().append((String) ((Pair) o).second);
            }
        }
    }, root) {

        @Override
        protected void onNodeStateChanged(CheckedTreeNode node) {
            final TemplateContextType type = (TemplateContextType) ((Pair) node.getUserObject()).first;
            if (type != null) {
                context.setEnabled(type, node.isChecked());
            }
            onChange.run();
        }
    };
    for (TemplateContextType type : hierarchy.get(null)) {
        addContextNode(hierarchy, root, type, context);
    }
    ((DefaultTreeModel) checkboxTree.getModel()).nodeStructureChanged(root);
    TreeUtil.traverse(root, _node -> {
        final CheckedTreeNode node = (CheckedTreeNode) _node;
        if (node.isChecked()) {
            final TreeNode[] path = node.getPath();
            if (path != null) {
                checkboxTree.expandPath(new TreePath(path).getParentPath());
            }
        }
        return true;
    });
    panel.add(ScrollPaneFactory.createScrollPane(checkboxTree));
    final Dimension size = checkboxTree.getPreferredSize();
    panel.setPreferredSize(new Dimension(size.width + 30, Math.min(size.height + 10, 500)));
    return panel;
}
Also used : DefaultTreeModel(javax.swing.tree.DefaultTreeModel) RelativePoint(com.intellij.ui.awt.RelativePoint) TreePath(javax.swing.tree.TreePath) TreeNode(javax.swing.tree.TreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TemplateContextType(com.intellij.codeInsight.template.TemplateContextType) Pair(com.intellij.openapi.util.Pair)

Example 57 with DefaultTreeModel

use of javax.swing.tree.DefaultTreeModel in project intellij-community by JetBrains.

the class HierarchyBrowserBaseEx method createTree.

protected final JTree createTree(boolean dndAware) {
    final Tree tree;
    final NullableFunction<Object, PsiElement> toPsiConverter = o -> {
        if (o instanceof HierarchyNodeDescriptor) {
            return ((HierarchyNodeDescriptor) o).getContainingFile();
        }
        return null;
    };
    if (dndAware) {
        //noinspection Duplicates
        tree = new DnDAwareTree(new DefaultTreeModel(new DefaultMutableTreeNode(""))) {

            @Override
            public void removeNotify() {
                super.removeNotify();
                if (ScreenUtil.isStandardAddRemoveNotify(this))
                    myRefreshAction.unregisterCustomShortcutSet(this);
            }

            @Override
            public boolean isFileColorsEnabled() {
                return ProjectViewTree.isFileColorsEnabledFor(this);
            }

            @Override
            public Color getFileColorFor(Object object) {
                return ProjectViewTree.getColorForObject(object, myProject, toPsiConverter);
            }
        };
        if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
            DnDManager.getInstance().registerSource(new DnDSource() {

                @Override
                public boolean canStartDragging(final DnDAction action, final Point dragOrigin) {
                    return getSelectedElements().length > 0;
                }

                @Override
                public DnDDragStartBean startDragging(final DnDAction action, final Point dragOrigin) {
                    return new DnDDragStartBean(new TransferableWrapper() {

                        @Override
                        public TreeNode[] getTreeNodes() {
                            return tree.getSelectedNodes(TreeNode.class, null);
                        }

                        @Override
                        public PsiElement[] getPsiElements() {
                            return getSelectedElements();
                        }

                        @Override
                        public List<File> asFileList() {
                            return PsiCopyPasteManager.asFileList(getPsiElements());
                        }
                    });
                }

                @Override
                public Pair<Image, Point> createDraggedImage(final DnDAction action, final Point dragOrigin) {
                    return null;
                }

                @Override
                public void dragDropEnd() {
                }

                @Override
                public void dropActionChanged(final int gestureModifiers) {
                }
            }, tree);
        }
    } else {
        //noinspection Duplicates
        tree = new Tree(new DefaultTreeModel(new DefaultMutableTreeNode(""))) {

            @Override
            public void removeNotify() {
                super.removeNotify();
                if (ScreenUtil.isStandardAddRemoveNotify(this))
                    myRefreshAction.unregisterCustomShortcutSet(this);
            }

            @Override
            public boolean isFileColorsEnabled() {
                return ProjectViewTree.isFileColorsEnabledFor(this);
            }

            @Override
            public Color getFileColorFor(Object object) {
                return ProjectViewTree.getColorForObject(object, myProject, toPsiConverter);
            }
        };
    }
    configureTree(tree);
    EditSourceOnDoubleClickHandler.install(tree);
    EditSourceOnEnterKeyHandler.install(tree);
    myRefreshAction.registerShortcutOn(tree);
    return tree;
}
Also used : EditScopesDialog(com.intellij.ide.util.scopeChooser.EditScopesDialog) java.util(java.util) AllIcons(com.intellij.icons.AllIcons) TreeNode(javax.swing.tree.TreeNode) MessageFormat(java.text.MessageFormat) PsiCopyPasteManager(com.intellij.ide.PsiCopyPasteManager) EditSourceOnDoubleClickHandler(com.intellij.util.EditSourceOnDoubleClickHandler) NamedScope(com.intellij.psi.search.scope.packageSet.NamedScope) JBUI(com.intellij.util.ui.JBUI) ProjectViewTree(com.intellij.ide.projectView.impl.ProjectViewTree) Disposer(com.intellij.openapi.util.Disposer) Project(com.intellij.openapi.project.Project) Tree(com.intellij.ui.treeStructure.Tree) com.intellij.ide.dnd(com.intellij.ide.dnd) ComboBoxAction(com.intellij.openapi.actionSystem.ex.ComboBoxAction) Logger(com.intellij.openapi.diagnostic.Logger) OccurenceNavigator(com.intellij.ide.OccurenceNavigator) PsiElementNavigatable(com.intellij.openapi.fileEditor.PsiElementNavigatable) EditSourceOnEnterKeyHandler(com.intellij.util.EditSourceOnEnterKeyHandler) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) NamedScopesHolder(com.intellij.psi.search.scope.packageSet.NamedScopesHolder) BrowseHierarchyActionBase(com.intellij.ide.hierarchy.actions.BrowseHierarchyActionBase) LanguageExtension(com.intellij.lang.LanguageExtension) ScreenUtil(com.intellij.ui.ScreenUtil) NullableFunction(com.intellij.util.NullableFunction) DnDAwareTree(com.intellij.ide.dnd.aware.DnDAwareTree) File(java.io.File) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) java.awt(java.awt) IdeBundle(com.intellij.ide.IdeBundle) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) Pair(com.intellij.openapi.util.Pair) ApplicationManager(com.intellij.openapi.application.ApplicationManager) OccurenceNavigatorSupport(com.intellij.ide.OccurenceNavigatorSupport) Navigatable(com.intellij.pom.Navigatable) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) NodeDescriptor(com.intellij.ide.util.treeView.NodeDescriptor) ScrollPaneFactory(com.intellij.ui.ScrollPaneFactory) Alarm(com.intellij.util.Alarm) javax.swing(javax.swing) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) TreeNode(javax.swing.tree.TreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ProjectViewTree(com.intellij.ide.projectView.impl.ProjectViewTree) Tree(com.intellij.ui.treeStructure.Tree) DnDAwareTree(com.intellij.ide.dnd.aware.DnDAwareTree) DnDAwareTree(com.intellij.ide.dnd.aware.DnDAwareTree) File(java.io.File) Pair(com.intellij.openapi.util.Pair)

Example 58 with DefaultTreeModel

use of javax.swing.tree.DefaultTreeModel in project intellij-community by JetBrains.

the class HierarchyBrowserBaseEx method changeView.

public final void changeView(@NotNull final String typeName) {
    setCurrentViewType(typeName);
    final PsiElement element = mySmartPsiElementPointer.getElement();
    if (element == null || !isApplicableElement(element)) {
        return;
    }
    if (myContent != null) {
        final String displayName = getContentDisplayName(typeName, element);
        if (displayName != null) {
            myContent.setDisplayName(displayName);
        }
    }
    myCardLayout.show(myTreePanel, typeName);
    if (!myType2BuilderMap.containsKey(typeName)) {
        try {
            setWaitCursor();
            // create builder
            final JTree tree = myType2TreeMap.get(typeName);
            final DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode(""));
            tree.setModel(model);
            final HierarchyTreeStructure structure = createHierarchyTreeStructure(typeName, element);
            if (structure == null) {
                return;
            }
            final Comparator<NodeDescriptor> comparator = getComparator();
            final HierarchyTreeBuilder builder = new HierarchyTreeBuilder(myProject, tree, model, structure, comparator);
            myType2BuilderMap.put(typeName, builder);
            Disposer.register(this, builder);
            Disposer.register(builder, () -> myType2BuilderMap.remove(typeName));
            final HierarchyNodeDescriptor descriptor = structure.getBaseDescriptor();
            builder.select(descriptor, () -> builder.expand(descriptor, null));
        } finally {
            restoreCursor();
        }
    }
    IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
        IdeFocusManager.getGlobalInstance().requestFocus(getCurrentTree(), true);
    });
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) NodeDescriptor(com.intellij.ide.util.treeView.NodeDescriptor) DefaultTreeModel(javax.swing.tree.DefaultTreeModel)

Example 59 with DefaultTreeModel

use of javax.swing.tree.DefaultTreeModel in project intellij-community by JetBrains.

the class ImageDuplicateResultsDialog method createCenterPanel.

@Override
protected JComponent createCenterPanel() {
    final JPanel panel = new JPanel(new BorderLayout());
    DataManager.registerDataProvider(panel, new DataProvider() {

        @Override
        public Object getData(@NonNls String dataId) {
            final TreePath path = myTree.getSelectionPath();
            if (path != null) {
                Object component = path.getLastPathComponent();
                VirtualFile file = null;
                if (component instanceof MyFileNode) {
                    component = ((MyFileNode) component).getParent();
                }
                if (component instanceof MyDuplicatesNode) {
                    file = ((MyDuplicatesNode) component).getUserObject().iterator().next();
                }
                if (CommonDataKeys.VIRTUAL_FILE.is(dataId)) {
                    return file;
                }
                if (CommonDataKeys.VIRTUAL_FILE_ARRAY.is(dataId) && file != null) {
                    return new VirtualFile[] { file };
                }
            }
            return null;
        }
    });
    final JBList list = new JBList(new ResourceModules().getModuleNames());
    final NotNullFunction<Object, JComponent> modulesRenderer = dom -> new JLabel(dom instanceof Module ? ((Module) dom).getName() : dom.toString(), PlatformIcons.SOURCE_FOLDERS_ICON, SwingConstants.LEFT);
    list.installCellRenderer(modulesRenderer);
    final JPanel modulesPanel = ToolbarDecorator.createDecorator(list).setAddAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            final Module[] all = ModuleManager.getInstance(myProject).getModules();
            Arrays.sort(all, (o1, o2) -> o1.getName().compareTo(o2.getName()));
            final JBList modules = new JBList(all);
            modules.installCellRenderer(modulesRenderer);
            JBPopupFactory.getInstance().createListPopupBuilder(modules).setTitle("Add Resource Module").setFilteringEnabled(o -> ((Module) o).getName()).setItemChoosenCallback(() -> {
                final Object value = modules.getSelectedValue();
                if (value instanceof Module && !myResourceModules.contains((Module) value)) {
                    myResourceModules.add((Module) value);
                    ((DefaultListModel) list.getModel()).addElement(((Module) value).getName());
                }
                ((DefaultTreeModel) myTree.getModel()).reload();
                TreeUtil.expandAll(myTree);
            }).createPopup().show(button.getPreferredPopupPoint());
        }
    }).setRemoveAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            final Object[] values = list.getSelectedValues();
            for (Object value : values) {
                myResourceModules.remove((String) value);
                ((DefaultListModel) list.getModel()).removeElement(value);
            }
            ((DefaultTreeModel) myTree.getModel()).reload();
            TreeUtil.expandAll(myTree);
        }
    }).disableDownAction().disableUpAction().createPanel();
    modulesPanel.setPreferredSize(new Dimension(-1, 60));
    final JPanel top = new JPanel(new BorderLayout());
    top.add(new JLabel("Image modules:"), BorderLayout.NORTH);
    top.add(modulesPanel, BorderLayout.CENTER);
    panel.add(top, BorderLayout.NORTH);
    panel.add(new JBScrollPane(myTree), BorderLayout.CENTER);
    new AnAction() {

        @Override
        public void actionPerformed(AnActionEvent e) {
            VirtualFile file = getFileFromSelection();
            if (file != null) {
                final PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file);
                if (psiFile != null) {
                    final ImplementationViewComponent viewComponent = new ImplementationViewComponent(new PsiElement[] { psiFile }, 0);
                    final TreeSelectionListener listener = new TreeSelectionListener() {

                        @Override
                        public void valueChanged(TreeSelectionEvent e) {
                            final VirtualFile selection = getFileFromSelection();
                            if (selection != null) {
                                final PsiFile newElement = PsiManager.getInstance(myProject).findFile(selection);
                                if (newElement != null) {
                                    viewComponent.update(new PsiElement[] { newElement }, 0);
                                }
                            }
                        }
                    };
                    myTree.addTreeSelectionListener(listener);
                    final JBPopup popup = JBPopupFactory.getInstance().createComponentPopupBuilder(viewComponent, viewComponent.getPreferredFocusableComponent()).setProject(myProject).setDimensionServiceKey(myProject, ImageDuplicateResultsDialog.class.getName(), false).setResizable(true).setMovable(true).setRequestFocus(false).setCancelCallback(() -> {
                        myTree.removeTreeSelectionListener(listener);
                        return true;
                    }).setTitle("Image Preview").createPopup();
                    final Window window = ImageDuplicateResultsDialog.this.getWindow();
                    popup.show(new RelativePoint(window, new Point(window.getWidth(), 0)));
                    viewComponent.setHint(popup, "Image Preview");
                }
            }
        }
    }.registerCustomShortcutSet(CustomShortcutSet.fromString("ENTER"), panel);
    int total = myDuplicates.values().stream().mapToInt(Set::size).sum() - myDuplicates.size();
    final JLabel label = new JLabel("<html>Press <b>Enter</b> to preview image<br>Total images found: " + myImages.size() + ". Total duplicates found: " + total + "</html>");
    panel.add(label, BorderLayout.SOUTH);
    return panel;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) UIUtil(com.intellij.util.ui.UIUtil) java.util(java.util) ModuleManager(com.intellij.openapi.module.ModuleManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NonNls(org.jetbrains.annotations.NonNls) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) PsiManager(com.intellij.psi.PsiManager) ModuleUtil(com.intellij.openapi.module.ModuleUtil) TreeSelectionListener(javax.swing.event.TreeSelectionListener) PropertyName(com.intellij.ide.util.PropertyName) DialogWrapper(com.intellij.openapi.ui.DialogWrapper) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Tree(com.intellij.ui.treeStructure.Tree) FileUtil(com.intellij.openapi.util.io.FileUtil) Module(com.intellij.openapi.module.Module) DataManager(com.intellij.ide.DataManager) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) PlatformIcons(com.intellij.util.PlatformIcons) JBList(com.intellij.ui.components.JBList) TreeUtil(com.intellij.util.ui.tree.TreeUtil) PropertiesComponent(com.intellij.ide.util.PropertiesComponent) TreePath(javax.swing.tree.TreePath) StringUtil(com.intellij.openapi.util.text.StringUtil) NotNullFunction(com.intellij.util.NotNullFunction) com.intellij.ui(com.intellij.ui) ActionEvent(java.awt.event.ActionEvent) Collectors(java.util.stream.Collectors) JBPopup(com.intellij.openapi.ui.popup.JBPopup) File(java.io.File) JBScrollPane(com.intellij.ui.components.JBScrollPane) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) java.awt(java.awt) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) NotNull(org.jetbrains.annotations.NotNull) ImplementationViewComponent(com.intellij.codeInsight.hint.ImplementationViewComponent) RelativePoint(com.intellij.ui.awt.RelativePoint) javax.swing(javax.swing) TreeSelectionListener(javax.swing.event.TreeSelectionListener) RelativePoint(com.intellij.ui.awt.RelativePoint) PsiFile(com.intellij.psi.PsiFile) JBPopup(com.intellij.openapi.ui.popup.JBPopup) PsiElement(com.intellij.psi.PsiElement) ImplementationViewComponent(com.intellij.codeInsight.hint.ImplementationViewComponent) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) TreePath(javax.swing.tree.TreePath) JBList(com.intellij.ui.components.JBList) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) Module(com.intellij.openapi.module.Module) JBScrollPane(com.intellij.ui.components.JBScrollPane)

Example 60 with DefaultTreeModel

use of javax.swing.tree.DefaultTreeModel in project intellij-community by JetBrains.

the class DependenciesPanel method selectElementInLeftTree.

private void selectElementInLeftTree(PsiElement elt) {
    PsiManager manager = PsiManager.getInstance(myProject);
    PackageDependenciesNode root = (PackageDependenciesNode) myLeftTree.getModel().getRoot();
    Enumeration enumeration = root.breadthFirstEnumeration();
    while (enumeration.hasMoreElements()) {
        PackageDependenciesNode child = (PackageDependenciesNode) enumeration.nextElement();
        if (manager.areElementsEquivalent(child.getPsiElement(), elt)) {
            myLeftTree.setSelectionPath(new TreePath(((DefaultTreeModel) myLeftTree.getModel()).getPathToRoot(child)));
            break;
        }
    }
}
Also used : TreePath(javax.swing.tree.TreePath) PsiManager(com.intellij.psi.PsiManager) DefaultTreeModel(javax.swing.tree.DefaultTreeModel)

Aggregations

DefaultTreeModel (javax.swing.tree.DefaultTreeModel)167 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)86 TreePath (javax.swing.tree.TreePath)43 Tree (com.intellij.ui.treeStructure.Tree)22 Test (org.junit.Test)21 MockTreeModelListener (com.facebook.buck.intellij.ideabuck.test.util.MockTreeModelListener)16 JTree (javax.swing.JTree)15 TreeSelectionEvent (javax.swing.event.TreeSelectionEvent)15 JScrollPane (javax.swing.JScrollPane)14 TreeSelectionListener (javax.swing.event.TreeSelectionListener)14 ArrayList (java.util.ArrayList)10 TreeNode (javax.swing.tree.TreeNode)10 NotNull (org.jetbrains.annotations.NotNull)10 Module (com.intellij.openapi.module.Module)9 DefaultTreeCellRenderer (javax.swing.tree.DefaultTreeCellRenderer)9 MouseEvent (java.awt.event.MouseEvent)8 List (java.util.List)8 Project (com.intellij.openapi.project.Project)7 javax.swing (javax.swing)7 JPanel (javax.swing.JPanel)7