Search in sources :

Example 16 with AsyncResult

use of com.intellij.openapi.util.AsyncResult in project intellij-community by JetBrains.

the class RichTextActionProcessor method process.

@Override
public JComponent process(@NotNull String s) {
    final ActionManager actionManager = ActionManager.getInstance();
    final AnAction action = actionManager.getAction(s);
    if (action == null) {
        return null;
    }
    final Presentation presentation = action.getTemplatePresentation();
    if (presentation.getIcon() != null) {
        return new ActionButton(action, presentation.clone(), GradleConstants.TOOL_WINDOW_TOOLBAR_PLACE, JBUI.emptySize()) {

            @Override
            protected void paintButtonLook(Graphics g) {
                // Don't draw border at the inline button.
                ActionButtonLook look = getButtonLook();
                look.paintBackground(g, this);
                look.paintIcon(g, this, getIcon());
            }
        };
    }
    final String text = action.getTemplatePresentation().getText();
    JLabel result = new JLabel(text) {

        public void paint(Graphics g) {
            super.paint(g);
            final int y = g.getClipBounds().height - getFontMetrics(getFont()).getDescent() + 2;
            final int width = getFontMetrics(getFont()).stringWidth(getText());
            g.drawLine(0, y, width, y);
        }
    };
    Color color = UIUtil.isUnderDarcula() ? Color.ORANGE : Color.BLUE;
    result.setForeground(color);
    result.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    new ClickListener() {

        @Override
        public boolean onClick(@NotNull MouseEvent e, int clickCount) {
            final AsyncResult<DataContext> callback = DataManager.getInstance().getDataContextFromFocus();
            final DataContext context = callback.getResult();
            if (context == null) {
                return false;
            }
            final Presentation presentation = new PresentationFactory().getPresentation(action);
            action.actionPerformed(new AnActionEvent(e, context, GradleConstants.TOOL_WINDOW_TOOLBAR_PLACE, presentation, ActionManager.getInstance(), e.getModifiers()));
            return true;
        }
    }.installOn(result);
    return result;
}
Also used : MouseEvent(java.awt.event.MouseEvent) PresentationFactory(com.intellij.openapi.actionSystem.impl.PresentationFactory) ActionButton(com.intellij.openapi.actionSystem.impl.ActionButton) AsyncResult(com.intellij.openapi.util.AsyncResult) ActionButtonLook(com.intellij.openapi.actionSystem.ex.ActionButtonLook) ClickListener(com.intellij.ui.ClickListener)

Example 17 with AsyncResult

use of com.intellij.openapi.util.AsyncResult in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoRenameQuickFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    if (!FileModificationService.getInstance().preparePsiElementsForWrite(startElement))
        return;
    Runnable runnable = () -> {
        AsyncResult<DataContext> dataContextContainer = DataManager.getInstance().getDataContextFromFocus();
        dataContextContainer.doWhenDone(new Consumer<DataContext>() {

            @Override
            public void consume(DataContext dataContext) {
                RenameHandler renameHandler = RenameHandlerRegistry.getInstance().getRenameHandler(dataContext);
                if (renameHandler != null) {
                    renameHandler.invoke(project, new PsiElement[] { startElement }, dataContext);
                } else {
                    RefactoringActionHandler renameRefactoringHandler = RefactoringActionHandlerFactory.getInstance().createRenameHandler();
                    renameRefactoringHandler.invoke(project, new PsiElement[] { startElement }, dataContext);
                }
            }
        });
    };
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        runnable.run();
    } else {
        ApplicationManager.getApplication().invokeLater(runnable, project.getDisposed());
    }
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) Consumer(com.intellij.util.Consumer) RenameHandler(com.intellij.refactoring.rename.RenameHandler) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler) AsyncResult(com.intellij.openapi.util.AsyncResult)

Aggregations

AsyncResult (com.intellij.openapi.util.AsyncResult)17 Consumer (com.intellij.util.Consumer)8 Project (com.intellij.openapi.project.Project)6 ActionCallback (com.intellij.openapi.util.ActionCallback)5 MouseEvent (java.awt.event.MouseEvent)4 Disposable (com.intellij.openapi.Disposable)3 NotNull (org.jetbrains.annotations.NotNull)3 DocumentInfo (com.intellij.flex.uiDesigner.DocumentFactoryManager.DocumentInfo)2 DataManager (com.intellij.ide.DataManager)2 UISettings (com.intellij.ide.ui.UISettings)2 CustomActionsSchema (com.intellij.ide.ui.customization.CustomActionsSchema)2 com.intellij.openapi.actionSystem (com.intellij.openapi.actionSystem)2 DataContext (com.intellij.openapi.actionSystem.DataContext)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 CommandProcessor (com.intellij.openapi.command.CommandProcessor)2 Document (com.intellij.openapi.editor.Document)2 Editor (com.intellij.openapi.editor.Editor)2 Module (com.intellij.openapi.module.Module)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 IdeFocusManager (com.intellij.openapi.wm.IdeFocusManager)2