Search in sources :

Example 1 with DataManager

use of com.intellij.ide.DataManager in project intellij-community by JetBrains.

the class FocusTrackback method restoreFocus.

public void restoreFocus() {
    if (isHeadlessOrWrongOS() || myConsumed || isScheduledForRestore())
        return;
    Project project = null;
    DataManager dataManager = DataManager.getInstance();
    if (dataManager != null) {
        DataContext context = myParentWindow == null ? dataManager.getDataContext() : dataManager.getDataContext(myParentWindow);
        if (context != null) {
            project = CommonDataKeys.PROJECT.getData(context);
        }
    }
    myScheduledForRestore = true;
    final List<FocusTrackback> stack = getCleanStackForRoot();
    final int index = stack.indexOf(this);
    for (int i = index - 1; i >= 0; i--) {
        if (stack.get(i).isScheduledForRestore()) {
            dispose();
            return;
        }
    }
    if (project != null && !project.isDisposed()) {
        final IdeFocusManager focusManager = IdeFocusManager.getInstance(project);
        cleanParentWindow();
        final Project finalProject = project;
        focusManager.requestFocus(new MyFocusCommand(), myForcedRestore).doWhenProcessed(() -> dispose()).doWhenRejected(() -> focusManager.revalidateFocus(new ExpirableRunnable.ForProject(finalProject) {

            @Override
            public void run() {
                if (UIUtil.isMeaninglessFocusOwner(focusManager.getFocusOwner())) {
                    focusManager.requestDefaultFocus(false);
                }
            }
        }));
    } else {
        // no ide focus manager, so no way -- do just later
        //noinspection SSBasedInspection
        SwingUtilities.invokeLater(() -> {
            _restoreFocus();
            dispose();
        });
    }
}
Also used : Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) DataManager(com.intellij.ide.DataManager)

Example 2 with DataManager

use of com.intellij.ide.DataManager in project intellij-community by JetBrains.

the class SearchEverywhereAction method doNavigate.

