update copyright
[fedora-idea.git] / xml / dom-openapi / src / com / intellij / util / xml / ui / actions / generate / GenerateDomElementAction.java
blob372bf84cc77ee84a9ae28f446dcfb2ade32c95f4
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.
17 package com.intellij.util.xml.ui.actions.generate;
19 import com.intellij.codeInsight.CodeInsightActionHandler;
20 import com.intellij.codeInsight.actions.CodeInsightAction;
21 import com.intellij.openapi.application.Result;
22 import com.intellij.openapi.command.WriteCommandAction;
23 import com.intellij.openapi.editor.Editor;
24 import com.intellij.openapi.project.Project;
25 import com.intellij.psi.PsiFile;
26 import com.intellij.util.xml.DomElement;
27 import com.intellij.util.xml.DomUtil;
28 import org.jetbrains.annotations.NotNull;
30 /**
31 * User: Sergey.Vasiliev
33 public class GenerateDomElementAction extends CodeInsightAction {
35 protected final GenerateDomElementProvider myProvider;
37 public GenerateDomElementAction(final GenerateDomElementProvider generateProvider) {
38 getTemplatePresentation().setDescription(generateProvider.getDescription());
39 getTemplatePresentation().setText(generateProvider.getDescription());
41 myProvider = generateProvider;
44 protected CodeInsightActionHandler getHandler() {
45 return new CodeInsightActionHandler() {
46 public void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file) {
47 final Runnable runnable = new Runnable() {
48 public void run() {
49 final DomElement element = myProvider.generate(project, editor, file);
50 myProvider.navigate(element);
54 if (GenerateDomElementAction.this.startInWriteAction()) {
55 new WriteCommandAction(project, file) {
56 protected void run(final Result result) throws Throwable {
57 runnable.run();
59 }.execute();
61 else {
62 runnable.run();
66 public boolean startInWriteAction() {
67 return false;
72 protected boolean startInWriteAction() {
73 return true;
76 protected boolean isValidForFile(final Project project, final Editor editor, final PsiFile file) {
77 final DomElement element = DomUtil.getContextElement(editor);
78 return element != null && myProvider.isAvailableForElement(element);