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");
}
}
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;
}
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();
}
});
}
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");
}
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());
}
});
}
Aggregations