Search in sources :

Example 51 with TreePath

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

the class MavenUIUtil method installCheckboxRenderer.

public static void installCheckboxRenderer(final SimpleTree tree, final CheckboxHandler handler) {
    final JCheckBox checkbox = new JCheckBox();
    final JPanel panel = new JPanel(new BorderLayout());
    panel.add(checkbox, BorderLayout.WEST);
    final TreeCellRenderer baseRenderer = tree.getCellRenderer();
    tree.setCellRenderer(new TreeCellRenderer() {

        public Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean selected, final boolean expanded, final boolean leaf, final int row, final boolean hasFocus) {
            final Component baseComponent = baseRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
            final Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
            if (!handler.isVisible(userObject)) {
                return baseComponent;
            }
            final Color foreground = selected ? UIUtil.getTreeSelectionForeground() : UIUtil.getTreeTextForeground();
            Color background = selected ? UIUtil.getTreeSelectionBackground(hasFocus) : UIUtil.getTreeTextBackground();
            panel.add(baseComponent, BorderLayout.CENTER);
            panel.setBackground(background);
            panel.setForeground(foreground);
            CheckBoxState state = handler.getState(userObject);
            checkbox.setSelected(state != CheckBoxState.UNCHECKED);
            checkbox.setEnabled(state != CheckBoxState.PARTIAL);
            checkbox.setBackground(background);
            checkbox.setForeground(foreground);
            return panel;
        }
    });
    tree.addMouseListener(new MouseAdapter() {

        public void mousePressed(MouseEvent e) {
            int row = tree.getRowForLocation(e.getX(), e.getY());
            if (row >= 0) {
                TreePath path = tree.getPathForRow(row);
                if (!isCheckboxEnabledFor(path, handler))
                    return;
                Rectangle checkBounds = checkbox.getBounds();
                checkBounds.setLocation(tree.getRowBounds(row).getLocation());
                if (checkBounds.contains(e.getPoint())) {
                    handler.toggle(path, e);
                    e.consume();
                    tree.setSelectionRow(row);
                }
            }
        }
    });
    tree.addKeyListener(new KeyAdapter() {

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                TreePath[] treePaths = tree.getSelectionPaths();
                if (treePaths != null) {
                    for (TreePath treePath : treePaths) {
                        if (!isCheckboxEnabledFor(treePath, handler))
                            continue;
                        handler.toggle(treePath, e);
                    }
                    e.consume();
                }
            }
        }
    });
}
Also used : TreeCellRenderer(javax.swing.tree.TreeCellRenderer) TreePath(javax.swing.tree.TreePath)

Example 52 with TreePath

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

the class DynamicToolWindowWrapper method deleteRow.

void deleteRow() {
    boolean isShowDialog = true;
    final int rowsCount = myTreeTable.getSelectedRows().length;
    int i = 0;
    final TreePath[] paths = myTreeTable.getTree().getSelectionPaths();
    if (paths == null)
        return;
    for (TreePath selectionPath : paths) {
        if (rowsCount > 1)
            isShowDialog = false;
        if (i == 0)
            isShowDialog = true;
        i++;
        //class
        final TreePath parent = selectionPath.getParentPath();
        if (parent.getParentPath() == null) {
            //selectionPath is class
            final Object classRow = selectionPath.getLastPathComponent();
            if (!(classRow instanceof DefaultMutableTreeNode))
                return;
            if (!removeClass(((DefaultMutableTreeNode) classRow), isShowDialog, rowsCount))
                return;
        } else {
            //selectionPath is dynamic item
            final Object classRow = parent.getLastPathComponent();
            final Object dynamicRow = selectionPath.getLastPathComponent();
            if (!(classRow instanceof DefaultMutableTreeNode))
                return;
            if (!(dynamicRow instanceof DefaultMutableTreeNode))
                return;
            final DefaultMutableTreeNode dynamicItemNode = (DefaultMutableTreeNode) dynamicRow;
            final DefaultMutableTreeNode classNode = (DefaultMutableTreeNode) classRow;
            final boolean removeClass = classNode.getChildCount() == 1;
            if (!removeDynamicElement(dynamicItemNode, isShowDialog, rowsCount))
                return;
            if (removeClass) {
                removeNamedElement((DNamedElement) classNode.getUserObject());
            }
        }
    }
    DynamicManager.getInstance(myProject).fireChange();
}
Also used : TreePath(javax.swing.tree.TreePath) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode)

