Browse Source

Assign IDs to windows.

This allows windows to be uniquely identified and shared with
clients that are running outside of the DMDirc process (e.g.
a web UI, or a remote instance).
pull/651/head
Chris Smith 8 years ago
parent
commit
869713b4db

+ 18
- 0
src/com/dmdirc/FrameContainer.java View File

82
     private Optional<InputModel> inputModel = Optional.empty();
82
     private Optional<InputModel> inputModel = Optional.empty();
83
     /** The connection associated with this model. */
83
     /** The connection associated with this model. */
84
     private Optional<Connection> connection = Optional.empty();
84
     private Optional<Connection> connection = Optional.empty();
85
+    /** The ID for this window. */
86
+    @Nullable private String id = null;
85
 
87
 
86
     /**
88
     /**
87
      * Instantiate new frame container.
89
      * Instantiate new frame container.
115
         backBuffer.startAddingEvents();
117
         backBuffer.startAddingEvents();
116
     }
118
     }
117
 
119
 
120
+    @Override
121
+    public String getId() {
122
+        if (this.id == null) {
123
+            throw new IllegalStateException("ID has not been set");
124
+        }
125
+        return id;
126
+    }
127
+
128
+    @Override
129
+    public void setId(@Nullable final String id) {
130
+        if (this.id != null) {
131
+            throw new IllegalStateException("ID already set");
132
+        }
133
+        this.id = id;
134
+    }
135
+
118
     @Override
136
     @Override
119
     public String getIcon() {
137
     public String getIcon() {
120
         return icon;
138
         return icon;

+ 15
- 0
src/com/dmdirc/interfaces/WindowModel.java View File

38
  */
38
  */
