Java Code Examples for com.intellij.ide.IdeView#selectElement()

The following examples show how to use com.intellij.ide.IdeView#selectElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NewBlazePackageAction.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
protected void actionPerformedInBlazeProject(Project project, AnActionEvent event) {
  IdeView view = event.getData(LangDataKeys.IDE_VIEW);
  if (view == null) {
    return;
  }
  PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
  if (directory == null) {
    return;
  }
  CreateDirectoryOrPackageHandler validator =
      new CreateDirectoryOrPackageHandler(project, directory, false, ".") {
        @Override
        protected void createDirectories(String subDirName) {
          super.createDirectories(subDirName);
          PsiFileSystemItem element = getCreatedElement();
          if (element instanceof PsiDirectory) {
            createBuildFile(project, (PsiDirectory) element);
          }
        }
      };
  Messages.showInputDialog(
      project,
      "Enter new package name:",
      String.format("New %s Package", Blaze.buildSystemName(project)),
      Messages.getQuestionIcon(),
      "",
      validator);
  PsiDirectory newDir = (PsiDirectory) validator.getCreatedElement();
  if (newDir != null) {
    PsiFile buildFile = findBuildFile(project, newDir);
    if (buildFile != null) {
      view.selectElement(buildFile);
      OpenFileAction.openFile(buildFile.getViewProvider().getVirtualFile(), project);
    }
  }
}
 
Example 2
Source File: CreateFromTemplateActionBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
public final void actionPerformed(@Nonnull AnActionEvent e) {
  DataContext dataContext = e.getDataContext();
  IdeView view = dataContext.getData(LangDataKeys.IDE_VIEW);
  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);
      Map<String, Object> properties = defaults != null ? defaults.getDefaultProperties() : null;
      CreateFromTemplateDialog dialog = new CreateFromTemplateDialog(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()));
        }
      }
    }
  }
}
 
Example 3
Source File: BxNewSectionAction.java    From bxfs with MIT License 4 votes vote down vote up
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
	IdeView view
		= event.getData(LangDataKeys.IDE_VIEW);

	if (view != null) {
		Project project
			= event.getProject();

		if (project != null) {
			PsiDirectory directory
				= DirectoryChooserUtil.getOrChooseDirectory(view);

			if (directory != null) {
				CreateDirectoryOrPackageHandler validator
					= new CreateDirectoryOrPackageHandler(project, directory, true, "\\/");

				// Сообщение не нужно, так как заголовок окна и так близко. Они резонируют.
				Messages.showInputDialog(project, null, "Битрикс: Создание Нового Раздела", BitrixFramework.bxIcon, "", validator);

				PsiElement result = validator.getCreatedElement();
				if (result instanceof PsiDirectory) {
					PsiDirectory createdDir = (PsiDirectory) result;

					FileTemplateManager templateManager
						= FileTemplateManager.getInstance(project);

					FileTemplate cfgTemplate = templateManager.findInternalTemplate("Битрикс - Раздел (настройки)");
					FileTemplate idxTemplate = templateManager.findInternalTemplate("Битрикс - Раздел (титульная)");

					Properties properties
						= FileTemplateManager.getInstance(project).getDefaultProperties();

					try {
						PsiElement cfgFile = FileTemplateUtil.createFromTemplate(cfgTemplate, ".section", properties, createdDir);
						PsiElement idxFile = FileTemplateUtil.createFromTemplate(idxTemplate, "index", properties, createdDir);

						view.selectElement(idxFile);
					}
					catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
 
Example 4
Source File: ServiceActionUtil.java    From idea-php-shopware-plugin with MIT License 4 votes vote down vote up
public static void buildFile(AnActionEvent event, final Project project, String templatePath, String fileName) {
    DataContext dataContext = event.getDataContext();
    IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (view == null) {
        return;
    }

    PsiDirectory[] directories = view.getDirectories();
    if(directories.length == 0) {
        return;
    }

    final PsiDirectory initialBaseDir = directories[0];
    if (initialBaseDir == null) {
        return;
    }

    if(initialBaseDir.findFile(fileName) != null) {
        Messages.showInfoMessage("File exists", "Error");
        return;
    }

    String content;
    try {
        content = StreamUtil.readText(ServiceActionUtil.class.getResourceAsStream(templatePath), "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    final PsiFileFactory factory = PsiFileFactory.getInstance(project);

    final PsiFile file = factory.createFileFromText(fileName, XmlFileType.INSTANCE, content);

    ApplicationManager.getApplication().runWriteAction(() -> {
        CodeStyleManager.getInstance(project).reformat(file);
        initialBaseDir.add(file);
    });

    PsiFile psiFile = initialBaseDir.findFile(fileName);
    if(psiFile != null) {
        view.selectElement(psiFile);
    }

}
 
Example 5
Source File: ServiceActionUtil.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public static void buildFile(AnActionEvent event, final Project project, String templatePath) {
    String extension = (templatePath.endsWith(".yml") || templatePath.endsWith(".yaml")) ? "yml" : "xml" ;

    String fileName = Messages.showInputDialog(project, "File name (without extension)", String.format("Create %s Service", extension), Symfony2Icons.SYMFONY);
    if(fileName == null || StringUtils.isBlank(fileName)) {
        return;
    }

    FileType fileType = (templatePath.endsWith(".yml") || templatePath.endsWith(".yaml")) ? YAMLFileType.YML : XmlFileType.INSTANCE ;

    if(!fileName.endsWith("." + extension)) {
        fileName = fileName.concat("." + extension);
    }

    DataContext dataContext = event.getDataContext();
    IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (view == null) {
        return;
    }

    PsiDirectory[] directories = view.getDirectories();
    if(directories.length == 0) {
        return;
    }

    final PsiDirectory initialBaseDir = directories[0];
    if (initialBaseDir == null) {
        return;
    }

    if(initialBaseDir.findFile(fileName) != null) {
        Messages.showInfoMessage("File exists", "Error");
        return;
    }

    String content;
    try {
        content = StreamUtil.readText(ServiceActionUtil.class.getResourceAsStream(templatePath), "UTF-8").replace("\r\n", "\n");
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    final PsiFileFactory factory = PsiFileFactory.getInstance(project);

    String bundleName = "Acme\\DemoBundle";

    SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(project);
    SymfonyBundle symfonyBundle = symfonyBundleUtil.getContainingBundle(initialBaseDir);

    if(symfonyBundle != null) {
        bundleName = StringUtils.strip(symfonyBundle.getNamespaceName(), "\\");
    }

    String underscoreBundle = bundleName.replace("\\", ".").toLowerCase();
    if(underscoreBundle.endsWith("bundle")) {
        underscoreBundle = underscoreBundle.substring(0, underscoreBundle.length() - 6);
    }

    content = content.replace("{{ BundleName }}", bundleName).replace("{{ BundleNameUnderscore }}", underscoreBundle);

    final PsiFile file = factory.createFileFromText(fileName, fileType, content);

    ApplicationManager.getApplication().runWriteAction(() -> {
        CodeStyleManager.getInstance(project).reformat(file);
        initialBaseDir.add(file);
    });

    PsiFile psiFile = initialBaseDir.findFile(fileName);
    if(psiFile != null) {
        view.selectElement(psiFile);
    }

}