Search in sources :

Example 1 with AndroidEditorSettings

use of org.jetbrains.android.uipreview.AndroidEditorSettings in project android by JetBrains.

the class NlPreviewManager method processFileEditorChange.

private void processFileEditorChange(@Nullable final TextEditor newEditor) {
    if (myPendingShowComponent != null) {
        myPendingShowComponent.removeHierarchyListener(myHierarchyListener);
        myPendingShowComponent = null;
    }
    myToolWindowUpdateQueue.cancelAllUpdates();
    myToolWindowUpdateQueue.queue(new Update("update") {

        @Override
        public void run() {
            if (!myToolWindowReady || myToolWindowDisposed) {
                return;
            }
            boolean renderImmediately = myRenderImmediately;
            myRenderImmediately = false;
            final Editor activeEditor = newEditor != null ? newEditor.getEditor() : null;
            if (myToolWindow == null) {
                if (activeEditor == null) {
                    return;
                } else if (!activeEditor.getComponent().isShowing()) {
                    // per project frame.)
                    if (!mySeenEditor) {
                        myPendingShowComponent = activeEditor.getComponent();
                        if (myHierarchyListener == null) {
                            myHierarchyListener = new HierarchyListener() {

                                @Override
                                public void hierarchyChanged(HierarchyEvent hierarchyEvent) {
                                    if ((hierarchyEvent.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {
                                        if (hierarchyEvent.getComponent() == myPendingShowComponent && myPendingShowComponent.isShowing()) {
                                            myPendingShowComponent.removeHierarchyListener(myHierarchyListener);
                                            mySeenEditor = true;
                                            myPendingShowComponent = null;
                                            processFileEditorChange(getActiveLayoutXmlEditor());
                                        }
                                    }
                                }
                            };
                        }
                        myPendingShowComponent.addHierarchyListener(myHierarchyListener);
                    }
                    return;
                }
                mySeenEditor = true;
                initToolWindow();
            }
            final AndroidEditorSettings settings = AndroidEditorSettings.getInstance();
            final boolean hideForNonLayoutFiles = settings.getGlobalState().isHideForNonLayoutFiles();
            if (activeEditor == null) {
                myToolWindowForm.setFile(null);
                myToolWindow.setAvailable(!hideForNonLayoutFiles, null);
                return;
            }
            final PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(activeEditor.getDocument());
            myToolWindowForm.setFile(psiFile);
            if (psiFile == null) {
                myToolWindow.setAvailable(!hideForNonLayoutFiles, null);
                return;
            }
            myToolWindow.setAvailable(true, null);
            final boolean visible = AndroidEditorSettings.getInstance().getGlobalState().isVisible();
            if (visible) {
                // tool window is shown and the time the render has completed
                if (!myToolWindow.isVisible()) {
                    RenderResult renderResult = myToolWindowForm.getRenderResult();
                    if (renderResult != null && renderResult.getFile() != psiFile) {
                        myToolWindowForm.setRenderResult(RenderResult.createBlank(psiFile));
                    }
                }
                myToolWindow.show(null);
            }
        }
    });
}
Also used : HierarchyEvent(java.awt.event.HierarchyEvent) AndroidEditorSettings(org.jetbrains.android.uipreview.AndroidEditorSettings) RenderResult(com.android.tools.idea.rendering.RenderResult) PsiFile(com.intellij.psi.PsiFile) Update(com.intellij.util.ui.update.Update) com.intellij.openapi.fileEditor(com.intellij.openapi.fileEditor) Editor(com.intellij.openapi.editor.Editor) HierarchyListener(java.awt.event.HierarchyListener)

Aggregations

RenderResult (com.android.tools.idea.rendering.RenderResult)1 Editor (com.intellij.openapi.editor.Editor)1 com.intellij.openapi.fileEditor (com.intellij.openapi.fileEditor)1 PsiFile (com.intellij.psi.PsiFile)1 Update (com.intellij.util.ui.update.Update)1 HierarchyEvent (java.awt.event.HierarchyEvent)1 HierarchyListener (java.awt.event.HierarchyListener)1 AndroidEditorSettings (org.jetbrains.android.uipreview.AndroidEditorSettings)1