update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / daemon / impl / quickfix / CreateClassFromUsageFix.java
blobdf827ef8e3488810b94c5d6245ab6dd72d5115db
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.codeInsight.daemon.impl.quickfix;
18 import com.intellij.codeInsight.daemon.QuickFixBundle;
19 import com.intellij.codeInsight.CodeInsightUtilBase;
20 import com.intellij.openapi.application.ApplicationManager;
21 import com.intellij.openapi.editor.Editor;
22 import com.intellij.openapi.fileEditor.FileEditorManager;
23 import com.intellij.openapi.fileEditor.OpenFileDescriptor;
24 import com.intellij.openapi.fileEditor.ex.IdeDocumentHistory;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.util.text.StringUtil;
27 import com.intellij.psi.PsiClass;
28 import com.intellij.psi.PsiFile;
29 import com.intellij.psi.PsiJavaCodeReferenceElement;
30 import com.intellij.util.IncorrectOperationException;
31 import org.jetbrains.annotations.NotNull;
33 /**
34 * @author Mike
36 public class CreateClassFromUsageFix extends CreateClassFromUsageBaseFix {
38 public CreateClassFromUsageFix(PsiJavaCodeReferenceElement refElement, CreateClassKind kind) {
39 super(kind, refElement);
42 public String getText(String varName) {
43 return QuickFixBundle.message("create.class.from.usage.text", StringUtil.capitalize(myKind.getDescription()), varName);
47 public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) {
48 final PsiJavaCodeReferenceElement element = getRefElement();
49 assert element != null;
50 if (!CodeInsightUtilBase.preparePsiElementForWrite(element)) return;
51 final String superClassName = getSuperClassName(element);
52 ApplicationManager.getApplication().runWriteAction(
53 new Runnable() {
54 public void run() {
55 PsiJavaCodeReferenceElement refElement = element;
56 final PsiClass aClass = CreateFromUsageUtils.createClass(refElement, myKind, superClassName);
57 if (aClass == null) return;
58 try {
59 refElement = (PsiJavaCodeReferenceElement)refElement.bindToElement(aClass);
61 catch (IncorrectOperationException e) {
62 LOG.error(e);
65 IdeDocumentHistory.getInstance(project).includeCurrentPlaceAsChangePlace();
67 OpenFileDescriptor descriptor = new OpenFileDescriptor(refElement.getProject(), aClass.getContainingFile().getVirtualFile(),
68 aClass.getTextOffset());
69 FileEditorManager.getInstance(aClass.getProject()).openTextEditor(descriptor, true);
75 public boolean startInWriteAction() {
76 return false;