From f56d41321192c71379217cb48848e577249e0e5c Mon Sep 17 00:00:00 2001 From: peter Date: Thu, 2 Jul 2015 16:51:55 +0200 Subject: [PATCH] extract LiveTemplateTree class --- .../template/impl/LiveTemplateTree.java | 64 +++++++++++++++++++ .../template/impl/TemplateListPanel.java | 49 +++----------- 2 files changed, 74 insertions(+), 39 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateTree.java diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateTree.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateTree.java new file mode 100644 index 000000000000..150b87bc6056 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateTree.java @@ -0,0 +1,64 @@ +/* + * Copyright 2000-2015 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.codeInsight.template.impl; + +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.ui.CheckboxTree; +import com.intellij.ui.CheckedTreeNode; +import com.intellij.ui.TreeSpeedSearch; +import com.intellij.util.containers.Convertor; + +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.TreePath; + +/** + * @author peter + */ +class LiveTemplateTree extends CheckboxTree { + LiveTemplateTree(final CheckboxTreeCellRenderer renderer, final CheckedTreeNode root) { + super(renderer, root); + } + + @Override + protected void onNodeStateChanged(final CheckedTreeNode node) { + Object obj = node.getUserObject(); + if (obj instanceof TemplateImpl) { + ((TemplateImpl)obj).setDeactivated(!node.isChecked()); + } + } + + @Override + protected void installSpeedSearch() { + new TreeSpeedSearch(this, new Convertor() { + @Override + public String convert(TreePath o) { + Object object = ((DefaultMutableTreeNode)o.getLastPathComponent()).getUserObject(); + if (object instanceof TemplateGroup) { + return ((TemplateGroup)object).getName(); + } + if (object instanceof TemplateImpl) { + TemplateImpl template = (TemplateImpl)object; + return StringUtil.notNullize(template.getKey()) + + " " + + StringUtil.notNullize(template.getDescription()) + + " " + + template.getTemplateText(); + } + return ""; + } + }, true); + } +} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateListPanel.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateListPanel.java index 24c80b3c12f7..217b74e424b6 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateListPanel.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateListPanel.java @@ -39,7 +39,6 @@ import com.intellij.util.NullableFunction; import com.intellij.util.ObjectUtils; import com.intellij.util.PlatformIcons; import com.intellij.util.containers.ContainerUtil; -import com.intellij.util.containers.Convertor; import com.intellij.util.ui.tree.TreeUtil; import com.intellij.util.ui.update.UiNotifyConnector; import org.jetbrains.annotations.NotNull; @@ -454,15 +453,15 @@ public class TemplateListPanel extends JPanel implements Disposable { private JPanel createTable() { myTreeRoot = new CheckedTreeNode(null); - myTree = new CheckboxTree(new CheckboxTree.CheckboxTreeCellRenderer(){ + myTree = new LiveTemplateTree(new CheckboxTree.CheckboxTreeCellRenderer() { @Override public void customizeRenderer(final JTree tree, - Object value, - final boolean selected, - final boolean expanded, - final boolean leaf, - final int row, - final boolean hasFocus) { + Object value, + final boolean selected, + final boolean expanded, + final boolean leaf, + final int row, + final boolean hasFocus) { if (!(value instanceof DefaultMutableTreeNode)) return; value = ((DefaultMutableTreeNode)value).getUserObject(); @@ -473,42 +472,14 @@ public class TemplateListPanel extends JPanel implements Disposable { getTextRenderer().append(template.getKey(), new SimpleTextAttributes(SimpleTextAttributes.STYLE_PLAIN, fgColor)); String description = template.getDescription(); if (StringUtil.isNotEmpty(description)) { - getTextRenderer().append (" (" + description + ")", SimpleTextAttributes.GRAY_ATTRIBUTES); + getTextRenderer().append(" (" + description + ")", SimpleTextAttributes.GRAY_ATTRIBUTES); } } else if (value instanceof TemplateGroup) { - getTextRenderer().append (((TemplateGroup)value).getName(), SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES); - } - - } - }, myTreeRoot) { - @Override - protected void onNodeStateChanged(final CheckedTreeNode node) { - Object obj = node.getUserObject(); - if (obj instanceof TemplateImpl) { - ((TemplateImpl)obj).setDeactivated(!node.isChecked()); + getTextRenderer().append(((TemplateGroup)value).getName(), SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES); } } - - @Override - protected void installSpeedSearch() { - new TreeSpeedSearch(this, new Convertor() { - @Override - public String convert(TreePath o) { - Object object = ((DefaultMutableTreeNode)o.getLastPathComponent()).getUserObject(); - if (object instanceof TemplateGroup) { - return ((TemplateGroup)object).getName(); - } - if (object instanceof TemplateImpl) { - TemplateImpl template = (TemplateImpl)object; - return StringUtil.notNullize(template.getKey()) + " " + StringUtil.notNullize(template.getDescription()) + " " + template.getTemplateText(); - } - return ""; - } - }, true); - - } - }; + }, myTreeRoot); myTree.setRootVisible(false); myTree.setShowsRootHandles(true); myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);