Search in sources :

Example 46 with Application

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

the class ReadonlyStatusHandlerImpl method checkThreading.

private static void checkThreading() {
    Application app = ApplicationManager.getApplication();
    app.assertIsDispatchThread();
    if (!app.isWriteAccessAllowed())
        return;
    if (app.isUnitTestMode() && Registry.is("tests.assert.clear.read.only.status.outside.write.action")) {
        LOG.error("ensureFilesWritable should be called outside write action");
    }
}
Also used : Application(com.intellij.openapi.application.Application)

Example 47 with Application

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

the class ChangeFileEncodingAction method chosen.

// returns true if charset was changed, false if failed
protected boolean chosen(final Document document, final Editor editor, @Nullable final VirtualFile virtualFile, byte[] bytes, @NotNull final Charset charset) {
    if (virtualFile == null)
        return false;
    String text = document.getText();
    EncodingUtil.Magic8 isSafeToConvert = EncodingUtil.isSafeToConvertTo(virtualFile, text, bytes, charset);
    EncodingUtil.Magic8 isSafeToReload = EncodingUtil.isSafeToReloadIn(virtualFile, text, bytes, charset);
    final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile);
    final Charset oldCharset = virtualFile.getCharset();
    final Runnable undo;
    final Runnable redo;
    if (isSafeToConvert == EncodingUtil.Magic8.ABSOLUTELY && isSafeToReload == EncodingUtil.Magic8.ABSOLUTELY) {
        //change and forget
        undo = () -> EncodingManager.getInstance().setEncoding(virtualFile, oldCharset);
        redo = () -> EncodingManager.getInstance().setEncoding(virtualFile, charset);
    } else {
        IncompatibleEncodingDialog dialog = new IncompatibleEncodingDialog(virtualFile, charset, isSafeToReload, isSafeToConvert);
        dialog.show();
        if (dialog.getExitCode() == IncompatibleEncodingDialog.RELOAD_EXIT_CODE) {
            undo = () -> EncodingUtil.reloadIn(virtualFile, oldCharset);
            redo = () -> EncodingUtil.reloadIn(virtualFile, charset);
        } else if (dialog.getExitCode() == IncompatibleEncodingDialog.CONVERT_EXIT_CODE) {
            undo = () -> EncodingUtil.saveIn(document, editor, virtualFile, oldCharset);
            redo = () -> EncodingUtil.saveIn(document, editor, virtualFile, charset);
        } else {
            return false;
        }
    }
    final UndoableAction action = new GlobalUndoableAction(virtualFile) {

        @Override
        public void undo() {
            // invoke later because changing document inside undo/redo is not allowed
            Application application = ApplicationManager.getApplication();
            application.invokeLater(undo, ModalityState.NON_MODAL, (project == null ? application : project).getDisposed());
        }

        @Override
        public void redo() {
            // invoke later because changing document inside undo/redo is not allowed
            Application application = ApplicationManager.getApplication();
            application.invokeLater(redo, ModalityState.NON_MODAL, (project == null ? application : project).getDisposed());
        }
    };
    redo.run();
    CommandProcessor.getInstance().executeCommand(project, () -> {
        UndoManager undoManager = project == null ? UndoManager.getGlobalInstance() : UndoManager.getInstance(project);
        undoManager.undoableActionPerformed(action);
    }, "Change encoding for '" + virtualFile.getName() + "'", null, UndoConfirmationPolicy.REQUEST_CONFIRMATION);
    return true;
}
Also used : Project(com.intellij.openapi.project.Project) UndoManager(com.intellij.openapi.command.undo.UndoManager) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) Charset(java.nio.charset.Charset) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) Application(com.intellij.openapi.application.Application)

Example 48 with Application

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

the class VfsImplUtil method checkSubscription.

