Search in sources :

Example 26 with ModalityState

use of com.intellij.openapi.application.ModalityState in project intellij-community by JetBrains.

the class CommandProcessor method run.

private boolean run(boolean synchronously) {
    synchronized (myLock) {
        final CommandGroup commandGroup = getNextCommandGroup();
        if (commandGroup == null || commandGroup.isEmpty())
            return false;
        final Condition conditionForGroup = commandGroup.getExpireCondition();
        final FinalizableCommand command = commandGroup.takeNextCommand();
        myCommandCount--;
        Condition expire = command.getExpireCondition() != null ? command.getExpireCondition() : conditionForGroup;
        if (expire == null)
            expire = ApplicationManager.getApplication().getDisposed();
        if (expire.value(null))
            return true;
        if (LOG.isDebugEnabled()) {
            LOG.debug("CommandProcessor.run " + command);
        }
        if (synchronously) {
            command.run();
            return true;
        }
        // max. I'm not actually quite sure this should have NON_MODAL modality but it should
        // definitely have some since runnables in command list may (and do) request some PSI activity
        final boolean queueNext = myCommandCount > 0;
        Application application = ApplicationManager.getApplication();
        ModalityState modalityState = Registry.is("ide.perProjectModality") ? ModalityState.current() : ModalityState.NON_MODAL;
        application.getInvokator().invokeLater(command, modalityState, expire).doWhenDone(() -> {
            if (queueNext) {
                run(false);
            }
        });
        return true;
    }
}
Also used : Condition(com.intellij.openapi.util.Condition) ModalityState(com.intellij.openapi.application.ModalityState) FinalizableCommand(com.intellij.openapi.wm.impl.commands.FinalizableCommand) Application(com.intellij.openapi.application.Application)

Example 27 with ModalityState

use of com.intellij.openapi.application.ModalityState in project intellij-community by JetBrains.

the class PaintersHelper method newWallpaperPainter.

private static AbstractPainter newWallpaperPainter(@NotNull final String propertyName, @NotNull final JComponent rootComponent) {
    return new ImagePainter() {

        Image image;

        float alpha;

        Insets insets;

        Fill fillType;

        Place place;

        String current;

        @Override
        public boolean needsRepaint() {
            return ensureImageLoaded();
        }

        @Override
        public void executePaint(Component component, Graphics2D g) {
            // covered by needsRepaint()
            if (image == null)
                return;
            executePaint(g, component, image, fillType, place, alpha, insets);
        }

        boolean ensureImageLoaded() {
            IdeFrame frame = UIUtil.getParentOfType(IdeFrame.class, rootComponent);
            Project project = frame == null ? null : frame.getProject();
            String value = getBackgroundSpec(project, propertyName);
            if (!Comparing.equal(value, current)) {
                current = value;
                loadImageAsync(value);
            // keep the current image for a while
            }
            return image != null;
        }

        private void resetImage(String value, Image newImage, float newAlpha, Fill newFill, Place newPlace) {
            if (!Comparing.equal(current, value))
                return;
            boolean prevOk = image != null;
            clearImages(-1);
            image = newImage;
            insets = JBUI.emptyInsets();
            alpha = newAlpha;
            fillType = newFill;
            place = newPlace;
            boolean newOk = newImage != null;
            if (prevOk || newOk) {
                ModalityState modalityState = ModalityState.stateForComponent(rootComponent);
                if (modalityState.dominates(ModalityState.NON_MODAL)) {
                    UIUtil.getActiveWindow().repaint();
                } else {
                    IdeBackgroundUtil.repaintAllWindows();
                }
            }
        }

        private void loadImageAsync(final String propertyValue) {
            String[] parts = (propertyValue != null ? propertyValue : propertyName + ".png").split(",");
            final float newAlpha = Math.abs(Math.min(StringUtil.parseInt(parts.length > 1 ? parts[1] : "", 10) / 100f, 1f));
            final Fill newFillType = StringUtil.parseEnum(parts.length > 2 ? parts[2].toUpperCase(Locale.ENGLISH) : "", Fill.SCALE, Fill.class);
            final Place newPlace = StringUtil.parseEnum(parts.length > 3 ? parts[3].toUpperCase(Locale.ENGLISH) : "", Place.CENTER, Place.class);
            String filePath = parts[0];
            if (StringUtil.isEmpty(filePath)) {
                resetImage(propertyValue, null, newAlpha, newFillType, newPlace);
                return;
            }
            try {
                URL url = filePath.contains("://") ? new URL(filePath) : (FileUtil.isAbsolutePlatformIndependent(filePath) ? new File(filePath) : new File(PathManager.getConfigPath(), filePath)).toURI().toURL();
                ApplicationManager.getApplication().executeOnPooledThread(() -> {
                    final Image m = ImageLoader.loadFromUrl(url);
                    ModalityState modalityState = ModalityState.stateForComponent(rootComponent);
                    ApplicationManager.getApplication().invokeLater(() -> resetImage(propertyValue, m, newAlpha, newFillType, newPlace), modalityState);
                });
            } catch (Exception e) {
                resetImage(propertyValue, null, newAlpha, newFillType, newPlace);
            }
        }
    };
}
Also used : VolatileImage(java.awt.image.VolatileImage) IdeFrame(com.intellij.openapi.wm.IdeFrame) URL(java.net.URL) Project(com.intellij.openapi.project.Project) ModalityState(com.intellij.openapi.application.ModalityState) File(java.io.File)

Example 28 with ModalityState

