|
@@ -0,0 +1,74 @@
|
|
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.addons.ui_web2;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.addons.ui_web2.serialisers.BackBufferSerializer;
|
|
26
|
+import com.dmdirc.addons.ui_web2.serialisers.WindowModelSerialiser;
|
|
27
|
+import com.dmdirc.interfaces.WindowModel;
|
|
28
|
+import com.dmdirc.ui.WindowManager;
|
|
29
|
+import com.dmdirc.ui.messages.BackBuffer;
|
|
30
|
+
|
|
31
|
+import com.google.gson.Gson;
|
|
32
|
+import com.google.gson.GsonBuilder;
|
|
33
|
+
|
|
34
|
+import java.util.Collection;
|
|
35
|
+import java.util.Collections;
|
|
36
|
+
|
|
37
|
+import javax.inject.Inject;
|
|
38
|
+
|
|
39
|
+/**
|
|
40
|
+ * Handles compiling the initial burst of state that will be sent to new web clients.
|
|
41
|
+ */
|
|
42
|
+public class InitialStateProducer {
|
|
43
|
+
|
|
44
|
+ private final Gson serialiser;
|
|
45
|
+ private final WindowManager windowManager;
|
|
46
|
+
|
|
47
|
+ @Inject
|
|
48
|
+ public InitialStateProducer(final WindowManager windowManager) {
|
|
49
|
+ serialiser = new GsonBuilder()
|
|
50
|
+ .registerTypeHierarchyAdapter(WindowModel.class, new WindowModelSerialiser())
|
|
51
|
+ .registerTypeAdapter(BackBuffer.class, new BackBufferSerializer())
|
|
52
|
+ .create();
|
|
53
|
+ this.windowManager = windowManager;
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ public String getInitialState() {
|
|
57
|
+ final InitialState state = new InitialState(windowManager.getRootWindows());
|
|
58
|
+ return serialiser.toJson(state);
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ private static class InitialState {
|
|
62
|
+
|
|
63
|
+ private final Collection<WindowModel> windows;
|
|
64
|
+
|
|
65
|
+ private InitialState(final Collection<WindowModel> windows) {
|
|
66
|
+ this.windows = windows;
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ public Collection<WindowModel> getWindows() {
|
|
70
|
+ return Collections.unmodifiableCollection(windows);
|
|
71
|
+ }
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+}
|