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
.lifecycle
;
18 import com
.intellij
.openapi
.application
.Application
;
19 import com
.intellij
.openapi
.application
.ApplicationManager
;
20 import com
.intellij
.openapi
.diagnostic
.Logger
;
21 import com
.intellij
.openapi
.progress
.EmptyProgressIndicator
;
22 import com
.intellij
.openapi
.progress
.ProgressIndicator
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import org
.jetbrains
.annotations
.NotNull
;
26 import java
.util
.concurrent
.ExecutorService
;
27 import java
.util
.concurrent
.Future
;
28 import java
.util
.concurrent
.ScheduledExecutorService
;
29 import java
.util
.concurrent
.TimeUnit
;
31 public class ControlledAlarmFactory
{
32 private static final Logger LOG
= Logger
.getInstance("#com.intellij.lifecycle.ControlledAlarmFactory");
33 private ControlledAlarmFactory() {
36 public static SlowlyClosingAlarm
createOnOwnThread(@NotNull final Project project
, @NotNull final String name
) {
37 return new SlowlyClosingAlarm(project
, name
);
40 public static SlowlyClosingAlarm
createOnSharedThread(@NotNull final Project project
, @NotNull final String name
,
41 final @NotNull ExecutorService executor
) {
42 return new SlowlyClosingAlarm(project
, name
, createExecutorWrapper(executor
), true);
45 public static ScheduledSlowlyClosingAlarm
createScheduledOnSharedThread(@NotNull final Project project
, @NotNull final String name
,
46 final @NotNull ScheduledExecutorService executor
) {
47 return new ScheduledSlowlyClosingAlarm(project
, name
, new MyScheduledExecutorServiceWrapper(executor
), true);
50 public static SlowlyClosingAlarm
createOnApplicationPooledThread(@NotNull final Project project
, @NotNull final String name
) {
51 return new SlowlyClosingAlarm(project
, name
, new MyApplicationPooledThreadExecutorWrapper(), true);
54 static MyExecutorWrapper
createExecutorWrapper(final ExecutorService executorService
) {
55 return new MyExecutorServiceWrapper(executorService
);
58 interface MyExecutorWrapper
{
59 Future
<?
> submit(final Runnable runnable
);
61 Future
<?
> schedule(final Runnable runnable
, final long delay
, final TimeUnit timeUnit
);
62 boolean supportsScheduling();
65 private static class MyExecutorServiceWrapper
implements MyExecutorWrapper
{
66 private final ExecutorService myExecutorService
;
68 public MyExecutorServiceWrapper(final ExecutorService executorService
) {
69 myExecutorService
= executorService
;
72 public Future
<?
> submit(Runnable runnable
) {
73 return myExecutorService
.submit(runnable
);
76 public void shutdown() {
77 myExecutorService
.shutdown();
80 public Future
<?
> schedule(Runnable runnable
, long delay
, TimeUnit timeUnit
) {
81 throw new UnsupportedOperationException();
84 public boolean supportsScheduling() {
89 private static class MyScheduledExecutorServiceWrapper
implements MyExecutorWrapper
{
90 private final ScheduledExecutorService myScheduledExecutorService
;
92 public MyScheduledExecutorServiceWrapper(final ScheduledExecutorService scheduledExecutorService
) {
93 myScheduledExecutorService
= scheduledExecutorService
;
96 public Future
<?
> submit(Runnable runnable
) {
97 return myScheduledExecutorService
.submit(runnable
);
100 public void shutdown() {
101 myScheduledExecutorService
.shutdown();
104 public Future
<?
> schedule(Runnable runnable
, long delay
, TimeUnit timeUnit
) {
105 return myScheduledExecutorService
.schedule(runnable
, delay
, timeUnit
);
108 public boolean supportsScheduling() {
113 private static class MyApplicationPooledThreadExecutorWrapper
implements MyExecutorWrapper
{
114 private Application myApplication
;
116 private MyApplicationPooledThreadExecutorWrapper() {
117 myApplication
= ApplicationManager
.getApplication();
120 public Future
<?
> submit(Runnable runnable
) {
121 return myApplication
.executeOnPooledThread(runnable
);
124 public void shutdown() {
128 public Future
<?
> schedule(final Runnable runnable
, long delay
, TimeUnit timeUnit
) {
129 throw new UnsupportedOperationException();
132 public boolean supportsScheduling() {
137 public static ProgressIndicator
createProgressIndicator(final AtomicSectionsAware atomicSectionsAware
) {
138 return new EmptyProgressIndicator() {
140 public boolean isCanceled() {
141 return atomicSectionsAware
.shouldExitAsap();
145 public void checkCanceled() {
146 atomicSectionsAware
.checkShouldExit();