Browse Source

Switch to Optional for active frame in MainFrame.

pull/96/head
Greg Holmes 9 years ago
parent
commit
55a2f4f54c
1 changed files with 23 additions and 25 deletions
  1. 23
    25
      ui_swing/src/com/dmdirc/addons/ui_swing/MainFrame.java

+ 23
- 25
ui_swing/src/com/dmdirc/addons/ui_swing/MainFrame.java View File

@@ -70,6 +70,7 @@ import net.miginfocom.swing.MigLayout;
70 70
 import net.engio.mbassy.listener.Handler;
71 71
 
72 72
 import static com.dmdirc.addons.ui_swing.SwingPreconditions.checkOnEDT;
73
+import static java.util.function.Predicate.isEqual;
73 74
 
74 75
 /**
75 76
  * The main application frame.
@@ -79,7 +80,7 @@ public class MainFrame extends JFrame implements WindowListener, ConfigChangeLis
79 80
     /** A version number for this class. */
80 81
     private static final long serialVersionUID = 9;
81 82
     /** Focus queue. */
82
-    private final QueuedLinkedHashSet<TextFrame> focusOrder;
83
+    private final QueuedLinkedHashSet<Optional<Window>> focusOrder;
83 84
     /** Apple instance. */
84 85
     private final Apple apple;
85 86
     /** Controller to use to end the program. */
@@ -105,7 +106,7 @@ public class MainFrame extends JFrame implements WindowListener, ConfigChangeLis
105 106
     /** The frame manager that's being used. */
106 107
     private FrameManager mainFrameManager;
107 108
     /** Active frame. */
108
-    private TextFrame activeFrame;
109
+    private Optional<Window> activeFrame;
109 110
     /** Panel holding frame. */
110 111
     private JPanel framePanel;
111 112
     /** Main panel. */
@@ -489,30 +490,31 @@ public class MainFrame extends JFrame implements WindowListener, ConfigChangeLis
489 490
         framePanel.setVisible(false);
490 491
         framePanel.removeAll();
491 492
 
492
-        activeFrame = (TextFrame) event.getWindow().get();
493
+        activeFrame = event.getWindow();
493 494
 
494
-        if (activeFrame == null) {
495
+        if (activeFrame.isPresent()) {
496
+            framePanel.add(((TextFrame) activeFrame.get()).getDisplayFrame(), "grow");
497
+            setTitle(activeFrame.get().getContainer().getTitle());
498
+        } else {
495 499
             framePanel.add(new JPanel(), "grow");
496 500
             setTitle(null);
497
-        } else {
498
-            framePanel.add(activeFrame.getDisplayFrame(), "grow");
499
-            setTitle(activeFrame.getContainer().getTitle());
500 501
         }
501 502
 
502 503
         framePanel.setVisible(true);
503 504
 
504
-        if (activeFrame != null) {
505
-            activeFrame.requestFocus();
506
-            activeFrame.requestFocusInWindow();
507
-            activeFrame.activateFrame();
505
+        if (activeFrame.isPresent()) {
506
+            final TextFrame textFrame = (TextFrame) activeFrame.get();
507
+            textFrame.requestFocus();
508
+            textFrame.requestFocusInWindow();
509
+            textFrame.activateFrame();
508 510
         }
509 511
 
510
-        swingEventBus.publish(new SwingWindowSelectedEvent(Optional.ofNullable((Window) activeFrame)));
512
+        swingEventBus.publish(new SwingWindowSelectedEvent(activeFrame));
511 513
     }
512 514
 
513 515
     @Handler
514 516
     public void doWindowAdded(final SwingWindowAddedEvent event) {
515
-        if (activeFrame == null) {
517
+        if (!activeFrame.isPresent()) {
516 518
             setActiveFrame(new SwingActiveWindowChangeRequestEvent(
517 519
                     Optional.of(event.getChildWindow())));
518 520
         }
@@ -520,10 +522,7 @@ public class MainFrame extends JFrame implements WindowListener, ConfigChangeLis
520 522
 
521 523
     @Handler
522 524
     public void doWindowDeleted(final SwingWindowDeletedEvent event) {
523
-        final TextFrame window = event.getChildWindow();
524
-        if (window == null) {
525
-            return; //Deleting a window that doesnt exist will just cause problems, stop
526
-        }
525
+        final Optional<Window> window = Optional.of(event.getChildWindow());
527 526
         focusOrder.remove(window);
528 527
         if (activeFrame.equals(window)) {
529 528
             activeFrame = null;
@@ -533,24 +532,23 @@ public class MainFrame extends JFrame implements WindowListener, ConfigChangeLis
533 532
             if (focusOrder.peek() == null) {
534 533
                 SwingUtilities.invokeLater(frameManager::scrollUp);
535 534
             } else {
536
-                setActiveFrame(new SwingActiveWindowChangeRequestEvent(
537
-                        Optional.of(focusOrder.peek())));
535
+                setActiveFrame(new SwingActiveWindowChangeRequestEvent(focusOrder.peek()));
538 536
             }
539 537
         }
540 538
     }
541 539
 
542 540
     @Handler
543 541
     public void titleChanged(final FrameTitleChangedEvent event) {
544
-        if (activeFrame != null && activeFrame.getContainer().equals(event.getContainer())) {
545
-            setTitle(event.getTitle());
546
-        }
542
+        activeFrame.map(Window::getContainer)
543
+                .filter(isEqual(event.getContainer()))
544
+                .ifPresent(c -> setTitle(event.getTitle()));
547 545
     }
548 546
 
549 547
     @Handler
550 548
     public void notificationSet(final NotificationSetEvent event) {
551
-        if (activeFrame != null && activeFrame.getContainer().equals(event.getWindow())) {
552
-            event.getWindow().clearNotification();
553
-        }
549
+        activeFrame.map(Window::getContainer)
550
+                .filter(isEqual(event.getWindow()))
551
+                .ifPresent(c -> event.getWindow().clearNotification());
554 552
     }
555 553
 
556 554
     @Override

Loading…
Cancel
Save