2 * Copyright 2000-2016 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInsight.template.impl;
18 import com.intellij.ide.CopyProvider;
19 import com.intellij.ide.PasteProvider;
20 import com.intellij.openapi.actionSystem.DataContext;
21 import com.intellij.openapi.actionSystem.DataProvider;
22 import com.intellij.openapi.actionSystem.PlatformDataKeys;
23 import com.intellij.openapi.ide.CopyPasteManager;
24 import com.intellij.openapi.util.JDOMUtil;
25 import com.intellij.openapi.util.text.StringUtil;
26 import com.intellij.ui.CheckboxTree;
27 import com.intellij.ui.CheckedTreeNode;
28 import com.intellij.ui.TreeSpeedSearch;
29 import com.intellij.util.JdomKt;
30 import com.intellij.util.SystemProperties;
31 import org.jdom.Element;
32 import org.jetbrains.annotations.NonNls;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
36 import javax.swing.tree.DefaultMutableTreeNode;
37 import java.awt.datatransfer.DataFlavor;
38 import java.awt.datatransfer.StringSelection;
44 class LiveTemplateTree extends CheckboxTree implements DataProvider, CopyProvider, PasteProvider {
45 private final TemplateListPanel myConfigurable;
47 LiveTemplateTree(final CheckboxTreeCellRenderer renderer, final CheckedTreeNode root, TemplateListPanel configurable) {
48 super(renderer, root);
49 myConfigurable = configurable;
53 protected void onNodeStateChanged(final CheckedTreeNode node) {
54 Object obj = node.getUserObject();
55 if (obj instanceof TemplateImpl) {
56 ((TemplateImpl)obj).setDeactivated(!node.isChecked());
61 protected void installSpeedSearch() {
62 new TreeSpeedSearch(this, o -> {
63 Object object = ((DefaultMutableTreeNode)o.getLastPathComponent()).getUserObject();
64 if (object instanceof TemplateGroup) {
65 return ((TemplateGroup)object).getName();
67 if (object instanceof TemplateImpl) {
68 TemplateImpl template = (TemplateImpl)object;
69 return StringUtil.notNullize(template.getGroupName()) + " " +
70 StringUtil.notNullize(template.getKey()) + " " +
71 StringUtil.notNullize(template.getDescription()) + " " +
72 template.getTemplateText();
80 public Object getData(@NonNls String dataId) {
81 if (PlatformDataKeys.COPY_PROVIDER.is(dataId) || PlatformDataKeys.PASTE_PROVIDER.is(dataId)) {
88 public void performCopy(@NotNull DataContext dataContext) {
89 final Set<TemplateImpl> templates = myConfigurable.getSelectedTemplates().keySet();
92 CopyPasteManager.getInstance().setContents(
93 new StringSelection(StringUtil.join(templates,
94 template -> JDOMUtil.writeElement(TemplateSettings.serializeTemplate(template)),
95 SystemProperties.getLineSeparator())));
100 public boolean isCopyEnabled(@NotNull DataContext dataContext) {
101 return !myConfigurable.getSelectedTemplates().isEmpty();
105 public boolean isCopyVisible(@NotNull DataContext dataContext) {
106 return isCopyEnabled(dataContext);
110 public boolean isPastePossible(@NotNull DataContext dataContext) {
111 if (myConfigurable.getSingleSelectedGroup() == null) return false;
113 String s = CopyPasteManager.getInstance().getContents(DataFlavor.stringFlavor);
114 return s != null && s.startsWith("<template ");
118 public boolean isPasteEnabled(@NotNull DataContext dataContext) {
119 return isPastePossible(dataContext);
123 public void performPaste(@NotNull DataContext dataContext) {
124 TemplateGroup group = myConfigurable.getSingleSelectedGroup();
125 assert group != null;
127 String buffer = CopyPasteManager.getInstance().getContents(DataFlavor.stringFlavor);
128 assert buffer != null;
131 for (Element templateElement : JdomKt.loadElement("<root>" + buffer + "</root>").getChildren(TemplateSettings.TEMPLATE)) {
132 TemplateImpl template = TemplateSettings.readTemplateFromElement(group.getName(), templateElement, getClass().getClassLoader());
133 while (group.containsTemplate(template.getKey(), template.getId())) {
134 template.setKey(template.getKey() + "1");
135 if (template.getId() != null) {
136 template.setId(template.getId() + "1");
139 myConfigurable.addTemplate(template);
142 catch (Exception ignore) {