private static void checkSubscription() {
    if (ourSubscribed.getAndSet(true))
        return;
    Application app = ApplicationManager.getApplication();
    app.getMessageBus().connect(app).subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener() {

        @Override
        public void after(@NotNull List<? extends VFileEvent> events) {
            InvalidationState state = null;
            synchronized (ourLock) {
                for (VFileEvent event : events) {
                    if (!(event.getFileSystem() instanceof LocalFileSystem))
                        continue;
                    // created file should not invalidate + getFile is costly
                    if (event instanceof VFileCreateEvent)
                        continue;
                    if (event instanceof VFilePropertyChangeEvent && !VirtualFile.PROP_NAME.equals(((VFilePropertyChangeEvent) event).getPropertyName())) {
                        continue;
                    }
                    String path = event.getPath();
                    if (event instanceof VFilePropertyChangeEvent) {
                        path = ((VFilePropertyChangeEvent) event).getOldPath();
                    } else if (event instanceof VFileMoveEvent) {
                        path = ((VFileMoveEvent) event).getOldPath();
                    }
                    VirtualFile file = event.getFile();
                    if (file == null || !file.isDirectory()) {
                        state = InvalidationState.invalidate(state, path);
                    } else {
                        Collection<String> affectedPaths = ourDominatorsMap.get(path);
                        if (affectedPaths != null) {
                            // defensive copying; original may be updated on invalidation
                            affectedPaths = ContainerUtil.newArrayList(affectedPaths);
                            for (String affectedPath : affectedPaths) {
                                state = InvalidationState.invalidate(state, affectedPath);
                            }
                        }
                    }
                }
            }
            if (state != null)
                state.scheduleRefresh();
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VFilePropertyChangeEvent(com.intellij.openapi.vfs.newvfs.events.VFilePropertyChangeEvent) VFileCreateEvent(com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent) VFileMoveEvent(com.intellij.openapi.vfs.newvfs.events.VFileMoveEvent) VFileEvent(com.intellij.openapi.vfs.newvfs.events.VFileEvent) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) Collection(java.util.Collection) Application(com.intellij.openapi.application.Application)

Example 49 with Application

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

the class LaterInvocatorTest method testDispatchInvocationEventsWorksForJustSubmitted.

public void testDispatchInvocationEventsWorksForJustSubmitted() {
    Application app = ApplicationManager.getApplication();
    app.invokeAndWait(() -> {
        app.invokeLater(() -> {
            myOrder.add("1");
            assertOrderedEquals(myOrder, "1");
            UIUtil.dispatchAllInvocationEvents();
            assertOrderedEquals(myOrder, "1", "2");
        });
        app.invokeLater(new MyRunnable("2"));
        UIUtil.dispatchAllInvocationEvents();
    });
    assertOrderedEquals(myOrder, "1", "2");
}
Also used : Application(com.intellij.openapi.application.Application)

Example 50 with Application

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

the class LaterInvocatorTest method testDispatchInvocationEventsVsInvokeLaterFromBgThreadRace.

public void testDispatchInvocationEventsVsInvokeLaterFromBgThreadRace() {
    Application app = ApplicationManager.getApplication();
    app.invokeAndWait(() -> {
        for (int i = 0; i < 1000; i++) {
            AtomicBoolean executed = new AtomicBoolean();
            app.executeOnPooledThread(() -> app.invokeLater(EmptyRunnable.INSTANCE));
            app.invokeLater(() -> executed.set(true));
            UIUtil.dispatchAllInvocationEvents();
            assertTrue(executed.get());
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Application(com.intellij.openapi.application.Application)

Aggregations

Application (com.intellij.openapi.application.Application)188 NotNull (org.jetbrains.annotations.NotNull)23 VirtualFile (com.intellij.openapi.vfs.VirtualFile)21 IOException (java.io.IOException)14 Project (com.intellij.openapi.project.Project)13 Nullable (org.jetbrains.annotations.Nullable)12 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)9 ModalityState (com.intellij.openapi.application.ModalityState)8 Ref (com.intellij.openapi.util.Ref)7 ArrayList (java.util.ArrayList)7 List (java.util.List)6 ApplicationImpl (com.intellij.openapi.application.impl.ApplicationImpl)5 ApplicationManager (com.intellij.openapi.application.ApplicationManager)4 ApplicationEx (com.intellij.openapi.application.ex.ApplicationEx)4 Document (com.intellij.openapi.editor.Document)4 Module (com.intellij.openapi.module.Module)4 Computable (com.intellij.openapi.util.Computable)4 PsiFile (com.intellij.psi.PsiFile)4 File (java.io.File)4 Editor (com.intellij.openapi.editor.Editor)3