Procházet zdrojové kódy

Merge pull request #651 from csmith/master

Assign IDs to windows.
pull/652/head
Greg Holmes před 8 roky
rodič
revize
15f3b92c59

+ 18
- 0
src/com/dmdirc/FrameContainer.java Zobrazit soubor

@@ -82,6 +82,8 @@ public class FrameContainer implements WindowModel {
82 82
     private Optional<InputModel> inputModel = Optional.empty();
83 83
     /** The connection associated with this model. */
84 84
     private Optional<Connection> connection = Optional.empty();
85
+    /** The ID for this window. */
86
+    @Nullable private String id = null;
85 87
 
86 88
     /**
87 89
      * Instantiate new frame container.
@@ -115,6 +117,22 @@ public class FrameContainer implements WindowModel {
115 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 136
     @Override
119 137
     public String getIcon() {
120 138
         return icon;

+ 15
- 0
src/com/dmdirc/interfaces/WindowModel.java Zobrazit soubor

@@ -38,6 +38,21 @@ import java.util.Set;
38 38
  */
39 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 56
     String getIcon();
42 57
 
43 58
     String getName();

+ 29
- 2
src/com/dmdirc/ui/WindowManager.java Zobrazit soubor

@@ -39,6 +39,7 @@ import java.util.HashMap;
39 39
 import java.util.Map;
40 40
 import java.util.Optional;
41 41
 import java.util.concurrent.CopyOnWriteArrayList;
42
+import java.util.concurrent.atomic.AtomicLong;
42 43
 
43 44
 import javax.inject.Inject;
44 45
 import javax.inject.Singleton;
@@ -60,8 +61,12 @@ public class WindowManager {
60 61
     private final Map<WindowModel, WindowModel> parents = new HashMap<>();
61 62
     /** Mapping of parents to their children. */
62 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 66
     /** A list of frame listeners. */
64 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 72
      * Creates a new instance of {@link WindowManager}.
@@ -182,7 +187,7 @@ public class WindowManager {
182 187
         checkArgument(!rootWindows.contains(window));
183 188
 
184 189
         rootWindows.add(window);
185
-
190
+        assignId(window);
186 191
         fireAddWindow(window, focus);
187 192
     }
188 193
 
@@ -220,7 +225,7 @@ public class WindowManager {
220 225
 
221 226
         parents.put(child, parent);
222 227
         children.put(parent, child);
223
-
228
+        assignId(child);
224 229
         fireAddWindow(parent, child, focus);
225 230
     }
226 231
 
@@ -256,6 +261,7 @@ public class WindowManager {
256 261
 
257 262
         children.get(window).forEach(WindowModel::close);
258 263
         children.removeAll(window);
264
+        windowsById.remove(window.getId());
259 265
 
260 266
         if (rootWindows.contains(window)) {
261 267
             fireDeleteWindow(window);
@@ -332,6 +338,27 @@ public class WindowManager {
332 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 363
      * Fires the addWindow(Window) callback.
337 364
      *

+ 36
- 0
test/com/dmdirc/ui/WindowManagerTest.java Zobrazit soubor

@@ -33,11 +33,15 @@ import java.util.Optional;
33 33
 import org.junit.Before;
34 34
 import org.junit.Test;
35 35
 import org.junit.runner.RunWith;
36
+import org.mockito.ArgumentCaptor;
37
+import org.mockito.Captor;
36 38
 import org.mockito.Mock;
37 39
 import org.mockito.runners.MockitoJUnitRunner;
38 40
 
39 41
 import static org.junit.Assert.assertEquals;
42
+import static org.junit.Assert.assertNotEquals;
40 43
 import static org.junit.Assert.assertNull;
44
+import static org.junit.Assert.assertSame;
41 45
 import static org.junit.Assert.assertTrue;
42 46
 import static org.mockito.Matchers.anyBoolean;
43 47
 import static org.mockito.Matchers.anyObject;
@@ -56,6 +60,7 @@ public class WindowManagerTest {
56 60
     @Mock private WindowModel child;
57 61
     @Mock private WindowModel grandchild;
58 62
     @Mock private DMDircMBassador eventBus;
63
+    @Captor private ArgumentCaptor<String> idCaptor;
59 64
     private WindowManager manager;
60 65
 
61 66
     @Before
@@ -269,4 +274,35 @@ public class WindowManagerTest {
269 274
 
270 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
 }

Načítá se…
Zrušit
Uložit