update copyright
[fedora-idea.git] / xml / dom-impl / src / com / intellij / util / xml / impl / DomTemplateRunnerImpl.java
blob2b3c5b29e5c09ac94b9bf8842e6a08975f34d011
1 /*
2 * Copyright 2000-2009 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.util.xml.impl;
18 import com.intellij.codeInsight.template.Template;
19 import com.intellij.codeInsight.template.TemplateManager;
20 import com.intellij.codeInsight.template.impl.TemplateSettings;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.editor.Editor;
23 import com.intellij.psi.PsiDocumentManager;
24 import com.intellij.psi.xml.XmlTag;
25 import com.intellij.util.xml.DomElement;
26 import com.intellij.util.xml.actions.generate.DomTemplateRunner;
27 import org.jetbrains.annotations.Nullable;
29 /**
30 * User: Sergey.Vasiliev
32 public class DomTemplateRunnerImpl extends DomTemplateRunner {
33 private final Project myProject;
35 public DomTemplateRunnerImpl(Project project) {
36 myProject = project;
39 public <T extends DomElement> void runTemplate(final T t, final String mappingId, final Editor editor) {
40 final Template template = getTemplate(mappingId);
41 runTemplate(t, editor, template);
44 public <T extends DomElement> void runTemplate(final T t, final Editor editor, @Nullable final Template template) {
45 if (template != null) {
46 DomElement copy = t.createStableCopy();
47 PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(editor.getDocument());
48 XmlTag tag = copy.getXmlTag();
49 assert tag != null;
50 editor.getCaretModel().moveToOffset(tag.getTextRange().getStartOffset());
51 copy.undefine();
53 PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(editor.getDocument());
55 template.setToReformat(true);
56 TemplateManager.getInstance(myProject).startTemplate(editor, template);
60 @Nullable
61 protected static Template getTemplate(final String mappingId) {
62 return mappingId != null ? TemplateSettings.getInstance().getTemplateById(mappingId) : null;