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
.javadoc
.actions
;
18 import com
.intellij
.javadoc
.JavadocGenerationManager
;
19 import com
.intellij
.openapi
.actionSystem
.*;
20 import com
.intellij
.openapi
.editor
.Editor
;
21 import com
.intellij
.openapi
.project
.Project
;
22 import com
.intellij
.openapi
.vfs
.VirtualFile
;
23 import com
.intellij
.psi
.*;
25 public final class GenerateJavadocAction
extends AnAction
{
27 public void actionPerformed(AnActionEvent e
) {
28 final DataContext dataContext
= e
.getDataContext();
29 final Project project
= PlatformDataKeys
.PROJECT
.getData(dataContext
);
30 final PsiDirectory dir
= getDirectoryFromContext(dataContext
);
31 JavadocGenerationManager
.getInstance(project
).generateJavadoc(dir
, dataContext
);
34 public void update(AnActionEvent event
){
35 final Presentation presentation
= event
.getPresentation();
36 presentation
.setEnabled(event
.getDataContext().getData(DataConstants
.PROJECT
) != null);
39 private static PsiDirectory
getDirectoryFromContext(final DataContext dataContext
) {
40 final Project project
= PlatformDataKeys
.PROJECT
.getData(dataContext
);
41 if (project
== null) return null;
42 final Editor editor
= PlatformDataKeys
.EDITOR
.getData(dataContext
);
44 PsiFile psiFile
= PsiDocumentManager
.getInstance(project
).getPsiFile(editor
.getDocument());
45 if (psiFile
!= null) return psiFile
.getContainingDirectory();
47 PsiElement element
= LangDataKeys
.PSI_ELEMENT
.getData(dataContext
);
48 if (element
!= null) {
49 if (element
instanceof PsiDirectory
) return (PsiDirectory
)element
;
51 PsiFile psiFile
= element
.getContainingFile();
52 if (psiFile
!= null) return psiFile
.getContainingDirectory();
55 //This is the case with GUI designer
56 VirtualFile virtualFile
= PlatformDataKeys
.VIRTUAL_FILE
.getData(dataContext
);
57 if (virtualFile
!= null && virtualFile
.isValid()) {
58 PsiFile psiFile
= PsiManager
.getInstance(project
).findFile(virtualFile
);
59 if (psiFile
!= null) return psiFile
.getContainingDirectory();