2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.util
;
18 import com
.intellij
.openapi
.application
.ApplicationManager
;
19 import com
.intellij
.openapi
.application
.ModalityState
;
20 import com
.intellij
.openapi
.util
.Computable
;
25 public abstract class ActionRunner
{
26 public static void runInsideWriteAction(final InterruptibleRunnable runnable
) throws Exception
{
27 final Exception
[] exception
= new Exception
[1];
28 Runnable swingRunnable
= new Runnable() {
30 ApplicationManager
.getApplication().runWriteAction(new Runnable() {
42 if (SwingUtilities
.isEventDispatchThread()) {
46 ApplicationManager
.getApplication().invokeAndWait(swingRunnable
, ModalityState
.NON_MODAL
);
48 Exception e
= exception
[0];
50 if (e
instanceof RuntimeException
) throw (RuntimeException
)e
;
51 throw new Exception(e
);
54 //public static <E extends Throwable> void runInsideWriteAction(final InterruptibleRunnable<E> runnable) throws E {
55 // runInsideWriteAction(new InterruptibleRunnableWithResult<E,Object>(){
56 // public Object run() throws E {
62 public static <T
> T
runInsideWriteAction(final InterruptibleRunnableWithResult
<T
> runnable
) throws Exception
{
63 final Throwable
[] exception
= new Throwable
[]{null};
64 final T
[] result
= (T
[])new Object
[1];
65 Runnable swingRunnable
= new Runnable() {
67 ApplicationManager
.getApplication().runWriteAction(new Runnable() {
70 result
[0] = runnable
.run();
79 if (SwingUtilities
.isEventDispatchThread()) {
83 ApplicationManager
.getApplication().invokeAndWait(swingRunnable
, ModalityState
.NON_MODAL
);
85 Throwable e
= exception
[0];
87 if (e
instanceof Exception
) throw (Exception
)e
;
88 throw new Exception(e
);
93 public static void runInsideReadAction(final InterruptibleRunnable runnable
) throws Exception
{
94 Throwable exception
= ApplicationManager
.getApplication().runReadAction(new Computable
<Throwable
>() {
95 public Throwable
compute() {
100 catch (Throwable e
) {
105 if (exception
!= null) {
106 if (exception
instanceof RuntimeException
) {
107 throw (RuntimeException
)exception
;
109 throw new Exception(exception
);
113 public static interface InterruptibleRunnable
{
114 void run() throws Exception
;
116 public static interface InterruptibleRunnableWithResult
<T
> {
117 T
run() throws Exception
;