]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/UpdateMessageBroker.java
Version 17, September 2014
[GpsPrune.git] / tim / prune / UpdateMessageBroker.java
1 package tim.prune;
2
3 /**
4  * Class responsible for distributing update information
5  * to all registered listeners
6  */
7 public abstract class UpdateMessageBroker
8 {
9         private static final int MAXIMUM_NUMBER_SUBSCRIBERS = 7;
10         /** Array of all subscribers */
11         private static DataSubscriber[] _subscribers = new DataSubscriber[MAXIMUM_NUMBER_SUBSCRIBERS];
12         /** Counter of the number of subscribers added so far */
13         private static int _subscriberNum = 0;
14         /** Enable/disabled flag */
15         private static boolean _enabled = true;
16
17
18         /**
19          * Add a data subscriber to the list
20          * @param inSub DataSubscriber to add
21          */
22         public static void addSubscriber(DataSubscriber inSub)
23         {
24                 _subscribers[_subscriberNum] = inSub;
25                 _subscriberNum++;
26         }
27
28         /**
29          * Enable or disable the messaging (to allow temporary disabling for multiple operations)
30          * @param inEnabled false to disable, true to enable again
31          */
32         public static void enableMessaging(boolean inEnabled)
33         {
34                 _enabled = inEnabled;
35         }
36
37         /**
38          * Send a message to all subscribers that
39          * the data has been updated
40          */
41         public static void informSubscribers()
42         {
43                 informSubscribers(DataSubscriber.ALL);
44         }
45
46
47         /**
48          * Send message to all subscribers
49          * @param inChange Change that occurred
50          */
51         public static void informSubscribers(byte inChange)
52         {
53                 // TODO: Launch separate thread so that whatever caused the inform can finish
54                 if (!_enabled) return;
55                 for (int i=0; i<_subscribers.length; i++)
56                 {
57                         if (_subscribers[i] != null)
58                         {
59                                 _subscribers[i].dataUpdated(inChange);
60                         }
61                 }
62         }
63
64         /**
65          * Send message to all subscribers
66          * @param inMessage message to display informing of action completed
67          */
68         public static void informSubscribers(String inMessage)
69         {
70                 if (!_enabled) return;
71                 for (int i=0; i<_subscribers.length; i++)
72                 {
73                         if (_subscribers[i] != null)
74                         {
75                                 _subscribers[i].actionCompleted(inMessage);
76                         }
77                 }
78         }
79 }