Java Code Examples for com.intellij.openapi.actionSystem.AnActionEvent#getProject()

The following examples show how to use com.intellij.openapi.actionSystem.AnActionEvent#getProject() . 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: NewClassAction.java    From json2java4idea with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(@Nonnull AnActionEvent event) {
    if (!isAvailable(event)) {
        return;
    }

    project = event.getProject();
    ideView = event.getData(DataKeys.IDE_VIEW);

    final Injector injector = GuiceManager.getInstance(project).getInjector();
    injector.injectMembers(this);

    // 'selected' is null when directory selection is canceled although multiple directories are chosen.
    final PsiDirectory selected = ideView.getOrChooseDirectory();
    if (selected == null) {
        return;
    }

    final NewClassDialog dialog = NewClassDialog.builder(project, bundle)
            .nameValidator(nameValidatorProvider.get())
            .jsonValidator(jsonValidatorProvider.get())
            .actionListener(this)
            .build();
    dialog.show();
}
 
Example 2
Source File: SimpleDiffChange.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private GutterIconRenderer createIconRenderer(@Nonnull final Side sourceSide,
                                              @Nonnull final String tooltipText,
                                              @Nonnull final Image icon,
                                              @Nonnull final Runnable perform) {
  if (!DiffUtil.isEditable(myViewer.getEditor(sourceSide.other()))) return null;
  return new DiffGutterRenderer(icon, tooltipText) {
    @Override
    protected void performAction(AnActionEvent e) {
      if (!myIsValid) return;
      final Project project = e.getProject();
      final Document document = myViewer.getEditor(sourceSide.other()).getDocument();
      DiffUtil.executeWriteCommand(document, project, "Replace change", perform);
    }
  };
}
 
Example 3
Source File: ProjectFileIndexSampleAction.java    From intellij-sdk-docs with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(@NotNull final AnActionEvent event) {
  Project project = event.getProject();
  final Editor editor = event.getData(CommonDataKeys.EDITOR);
  if (project == null || editor == null) return;
  Document document = editor.getDocument();
  FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
  VirtualFile virtualFile = fileDocumentManager.getFile(document);
  ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
  if (virtualFile != null) {
    Module module = projectFileIndex.getModuleForFile(virtualFile);
    String moduleName;
    moduleName = module != null ? module.getName() : "No module defined for file";

    VirtualFile moduleContentRoot = projectFileIndex.getContentRootForFile(virtualFile);
    boolean isLibraryFile = projectFileIndex.isLibraryClassFile(virtualFile);
    boolean isInLibraryClasses = projectFileIndex.isInLibraryClasses(virtualFile);
    boolean isInLibrarySource = projectFileIndex.isInLibrarySource(virtualFile);
    Messages.showInfoMessage("Module: " + moduleName + "\n" +
                             "Module content root: " + moduleContentRoot + "\n" +
                             "Is library file: " + isLibraryFile + "\n" +
                             "Is in library classes: " + isInLibraryClasses +
                             ", Is in library source: " + isInLibrarySource,
                             "Main File Info for" + virtualFile.getName());
  }
}
 
Example 4
Source File: GotoTestDataAction.java    From reasonml-idea-plugin with MIT License 6 votes vote down vote up
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    Project project = e.getProject();
    if (project != null) {
        DataContext dataContext = e.getDataContext();
        List<String> fileNames = findRelatedFiles(project, dataContext);
        if (fileNames.isEmpty()) {
            Notifications.Bus.notify(new ORNotification("testdata", "Found no testdata files", "Cannot find related files", INFORMATION, null), project);
            return;
        }

        Editor editor = e.getData(CommonDataKeys.EDITOR);
        JBPopupFactory popupFactory = JBPopupFactory.getInstance();
        RelativePoint point = editor == null ? popupFactory.guessBestPopupLocation(dataContext) : popupFactory.guessBestPopupLocation(editor);

        TestDataNavigationHandler.navigate(point, fileNames, project);
    }
}
 