private void doNavigate(final int index) {
    final DataManager dataManager = DataManager.getInstance();
    if (dataManager == null)
        return;
    final Project project = CommonDataKeys.PROJECT.getData(dataManager.getDataContext(getField().getTextEditor()));
    assert project != null;
    final SearchListModel model = getModel();
    if (isMoreItem(index)) {
        final String pattern = myPopupField.getText();
        WidgetID wid = null;
        if (index == model.moreIndex.classes)
            wid = WidgetID.CLASSES;
        else if (index == model.moreIndex.files)
            wid = WidgetID.FILES;
        else if (index == model.moreIndex.settings)
            wid = WidgetID.SETTINGS;
        else if (index == model.moreIndex.actions)
            wid = WidgetID.ACTIONS;
        else if (index == model.moreIndex.symbols)
            wid = WidgetID.SYMBOLS;
        else if (index == model.moreIndex.runConfigurations)
            wid = WidgetID.RUN_CONFIGURATIONS;
        if (wid != null) {
            final WidgetID widgetID = wid;
            myCurrentWorker.doWhenProcessed(() -> {
                myCalcThread = new CalcThread(project, pattern, true);
                myPopupActualWidth = 0;
                myCurrentWorker = myCalcThread.insert(index, widgetID);
            });
            return;
        }
    }
    final String pattern = getField().getText();
    final Object value = myList.getSelectedValue();
    saveHistory(project, pattern, value);
    IdeFocusManager focusManager = IdeFocusManager.findInstanceByComponent(getField().getTextEditor());
    if (myPopup != null && myPopup.isVisible()) {
        myPopup.cancel();
    }
    if (value instanceof BooleanOptionDescription) {
        final BooleanOptionDescription option = (BooleanOptionDescription) value;
        option.setOptionState(!option.isOptionEnabled());
        myList.revalidate();
        myList.repaint();
        getGlobalInstance().doWhenFocusSettlesDown(() -> {
            getGlobalInstance().requestFocus(getField(), true);
        });
        return;
    }
    if (value instanceof OptionsTopHitProvider) {
        //noinspection SSBasedInspection
        SwingUtilities.invokeLater(() -> getField().setText("#" + ((OptionsTopHitProvider) value).getId() + " "));
        return;
    }
    Runnable onDone = null;
    AccessToken token = ApplicationManager.getApplication().acquireReadActionLock();
    try {
        if (value instanceof PsiElement) {
            onDone = () -> NavigationUtil.activateFileWithPsiElement((PsiElement) value, true);
            return;
        } else if (isVirtualFile(value)) {
            onDone = () -> OpenSourceUtil.navigate(true, new OpenFileDescriptor(project, (VirtualFile) value));
            return;
        } else if (isActionValue(value) || isSetting(value) || isRunConfiguration(value)) {
            focusManager.requestDefaultFocus(true);
            final Component comp = myContextComponent;
            final AnActionEvent event = myActionEvent;
            IdeFocusManager.getInstance(project).doWhenFocusSettlesDown(() -> {
                Component c = comp;
                if (c == null) {
                    c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
                }
                if (isRunConfiguration(value)) {
                    ChooseRunConfigurationPopup.ItemWrapper itemWrapper = (ChooseRunConfigurationPopup.ItemWrapper) value;
                    RunnerAndConfigurationSettings settings = ObjectUtils.tryCast(itemWrapper.getValue(), RunnerAndConfigurationSettings.class);
                    if (settings != null) {
                        Executor executor = findExecutor(settings);
                        if (executor != null) {
                            itemWrapper.perform(project, executor, dataManager.getDataContext(c));
                        }
                    }
                } else {
                    GotoActionAction.openOptionOrPerformAction(value, pattern, project, c, event);
                    if (isToolWindowAction(value))
                        return;
                }
            });
            return;
        } else if (value instanceof Navigatable) {
            onDone = () -> OpenSourceUtil.navigate(true, (Navigatable) value);
            return;
        }
    } finally {
        token.finish();
        final ActionCallback callback = onFocusLost();
        if (onDone != null) {
            callback.doWhenDone(onDone);
        }
    }
    focusManager.requestDefaultFocus(true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DataManager(com.intellij.ide.DataManager) Navigatable(com.intellij.pom.Navigatable) Project(com.intellij.openapi.project.Project) DefaultRunExecutor(com.intellij.execution.executors.DefaultRunExecutor) Executor(com.intellij.execution.Executor) AccessToken(com.intellij.openapi.application.AccessToken) BooleanOptionDescription(com.intellij.ide.ui.search.BooleanOptionDescription) ChooseRunConfigurationPopup(com.intellij.execution.actions.ChooseRunConfigurationPopup) RunnerAndConfigurationSettings(com.intellij.execution.RunnerAndConfigurationSettings) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) OptionsTopHitProvider(com.intellij.ide.ui.OptionsTopHitProvider) PropertiesComponent(com.intellij.ide.util.PropertiesComponent)

Example 3 with DataManager

use of com.intellij.ide.DataManager in project intellij-community by JetBrains.

the class GotoActionAction method performAction.

public static void performAction(Object element, @Nullable final Component component, @Nullable final AnActionEvent e, @Nullable final Runnable callback) {
    // element could be AnAction (SearchEverywhere)
    if (component == null)
        return;
    final AnAction action = element instanceof AnAction ? (AnAction) element : ((GotoActionModel.ActionWrapper) element).getAction();
    TransactionGuard.getInstance().submitTransactionLater(ApplicationManager.getApplication(), () -> {
        DataManager instance = DataManager.getInstance();
        DataContext context = instance != null ? instance.getDataContext(component) : DataContext.EMPTY_CONTEXT;
        InputEvent inputEvent = e == null ? null : e.getInputEvent();
        AnActionEvent event = AnActionEvent.createFromAnAction(action, inputEvent, ActionPlaces.ACTION_SEARCH, context);
        if (ActionUtil.lastUpdateAndCheckDumb(action, event, false)) {
            if (action instanceof ActionGroup && ((ActionGroup) action).getChildren(event).length > 0) {
                ListPopup popup = JBPopupFactory.getInstance().createActionGroupPopup(event.getPresentation().getText(), (ActionGroup) action, context, false, callback, -1);
                Window window = SwingUtilities.getWindowAncestor(component);
                if (window != null) {
                    popup.showInCenterOf(window);
                } else {
                    popup.showInFocusCenter();
                }
            } else {
                ActionUtil.performActionDumbAware(action, event);
                if (callback != null)
                    callback.run();
            }
        }
    });
}
Also used : ProgressWindow(com.intellij.openapi.progress.util.ProgressWindow) ListPopup(com.intellij.openapi.ui.popup.ListPopup) GotoActionModel(com.intellij.ide.util.gotoByName.GotoActionModel) DataManager(com.intellij.ide.DataManager)

Example 4 with DataManager

use of com.intellij.ide.DataManager in project intellij-community by JetBrains.

the class FileInEditorProcessor method processCode.

public void processCode() {
    if (myShouldOptimizeImports) {
        myProcessor = new OptimizeImportsProcessor(myProject, myFile);
    }
    if (myProcessChangesTextOnly && !FormatChangedTextUtil.hasChanges(myFile)) {
        myNoChangesDetected = true;
    }
    myProcessor = mixWithReformatProcessor(myProcessor);
    if (myShouldRearrangeCode) {
        myProcessor = mixWithRearrangeProcessor(myProcessor);
    }
    if (shouldNotify()) {
        myProcessor.setCollectInfo(true);
        myProcessor.setPostRunnable(() -> {
            String message = prepareMessage();
            if (!myEditor.isDisposed() && myEditor.getComponent().isShowing()) {
                HyperlinkListener hyperlinkListener = new HyperlinkAdapter() {

                    @Override
                    protected void hyperlinkActivated(HyperlinkEvent e) {
                        AnAction action = ActionManager.getInstance().getAction("ShowReformatFileDialog");
                        DataManager manager = DataManager.getInstance();
                        if (manager != null) {
                            DataContext context = manager.getDataContext(myEditor.getContentComponent());
                            action.actionPerformed(AnActionEvent.createFromAnAction(action, null, "", context));
                        }
                    }
                };
                showHint(myEditor, message, hyperlinkListener);
            }
        });
    }
    myProcessor.run();
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) HyperlinkEvent(javax.swing.event.HyperlinkEvent) HyperlinkListener(javax.swing.event.HyperlinkListener) DataManager(com.intellij.ide.DataManager) AnAction(com.intellij.openapi.actionSystem.AnAction) HyperlinkAdapter(com.intellij.ui.HyperlinkAdapter)

Example 5 with DataManager

use of com.intellij.ide.DataManager in project intellij-community by JetBrains.

the class MoveClassToModuleFix method moveClass.

private void moveClass(Project project, Editor editor, PsiFile file, PsiClass aClass) {
    RefactoringActionHandler moveHandler = RefactoringActionHandlerFactory.getInstance().createMoveHandler();
    DataManager dataManager = DataManager.getInstance();
    DataContext dataContext = dataManager.getDataContext();
    final String fqName = aClass.getQualifiedName();
    LOG.assertTrue(fqName != null);
    PsiDirectory directory = PackageUtil.findOrCreateDirectoryForPackage(myCurrentModule, StringUtil.getPackageName(fqName), mySourceRoot, true);
    DataContext context = SimpleDataContext.getSimpleContext(LangDataKeys.TARGET_PSI_ELEMENT.getName(), directory, dataContext);
    moveHandler.invoke(project, new PsiElement[] { aClass }, context);
    PsiReference reference = file.findReferenceAt(editor.getCaretModel().getOffset());
    PsiClass newClass = JavaPsiFacade.getInstance(project).findClass(fqName, GlobalSearchScope.moduleScope(myCurrentModule));
    if (reference != null && newClass != null) {
        final QuestionAction action = new AddImportAction(project, reference, editor, newClass);
        action.execute();
    }
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) SimpleDataContext(com.intellij.openapi.actionSystem.impl.SimpleDataContext) QuestionAction(com.intellij.codeInsight.hint.QuestionAction) AddImportAction(com.intellij.codeInsight.daemon.impl.actions.AddImportAction) DataManager(com.intellij.ide.DataManager) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler)