Example 53 with TreePath

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

the class DynamicToolWindowWrapper method createTable.

private JScrollPane createTable(final MutableTreeNode myTreeRoot) {
    ColumnInfo[] columnInfos = { new ClassColumnInfo(myColumnNames[CLASS_OR_ELEMENT_NAME_COLUMN]), new PropertyTypeColumnInfo(myColumnNames[TYPE_COLUMN]) };
    myTreeTableModel = new ListTreeTableModelOnColumns(myTreeRoot, columnInfos);
    myTreeTable = new MyTreeTable(myTreeTableModel);
    new TreeTableSpeedSearch(myTreeTable, new Convertor<TreePath, String>() {

        @Override
        public String convert(TreePath o) {
            final Object node = o.getLastPathComponent();
            if (node instanceof DefaultMutableTreeNode) {
                final Object object = ((DefaultMutableTreeNode) node).getUserObject();
                if (object instanceof DNamedElement) {
                    return ((DNamedElement) object).getName();
                }
            }
            return "";
        }
    });
    DefaultActionGroup group = new DefaultActionGroup();
    group.add(ActionManager.getInstance().getAction(RemoveDynamicAction.GROOVY_DYNAMIC_REMOVE));
    PopupHandler.installUnknownPopupHandler(myTreeTable, group, ActionManager.getInstance());
    final MyColoredTreeCellRenderer treeCellRenderer = new MyColoredTreeCellRenderer();
    myTreeTable.setDefaultRenderer(String.class, new TableCellRenderer() {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (value instanceof String) {
                try {
                    final PsiType type = JavaPsiFacade.getElementFactory(myProject).createTypeFromText((String) value, null);
                    String shortName = type.getPresentableText();
                    return new JLabel(shortName);
                } catch (IncorrectOperationException e) {
                    LOG.debug("Type cannot be created", e);
                }
                return new JLabel(QuickfixUtil.shortenType((String) value));
            }
            return new JLabel();
        }
    });
    myTreeTable.setTreeCellRenderer(treeCellRenderer);
    myTreeTable.setRootVisible(false);
    myTreeTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    final MyPropertyTypeCellEditor typeCellEditor = new MyPropertyTypeCellEditor();
    typeCellEditor.addCellEditorListener(new CellEditorListener() {

        @Override
        public void editingStopped(ChangeEvent e) {
            final TreeTableTree tree = getTree();
            String newTypeValue = ((MyPropertyTypeCellEditor) e.getSource()).getCellEditorValue();
            if (newTypeValue == null || tree == null) {
                myTreeTable.editingStopped(e);
                return;
            }
            try {
                final PsiType type = JavaPsiFacade.getElementFactory(myProject).createTypeFromText(newTypeValue, null);
                String canonical = type.getCanonicalText();
                if (canonical != null)
                    newTypeValue = canonical;
            } catch (IncorrectOperationException ex) {
            //do nothing in case bad string is entered
            }
            final TreePath editingTypePath = tree.getSelectionPath();
            if (editingTypePath == null)
                return;
            final TreePath editingClassPath = editingTypePath.getParentPath();
            Object oldTypeValue = myTreeTable.getValueAt(tree.getRowForPath(editingTypePath), TYPE_COLUMN);
            if (!(oldTypeValue instanceof String)) {
                myTreeTable.editingStopped(e);
                return;
            }
            final Object editingPropertyObject = myTreeTable.getValueAt(tree.getRowForPath(editingTypePath), CLASS_OR_ELEMENT_NAME_COLUMN);
            final Object editingClassObject = myTreeTable.getValueAt(tree.getRowForPath(editingClassPath), CLASS_OR_ELEMENT_NAME_COLUMN);
            if (!(editingPropertyObject instanceof DItemElement) || !(editingClassObject instanceof DClassElement)) {
                myTreeTable.editingStopped(e);
                return;
            }
            final DItemElement dynamicElement = (DItemElement) editingPropertyObject;
            final String name = dynamicElement.getName();
            final String className = ((DClassElement) editingClassObject).getName();
            if (dynamicElement instanceof DPropertyElement) {
                DynamicManager.getInstance(myProject).replaceDynamicPropertyType(className, name, (String) oldTypeValue, newTypeValue);
            } else if (dynamicElement instanceof DMethodElement) {
                final List<ParamInfo> myPairList = ((DMethodElement) dynamicElement).getPairs();
                DynamicManager.getInstance(myProject).replaceDynamicMethodType(className, name, myPairList, (String) oldTypeValue, newTypeValue);
            }
        }

        @Override
        public void editingCanceled(ChangeEvent e) {
            myTreeTable.editingCanceled(e);
        }
    });
    RefactoringListenerManager.getInstance(myProject).addListenerProvider(new RefactoringElementListenerProvider() {

        @Override
        @Nullable
        public RefactoringElementListener getListener(final PsiElement element) {
            if (element instanceof PsiClass) {
                final String qualifiedName = ((PsiClass) element).getQualifiedName();
                return new RefactoringElementListener() {

                    @Override
                    public void elementMoved(@NotNull PsiElement newElement) {
                        renameElement(qualifiedName, newElement);
                    }

                    @Override
                    public void elementRenamed(@NotNull PsiElement newElement) {
                        renameElement(qualifiedName, newElement);
                    }

                    private void renameElement(String oldClassName, PsiElement newElement) {
                        if (newElement instanceof PsiClass) {
                            final String newClassName = ((PsiClass) newElement).getQualifiedName();
                            final DRootElement rootElement = DynamicManager.getInstance(myProject).getRootElement();
                            final DClassElement oldClassElement = rootElement.getClassElement(oldClassName);
                            final TreeNode oldClassNode = TreeUtil.findNodeWithObject((DefaultMutableTreeNode) myTreeRoot, oldClassElement);
                            DynamicManager.getInstance(myProject).replaceClassName(oldClassElement, newClassName);
                            myTreeTableModel.nodeChanged(oldClassNode);
                        }
                    }
                };
            }
            return null;
        }
    });
    myTreeTable.setDefaultEditor(String.class, typeCellEditor);
    myTreeTable.registerKeyboardAction(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent event) {
            final int selectionRow = myTreeTable.getTree().getLeadSelectionRow();
            myTreeTable.editCellAt(selectionRow, CLASS_OR_ELEMENT_NAME_COLUMN, event);
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), JComponent.WHEN_FOCUSED);
    myTreeTable.registerKeyboardAction(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent event) {
            final int selectionRow = myTreeTable.getTree().getLeadSelectionRow();
            myTreeTable.editCellAt(selectionRow, TYPE_COLUMN, event);
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_F2, InputEvent.CTRL_MASK), JComponent.WHEN_FOCUSED);
    myTreeTable.getTree().setShowsRootHandles(true);
    myTreeTable.getTableHeader().setReorderingAllowed(false);
    myTreeTable.setPreferredScrollableViewportSize(new Dimension(300, myTreeTable.getRowHeight() * 10));
    myTreeTable.getColumn(myColumnNames[CLASS_OR_ELEMENT_NAME_COLUMN]).setPreferredWidth(200);
    myTreeTable.getColumn(myColumnNames[TYPE_COLUMN]).setPreferredWidth(160);
    JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(myTreeTable);
    scrollPane.setPreferredSize(JBUI.size(600, 400));
    return scrollPane;
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ActionEvent(java.awt.event.ActionEvent) ColumnInfo(com.intellij.util.ui.ColumnInfo) CellEditorListener(javax.swing.event.CellEditorListener) MutableTreeNode(javax.swing.tree.MutableTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeNode(javax.swing.tree.TreeNode) TreeTableTree(com.intellij.ui.treeStructure.treetable.TreeTableTree) TableCellRenderer(javax.swing.table.TableCellRenderer) ListTreeTableModelOnColumns(com.intellij.ui.treeStructure.treetable.ListTreeTableModelOnColumns) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) TreePath(javax.swing.tree.TreePath) ChangeEvent(javax.swing.event.ChangeEvent) RefactoringElementListenerProvider(com.intellij.refactoring.listeners.RefactoringElementListenerProvider) ActionListener(java.awt.event.ActionListener) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Nullable(org.jetbrains.annotations.Nullable)