Example 5
Source File: VcsLogAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void update(@Nonnull AnActionEvent e) {
  Project project = e.getProject();
  VcsLog log = e.getData(VcsLogDataKeys.VCS_LOG);
  if (project == null || log == null) {
    e.getPresentation().setEnabledAndVisible(false);
    return;
  }

  List<VcsFullCommitDetails> details = log.getSelectedDetails();
  MultiMap<Repo, VcsFullCommitDetails> grouped = groupByRootWithCheck(project, details);
  if (grouped == null) {
    e.getPresentation().setEnabledAndVisible(false);
  }
  else {
    e.getPresentation().setVisible(isVisible(project, grouped));
    e.getPresentation().setEnabled(!grouped.isEmpty() && isEnabled(grouped));
  }
}
 
Example 6
Source File: BuckBuildAction.java    From buck with Apache License 2.0 6 votes vote down vote up
@Override
public void executeOnPooledThread(final AnActionEvent e) {
  Project project = e.getProject();
  if (project == null) {
    return;
  }
  BuckBuildManager buildManager = BuckBuildManager.getInstance(project);

  String target = buildManager.getCurrentSavedTarget(project);
  // Initiate a buck build
  BuckModule buckModule = project.getComponent(BuckModule.class);
  buckModule.attach(target);

  if (target == null) {
    buildManager.showNoTargetMessage(project);
    return;
  }

  BuckBuildCommandHandler handler = new BuckBuildCommandHandler(project, BuckCommand.BUILD);
  handler.command().addParameter(target);
  buildManager.runBuckCommandWhileConnectedToBuck(handler, ACTION_TITLE, buckModule);
}
 
Example 7
Source File: BuckProjectAction.java    From Buck-IntelliJ-Plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void update(AnActionEvent e) {
    Project project = e.getProject();
    if (project != null) {
        e.getPresentation().setEnabled(!BuckBuildManager.getInstance(project).isBuilding());
    }
}
 
Example 8
Source File: LibraryActionHelper.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
static Library findLibraryForAction(AnActionEvent e) {
  Project project = e.getProject();
  if (project != null) {
    NamedLibraryElementNode node = findLibraryNode(e.getDataContext());
    if (node != null) {
      String libraryName = node.getName();
      if (StringUtil.isNotEmpty(libraryName)) {
        LibraryTable libraryTable = ProjectLibraryTable.getInstance(project);
        return libraryTable.getLibraryByName(libraryName);
      }
    }
  }
  return null;
}
 
Example 9
Source File: BuckKillAction.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public void update(AnActionEvent e) {
  Project project = e.getProject();
  if (project != null) {
    BuckBuildManager buildManager = BuckBuildManager.getInstance(project);
    e.getPresentation()
        .setEnabled(
            !buildManager.isKilling() && project.getComponent(BuckModule.class).isConnected());
  }
}
 
Example 10
Source File: TransformAction.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    PsiFile file = e.getData(PSI_FILE);
    Project project = e.getProject();

    if (project != null && file != null) {
        apply(project, file, true);
    }
}
 
Example 11
Source File: PopupDialogAction.java    From intellij-sdk-docs with Apache License 2.0 5 votes vote down vote up
/**
 * Determines whether this menu item is available for the current context.
 * Requires a project to be open.
 * @param e Event received when the associated group-id menu is chosen.
 */
@Override
public void update(AnActionEvent e) {
  // Set the availability based on whether a project is open
  Project project = e.getProject();
  e.getPresentation().setEnabledAndVisible(project != null);
}
 
Example 12
Source File: FlutterBuildActionGroup.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Module findFlutterModule(@NotNull AnActionEvent event) {
  Project project = event.getProject();
  if (project == null) {
    return null;
  }
  VirtualFile file = event.getData(CommonDataKeys.VIRTUAL_FILE);
  if (file == null) {
    return null;
  }
  return findFlutterModule(project, file);
}
 
Example 13
Source File: BsMakeAction.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    Project project = e.getProject();
    if (project != null) {
        doAction(project, CliType.Bs.MAKE);
    }
}
 
Example 14
Source File: ImportTestsGroup.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public AnAction[] getChildren(@Nullable AnActionEvent e) {
  if (e == null) return EMPTY_ARRAY;
  final Project project = e.getProject();
  if (project == null) return EMPTY_ARRAY;
  final Collection<String> filePaths = TestHistoryConfiguration.getInstance(project).getFiles();
  final File testHistoryRoot = TestStateStorage.getTestHistoryRoot(project);
  final List<File> fileNames = ContainerUtil.map(filePaths, new Function<String, File>() {
    @Override
    public File fun(String fileName) {
      return new File(testHistoryRoot, fileName);
    }
  });
  Collections.sort(fileNames, new Comparator<File>() {
    @Override
    public int compare(File f1, File f2) {
      return f1.lastModified() > f2.lastModified() ? -1 : 1;
    }
  });
  final int historySize = fileNames.size();
  final AnAction[] actions = new AnAction[historySize + 2];
  for (int i = 0; i < historySize; i++) {
    actions[i] = new ImportTestsFromHistoryAction(myProperties, project, fileNames.get(i).getName());
  }
  actions[historySize] = AnSeparator.getInstance();
  actions[historySize + 1] = new ImportTestsFromFileAction(myProperties);
  return actions;
}
 
