Example usage for com.intellij.openapi.actionSystem PlatformDataKeys CONTEXT_COMPONENT

List of usage examples for com.intellij.openapi.actionSystem PlatformDataKeys CONTEXT_COMPONENT

Introduction

In this page you can find the example usage for com.intellij.openapi.actionSystem PlatformDataKeys CONTEXT_COMPONENT.

Prototype

DataKey CONTEXT_COMPONENT

To view the source code for com.intellij.openapi.actionSystem PlatformDataKeys CONTEXT_COMPONENT.

Click Source Link

Document

Returns Component currently in focus, DataContext should be retrieved for.

Usage

From source file:com.codeflections.typengo.TypeAndGoInvokeAction.java

License:Open Source License

@Override
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
    if (!CommandInputForm.isShown()) {
        Component component = anActionEvent.getData(PlatformDataKeys.CONTEXT_COMPONENT);
        if (component == null) {
            component = anActionEvent.getInputEvent().getComponent();
        }// www  .  j  a  v a2s .com
        CommandInputForm.show(component, anActionEvent);
    }
}

From source file:com.gogh.plugin.action.ExternalTranslationAction.java

License:Apache License

public static void showExternalTranslation(String query, DataContext dataContext) {
    final Component contextComponent = PlatformDataKeys.CONTEXT_COMPONENT.getData(dataContext);

    ApplicationManager.getApplication().executeOnPooledThread(() -> {
        final List<String> menuItems = new ArrayList<>();
        final List<String> urls = new ArrayList<>();
        final List<Icon> icons = new ArrayList<Icon>();

        for (Translator translator : Translators.getTranslator()) {
            String url = translator.getExternalUrl(query);
            if (url != null) {
                menuItems.add(translator.getMenuItem());
                urls.add(url);//  w w  w .j a  v a 2  s.  c o  m
                icons.add(translator.getIcon());
            }
        }

        ApplicationManager.getApplication()
                .invokeLater(() -> JBPopupFactory.getInstance()
                        .createListPopup(new BaseListPopupStep<String>("Choose web platform",
                                ArrayUtil.toStringArray(menuItems),
                                ArrayUtil.toObjectArray(icons, Icon.class)) {
                            @Override
                            public PopupStep onChosen(final String selectedValue, final boolean finalChoice) {
                                if (selectedValue.equals(menuItems.get(0))) {
                                    BrowserUtil.browse(urls.get(0));
                                } else if (selectedValue.equals(menuItems.get(1))) {
                                    BrowserUtil.browse(urls.get(1));
                                }
                                return FINAL_CHOICE;
                            }
                        }).showInBestPositionFor(DataManager.getInstance().getDataContext(contextComponent)),
                        ModalityState.NON_MODAL);
    });
}

From source file:com.gogh.plugin.action.ExternalTranslationAction.java

License:Apache License

public static void showExternalTranslation(String query, String externalUrl, DataContext dataContext) {
    final Component contextComponent = PlatformDataKeys.CONTEXT_COMPONENT.getData(dataContext);

    ApplicationManager.getApplication().executeOnPooledThread(() -> {
        final List<String> urls = new ArrayList<String>();
        final List<Icon> icons = new ArrayList<Icon>();

        if (StringUtil.isEmptyOrSpaces(externalUrl)) {
            for (Translator translator : Translators.getTranslator()) {
                String url = translator.getExternalUrl(query);
                if (url != null) {
                    urls.add(url);/*from   w w  w . j a v  a2  s .  c o m*/
                    icons.add(translator.getIcon());
                }
            }
        } else {
            urls.add(externalUrl);
        }

        ApplicationManager.getApplication().invokeLater(() -> {
            if (ContainerUtil.isEmpty(urls)) {
                // do nothing
            } else if (urls.size() == 1) {
                BrowserUtil.browse(urls.get(0));
            } else {
                JBPopupFactory.getInstance()
                        .createListPopup(new BaseListPopupStep<String>("Choose web platform",
                                ArrayUtil.toStringArray(urls), ArrayUtil.toObjectArray(icons, Icon.class)) {
                            @Override
                            public PopupStep onChosen(final String selectedValue, final boolean finalChoice) {
                                BrowserUtil.browse(selectedValue);
                                return FINAL_CHOICE;
                            }
                        }).showInBestPositionFor(DataManager.getInstance().getDataContext(contextComponent));
            }

        }, ModalityState.NON_MODAL);
    });
}

