01 /*
02 * Copyright 2009-2013 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
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
17 package org.codehaus.griffon.runtime.core;
18
19 import griffon.core.UIThreadManager;
20
21 import java.util.concurrent.BlockingQueue;
22 import java.util.concurrent.LinkedBlockingQueue;
23
24 /**
25 * @author Andres Almiray
26 */
27 public class DefaultEventRouter extends AbstractEventRouter {
28 private final BlockingQueue<Runnable> deferredEvents = new LinkedBlockingQueue<Runnable>();
29 protected static int count = 1;
30
31 public DefaultEventRouter() {
32 Thread t = new Thread(new Runnable() {
33 public void run() {
34 while (true) {
35 try {
36 deferredEvents.take().run();
37 } catch (InterruptedException e) {
38 // ignore ?
39 }
40 }
41 }
42 }, getClass().getSimpleName() + "-" + identifier());
43 t.setDaemon(true);
44 t.start();
45 }
46
47 private static int identifier() {
48 synchronized (LOCK) {
49 return count++;
50 }
51 }
52
53 protected void doPublishOutsideUI(Runnable publisher) {
54 UIThreadManager.getInstance().executeOutside(publisher);
55 }
56
57 protected void doPublishAsync(Runnable publisher) {
58 deferredEvents.offer(publisher);
59 }
60 }
|