Search in sources :

Example 11 with AsyncResult

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

the class DesignerApplicationManager method renderIfNeed.

public void renderIfNeed(@NotNull XmlFile psiFile, @Nullable final Consumer<DocumentInfo> handler, @Nullable ActionCallback renderRejectedCallback, final boolean debug) {
    boolean needInitialRender = isApplicationClosed();
    DocumentInfo documentInfo = null;
    if (!needInitialRender) {
        Document[] unsavedDocuments = FileDocumentManager.getInstance().getUnsavedDocuments();
        if (unsavedDocuments.length > 0) {
            renderDocumentsAndCheckLocalStyleModification(unsavedDocuments);
        }
        documentInfo = DocumentFactoryManager.getInstance().getNullableInfo(psiFile);
        needInitialRender = documentInfo == null;
    }
    if (!needInitialRender) {
        if (handler == null) {
            return;
        }
        Application app = ApplicationManager.getApplication();
        if (app.isDispatchThread()) {
            final DocumentInfo finalDocumentInfo = documentInfo;
            app.executeOnPooledThread(() -> handler.consume(finalDocumentInfo));
        } else {
            handler.consume(documentInfo);
        }
        return;
    }
    synchronized (initialRenderQueue) {
        AsyncResult<DocumentInfo> renderResult = initialRenderQueue.findResult(psiFile);
        if (renderResult == null) {
            renderResult = new AsyncResult<>();
            if (renderRejectedCallback != null) {
                renderResult.notifyWhenRejected(renderRejectedCallback);
            }
            initialRenderQueue.add(new RenderAction<AsyncResult<DocumentInfo>>(psiFile.getProject(), psiFile.getViewProvider().getVirtualFile(), renderResult) {

                @Override
                protected boolean isNeedEdt() {
                    // ProgressManager requires dispatch thread
                    return true;
                }

                @Override
                protected void doRun() {
                    assert project != null;
                    if (project.isDisposed()) {
                        return;
                    }
                    assert file != null;
                    PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
                    if (!(psiFile instanceof XmlFile)) {
                        return;
                    }
                    Module module = ModuleUtilCore.findModuleForFile(file, project);
                    if (module != null) {
                        renderDocument(module, (XmlFile) psiFile, debug, result);
                    }
                }
            });
        }
        if (handler != null) {
            renderResult.doWhenDone(handler);
        }
        renderResult.doWhenDone(createDocumentRenderedNotificationDoneHandler(false));
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) Document(com.intellij.openapi.editor.Document) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) Application(com.intellij.openapi.application.Application) AsyncResult(com.intellij.openapi.util.AsyncResult) DocumentInfo(com.intellij.flex.uiDesigner.DocumentFactoryManager.DocumentInfo)

Example 12 with AsyncResult

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

the class Client method renderDocument.

/**
   * final, full render document - responsible for handle problemsHolder and assetCounter - you must not do it
   */