From source file:com.intellij.execution.actions.ConfigurationContext.java

License:Apache License

private ConfigurationContext(final DataContext dataContext) {
    myRuntimeConfiguration = RunConfiguration.DATA_KEY.getData(dataContext);
    myContextComponent = PlatformDataKeys.CONTEXT_COMPONENT.getData(dataContext);
    myModule = LangDataKeys.MODULE.getData(dataContext);
    @SuppressWarnings({ "unchecked" })
    final Location<PsiElement> location = (Location<PsiElement>) Location.DATA_KEY.getData(dataContext);
    if (location != null) {
        myLocation = location;/*  w w  w  .j  a v a 2s  . co m*/
        return;
    }
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null) {
        myLocation = null;
        return;
    }
    final PsiElement element = getSelectedPsiElement(dataContext, project);
    if (element == null) {
        myLocation = null;
        return;
    }
    myLocation = new PsiLocation<PsiElement>(project, myModule, element);
}

From source file:com.intellij.ide.actions.DeleteAction.java

License:Apache License

public void update(AnActionEvent event) {
    String place = event.getPlace();
    Presentation presentation = event.getPresentation();
    if (ActionPlaces.PROJECT_VIEW_POPUP.equals(place) || ActionPlaces.COMMANDER_POPUP.equals(place))
        presentation.setText(IdeBundle.message("action.delete.ellipsis"));
    else/*w w w. j ava  2s. c  o m*/
        presentation.setText(IdeBundle.message("action.delete"));
    DataContext dataContext = event.getDataContext();
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null) {
        presentation.setEnabled(false);
        return;
    }
    DeleteProvider provider = getDeleteProvider(dataContext);
    if (event.getInputEvent() instanceof KeyEvent) {
        KeyEvent keyEvent = (KeyEvent) event.getInputEvent();
        Object component = PlatformDataKeys.CONTEXT_COMPONENT.getData(dataContext);
        if (component instanceof JTextComponent)
            provider = null; // Do not override text deletion
        if (keyEvent.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
            // Do not override text deletion in speed search
            if (component instanceof JComponent) {
                SpeedSearchSupply searchSupply = SpeedSearchSupply.getSupply((JComponent) component);
                if (searchSupply != null)
                    provider = null;
            }

            String activeSpeedSearchFilter = SpeedSearchSupply.SPEED_SEARCH_CURRENT_QUERY.getData(dataContext);
            if (!StringUtil.isEmpty(activeSpeedSearchFilter)) {
                provider = null;
            }
        }
    }
    if (provider instanceof TitledHandler) {
        presentation.setText(((TitledHandler) provider).getActionTitle());
    }
    final boolean canDelete = provider != null && provider.canDeleteElement(dataContext);
    if (ActionPlaces.isPopupPlace(event.getPlace())) {
        presentation.setVisible(canDelete);
    } else {
        presentation.setEnabled(canDelete);
    }
}

From source file:com.intellij.ide.actions.ExternalJavaDocAction.java

License:Apache License