Example 15
Source File: MoveToOtherStorageAction.java    From markdown-image-kit with MIT License 5 votes vote down vote up
@Override
public void actionPerformed(@NotNull AnActionEvent event) {

    final Project project = event.getProject();
    if (project != null) {
        MoveToOtherOssSettingsDialog dialog = showDialog();
        if (dialog == null) {
            return;
        }
        String domain = dialog.getDomain().getText().trim();
        if (StringUtils.isBlank(domain)) {
            return;
        }
        if (!OssState.getStatus(dialog.getCloudComboBox().getSelectedIndex()- 1)) {
            return;
        }

        int cloudIndex = dialog.getCloudComboBox().getSelectedIndex() - 1;
        CloudEnum cloudEnum = OssState.getCloudType(cloudIndex);

        EventData data = new EventData()
            .setActionEvent(event)
            .setProject(project)
            .setClient(ClientUtils.getClient(cloudEnum))
            // client 有可能为 null, 使用 cloudEnum 安全点
            .setClientName(cloudEnum.title);

        // http://www.jetbrains.org/intellij/sdk/docs/basics/persisting_state_of_components.html
        PropertiesComponent propComp = PropertiesComponent.getInstance();
        // 过滤掉配置用户输入后的其他标签
        propComp.setValue(MarkdownFileFilter.FILTER_KEY, domain.equals(MOVE_ALL) ? "" : domain);

        new ActionTask(project,
                       MikBundle.message("mik.action.move.process", cloudEnum.title),
                       ActionManager.buildMoveImageChain(data)).queue();
    }
}
 
Example 16
Source File: ParseContestAction.java    From JHelper with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void performAction(AnActionEvent e) {
	Project project = e.getProject();
	ParseDialog dialog = new ParseDialog(project);
	dialog.show();
	if (!dialog.isOK()) {
		return;
	}
	for (TaskData taskData : dialog.getResult()) {
		PsiElement generatedFile = TaskUtils.saveNewTask(taskData, project);
		UIUtils.openMethodInEditor(project, (OCFile) generatedFile, "solve");
	}

	IDEUtils.reloadProject(project);
}
 
Example 17
Source File: OpenContentAction.java    From leetcode-editor with Apache License 2.0 4 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent anActionEvent, Config config, JTree tree, Question question) {
    Project project = anActionEvent.getProject();
    CodeManager.openContent(question, project);
}
 
Example 18
Source File: AnnotateRevisionActionBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
public boolean isEnabled(@Nonnull AnActionEvent e) {
  if (e.getProject() == null) return false;

  VcsFileRevision fileRevision = getFileRevision(e);
  if (fileRevision == null) return false;

  VirtualFile file = getFile(e);
  if (file == null) return false;

  AbstractVcs vcs = getVcs(e);
  if (vcs == null) return false;

  AnnotationProvider provider = vcs.getAnnotationProvider();
  if (provider == null || !provider.isAnnotationValid(fileRevision)) return false;

  if (VcsAnnotateUtil.getBackgroundableLock(vcs.getProject(), file).isLocked()) return false;

  return true;
}
 
Example 19
Source File: SymbolSearchEverywhereContributor.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public SearchEverywhereContributor<Object> createContributor(@Nonnull AnActionEvent initEvent) {
  return new SymbolSearchEverywhereContributor(initEvent.getProject(), GotoActionBase.getPsiContext(initEvent));
}
 
Example 20
Source File: VcsPushAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void update(@Nonnull AnActionEvent e) {
  super.update(e);
  Project project = e.getProject();
  e.getPresentation().setEnabledAndVisible(project != null && !ServiceManager.getService(project, VcsRepositoryManager.class).getRepositories().isEmpty());
}