Search in sources :

Example 6 with TreeSelectionListener

use of javax.swing.event.TreeSelectionListener in project intellij-community by JetBrains.

the class InspectionResultsView method initTreeListeners.

private void initTreeListeners() {
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        return;
    }
    myTree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            if (myTree.isUnderQueueUpdate())
                return;
            syncRightPanel();
            if (isAutoScrollMode()) {
                OpenSourceUtil.openSourcesFrom(DataManager.getInstance().getDataContext(InspectionResultsView.this), false);
            }
        }
    });
    EditSourceOnDoubleClickHandler.install(myTree);
    myTree.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                OpenSourceUtil.openSourcesFrom(DataManager.getInstance().getDataContext(InspectionResultsView.this), false);
            }
        }
    });
    PopupHandler.installPopupHandler(myTree, IdeActions.INSPECTION_TOOL_WINDOW_TREE_POPUP, ActionPlaces.CODE_INSPECTION);
    SmartExpander.installOn(myTree);
}
Also used : KeyEvent(java.awt.event.KeyEvent) KeyAdapter(java.awt.event.KeyAdapter) TreeSelectionListener(javax.swing.event.TreeSelectionListener) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent)

Example 7 with TreeSelectionListener

use of javax.swing.event.TreeSelectionListener 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();
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) DoubleClickListener(com.intellij.ui.DoubleClickListener) KeyAdapter(java.awt.event.KeyAdapter) TreeSpeedSearch(com.intellij.ui.TreeSpeedSearch) TreeSelectionListener(javax.swing.event.TreeSelectionListener) NotNull(org.jetbrains.annotations.NotNull) AbstractProjectTreeStructure(com.intellij.ide.projectView.impl.AbstractProjectTreeStructure) NodeRenderer(com.intellij.ide.util.treeView.NodeRenderer) KeyEvent(java.awt.event.KeyEvent) ProjectAbstractTreeStructureBase(com.intellij.ide.projectView.impl.ProjectAbstractTreeStructureBase) Tree(com.intellij.ui.treeStructure.Tree) ChangeListener(javax.swing.event.ChangeListener) MouseEvent(java.awt.event.MouseEvent) TabbedPaneWrapper(com.intellij.ui.TabbedPaneWrapper) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) TreePath(javax.swing.tree.TreePath) ChangeEvent(javax.swing.event.ChangeEvent) BaseProjectTreeBuilder(com.intellij.ide.projectView.BaseProjectTreeBuilder) ProjectTreeBuilder(com.intellij.ide.projectView.impl.ProjectTreeBuilder) ModalityState(com.intellij.openapi.application.ModalityState) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent)

Example 8 with TreeSelectionListener

use of javax.swing.event.TreeSelectionListener 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 9 with TreeSelectionListener

use of javax.swing.event.TreeSelectionListener in project intellij-community by JetBrains.

the class AbstractProjectViewPSIPane method initTree.