@Override
public void actionPerformed(AnActionEvent e) {
    DataContext dataContext = e.getDataContext();
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null) {
        return;//w  w w .j av  a  2 s.c o  m
    }

    Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    PsiElement element = getElement(dataContext, editor);
    if (element == null) {
        Messages.showMessageDialog(project, IdeBundle.message("message.please.select.element.for.javadoc"),
                IdeBundle.message("title.no.element.selected"), Messages.getErrorIcon());
        return;
    }

    PsiFile context = CommonDataKeys.PSI_FILE.getData(dataContext);

    PsiElement originalElement = getOriginalElement(context, editor);
    DocumentationManager.storeOriginalElement(project, originalElement, element);
    final DocumentationProvider provider = DocumentationManager.getProviderFromElement(element);

    if (provider instanceof ExternalDocumentationHandler
            && ((ExternalDocumentationHandler) provider).handleExternal(element, originalElement)) {
        return;
    }

    final List<String> urls = provider.getUrlFor(element, originalElement);
    if (urls != null && !urls.isEmpty()) {
        showExternalJavadoc(urls, PlatformDataKeys.CONTEXT_COMPONENT.getData(dataContext));
    } else if (provider instanceof ExternalDocumentationProvider) {
        final ExternalDocumentationProvider externalDocumentationProvider = (ExternalDocumentationProvider) provider;
        if (externalDocumentationProvider.canPromptToConfigureDocumentation(element)) {
            externalDocumentationProvider.promptToConfigureDocumentation(element);
        }
    }
}

From source file:com.intellij.ide.actions.GotoActionAction.java

License:Apache License

@Override
public void gotoActionPerformed(@NotNull final AnActionEvent e) {
    final Project project = e.getData(CommonDataKeys.PROJECT);
    final Component component = e.getData(PlatformDataKeys.CONTEXT_COMPONENT);
    Editor editor = e.getData(CommonDataKeys.EDITOR);
    PsiFile file = e.getData(CommonDataKeys.PSI_FILE);

    FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.popup.action");
    GotoActionModel model = new GotoActionModel(project, component, editor, file);
    GotoActionCallback<Object> callback = new GotoActionCallback<Object>() {
        @Override/*from   w  ww  .j  a v a 2s  .co m*/
        public void elementChosen(@NotNull ChooseByNamePopup popup, @NotNull Object element) {
            String enteredText = popup.getEnteredText();
            openOptionOrPerformAction(((GotoActionModel.MatchedValue) element).value, enteredText, project,
                    component, e);
        }
    };

    Pair<String, Integer> start = getInitialText(false, e);
    showNavigationPopup(callback, null, createPopup(project, model, start.first, start.second, component, e),
            false);
}

From source file:com.intellij.ide.actions.GotoClassAction.java

License:Apache License

@Override
public void actionPerformed(@NotNull final AnActionEvent e) {
    final Project project = e.getData(CommonDataKeys.PROJECT);
    assert project != null;
    if (!DumbService.getInstance(project).isDumb()) {
        super.actionPerformed(e);
    } else {//from ww w.j  av a2s. co  m
        DumbService.getInstance(project).showDumbModeNotification(
                "Goto Class action is not available until indices are built, using Goto File instead");
        ActionManager.getInstance().tryToExecute(ActionManager.getInstance().getAction(GotoFileAction.ID),
                ActionCommand.getInputEvent(GotoFileAction.ID), e.getData(PlatformDataKeys.CONTEXT_COMPONENT),
                e.getPlace(), true);
    }
}

From source file:com.intellij.ide.actions.ImportSettingsAction.java

License:Apache License

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    final DataContext dataContext = e.getDataContext();
    final Component component = PlatformDataKeys.CONTEXT_COMPONENT.getData(dataContext);
    ChooseComponentsToExportDialog.chooseSettingsFile(PathManager.getConfigPath(), component,
            IdeBundle.message("title.import.file.location"),
            IdeBundle.message("prompt.choose.import.file.path")).doWhenDone(new Consumer<String>() {
                @Override// w w w  .  j  av  a  2s. c o m
                public void consume(String path) {
                    doImport(path);
                }
            });
}

From source file:com.intellij.ide.actions.ScrollTreeToCenterAction.java

License:Apache License

@Override
public void actionPerformed(AnActionEvent e) {
    final Component component = e.getData(PlatformDataKeys.CONTEXT_COMPONENT);
    if (component instanceof JTree) {
        JTree tree = (JTree) component;
        final int[] selection = tree.getSelectionRows();
        if (selection != null && selection.length > 0) {
            TreeUtil.showRowCentered(tree, selection[0], false);
        }// w w  w . ja  v  a  2  s.c o m
    }
}