Example 54 with TreePath

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

the class GrDynamicImplicitProperty method navigate.

@Override
public void navigate(boolean requestFocus) {
    if (canNavigateToSource()) {
        super.navigate(requestFocus);
        return;
    }
    DynamicToolWindowWrapper.getInstance(myProject).getToolWindow().activate(() -> {
        DynamicToolWindowWrapper toolWindowWrapper = DynamicToolWindowWrapper.getInstance(myProject);
        final TreeTable treeTable = toolWindowWrapper.getTreeTable();
        final ListTreeTableModelOnColumns model = toolWindowWrapper.getTreeTableModel();
        Object root = model.getRoot();
        if (root == null || !(root instanceof DefaultMutableTreeNode))
            return;
        DefaultMutableTreeNode treeRoot = ((DefaultMutableTreeNode) root);
        final PsiClass psiClass = getContainingClassElement();
        if (psiClass == null)
            return;
        final DefaultMutableTreeNode desiredNode;
        DPropertyElement dynamicProperty = null;
        PsiClass trueSuper = null;
        for (PsiClass aSuper : PsiUtil.iterateSupers(psiClass, true)) {
            dynamicProperty = DynamicManager.getInstance(myProject).findConcreteDynamicProperty(aSuper.getQualifiedName(), getName());
            if (dynamicProperty != null) {
                trueSuper = aSuper;
                break;
            }
        }
        if (trueSuper == null)
            return;
        final DefaultMutableTreeNode classNode = TreeUtil.findNodeWithObject(treeRoot, new DClassElement(myProject, trueSuper.getQualifiedName()));
        if (classNode == null)
            return;
        desiredNode = TreeUtil.findNodeWithObject(classNode, dynamicProperty);
        if (desiredNode == null)
            return;
        final TreePath path = TreeUtil.getPathFromRoot(desiredNode);
        treeTable.getTree().expandPath(path);
        treeTable.getTree().setSelectionPath(path);
        treeTable.getTree().fireTreeExpanded(path);
        ToolWindowManager.getInstance(myProject).getFocusManager().requestFocus(treeTable, true);
        treeTable.revalidate();
        treeTable.repaint();
    }, true);
}
Also used : ListTreeTableModelOnColumns(com.intellij.ui.treeStructure.treetable.ListTreeTableModelOnColumns) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) DPropertyElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DPropertyElement) DClassElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement) TreePath(javax.swing.tree.TreePath) TreeTable(com.intellij.ui.treeStructure.treetable.TreeTable)

