update copyrights
[fedora-idea.git] / platform / lang-api / src / com / intellij / util / ActionRunner.java
blob8364d64b92011a4163f44951cb44f0a8e1b9b7e4
1 /*
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;
22 import javax.swing.*;
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() {
29 public void run() {
30 ApplicationManager.getApplication().runWriteAction(new Runnable() {
31 public void run() {
32 try {
33 runnable.run();
35 catch (Exception e) {
36 exception[0] = e;
39 });
42 if (SwingUtilities.isEventDispatchThread()) {
43 swingRunnable.run();
45 else {
46 ApplicationManager.getApplication().invokeAndWait(swingRunnable, ModalityState.NON_MODAL);
48 Exception e = exception[0];
49 if (e != null) {
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 {
57 // runnable.run();
58 // return null;
59 // }
60 // });
61 //}
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() {
66 public void run() {
67 ApplicationManager.getApplication().runWriteAction(new Runnable() {
68 public void run() {
69 try {
70 result[0] = runnable.run();
72 catch (Exception e) {
73 exception[0] = e;
76 });
79 if (SwingUtilities.isEventDispatchThread()) {
80 swingRunnable.run();
82 else {
83 ApplicationManager.getApplication().invokeAndWait(swingRunnable, ModalityState.NON_MODAL);
85 Throwable e = exception[0];
86 if (e != null) {
87 if (e instanceof Exception) throw (Exception)e;
88 throw new Exception(e);
90 return result[0];
93 public static void runInsideReadAction(final InterruptibleRunnable runnable) throws Exception {
94 Throwable exception = ApplicationManager.getApplication().runReadAction(new Computable<Throwable>() {
95 public Throwable compute() {
96 try {
97 runnable.run();
98 return null;
100 catch (Throwable e) {
101 return 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;