use of com.intellij.openapi.application.ModalityState in project intellij-community by JetBrains.

the class InstalledPackagesPanel method uninstallAction.

private void uninstallAction() {
    final List<InstalledPackage> packages = getSelectedPackages();
    final PackageManagementService selPackageManagementService = myPackageManagementService;
    if (selPackageManagementService != null) {
        ModalityState modalityState = ModalityState.current();
        PackageManagementService.Listener listener = new PackageManagementService.Listener() {

            @Override
            public void operationStarted(String packageName) {
                ApplicationManager.getApplication().invokeLater(() -> myPackagesTable.setPaintBusy(true), modalityState);
            }

            @Override
            public void operationFinished(final String packageName, @Nullable final PackageManagementService.ErrorDescription errorDescription) {
                ApplicationManager.getApplication().invokeLater(() -> {
                    myPackagesTable.clearSelection();
                    updatePackages(selPackageManagementService);
                    myPackagesTable.setPaintBusy(false);
                    if (errorDescription == null) {
                        if (packageName != null) {
                            myNotificationArea.showSuccess("Package '" + packageName + "' successfully uninstalled");
                        } else {
                            myNotificationArea.showSuccess("Packages successfully uninstalled");
                        }
                    } else {
                        myNotificationArea.showError("Uninstall packages failed. <a href=\"xxx\">Details...</a>", "Uninstall Packages Failed", errorDescription);
                    }
                }, modalityState);
            }
        };
        myPackageManagementService.uninstallPackages(packages, listener);
    }
}
Also used : DoubleClickListener(com.intellij.ui.DoubleClickListener) ListSelectionListener(javax.swing.event.ListSelectionListener) ModalityState(com.intellij.openapi.application.ModalityState) Nullable(org.jetbrains.annotations.Nullable)

Example 29 with ModalityState

use of com.intellij.openapi.application.ModalityState in project intellij-community by JetBrains.

the class AbstractLayoutCodeProcessor method runLayoutCodeProcess.

private void runLayoutCodeProcess(final Runnable readAction, final Runnable writeAction) {
    final ProgressWindow progressWindow = new ProgressWindow(true, myProject);
    progressWindow.setTitle(myCommandName);
    progressWindow.setText(myProgressText);
    final ModalityState modalityState = ModalityState.current();
    final Runnable process = () -> ApplicationManager.getApplication().runReadAction(readAction);
    Runnable runnable = () -> {
        try {
            ProgressManager.getInstance().runProcess(process, progressWindow);
        } catch (ProcessCanceledException e) {
            return;
        } catch (IndexNotReadyException e) {
            LOG.warn(e);
            return;
        }
        final Runnable writeRunnable = () -> CommandProcessor.getInstance().executeCommand(myProject, () -> {
            try {
                writeAction.run();
                if (myPostRunnable != null) {
                    ApplicationManager.getApplication().invokeLater(myPostRunnable);
                }
            } catch (IndexNotReadyException e) {
                LOG.warn(e);
            }
        }, myCommandName, null);
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            writeRunnable.run();
        } else {
            ApplicationManager.getApplication().invokeLater(writeRunnable, modalityState, myProject.getDisposed());
        }
    };
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        runnable.run();
    } else {
        ApplicationManager.getApplication().executeOnPooledThread(runnable);
    }
}
Also used : ProgressWindow(com.intellij.openapi.progress.util.ProgressWindow) ModalityState(com.intellij.openapi.application.ModalityState) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 30 with ModalityState

use of com.intellij.openapi.application.ModalityState in project intellij-community by JetBrains.

the class DiffUtil method markDirtyAndRefresh.

/**
   * Difference with {@link VfsUtil#markDirtyAndRefresh} is that refresh from VfsUtil will be performed with ModalityState.NON_MODAL.
   */
public static void markDirtyAndRefresh(boolean async, boolean recursive, boolean reloadChildren, @NotNull VirtualFile... files) {
    ModalityState modalityState = ApplicationManager.getApplication().getDefaultModalityState();
    VfsUtil.markDirty(recursive, reloadChildren, files);
    RefreshQueue.getInstance().refresh(async, recursive, null, modalityState, files);
}
Also used : ModalityState(com.intellij.openapi.application.ModalityState)

Aggregations

ModalityState (com.intellij.openapi.application.ModalityState)40 Application (com.intellij.openapi.application.Application)6 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)6 NotNull (org.jetbrains.annotations.NotNull)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Nullable (org.jetbrains.annotations.Nullable)5 Disposable (com.intellij.openapi.Disposable)4 ListSelectionListener (javax.swing.event.ListSelectionListener)4 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)3 Project (com.intellij.openapi.project.Project)3 List (java.util.List)3 CommonBundle (com.intellij.CommonBundle)2 com.intellij.find (com.intellij.find)2 ShowUsagesAction (com.intellij.find.actions.ShowUsagesAction)2 BaseProjectTreeBuilder (com.intellij.ide.projectView.BaseProjectTreeBuilder)2 AbstractProjectTreeStructure (com.intellij.ide.projectView.impl.AbstractProjectTreeStructure)2 ProjectAbstractTreeStructureBase (com.intellij.ide.projectView.impl.ProjectAbstractTreeStructureBase)2 ProjectTreeBuilder (com.intellij.ide.projectView.impl.ProjectTreeBuilder)2 NodeRenderer (com.intellij.ide.util.treeView.NodeRenderer)2 com.intellij.openapi.actionSystem (com.intellij.openapi.actionSystem)2