public AsyncResult<DocumentInfo> renderDocument(Module module, XmlFile psiFile, ProblemsHolder problemsHolder) {
    VirtualFile virtualFile = psiFile.getVirtualFile();
    final int factoryId = registerDocumentFactoryIfNeed(module, psiFile, virtualFile, false, problemsHolder);
    final AsyncResult<DocumentInfo> result = new AsyncResult<>();
    if (factoryId == -1) {
        result.setRejected();
        return result;
    }
    FlexLibrarySet flexLibrarySet = registeredModules.getInfo(module).getFlexLibrarySet();
    if (flexLibrarySet != null) {
        fillAssetClassPoolIfNeed(flexLibrarySet);
    }
    if (!problemsHolder.isEmpty()) {
        DocumentProblemManager.getInstance().report(module.getProject(), problemsHolder);
    }
    final ActionCallback callback = new ActionCallback("renderDocument");
    boolean hasError = true;
    try {
        beginMessage(ClientMethod.renderDocument, callback, result, () -> result.setDone(DocumentFactoryManager.getInstance().getInfo(factoryId)));
        out.writeShort(factoryId);
        hasError = false;
    } finally {
        finalizeMessageAndFlush(hasError);
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ActionCallback(com.intellij.openapi.util.ActionCallback) AsyncResult(com.intellij.openapi.util.AsyncResult) DocumentInfo(com.intellij.flex.uiDesigner.DocumentFactoryManager.DocumentInfo)

Example 13 with AsyncResult

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

the class WindowSystemPlaybackCall method printFocus.

public static AsyncResult<String> printFocus(final PlaybackContext context) {
    final AsyncResult result = new AsyncResult<String>();
    getUiReady(context).doWhenProcessed(() -> {
        final LinkedHashMap<String, String> focusInfo = getFocusInfo();
        if (focusInfo == null) {
            result.setRejected("No component focused");
            return;
        }
        StringBuffer text = new StringBuffer();
        for (Iterator<String> iterator = focusInfo.keySet().iterator(); iterator.hasNext(); ) {
            String key = iterator.next();
            text.append(key + "=" + focusInfo.get(key));
            if (iterator.hasNext()) {
                text.append("|");
            }
        }
        result.setDone(text.toString());
    });
    return result;
}
Also used : AsyncResult(com.intellij.openapi.util.AsyncResult)

Example 14 with AsyncResult

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

the class EditorPlaybackCall method assertEditorLine.

public static AsyncResult<String> assertEditorLine(final PlaybackContext context, final String expected) {
    final AsyncResult<String> result = new AsyncResult<>();
    WindowSystemPlaybackCall.getUiReady(context).doWhenDone(() -> {
        Editor editor = CommonDataKeys.EDITOR.getData(DataManager.getInstance().getDataContextFromFocus().getResult());
        if (editor == null) {
            editor = CommonDataKeys.EDITOR_EVEN_IF_INACTIVE.getData(DataManager.getInstance().getDataContextFromFocus().getResult());
        }
        if (editor == null) {
            result.setRejected("Cannot find editor");
            return;
        }
        final int line = editor.getCaretModel().getLogicalPosition().line;
        final int caret = editor.getCaretModel().getOffset();
        final int start = editor.getDocument().getLineStartOffset(line);
        final int end = editor.getDocument().getLineEndOffset(line);
        final StringBuffer actualText = new StringBuffer(editor.getDocument().getText(new TextRange(start, caret)));
        actualText.append("<caret>").append(editor.getDocument().getText(new TextRange(caret, end)));
        if (expected.equals(actualText.toString())) {
            result.setDone();
        } else {
            result.setRejected("Expected:" + expected + " but was:" + actualText);
        }
    });
    return result;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) AsyncResult(com.intellij.openapi.util.AsyncResult)

Example 15 with AsyncResult

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

the class NavBarPanel method getHintContainerShowPoint.

AsyncResult<RelativePoint> getHintContainerShowPoint() {
    final AsyncResult<RelativePoint> result = new AsyncResult<>();
    if (myLocationCache == null) {
        if (myHintContainer != null) {
            final Point p = AbstractPopup.getCenterOf(myHintContainer, this);
            p.y -= myHintContainer.getVisibleRect().height / 4;
            myLocationCache = RelativePoint.fromScreen(p);
        } else {
            if (myContextComponent != null) {
                myLocationCache = JBPopupFactory.getInstance().guessBestPopupLocation(DataManager.getInstance().getDataContext(myContextComponent));
            } else {
                DataManager.getInstance().getDataContextFromFocus().doWhenDone((Consumer<DataContext>) dataContext -> {
                    myContextComponent = PlatformDataKeys.CONTEXT_COMPONENT.getData(dataContext);
                    myLocationCache = JBPopupFactory.getInstance().guessBestPopupLocation(DataManager.getInstance().getDataContext(myContextComponent));
                });
            }
        }
    }
    final Component c = myLocationCache.getComponent();
    if (!(c instanceof JComponent && c.isShowing())) {
        //Yes. It happens sometimes.
        // 1. Empty frame. call nav bar, select some package and open it in Project View
        // 2. Call nav bar, then Esc
        // 3. Hide all tool windows (Ctrl+Shift+F12), so we've got empty frame again
        // 4. Call nav bar. NPE. ta da
        final JComponent ideFrame = WindowManager.getInstance().getIdeFrame(getProject()).getComponent();
        final JRootPane rootPane = UIUtil.getRootPane(ideFrame);
        myLocationCache = JBPopupFactory.getInstance().guessBestPopupLocation(rootPane);
    }
    result.setDone(myLocationCache);
    return result;
}
Also used : UIUtil(com.intellij.util.ui.UIUtil) ToolWindowManager(com.intellij.openapi.wm.ToolWindowManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) DnDDragStartBean(com.intellij.ide.dnd.DnDDragStartBean) NavBarUI(com.intellij.ide.navigationToolbar.ui.NavBarUI) ProjectView(com.intellij.ide.projectView.ProjectView) IdeView(com.intellij.ide.IdeView) Disposer(com.intellij.openapi.util.Disposer) MouseAdapter(java.awt.event.MouseAdapter) Module(com.intellij.openapi.module.Module) AsyncResult(com.intellij.openapi.util.AsyncResult) ProjectRootsUtil(com.intellij.ide.projectView.impl.ProjectRootsUtil) DeleteHandler(com.intellij.ide.util.DeleteHandler) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) ActionCallback(com.intellij.openapi.util.ActionCallback) WindowManager(com.intellij.openapi.wm.WindowManager) PopupOwner(com.intellij.ui.popup.PopupOwner) AbstractPopup(com.intellij.ui.popup.AbstractPopup) com.intellij.ui(com.intellij.ui) PanelUI(javax.swing.plaf.PanelUI) TransferableWrapper(com.intellij.ide.dnd.TransferableWrapper) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) PopupMenuEvent(javax.swing.event.PopupMenuEvent) CustomizationUtil(com.intellij.ide.ui.customization.CustomizationUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) PsiDirectory(com.intellij.psi.PsiDirectory) NotNull(org.jetbrains.annotations.NotNull) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) RelativePoint(com.intellij.ui.awt.RelativePoint) NavBarUIManager(com.intellij.ide.navigationToolbar.ui.NavBarUIManager) Consumer(com.intellij.util.Consumer) AbstractProjectViewPane(com.intellij.ide.projectView.impl.AbstractProjectViewPane) java.util(java.util) CopyPasteDelegator(com.intellij.ide.CopyPasteDelegator) TreeNode(javax.swing.tree.TreeNode) LineBorder(javax.swing.border.LineBorder) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) DataManager(com.intellij.ide.DataManager) DnDSupport(com.intellij.ide.dnd.DnDSupport) JBList(com.intellij.ui.components.JBList) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) StringUtil(com.intellij.openapi.util.text.StringUtil) UISettings(com.intellij.ide.ui.UISettings) Editor(com.intellij.openapi.editor.Editor) Disposable(com.intellij.openapi.Disposable) SystemInfo(com.intellij.openapi.util.SystemInfo) MouseEvent(java.awt.event.MouseEvent) File(java.io.File) java.awt(java.awt) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) ModuleDeleteProvider(com.intellij.openapi.roots.ui.configuration.actions.ModuleDeleteProvider) HintManagerImpl(com.intellij.codeInsight.hint.HintManagerImpl) Queryable(com.intellij.openapi.ui.Queryable) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) CustomActionsSchema(com.intellij.ide.ui.customization.CustomActionsSchema) Navigatable(com.intellij.pom.Navigatable) HintManager(com.intellij.codeInsight.hint.HintManager) javax.swing(javax.swing) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) 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