2 * Copyright 2000-2010 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.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;
33 * Created by IntelliJ IDEA.
34 * User: Eugene.Kudelevsky
37 * To change this template use File | Settings | File Templates.
39 public class WrapWithCustomTemplateAction extends AnAction {
40 private final CustomLiveTemplate myTemplate;
41 private final Editor myEditor;
42 private final PsiFile myFile;
44 public WrapWithCustomTemplateAction(CustomLiveTemplate template,
47 final Set<Character> usedMnemonicsSet) {
48 super(InvokeTemplateAction.extractMnemonic(template.getTitle(), usedMnemonicsSet));
49 myTemplate = template;
55 public void actionPerformed(AnActionEvent e) {
56 final Document document = myEditor.getDocument();
57 final VirtualFile file = FileDocumentManager.getInstance().getFile(document);
59 ReadonlyStatusHandler.getInstance(myFile.getProject()).ensureFilesWritable(file);
62 String selection = myEditor.getSelectionModel().getSelectedText();
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();