renaming; show popup, if it is simple live template invokation by zen-coding shortcut...
[idea/community.git] / platform / lang-impl / src / com / intellij / codeInsight / template / impl / WrapWithCustomTemplateAction.java
1 /*
2  * Copyright 2000-2010 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.codeInsight.template.impl;
17
18 import com.intellij.codeInsight.template.CustomLiveTemplate;
19 import com.intellij.codeInsight.template.CustomTemplateCallback;
20 import com.intellij.codeInsight.template.TemplateInvokationListener;
21 import com.intellij.openapi.actionSystem.AnAction;
22 import com.intellij.openapi.actionSystem.AnActionEvent;
23 import com.intellij.openapi.editor.Document;
24 import com.intellij.openapi.editor.Editor;
25 import com.intellij.openapi.fileEditor.FileDocumentManager;
26 import com.intellij.openapi.vfs.ReadonlyStatusHandler;
27 import com.intellij.openapi.vfs.VirtualFile;
28 import com.intellij.psi.PsiFile;
29
30 import java.util.Set;
31
32 /**
33  * Created by IntelliJ IDEA.
34  * User: Eugene.Kudelevsky
35  * Date: 25.03.2010
36  * Time: 1:50:53
37  * To change this template use File | Settings | File Templates.
38  */
39 public class WrapWithCustomTemplateAction extends AnAction {
40   private final CustomLiveTemplate myTemplate;
41   private final Editor myEditor;
42   private final PsiFile myFile;
43
44   public WrapWithCustomTemplateAction(CustomLiveTemplate template,
45                                       final Editor editor,
46                                       final PsiFile file,
47                                       final Set<Character> usedMnemonicsSet) {
48     super(InvokeTemplateAction.extractMnemonic(template.getTitle(), usedMnemonicsSet));
49     myTemplate = template;
50     myFile = file;
51     myEditor = editor;
52   }
53
54
55   public void actionPerformed(AnActionEvent e) {
56     final Document document = myEditor.getDocument();
57     final VirtualFile file = FileDocumentManager.getInstance().getFile(document);
58     if (file != null) {
59       ReadonlyStatusHandler.getInstance(myFile.getProject()).ensureFilesWritable(file);
60     }
61
62     String selection = myEditor.getSelectionModel().getSelectedText();
63
64     if (selection != null) {
65       selection = selection.trim();
66       final CustomTemplateCallback callback = new CustomTemplateCallback(myEditor, myFile);
67       myTemplate.wrap(selection, callback, new TemplateInvokationListener() {
68         public void finished() {
69           callback.startAllExpandedTemplates();
70         }
71       });
72     }
73   }
74 }