39
 public interface WindowModel {
39
 public interface WindowModel {
40
 
40
 
41
+    /**
42
+     * Gets a unique ID that identifies this window.
43
+     *
44
+     * @return This window's unique ID.
45
+     */
46
+    String getId();
47
+
48
+    /**
49
+     * Sets the ID for this window. This may only be called once; attempting to overwrite a
50
+     * previous ID will throw an exception.
51
+     *
52
+     * @param id The new ID for this window.
53
+     */
54
+    void setId(String id);
55
+
41
     String getIcon();
56
     String getIcon();
42
 
57
 
43
     String getName();
58
     String getName();

+ 29
- 2
src/com/dmdirc/ui/WindowManager.java View File

39
 import java.util.Map;
39
 import java.util.Map;
40
 import java.util.Optional;
40
 import java.util.Optional;
41
 import java.util.concurrent.CopyOnWriteArrayList;
41
 import java.util.concurrent.CopyOnWriteArrayList;
42
+import java.util.concurrent.atomic.AtomicLong;
42
 
43
 
43
 import javax.inject.Inject;
44
 import javax.inject.Inject;
44
 import javax.inject.Singleton;
45
 import javax.inject.Singleton;
60
     private final Map<WindowModel, WindowModel> parents = new HashMap<>();
61
     private final Map<WindowModel, WindowModel> parents = new HashMap<>();
61
     /** Mapping of parents to their children. */
62
     /** Mapping of parents to their children. */
62
     private final Multimap<WindowModel, WindowModel> children = ArrayListMultimap.create();
63
     private final Multimap<WindowModel, WindowModel> children = ArrayListMultimap.create();
64
+    /** Mapping of IDs to windows. */
65
+    private final Map<String, WindowModel> windowsById = new HashMap<>();
63
     /** A list of frame listeners. */
66
     /** A list of frame listeners. */
64
     private final ListenerList listeners = new ListenerList();
67
     private final ListenerList listeners = new ListenerList();
68
+    /** Counter to use for ID assignments. */
69
+    private final AtomicLong nextId = new AtomicLong(0L);
65
 
70
 
66
     /**
71
     /**
67
      * Creates a new instance of {@link WindowManager}.
72
      * Creates a new instance of {@link WindowManager}.
182
         checkArgument(!rootWindows.contains(window));
187
         checkArgument(!rootWindows.contains(window));
183
 
188
 
184
         rootWindows.add(window);
189
         rootWindows.add(window);
185
-
190
+        assignId(window);
186
         fireAddWindow(window, focus);
191
         fireAddWindow(window, focus);
187
     }
192
     }
188
 
193
 
220
 
225
 
221
         parents.put(child, parent);
226
         parents.put(child, parent);
222
         children.put(parent, child);
227
         children.put(parent, child);
223
-
228
+        assignId(child);
224
         fireAddWindow(parent, child, focus);
229
         fireAddWindow(parent, child, focus);
225
     }
230
     }
226
 
231
 
256
 
261
 
257
         children.get(window).forEach(WindowModel::close);
262
         children.get(window).forEach(WindowModel::close);
258
         children.removeAll(window);
263
         children.removeAll(window);
264
+        windowsById.remove(window.getId());
259
 
265
 
260
         if (rootWindows.contains(window)) {
266
         if (rootWindows.contains(window)) {
261
             fireDeleteWindow(window);
267
             fireDeleteWindow(window);
332
         return Collections.unmodifiableCollection(rootWindows);
338
         return Collections.unmodifiableCollection(rootWindows);
333
     }
339
     }
334
 
340
 
341
+    /**
342
+     * Retrieves the window with the specified ID.
343
+     *
344
+     * @param id The ID of the window to retrieve
345
+     * @return The window with the given ID, if it exists.
346
+     */
347
+    public Optional<WindowModel> getWindowById(final String id) {
348
+        return Optional.ofNullable(windowsById.get(id));
349
+    }
350
+
351
+    /**
352
+     * Assigns a unique ID to the given window.
353
+     *
354
+     * @param window The window to assign an ID to.
355
+     */
356
+    private void assignId(final WindowModel window) {
357
+        final String id = "WINDOW/" + nextId.getAndIncrement();
358
+        window.setId(id);
359
+        windowsById.put(id, window);
360
+    }
361
+
335
     /**
362
     /**
336
      * Fires the addWindow(Window) callback.
363
      * Fires the addWindow(Window) callback.
337
      *
364
      *

+ 36
- 0
test/com/dmdirc/ui/WindowManagerTest.java View File

33
 import org.junit.Before;
33
 import org.junit.Before;
34
 import org.junit.Test;
34
 import org.junit.Test;
35
 import org.junit.runner.RunWith;
35
 import org.junit.runner.RunWith;
36
+import org.mockito.ArgumentCaptor;
37
+import org.mockito.Captor;
36
 import org.mockito.Mock;
38
 import org.mockito.Mock;
37
 import org.mockito.runners.MockitoJUnitRunner;
39
 import org.mockito.runners.MockitoJUnitRunner;
38
 
40
 
39
 import static org.junit.Assert.assertEquals;
41
 import static org.junit.Assert.assertEquals;
42
+import static org.junit.Assert.assertNotEquals;
40
 import static org.junit.Assert.assertNull;
43
 import static org.junit.Assert.assertNull;
44
+import static org.junit.Assert.assertSame;
41
 import static org.junit.Assert.assertTrue;
45
 import static org.junit.Assert.assertTrue;
42
 import static org.mockito.Matchers.anyBoolean;
46
 import static org.mockito.Matchers.anyBoolean;
43
 import static org.mockito.Matchers.anyObject;
47
 import static org.mockito.Matchers.anyObject;
56
     @Mock private WindowModel child;
60
     @Mock private WindowModel child;
57
     @Mock private WindowModel grandchild;
61
     @Mock private WindowModel grandchild;
58
     @Mock private DMDircMBassador eventBus;
62
     @Mock private DMDircMBassador eventBus;
63
+    @Captor private ArgumentCaptor<String> idCaptor;
59
     private WindowManager manager;
64
     private WindowManager manager;
60
 
65
 
61
     @Before
66
     @Before
269
 
274
 
270
         assertNull(manager.findCustomWindow(customContainer, "test"));
275
         assertNull(manager.findCustomWindow(customContainer, "test"));
271
     }
276
     }
277
+
278
+    @Test
279
+    public void testAssignId() {
280
+        manager.addWindow(container);
281
+        manager.addWindow(container, child);
282
+
283
+        verify(container).setId(idCaptor.capture());
284
+        final String parentId = idCaptor.getValue();
285
+
286
+        verify(child).setId(idCaptor.capture());
287
+        final String childId = idCaptor.getValue();
288
+
289
+        assertNotEquals(parentId, childId);
290
+    }
291
+
292
+    @Test
293
+    public void testGetById() {
294
+        manager.addWindow(container);
295
+        manager.addWindow(container, child);
296
+
297
+        verify(container).setId(idCaptor.capture());
298
+        final String parentId = idCaptor.getValue();
299
+
300
+        verify(child).setId(idCaptor.capture());
301
+        final String childId = idCaptor.getValue();
302
+
303
+        assertSame(container, manager.getWindowById(parentId).get());
304
+        assertSame(child, manager.getWindowById(childId).get());
305
+        assertEquals(Optional.empty(), manager.getWindowById("invalid"));
306
+    }
307
+
272
 }
308
 }

Loading…
Cancel
Save