DefaultTreeExpander refactoring

GitOrigin-RevId: 1d1852d499bd2b78f16ab13d4373d06d0689e405
This commit is contained in:
Sergey Malenkov
2020-06-02 22:58:15 +03:00
committed by intellij-monorepo-bot
parent 63f6524990
commit 2a4b4142d3
14 changed files with 95 additions and 281 deletions
@@ -3,6 +3,7 @@ package com.intellij.openapi.roots.ui.configuration.projectRoot;
import com.intellij.facet.Facet;
import com.intellij.ide.CommonActionsManager;
import com.intellij.ide.DefaultTreeExpander;
import com.intellij.ide.TreeExpander;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.*;
@@ -153,27 +154,7 @@ public abstract class BaseStructureConfigurable extends MasterDetailsComponent i
}
protected void addCollapseExpandActions(final List<? super AnAction> result) {
final TreeExpander expander = new TreeExpander() {
@Override
public void expandAll() {
TreeUtil.expandAll(myTree);
}
@Override
public boolean canExpand() {
return true;
}
@Override
public void collapseAll() {
TreeUtil.collapseAll(myTree, 0);
}
@Override
public boolean canCollapse() {
return true;
}
};
final TreeExpander expander = new DefaultTreeExpander(myTree);
final CommonActionsManager actionsManager = CommonActionsManager.getInstance();
result.add(actionsManager.createExpandAllAction(expander, myTree));
result.add(actionsManager.createCollapseAllAction(expander, myTree));
@@ -956,26 +956,10 @@ public class ProjectViewImpl extends ProjectView implements PersistentStateCompo
protected void createTitleActions(@NotNull List<? super AnAction> titleActions) {
titleActions.add(new ScrollFromSourceAction());
AnAction collapseAllAction = CommonActionsManager.getInstance().createCollapseAllAction(new TreeExpander() {
@Override
public boolean canExpand() {
return false;
}
@Override
public void collapseAll() {
AbstractProjectViewPane pane = getCurrentProjectViewPane();
JTree tree = pane.myTree;
if (tree != null) {
TreeUtil.collapseAll(tree, 0);
}
}
@Override
public boolean canCollapse() {
return true;
}
}, getComponent());
AnAction collapseAllAction = CommonActionsManager.getInstance().createCollapseAllAction(new DefaultTreeExpander(() -> {
AbstractProjectViewPane pane = getCurrentProjectViewPane();
return pane == null ? null : pane.myTree;
}), getComponent());
collapseAllAction.getTemplatePresentation().setIcon(AllIcons.Actions.Collapseall);
titleActions.add(collapseAllAction);
}
@@ -70,7 +70,7 @@ public abstract class TodoPanel extends SimpleToolWindowPanel implements Occuren
private final Content myContent;
private final Tree myTree;
private final MyTreeExpander myTreeExpander;
private final TreeExpander myTreeExpander;
private final MyOccurenceNavigator myOccurenceNavigator;
protected final TodoTreeBuilder myTodoTreeBuilder;
private MyVisibilityWatcher myVisibilityWatcher;
@@ -93,7 +93,7 @@ public abstract class TodoPanel extends SimpleToolWindowPanel implements Occuren
DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode());
myTree = new Tree(model);
myTreeExpander = new MyTreeExpander();
myTreeExpander = new DefaultTreeExpander(myTree);
myOccurenceNavigator = new MyOccurenceNavigator();
initUI();
myTodoTreeBuilder = setupTreeStructure();
@@ -475,32 +475,6 @@ public abstract class TodoPanel extends SimpleToolWindowPanel implements Occuren
}, 300);
}
TreeExpander getTreeExpander() {
return myTreeExpander;
}
private final class MyTreeExpander implements TreeExpander {
@Override
public boolean canCollapse() {
return true;
}
@Override
public boolean canExpand() {
return true;
}
@Override
public void collapseAll() {
TreeUtil.collapseAll(myTree, 0);
}
@Override
public void expandAll() {
TreeUtil.expandAll(myTree);
}
}
/**
* Provides support for "auto scroll to source" functionality
*/
@@ -6,6 +6,7 @@ import com.intellij.ProjectTopics;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.icons.AllIcons;
import com.intellij.ide.CommonActionsManager;
import com.intellij.ide.DefaultTreeExpander;
import com.intellij.ide.TreeExpander;
import com.intellij.idea.ActionsBundle;
import com.intellij.openapi.Disposable;
@@ -433,33 +434,22 @@ public class ModulesDependenciesPanel extends JPanel implements Disposable {
}
}
private static class MyTreeExpander implements TreeExpander {
private final Tree myTree;
private static class MyTreeExpander extends DefaultTreeExpander {
private final boolean myEnableExpandAll;
MyTreeExpander(Tree tree, boolean enableExpandAll) {
myTree = tree;
super(tree);
myEnableExpandAll = enableExpandAll;
}
@Override
public void expandAll() {
TreeUtil.expandAll(myTree);
}
@Override
public boolean canExpand() {
return myEnableExpandAll;
return myEnableExpandAll && super.canExpand();
}
@Override
public void collapseAll() {
TreeUtil.collapseAll(myTree, 3);
}
@Override
public boolean canCollapse() {
return true;
protected void collapseAll(@NotNull JTree tree, int keepSelectionLevel) {
super.collapseAll(tree, 3);
}
}
}
@@ -48,6 +48,7 @@ import com.intellij.ui.*;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.labels.LinkLabel;
import com.intellij.ui.treeStructure.treetable.DefaultTreeTableExpander;
import com.intellij.util.Alarm;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
@@ -615,17 +616,7 @@ public class SingleInspectionProfilePanel extends JPanel {
}
});
myTreeExpander = new DefaultTreeExpander(myTreeTable.getTree()) {
@Override
public boolean canExpand() {
return myTreeTable.isShowing();
}
@Override
public boolean canCollapse() {
return myTreeTable.isShowing();
}
};
myTreeExpander = new DefaultTreeTableExpander(myTreeTable);
myProfileFilter = new MyFilterComponent();
return scrollPane;
@@ -1,62 +0,0 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.ide;
import com.intellij.util.ui.tree.TreeUtil;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
/**
* @author yole
*/
public class DefaultTreeExpander implements TreeExpander {
private final JTree myTree;
public DefaultTreeExpander(@NotNull final JTree tree) {
myTree = tree;
}
@Override
public void expandAll() {
TreeUtil.expandAll(myTree);
showSelectionCentered();
}
@Override
public boolean canExpand() {
return myTree.isShowing();
}
@Override
public void collapseAll() {
TreeUtil.collapseAll(myTree, 1);
showSelectionCentered();
}
private void showSelectionCentered() {
int[] rows = myTree.getSelectionRows();
if (rows != null && rows.length > 0) {
TreeUtil.showRowCentered(myTree, rows[0], false);
}
}
@Override
public boolean canCollapse() {
return myTree.isShowing();
}
}
@@ -0,0 +1,49 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide
import com.intellij.util.ui.tree.TreeUtil
import javax.swing.JTree
open class DefaultTreeExpander(private val supplier: () -> JTree?) : TreeExpander {
constructor(tree: JTree) : this({ tree })
override fun canExpand() = supplier.invoke()?.let { canCollapse(it) } ?: false
protected open fun canExpand(tree: JTree) = isEnabled(tree)
override fun expandAll() {
supplier.invoke()?.let { expandAll(it) }
}
protected open fun expandAll(tree: JTree) {
TreeUtil.expandAll(tree)
showSelectionCentered(tree)
}
override fun canCollapse() = supplier.invoke()?.let { canCollapse(it) } ?: false
protected open fun canCollapse(tree: JTree) = isEnabled(tree)
override fun collapseAll() {
supplier.invoke()?.let { collapseAll(it, 1) }
}
protected open fun collapseAll(tree: JTree, keepSelectionLevel: Int) {
TreeUtil.collapseAll(tree, keepSelectionLevel)
showSelectionCentered(tree)
}
protected open fun isEnabled(tree: JTree) = isShowing(tree) && tree.rowCount > 0
protected open fun isShowing(tree: JTree) = tree.isShowing
protected open fun showSelectionCentered(tree: JTree) {
tree.selectionPath?.let { TreeUtil.scrollToVisible(tree, it, true) }
}
}
@@ -0,0 +1,9 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ui.treeStructure.treetable
import com.intellij.ide.DefaultTreeExpander
import javax.swing.JTree
open class DefaultTreeTableExpander(table: TreeTable) : DefaultTreeExpander({ table.tree }) {
override fun isShowing(tree: JTree) = (tree as? TreeTableTree)?.treeTable?.isShowing ?: false
}
@@ -5,6 +5,7 @@ import com.intellij.diagnostic.VMOptions;
import com.intellij.icons.AllIcons;
import com.intellij.ide.CommonActionsManager;
import com.intellij.ide.DataManager;
import com.intellij.ide.DefaultTreeExpander;
import com.intellij.ide.IdeBundle;
import com.intellij.ide.TreeExpander;
import com.intellij.openapi.Disposable;
@@ -435,27 +436,7 @@ public class KeymapPanel extends JPanel implements SearchableConfigurable, Confi
@NotNull
public static TreeExpander createTreeExpander(ActionsTree actionsTree) {
return new TreeExpander() {
@Override
public void expandAll() {
TreeUtil.expandAll(actionsTree.getTree());
}
@Override
public boolean canExpand() {
return true;
}
@Override
public void collapseAll() {
TreeUtil.collapseAll(actionsTree.getTree(), 0);
}
@Override
public boolean canCollapse() {
return true;
}
};
return new DefaultTreeExpander(() -> actionsTree == null ? null : actionsTree.getTree());
}
private void filterTreeByShortcut(Shortcut shortcut) {
@@ -13,19 +13,24 @@ import com.intellij.ui.IdeBorderFactory.createBorder
import com.intellij.ui.JBColor
import com.intellij.ui.ScrollPaneFactory.createScrollPane
import com.intellij.ui.SideBorder
import com.intellij.ui.treeStructure.Tree
import com.intellij.util.EditSourceOnDoubleClickHandler.isToggleEvent
import com.intellij.util.OpenSourceUtil.openSourcesFrom
import com.intellij.util.Processor
import com.intellij.util.ui.JBUI.Panels.simplePanel
import com.intellij.util.ui.components.BorderLayoutPanel
import com.intellij.util.ui.tree.TreeUtil
import javax.swing.JTree
import javax.swing.SwingConstants
import kotlin.properties.Delegates.observable
private class ChangesViewPanel(project: Project) : BorderLayoutPanel() {
val changesView: ChangesListView = ChangesListView(project, false).apply {
treeExpander = ChangesViewTreeExpander(this)
treeExpander = object : DefaultTreeExpander(this) {
override fun collapseAll(tree: JTree, keepSelectionLevel: Int) {
super.collapseAll(tree, 2)
TreeUtil.expand(tree, 1)
}
}
doubleClickHandler = Processor { e ->
if (isToggleEvent(this, e)) return@Processor false
@@ -71,10 +76,3 @@ private class ChangesViewPanel(project: Project) : BorderLayoutPanel() {
}
}
}
private class ChangesViewTreeExpander(private val tree: Tree) : DefaultTreeExpander(tree) {
override fun collapseAll() {
TreeUtil.collapseAll(tree, 2)
TreeUtil.expand(tree, 1)
}
}
@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.lang.ant.config.explorer;
import com.intellij.execution.ExecutionBundle;
@@ -9,6 +9,7 @@ import com.intellij.execution.impl.RunDialog;
import com.intellij.icons.AllIcons;
import com.intellij.ide.CommonActionsManager;
import com.intellij.ide.DataManager;
import com.intellij.ide.DefaultTreeExpander;
import com.intellij.ide.TreeExpander;
import com.intellij.ide.dnd.FileCopyPasteUtil;
import com.intellij.ide.highlighter.XmlFileType;
@@ -77,26 +78,11 @@ public class AntExplorer extends SimpleToolWindowPanel implements DataProvider,
private final AntExplorerTreeStructure myTreeStructure;
private StructureTreeModel myTreeModel;
private final TreeExpander myTreeExpander = new TreeExpander() {
private final TreeExpander myTreeExpander = new DefaultTreeExpander(() -> myTree) {
@Override
public void expandAll() {
TreeUtil.expandAll(myTree);
}
@Override
public void collapseAll() {
TreeUtil.collapseAll(myTree, 1);
}
@Override
public boolean canExpand() {
protected boolean isEnabled(@NotNull JTree tree) {
final AntConfiguration config = myConfig;
return config != null && !config.getBuildFileList().isEmpty();
}
@Override
public boolean canCollapse() {
return canExpand();
return config != null && !config.getBuildFileList().isEmpty() && super.isEnabled(tree);
}
};
@@ -1,10 +1,10 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.idea.svn.dialogs;
import com.intellij.CommonBundle;
import com.intellij.icons.AllIcons;
import com.intellij.ide.CommonActionsManager;
import com.intellij.ide.TreeExpander;
import com.intellij.ide.DefaultTreeExpander;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.diagnostic.Logger;
@@ -150,27 +150,8 @@ public class RepositoryBrowserDialog extends DialogWrapper {
mkDirAction.registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT,
InputEvent.ALT_MASK | InputEvent.ALT_DOWN_MASK)), browser);
AnAction action = CommonActionsManager.getInstance().createCollapseAllAction(new TreeExpander() {
@Override
public boolean canExpand() {
return false;
}
@Override
public void collapseAll() {
JTree tree = browser.getRepositoryTree();
int row = tree.getRowCount() - 1;
while (row >= 0) {
tree.collapseRow(row);
row--;
}
}
@Override
public boolean canCollapse() {
return true;
}
}, browser);
AnAction action = CommonActionsManager.getInstance().createCollapseAllAction(
new DefaultTreeExpander(browser::getRepositoryTree), browser);
group.add(action);
if (!isEmpty(additionalActions)) {
@@ -1,7 +1,9 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.intellij.plugins.xsltDebugger.ui;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.ide.CommonActionsManager;
import com.intellij.ide.DefaultTreeExpander;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
@@ -29,7 +31,7 @@ public class StructureTabComponent extends AbstractTabComponent {
myEventModel.addTreeModelListener(new SmartStructureTracker(tree, disposable));
final DefaultActionGroup structureActions = new DefaultActionGroup();
final StructureTreeExpander expander = new StructureTreeExpander(tree);
final DefaultTreeExpander expander = new DefaultTreeExpander(tree);
final CommonActionsManager actionsManager = CommonActionsManager.getInstance();
structureActions.add(new HideWhitespaceAction(tree, myEventModel));
structureActions.add(actionsManager.createExpandAllAction(expander, tree));
@@ -1,50 +0,0 @@
/*
* Copyright 2002-2007 Sascha Weinreuter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.intellij.plugins.xsltDebugger.ui;
import com.intellij.ide.TreeExpander;
import com.intellij.util.ui.tree.TreeUtil;
import javax.swing.*;
public class StructureTreeExpander implements TreeExpander {
private final JTree myTree;
public StructureTreeExpander(JTree tree) {
myTree = tree;
}
@Override
public void expandAll() {
TreeUtil.expandAll(myTree);
}
@Override
public boolean canExpand() {
return true;
}
@Override
public void collapseAll() {
TreeUtil.collapseAll(myTree, 1);
}
@Override
public boolean canCollapse() {
return true;
}
}