/
Application.java
452 lines (401 loc) · 16.1 KB
/
Application.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.application;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.components.ComponentManager;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.ThrowableComputable;
import com.intellij.util.ThrowableRunnable;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
/**
* Provides access to core application-wide functionality and methods for working with the IDE's
* thread model.
* <p>
* The thread model defines two main types of actions which can access the PSI and other
* IDE data structures: read actions (which do not modify the data) and write actions (which modify
* some data).
* <p>
* You can call methods requiring read access from the Swing event-dispatch thread without using
* {@link #runReadAction} method. If you need to invoke such methods from another thread you have to use
* {@link #runReadAction}. Multiple read actions can run at the same time without locking each other.
* <p>
* Write actions can be called only from the Swing thread using {@link #runWriteAction} method.
* If there are read actions running at this moment {@code runWriteAction} is blocked until they are completed.
* <p>
* See also <a href="https://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/general_threading_rules.html">General Threading Rules</a>.
*/
public interface Application extends ComponentManager {
/**
* Runs the specified read action. Can be called from any thread. The action is executed immediately
* if no write action is currently running, or blocked until the currently running write action completes.<p></p>
* <p>
* See also {@link ReadAction#run} for a more lambda-friendly version.
*
* @param action the action to run.
*/
void runReadAction(@NotNull Runnable action);
/**
* Runs the specified computation in a read action. Can be called from any thread. The action is executed
* immediately if no write action is currently running, or blocked until the currently running write action
* completes.<p></p>
* <p>
* See also {@link ReadAction#compute} for a more lambda-friendly version.
*
* @param computation the computation to perform.
* @return the result returned by the computation.
*/
<T> T runReadAction(@NotNull Computable<T> computation);
/**
* Runs the specified computation in a read action. Can be called from any thread. The action is executed
* immediately if no write action is currently running, or blocked until the currently running write action
* completes.<p></p>
* <p>
* See also {@link ReadAction#compute} for a more lambda-friendly version.
*
* @param computation the computation to perform.
* @return the result returned by the computation.
* @throws E re-frown from ThrowableComputable
*/
<T, E extends Throwable> T runReadAction(@NotNull ThrowableComputable<T, E> computation) throws E;
/**
* Runs the specified write action. Must be called from the Swing dispatch thread. The action is executed
* immediately if no read actions are currently running, or blocked until all read actions complete.<p></p>
* <p>
* See also {@link WriteAction#run} for a more lambda-friendly version.
*
* @param action the action to run
*/
void runWriteAction(@NotNull Runnable action);
/**
* Runs the specified computation in a write action. Must be called from the Swing dispatch thread.
* The action is executed immediately if no read actions or write actions are currently running,
* or blocked until all read actions and write actions complete.<p></p>
* <p>
* See also {@link WriteAction#compute} for a more lambda-friendly version.
*
* @param computation the computation to run
* @return the result returned by the computation.
*/
<T> T runWriteAction(@NotNull Computable<T> computation);
/**
* Runs the specified computation in a write action. Must be called from the Swing dispatch thread.
* The action is executed immediately if no read actions or write actions are currently running,
* or blocked until all read actions and write actions complete.<p></p>
* <p>
* See also {@link WriteAction#compute} for a more lambda-friendly version.
*
* @param computation the computation to run
* @return the result returned by the computation.
* @throws E re-frown from ThrowableComputable
*/
<T, E extends Throwable> T runWriteAction(@NotNull ThrowableComputable<T, E> computation) throws E;
/**
* Returns {@code true} if there is currently executing write action of the specified class.
*
* @param actionClass the class of the write action to return.
* @return {@code true} if the action is running, or {@code false} if no action of the specified class is currently executing.
*/
boolean hasWriteAction(@NotNull Class<?> actionClass);
/**
* Asserts whether the read access is allowed.
*/
void assertReadAccessAllowed();
/**
* Asserts whether the write access is allowed.
*/
void assertWriteAccessAllowed();
/**
* Asserts whether the method is being called from the event dispatch thread.
*/
void assertIsDispatchThread();
/**
* @deprecated Use {@link #addApplicationListener(ApplicationListener, Disposable)} instead
* Adds an {@link ApplicationListener}.
*
* @param listener the listener to add
*/
@Deprecated
void addApplicationListener(@NotNull ApplicationListener listener);
/**
* Adds an {@link ApplicationListener}.
*
* @param listener the listener to add
* @param parent the parent disposable which dispose will trigger this listener removal
*/
void addApplicationListener(@NotNull ApplicationListener listener, @NotNull Disposable parent);
/**
* @deprecated Instead, just call {@code Disposer.dispose(disposable);} on disposable you've passed to {@link #addApplicationListener(ApplicationListener, Disposable)}
* Removes an {@link ApplicationListener}.
*
* @param listener the listener to remove
*/
@Deprecated
void removeApplicationListener(@NotNull ApplicationListener listener);
/**
* Saves all open documents and projects.
*/
void saveAll();
/**
* Saves application settings.
*/
void saveSettings();
/**
* Exits the application, showing the exit confirmation prompt if it is enabled.
*/
void exit();
default void exit(boolean force, boolean exitConfirmed, boolean restart) {
exit();
}
/**
* Checks if the write access is currently allowed.
*
* @return {@code true} if the write access is currently allowed, {@code false} otherwise.
* @see #assertWriteAccessAllowed()
* @see #runWriteAction(Runnable)
*/
@Contract(pure = true)
boolean isWriteAccessAllowed();
/**
* Checks if the read access is currently allowed.
*
* @return {@code true} if the read access is currently allowed, {@code false} otherwise.
* @see #assertReadAccessAllowed()
* @see #runReadAction(Runnable)
*/
@Contract(pure = true)
boolean isReadAccessAllowed();
/**
* Checks if the current thread is the Swing dispatch thread.
*
* @return {@code true} if the current thread is the Swing dispatch thread, {@code false} otherwise.
*/
@Contract(pure = true)
boolean isDispatchThread();
/**
* @return a facade, which lets to call all those invokeLater() with a ActionCallback handle returned.
*/
@NotNull
ModalityInvokator getInvokator();
/**
* Causes {@code runnable.run()} to be executed asynchronously on the
* AWT event dispatching thread, with {@link ModalityState#defaultModalityState()} modality state. This will happen after all
* pending AWT events have been processed.<p/>
* <p>
* Please use this method instead of {@link javax.swing.SwingUtilities#invokeLater(Runnable)} or {@link com.intellij.util.ui.UIUtil} methods
* for the reasons described in {@link ModalityState} documentation.
*
* @param runnable the runnable to execute.
*/
void invokeLater(@NotNull Runnable runnable);
/**
* Causes {@code runnable.run()} to be executed asynchronously on the
* AWT event dispatching thread - unless the expiration condition is fulfilled.
* This will happen after all pending AWT events have been processed and in {@link ModalityState#defaultModalityState()} modality state
* (or a state with less modal dialogs open).<p/>
* <p>
* Please use this method instead of {@link javax.swing.SwingUtilities#invokeLater(Runnable)} or {@link com.intellij.util.ui.UIUtil} methods
* for the reasons described in {@link ModalityState} documentation.
*
* @param runnable the runnable to execute.
* @param expired condition to check before execution.
*/
void invokeLater(@NotNull Runnable runnable, @NotNull Condition<?> expired);
/**
* Causes {@code runnable.run()} to be executed asynchronously on the
* AWT event dispatching thread, when IDE is in the specified modality
* state (or a state with less modal dialogs open).
* <p>
* Please use this method instead of {@link javax.swing.SwingUtilities#invokeLater(Runnable)} or {@link com.intellij.util.ui.UIUtil} methods
* for the reasons described in {@link ModalityState} documentation.
*
* @param runnable the runnable to execute.
* @param state the state in which the runnable will be executed.
*/
void invokeLater(@NotNull Runnable runnable, @NotNull ModalityState state);
/**
* Causes {@code runnable.run()} to be executed asynchronously on the
* AWT event dispatching thread, when IDE is in the specified modality
* state(or a state with less modal dialogs open) - unless the expiration condition is fulfilled.
* This will happen after all pending AWT events have been processed.
* <p>
* Please use this method instead of {@link javax.swing.SwingUtilities#invokeLater(Runnable)} or {@link com.intellij.util.ui.UIUtil} methods
* for the reasons described in {@link ModalityState} documentation.
*
* @param runnable the runnable to execute.
* @param state the state in which the runnable will be executed.
* @param expired condition to check before execution.
*/
void invokeLater(@NotNull Runnable runnable, @NotNull ModalityState state, @NotNull Condition<?> expired);
/**
* <p>Causes {@code runnable.run()} to be executed synchronously on the
* AWT event dispatching thread, when the IDE is in the specified modality
* state (or a state with less modal dialogs open). This call blocks until all pending AWT events have been processed and (then)
* {@code runnable.run()} returns.</p>
*
* <p>If current thread is an event dispatch thread then {@code runnable.run()}
* is executed immediately regardless of the modality state.</p>
* <p>
* Please use this method instead of {@link javax.swing.SwingUtilities#invokeAndWait(Runnable)} or {@link com.intellij.util.ui.UIUtil} methods
* for the reasons described in {@link ModalityState} documentation.
*
* @param runnable the runnable to execute.
* @param modalityState the state in which the runnable will be executed.
* @throws ProcessCanceledException when the current thread is interrupted
*/
void invokeAndWait(@NotNull Runnable runnable, @NotNull ModalityState modalityState) throws ProcessCanceledException;
/**
* Same as {@link #invokeAndWait(Runnable, ModalityState)}, using {@link ModalityState#defaultModalityState()}.
*/
void invokeAndWait(@NotNull Runnable runnable) throws ProcessCanceledException;
/**
* Please use {@link ModalityState#current()} instead.
*
* @return the current modality state.
*/
@NotNull
ModalityState getCurrentModalityState();
/**
* Please use {@link ModalityState#stateForComponent(Component)} instead.
*
* @return the modality state for the dialog to which the specified component belongs.
*/
@NotNull
ModalityState getModalityStateForComponent(@NotNull Component c);
/**
* Please use {@link ModalityState#defaultModalityState()} instead.
*
* @return the modality state for the current thread.
*/
@NotNull
ModalityState getDefaultModalityState();
/**
* Please use {@link ModalityState#NON_MODAL} instead.
*
* @return the modality state for no modal dialogs.
*/
@NotNull
ModalityState getNoneModalityState();
/**
* Please use {@link ModalityState#any()} instead, and only if you absolutely must, after carefully reading its documentation.
*
* @return modality state which is always applicable
*/
@NotNull
ModalityState getAnyModalityState();
/**
* Returns the time of IDE start, in milliseconds since midnight, January 1, 1970 UTC.
*
* @return the IDE's start time.
*/
long getStartTime();
/**
* Returns the time in milliseconds during which IDE received no input events.
*
* @return the idle time of IDE.
*/
long getIdleTime();
/**
* Checks if IDE is currently running unit tests. No UI should be shown when unit
* tests are being executed.
*
* @return {@code true} if IDE is running unit tests, {@code false} otherwise
*/
boolean isUnitTestMode();
/**
* Checks if IDE is running as a command line applet or in unit test mode.
* No UI should be shown when IDE is running in this mode.
*
* @return {@code true} if IDE is running in UI-less mode, {@code false} otherwise
*/
boolean isHeadlessEnvironment();
/**
* Checks if IDE is running as a command line applet or in unit test mode.
* UI can be shown (e.g. diff frame)
*
* @return {@code true} if IDE is running in command line mode, {@code false} otherwise
*/
boolean isCommandLine();
/**
* Requests pooled thread to execute the action.
* <p>
* This pool is an
* <ul>
* <li>Unbounded.</li>
* <li>Application-wide, always active, non-shutdownable singleton.</li>
* </ul>
* You can use this pool for long-running and/or IO-bound tasks.
*
* @param action to be executed
* @return future result
*/
@NotNull
Future<?> executeOnPooledThread(@NotNull Runnable action);
/**
* Requests pooled thread to execute the action.
* <p>
* This pool is an
* <ul>
* <li>Unbounded.</li>
* <li>Application-wide, always active, non-shutdownable singleton.</li>
* </ul>
* You can use this pool for long-running and/or IO-bound tasks.
*
* @param action to be executed
* @return future result
*/
@NotNull
<T> Future<T> executeOnPooledThread(@NotNull Callable<T> action);
/**
* @deprecated Use {@link #isDisposed()}.
*/
@Deprecated
default boolean isDisposeInProgress() {
return isDisposed();
}
/**
* Checks if IDE is capable of restarting itself on the current platform and with the current execution mode.
*
* @return {@code true} if IDE can restart itself, {@code false} otherwise.
*/
boolean isRestartCapable();
/**
* Exits and restarts IDE. If the current platform is not restart capable, only exits.
*/
void restart();
/**
* Checks if the application is active.
*
* @return {@code true} if one of application windows is focused, {@code false} otherwise
*/
boolean isActive();
/**
* @deprecated Use {@link #runReadAction(Runnable)} instead
*/
@NotNull
@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "2020.3")
AccessToken acquireReadActionLock();
/**
* @deprecated use {@link #runWriteAction}, {@link WriteAction#run(ThrowableRunnable)} or {@link WriteAction#compute(ThrowableComputable)} instead
*/
@NotNull
@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "2020.3")
AccessToken acquireWriteActionLock(@NotNull Class<?> marker);
/**
* Checks if IDE is running in <a href="http://www.jetbrains.org/intellij/sdk/docs/reference_guide/internal_actions/enabling_internal.html">Internal Mode</a> to enable additional features for plugin development.
*/
boolean isInternal();
boolean isEAP();
/** DO NOT USE */
@ApiStatus.Internal
default boolean isInImpatientReader() {
return false;
}
}