private void initTree() {
    myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    UIUtil.setLineStyleAngled(myTree);
    myTree.setRootVisible(false);
    myTree.setShowsRootHandles(true);
    myTree.expandPath(new TreePath(myTree.getModel().getRoot()));
    myTree.setSelectionPath(new TreePath(myTree.getModel().getRoot()));
    EditSourceOnDoubleClickHandler.install(myTree);
    ToolTipManager.sharedInstance().registerComponent(myTree);
    TreeUtil.installActions(myTree);
    myTree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            fireTreeChangeListener();
        }
    });
    myTree.getModel().addTreeModelListener(new TreeModelListener() {

        @Override
        public void treeNodesChanged(TreeModelEvent e) {
            fireTreeChangeListener();
        }

        @Override
        public void treeNodesInserted(TreeModelEvent e) {
            fireTreeChangeListener();
        }

        @Override
        public void treeNodesRemoved(TreeModelEvent e) {
            fireTreeChangeListener();
        }

        @Override
        public void treeStructureChanged(TreeModelEvent e) {
            fireTreeChangeListener();
        }
    });
    new MySpeedSearch(myTree);
    myTree.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (KeyEvent.VK_ENTER == e.getKeyCode()) {
                final DefaultMutableTreeNode selectedNode = ((ProjectViewTree) myTree).getSelectedNode();
                if (selectedNode != null && !selectedNode.isLeaf()) {
                    return;
                }
                DataContext dataContext = DataManager.getInstance().getDataContext(myTree);
                OpenSourceUtil.openSourcesFrom(dataContext, ScreenReader.isActive());
            } else if (KeyEvent.VK_ESCAPE == e.getKeyCode()) {
                if (e.isConsumed())
                    return;
                PsiCopyPasteManager copyPasteManager = PsiCopyPasteManager.getInstance();
                boolean[] isCopied = new boolean[1];
                if (copyPasteManager.getElements(isCopied) != null && !isCopied[0]) {
                    copyPasteManager.clear();
                    e.consume();
                }
            }
        }
    });
    CustomizationUtil.installPopupHandler(myTree, IdeActions.GROUP_PROJECT_VIEW_POPUP, ActionPlaces.PROJECT_VIEW_POPUP);
}
Also used : TreeModelEvent(javax.swing.event.TreeModelEvent) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeModelListener(javax.swing.event.TreeModelListener) KeyAdapter(java.awt.event.KeyAdapter) PsiCopyPasteManager(com.intellij.ide.PsiCopyPasteManager) TreeSelectionListener(javax.swing.event.TreeSelectionListener) KeyEvent(java.awt.event.KeyEvent) DataContext(com.intellij.openapi.actionSystem.DataContext) TreePath(javax.swing.tree.TreePath) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent)

Example 10 with TreeSelectionListener

use of javax.swing.event.TreeSelectionListener in project gradle by gradle.

the class TaskTreeTab method createTreePanel.

private Component createTreePanel() {
    treeComponent = new TaskTreeComponent(gradlePluginLord, new TaskTreeComponent.Interaction() {

        public void rightClick(JTree tree, int x, int y) {
            enableThingsAppropriately();
            popupMenu.show(tree, x, y);
        }

        public void taskInvoked(TaskView task, boolean isCtrlKeyDown) {
            if (isCtrlKeyDown) {
                gradlePluginLord.addExecutionRequestToQueue(task, false, "-a");
            } else {
                gradlePluginLord.addExecutionRequestToQueue(task, false);
            }
        }

        public void projectInvoked(ProjectView project) {
            executeDefaultTasksInProject(project);
        }
    });
    treeComponent.getTree().addTreeSelectionListener(new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            enableThingsAppropriately();
        }
    });
    defaultTreeBackground = treeComponent.getTree().getBackground();
    treeScrollPane = new JScrollPane();
    //change the color to better indicate that
    treeComponent.getTree().setBackground(workingBackgroundColor);
    showTextInViewport("Has not built projects/tasks yet.");
    return treeScrollPane;
}
Also used : TaskView(org.gradle.foundation.TaskView) AlternateUIInteraction(org.gradle.gradleplugin.userinterface.AlternateUIInteraction) SwingAddMultipleFavoritesInteraction(org.gradle.gradleplugin.userinterface.swing.generic.SwingAddMultipleFavoritesInteraction) TreeSelectionListener(javax.swing.event.TreeSelectionListener) ProjectView(org.gradle.foundation.ProjectView) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) TaskTreeComponent(org.gradle.gradleplugin.userinterface.swing.generic.TaskTreeComponent)

Aggregations

TreeSelectionListener (javax.swing.event.TreeSelectionListener)86 TreeSelectionEvent (javax.swing.event.TreeSelectionEvent)81 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)36 TreePath (javax.swing.tree.TreePath)32 JTree (javax.swing.JTree)28 JScrollPane (javax.swing.JScrollPane)26 ActionEvent (java.awt.event.ActionEvent)23 JPanel (javax.swing.JPanel)22 ActionListener (java.awt.event.ActionListener)20 MouseEvent (java.awt.event.MouseEvent)16 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)16 Dimension (java.awt.Dimension)15 BorderLayout (java.awt.BorderLayout)14 GridBagConstraints (java.awt.GridBagConstraints)11 GridBagLayout (java.awt.GridBagLayout)11 Insets (java.awt.Insets)11 JButton (javax.swing.JButton)11 Tree (com.intellij.ui.treeStructure.Tree)10 KeyAdapter (java.awt.event.KeyAdapter)9 KeyEvent (java.awt.event.KeyEvent)9