View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
3    *
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
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
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.
15   */
16  package com.atlassian.theplugin.idea.util;
17  
18  import com.atlassian.theplugin.commons.UiTask;
19  import com.atlassian.theplugin.commons.UiTaskExecutor;
20  import com.atlassian.theplugin.commons.util.LoggerImpl;
21  import com.atlassian.theplugin.idea.ui.DialogWithDetails;
22  import com.intellij.openapi.application.ApplicationManager;
23  import com.intellij.openapi.application.ModalityState;
24  
25  public class IdeaUiTaskExecutor implements UiTaskExecutor {
26  
27  
28  	public void execute(final UiTask uiTask) {
29  		final ModalityState modalityState = ModalityState.stateForComponent(uiTask.getComponent());
30  
31  		ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
32  			public void run() {
33  				try {
34  					uiTask.run();
35  				} catch (final Exception e) {
36  					LoggerImpl.getInstance().warn(e);
37  					ApplicationManager.getApplication().invokeLater(new Runnable() {
38  						public void run() {
39  							uiTask.onError();
40  							if (uiTask.getComponent() != null && uiTask.getComponent().isShowing()) {
41  								DialogWithDetails.showExceptionDialog(uiTask.getComponent(),
42  										"Error while " + uiTask.getLastAction(), e);
43  							}
44  						}
45  					}, modalityState);
46  					return;
47  				}
48  
49  				ApplicationManager.getApplication().invokeLater(new Runnable() {
50  					public void run() {
51  						try {
52  							uiTask.onSuccess();
53  						} catch (Exception e) {
54  							LoggerImpl.getInstance().warn(e);
55  							if (uiTask.getComponent() != null && uiTask.getComponent().isShowing()) {
56  								DialogWithDetails.showExceptionDialog(uiTask.getComponent(),
57  										"Error while " + uiTask.getLastAction(), e);
58  							}
59  						}
60  					}
61  				}, modalityState);
62  			}
63  		});
64  	}
65  }