Aggregations

DataManager (com.intellij.ide.DataManager)8 DataContext (com.intellij.openapi.actionSystem.DataContext)4 Project (com.intellij.openapi.project.Project)2 AddImportAction (com.intellij.codeInsight.daemon.impl.actions.AddImportAction)1 QuestionAction (com.intellij.codeInsight.hint.QuestionAction)1 Executor (com.intellij.execution.Executor)1 RunnerAndConfigurationSettings (com.intellij.execution.RunnerAndConfigurationSettings)1 ChooseRunConfigurationPopup (com.intellij.execution.actions.ChooseRunConfigurationPopup)1 DefaultRunExecutor (com.intellij.execution.executors.DefaultRunExecutor)1 OptionsTopHitProvider (com.intellij.ide.ui.OptionsTopHitProvider)1 BooleanOptionDescription (com.intellij.ide.ui.search.BooleanOptionDescription)1 PropertiesComponent (com.intellij.ide.util.PropertiesComponent)1 GotoActionModel (com.intellij.ide.util.gotoByName.GotoActionModel)1 ActionManager (com.intellij.openapi.actionSystem.ActionManager)1 AnAction (com.intellij.openapi.actionSystem.AnAction)1 SimpleDataContext (com.intellij.openapi.actionSystem.impl.SimpleDataContext)1 AccessToken (com.intellij.openapi.application.AccessToken)1 Application (com.intellij.openapi.application.Application)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 ShortcutTextField (com.intellij.openapi.keymap.impl.ui.ShortcutTextField)1