use of com.intellij.ide.IdeView in project intellij-community by JetBrains.
the class CreateFromTemplateActionBase method actionPerformed.
@Override
public final void actionPerformed(AnActionEvent e) {
DataContext dataContext = e.getDataContext();
IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
if (view == null)
return;
PsiDirectory dir = getTargetDirectory(dataContext, view);
if (dir == null)
return;
Project project = dir.getProject();
FileTemplate selectedTemplate = getTemplate(project, dir);
if (selectedTemplate != null) {
AnAction action = getReplacedAction(selectedTemplate);
if (action != null) {
action.actionPerformed(e);
} else {
FileTemplateManager.getInstance(project).addRecentName(selectedTemplate.getName());
AttributesDefaults defaults = getAttributesDefaults(dataContext);
Properties properties = defaults != null ? defaults.getDefaultProperties() : null;
CreateFromTemplateDialog dialog = new CreateFromTemplateDialog(project, dir, selectedTemplate, defaults, properties);
PsiElement createdElement = dialog.create();
if (createdElement != null) {
elementCreated(dialog, createdElement);
view.selectElement(createdElement);
if (selectedTemplate.isLiveTemplateEnabled() && createdElement instanceof PsiFile) {
Map<String, String> defaultValues = getLiveTemplateDefaults(dataContext, ((PsiFile) createdElement));
startLiveTemplate((PsiFile) createdElement, notNull(defaultValues, Collections.emptyMap()));
}
}
}
}
}
use of com.intellij.ide.IdeView in project intellij-community by JetBrains.
the class TextEditorPsiDataProvider method getData.
@Override
@Nullable
public Object getData(@NotNull final String dataId, @NotNull final Editor e, @NotNull final Caret caret) {
if (e.isDisposed() || !(e instanceof EditorEx)) {
return null;
}
VirtualFile file = ((EditorEx) e).getVirtualFile();
if (file == null || !file.isValid())
return null;
Project project = e.getProject();
if (dataId.equals(injectedId(EDITOR.getName()))) {
if (project == null || PsiDocumentManager.getInstance(project).isUncommited(e.getDocument())) {
return e;
} else {
return InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(e, caret, getPsiFile(e, file));
}
}
if (HOST_EDITOR.is(dataId)) {
return e instanceof EditorWindow ? ((EditorWindow) e).getDelegate() : e;
}
if (CARET.is(dataId)) {
return caret;
}
if (dataId.equals(injectedId(CARET.getName()))) {
Editor editor = (Editor) getData(injectedId(EDITOR.getName()), e, caret);
assert editor != null;
return getInjectedCaret(editor, caret);
}
if (dataId.equals(injectedId(PSI_ELEMENT.getName()))) {
Editor editor = (Editor) getData(injectedId(EDITOR.getName()), e, caret);
assert editor != null;
Caret injectedCaret = getInjectedCaret(editor, caret);
return getPsiElementIn(editor, injectedCaret, file);
}
if (PSI_ELEMENT.is(dataId)) {
return getPsiElementIn(e, caret, file);
}
if (dataId.equals(injectedId(LANGUAGE.getName()))) {
PsiFile psiFile = (PsiFile) getData(injectedId(PSI_FILE.getName()), e, caret);
Editor editor = (Editor) getData(injectedId(EDITOR.getName()), e, caret);
if (psiFile == null || editor == null)
return null;
Caret injectedCaret = getInjectedCaret(editor, caret);
return getLanguageAtCurrentPositionInEditor(injectedCaret, psiFile);
}
if (LANGUAGE.is(dataId)) {
final PsiFile psiFile = getPsiFile(e, file);
if (psiFile == null)
return null;
return getLanguageAtCurrentPositionInEditor(caret, psiFile);
}
if (dataId.equals(injectedId(VIRTUAL_FILE.getName()))) {
PsiFile psiFile = (PsiFile) getData(injectedId(PSI_FILE.getName()), e, caret);
if (psiFile == null)
return null;
return psiFile.getVirtualFile();
}
if (dataId.equals(injectedId(PSI_FILE.getName()))) {
Editor editor = (Editor) getData(injectedId(EDITOR.getName()), e, caret);
if (editor == null) {
return null;
}
if (project == null) {
return null;
}
return PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
}
if (PSI_FILE.is(dataId)) {
return getPsiFile(e, file);
}
if (IDE_VIEW.is(dataId)) {
final PsiFile psiFile = project == null ? null : PsiManager.getInstance(project).findFile(file);
final PsiDirectory psiDirectory = psiFile != null ? psiFile.getParent() : null;
if (psiDirectory != null && (psiDirectory.isPhysical() || ApplicationManager.getApplication().isUnitTestMode())) {
return new IdeView() {
@Override
public void selectElement(final PsiElement element) {
Editor editor = EditorHelper.openInEditor(element);
if (editor != null) {
ToolWindowManager.getInstance(element.getProject()).activateEditorComponent();
}
}
@NotNull
@Override
public PsiDirectory[] getDirectories() {
return new PsiDirectory[] { psiDirectory };
}
@Override
public PsiDirectory getOrChooseDirectory() {
return psiDirectory;
}
};
}
}
if (CONTEXT_LANGUAGES.is(dataId)) {
return computeLanguages(e, caret);
}
return null;
}
use of com.intellij.ide.IdeView in project intellij-community by JetBrains.
the class GenerateVisitorByHierarchyAction method actionPerformed.
public void actionPerformed(AnActionEvent e) {
final Ref<String> visitorNameRef = Ref.create("MyVisitor");
final Ref<PsiClass> parentClassRef = Ref.create(null);
final Project project = e.getData(CommonDataKeys.PROJECT);
assert project != null;
final PsiNameHelper helper = PsiNameHelper.getInstance(project);
final PackageChooserDialog dialog = new PackageChooserDialog("Choose Target Package and Hierarchy Root Class", project) {
@Override
protected ValidationInfo doValidate() {
PsiDocumentManager.getInstance(project).commitAllDocuments();
if (!helper.isQualifiedName(visitorNameRef.get())) {
return new ValidationInfo("Visitor class name is not valid");
} else if (parentClassRef.isNull()) {
return new ValidationInfo("Hierarchy root class should be specified");
} else if (parentClassRef.get().isAnnotationType() || parentClassRef.get().isEnum()) {
return new ValidationInfo("Hierarchy root class should be an interface or a class");
}
return super.doValidate();
}
protected JComponent createCenterPanel() {
final JPanel panel = new JPanel(new BorderLayout());
panel.add(super.createCenterPanel(), BorderLayout.CENTER);
panel.add(createNamePanel(), BorderLayout.NORTH);
panel.add(createBaseClassPanel(), BorderLayout.SOUTH);
return panel;
}
private JComponent createNamePanel() {
LabeledComponent<JTextField> labeledComponent = new LabeledComponent<>();
labeledComponent.setText("Visitor class");
final JTextField nameField = new JTextField(visitorNameRef.get());
labeledComponent.setComponent(nameField);
nameField.getDocument().addDocumentListener(new DocumentAdapter() {
protected void textChanged(final DocumentEvent e) {
visitorNameRef.set(nameField.getText());
}
});
return labeledComponent;
}
private JComponent createBaseClassPanel() {
LabeledComponent<EditorTextField> labeledComponent = new LabeledComponent<>();
labeledComponent.setText("Hierarchy root class");
final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(project);
final PsiTypeCodeFragment codeFragment = factory.createTypeCodeFragment("", null, true, JavaCodeFragmentFactory.ALLOW_VOID);
final Document document = PsiDocumentManager.getInstance(project).getDocument(codeFragment);
final EditorTextField editorTextField = new EditorTextField(document, project, StdFileTypes.JAVA);
labeledComponent.setComponent(editorTextField);
editorTextField.addDocumentListener(new com.intellij.openapi.editor.event.DocumentAdapter() {
public void documentChanged(final com.intellij.openapi.editor.event.DocumentEvent e) {
parentClassRef.set(null);
try {
final PsiType psiType = codeFragment.getType();
final PsiClass psiClass = psiType instanceof PsiClassType ? ((PsiClassType) psiType).resolve() : null;
parentClassRef.set(psiClass);
} catch (PsiTypeCodeFragment.IncorrectTypeException e1) {
// ok
}
}
});
return labeledComponent;
}
};
final PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(e.getDataContext());
if (element instanceof PsiPackage) {
dialog.selectPackage(((PsiPackage) element).getQualifiedName());
} else if (element instanceof PsiDirectory) {
final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage((PsiDirectory) element);
if (aPackage != null) {
dialog.selectPackage(aPackage.getQualifiedName());
}
}
dialog.show();
if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE || dialog.getSelectedPackage() == null || dialog.getSelectedPackage().getQualifiedName().length() == 0 || parentClassRef.isNull()) {
return;
}
final String visitorQName = generateEverything(dialog.getSelectedPackage(), parentClassRef.get(), visitorNameRef.get());
final IdeView ideView = LangDataKeys.IDE_VIEW.getData(e.getDataContext());
final PsiClass visitorClass = JavaPsiFacade.getInstance(project).findClass(visitorQName, GlobalSearchScope.projectScope(project));
if (ideView != null && visitorClass != null) {
ideView.selectElement(visitorClass);
}
}
use of com.intellij.ide.IdeView in project intellij-community by JetBrains.
the class GeneratePluginClassAction method update.
public void update(final AnActionEvent e) {
super.update(e);
final Presentation presentation = e.getPresentation();
if (presentation.isEnabled()) {
final Project project = e.getProject();
final Module module = e.getData(LangDataKeys.MODULE);
if (project != null && module != null && PsiUtil.isPluginModule(module)) {
final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
if (view != null) {
// from com.intellij.ide.actions.CreateClassAction.update()
ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
PsiDirectory[] dirs = view.getDirectories();
for (PsiDirectory dir : dirs) {
if (projectFileIndex.isUnderSourceRootOfType(dir.getVirtualFile(), JavaModuleSourceRootTypes.SOURCES) && JavaDirectoryService.getInstance().getPackage(dir) != null) {
return;
}
}
}
}
presentation.setEnabledAndVisible(false);
}
}
use of com.intellij.ide.IdeView in project android by JetBrains.
the class CreateClassAction method isAvailable.
private boolean isAvailable(@NotNull DataContext dataContext) {
Project project = CommonDataKeys.PROJECT.getData(dataContext);
IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
if (project == null || view == null || view.getDirectories().length == 0) {
return false;
}
ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
for (PsiDirectory dir : view.getDirectories()) {
if (projectFileIndex.isUnderSourceRootOfType(dir.getVirtualFile(), JavaModuleSourceRootTypes.SOURCES) && checkPackageExists(dir)) {
return true;
}
}
return false;
}
Aggregations