Example 55 with TreePath

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

the class SvnMergeSourceDetails method addRecursively.

private void addRecursively(final SvnFileRevision revision, final MyNode node, final List<TreePath> nodesToExpand) {
    final MyNode current = new MyNode(new MyNamedConfigurable(revision, myFile, myProject, myListsMap));
    node.add(current);
    final TreeNode[] path = ((DefaultTreeModel) myTree.getModel()).getPathToRoot(node);
    nodesToExpand.add(new TreePath(path));
    final List<SvnFileRevision> mergeSources = revision.getMergeSources();
    for (SvnFileRevision source : mergeSources) {
        addRecursively(source, current, nodesToExpand);
    }
}
Also used : TreePath(javax.swing.tree.TreePath) TreeNode(javax.swing.tree.TreeNode) DefaultTreeModel(javax.swing.tree.DefaultTreeModel)

Aggregations

TreePath (javax.swing.tree.TreePath)539 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)177 ArrayList (java.util.ArrayList)58 TreeNode (javax.swing.tree.TreeNode)56 Nullable (org.jetbrains.annotations.Nullable)49 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)46 JTree (javax.swing.JTree)34 NotNull (org.jetbrains.annotations.NotNull)32 Test (org.junit.Test)22 Tree (com.intellij.ui.treeStructure.Tree)21 TreeSelectionEvent (javax.swing.event.TreeSelectionEvent)21 TreeSelectionListener (javax.swing.event.TreeSelectionListener)21 MouseEvent (java.awt.event.MouseEvent)18 Point (java.awt.Point)17 ActionEvent (java.awt.event.ActionEvent)15 KeyEvent (java.awt.event.KeyEvent)12 EVTask (net.sourceforge.processdash.ev.EVTask)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 KeyAdapter (java.awt.event.KeyAdapter)11 List (java.util.List)11