|
@@ -1,100 +0,0 @@
|
1
|
|
-/*
|
2
|
|
- * Copyright (c) 2006-2015 DMDirc Developers
|
3
|
|
- *
|
4
|
|
- * Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
|
- * of this software and associated documentation files (the "Software"), to deal
|
6
|
|
- * in the Software without restriction, including without limitation the rights
|
7
|
|
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
|
- * copies of the Software, and to permit persons to whom the Software is
|
9
|
|
- * furnished to do so, subject to the following conditions:
|
10
|
|
- *
|
11
|
|
- * The above copyright notice and this permission notice shall be included in
|
12
|
|
- * all copies or substantial portions of the Software.
|
13
|
|
- *
|
14
|
|
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
|
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
|
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
|
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
|
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
|
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
20
|
|
- * SOFTWARE.
|
21
|
|
- */
|
22
|
|
-
|
23
|
|
-package com.dmdirc.util;
|
24
|
|
-
|
25
|
|
-import com.dmdirc.DMDircMBassador;
|
26
|
|
-import com.dmdirc.events.DMDircEvent;
|
27
|
|
-
|
28
|
|
-import net.engio.mbassy.listener.Handler;
|
29
|
|
-
|
30
|
|
-import static com.google.common.base.Preconditions.checkNotNull;
|
31
|
|
-
|
32
|
|
-/**
|
33
|
|
- * Utility for creating and managing instances of {@link DMDircMBassador} that are slaved to a
|
34
|
|
- * parent bus.
|
35
|
|
- * <p>
|
36
|
|
- * Any events sent on the child bus will be propagated up to the parent bus.
|
37
|
|
- */
|
38
|
|
-public class ChildEventBusManager {
|
39
|
|
-
|
40
|
|
- private final DMDircMBassador parent;
|
41
|
|
- private final DMDircMBassador child;
|
42
|
|
- private final EventPropagator propagator;
|
43
|
|
-
|
44
|
|
- public ChildEventBusManager(final DMDircMBassador parent) {
|
45
|
|
- this.parent = checkNotNull(parent);
|
46
|
|
- this.child = new DMDircMBassador();
|
47
|
|
- this.propagator = new EventPropagator();
|
48
|
|
- }
|
49
|
|
-
|
50
|
|
- /**
|
51
|
|
- * Connects the child event bus to the parent.
|
52
|
|
- * <p>
|
53
|
|
- * After this method is called, all events sent to the child bus will be passed to the parent.
|
54
|
|
- */
|
55
|
|
- public void connect() {
|
56
|
|
- child.subscribe(propagator);
|
57
|
|
- }
|
58
|
|
-
|
59
|
|
- /**
|
60
|
|
- * Disconnects the child event bus from the parent.
|
61
|
|
- * <p>
|
62
|
|
- * The child will be disconnected asynchronously, to allow any pending async events to be
|
63
|
|
- * dispatched. After the bus is disconnected, no future events will be passed to the parent.
|
64
|
|
- */
|
65
|
|
- public void disconnect() {
|
66
|
|
- child.publishAsync(new ChildEventBusDisconnectingEvent());
|
67
|
|
- }
|
68
|
|
-
|
69
|
|
- /**
|
70
|
|
- * Gets the child bus.
|
71
|
|
- *
|
72
|
|
- * @return The child bus belonging to this manager.
|
73
|
|
- */
|
74
|
|
- public DMDircMBassador getChildBus() {
|
75
|
|
- return child;
|
76
|
|
- }
|
77
|
|
-
|
78
|
|
- private class EventPropagator {
|
79
|
|
-
|
80
|
|
- @Handler
|
81
|
|
- public void handleEvent(final DMDircEvent event) {
|
82
|
|
- if (!(event instanceof ChildEventBusDisconnectingEvent)) {
|
83
|
|
- // Don't propagate our private event
|
84
|
|
- parent.publish(event);
|
85
|
|
- }
|
86
|
|
- }
|
87
|
|
-
|
88
|
|
- // Allow all other handlers on the child bus to process this first
|
89
|
|
- @Handler(priority = EventUtils.PRIORITY_LOWEST)
|
90
|
|
- public void handleDisconnect(final ChildEventBusDisconnectingEvent event) {
|
91
|
|
- child.unsubscribe(propagator);
|
92
|
|
- }
|
93
|
|
-
|
94
|
|
- }
|
95
|
|
-
|
96
|
|
- private static class ChildEventBusDisconnectingEvent extends DMDircEvent {
|
97
|
|
-
|
98
|
|
- }
|
99
|
|
-
|
100
|
|
-}
|