bump product version to 7.2.5.1
[LibreOffice.git] / toolkit / test / accessibility / EventQueue.java
blobb6f6c6415e7625da7eccb664eb15a412284ae766
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import java.util.LinkedList;
21 /** The event queue singleton dispatches events received from OpenOffice.org
22 applications in a thread separate from the AWB main thread.
24 The queue of event objects, LinkedList<Runnable> The queue object will
25 also serve as lock for the consumer/producer type synchronization.
27 class EventQueue
28 implements Runnable
30 private boolean mbVerbose = false;
31 private boolean mbHandleDisposingEventsSynchronous = true;
33 public synchronized static EventQueue Instance ()
35 if (maInstance == null)
36 maInstance = new EventQueue ();
37 return maInstance;
40 public void addEvent (Runnable aEvent)
42 synchronized (maMonitor)
44 if (mbVerbose)
45 System.out.println ("queueing regular event " + aEvent);
46 maRegularQueue.addLast (aEvent);
47 maMonitor.notify ();
52 public void addDisposingEvent (Runnable aEvent)
54 if (mbHandleDisposingEventsSynchronous)
55 aEvent.run ();
56 else
57 synchronized (maMonitor)
59 if (mbVerbose)
60 System.out.println ("queueing disposing event " + aEvent);
61 maDisposingQueue.addLast (aEvent);
62 maMonitor.notify ();
67 private EventQueue ()
69 maRegularQueue = new LinkedList<Runnable>();
70 maDisposingQueue = new LinkedList<Runnable>();
71 new Thread(this, "AWB.EventQueue").start();
75 /// This thread's main method: deliver all events
76 public void run()
78 // in an infinite loop, check for events to deliver, then
79 // wait on lock (which will be notified when new events arrive)
80 while( true )
82 Runnable aEvent = null;
85 synchronized (maMonitor)
87 if (maDisposingQueue.size() > 0)
89 aEvent = maDisposingQueue.removeFirst();
90 if (mbVerbose)
91 System.out.println ("delivering disposing event " + aEvent);
93 else if (maRegularQueue.size() > 0)
95 aEvent = maRegularQueue.removeFirst();
96 if (mbVerbose)
97 System.out.println ("delivering regular event " + aEvent);
99 else
100 aEvent = null;
102 if (aEvent != null)
106 aEvent.run();
108 catch( Throwable e )
110 System.out.println(
111 "caught exception during event delivery: " + e );
112 e.printStackTrace();
116 while( aEvent != null );
120 synchronized (maMonitor)
122 maMonitor.wait();
125 catch (Exception e)
127 // can't wait? odd!
128 System.err.println("Can't wait!");
129 e.printStackTrace();
134 private static EventQueue maInstance = null;
135 private final Object maMonitor = new Object();
136 private final LinkedList<Runnable> maRegularQueue;
137 private final LinkedList<Runnable> maDisposingQueue;