Pull all interfaces into a single interface: Filter.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / gui / MainWindow.java
index 7b515fb..85a49fb 100644 (file)
 package net.pterodactylus.sonitus.gui;
 
 import java.awt.BorderLayout;
+import java.awt.CardLayout;
 import java.awt.Dimension;
-import java.util.List;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.Map;
+import javax.swing.BorderFactory;
 import javax.swing.JFrame;
+import javax.swing.JPanel;
 import javax.swing.JTabbedPane;
+import javax.swing.Timer;
 import javax.swing.WindowConstants;
 
-import net.pterodactylus.sonitus.data.Controlled;
-import net.pterodactylus.sonitus.data.Controller;
+import net.pterodactylus.sonitus.data.Filter;
+import net.pterodactylus.sonitus.data.Pipeline;
+import net.pterodactylus.sonitus.gui.PipelinePanel.FilterSelectionListener;
 import net.pterodactylus.sonitus.main.Version;
 
+import com.google.common.base.Optional;
+import com.google.common.collect.Maps;
+
 /**
  * Sonitus main window.
  *
@@ -35,36 +45,70 @@ import net.pterodactylus.sonitus.main.Version;
  */
 public class MainWindow extends JFrame {
 
-       /** The tabbed pane displaying all controlled components. */
+       /** The pipeline to display. */
+       private final Pipeline pipeline;
+
+       /** The tabbed pane displaying all pipelines. */
        private final JTabbedPane tabbedPane = new JTabbedPane();
 
-       /** Creates a new main window. */
-       public MainWindow() {
-               super(String.format("Sonitus %s", Version.version()));
-               getContentPane().add(tabbedPane, BorderLayout.CENTER);
-               setSize(new Dimension(800, 450));
+       /** The info panel card layout. */
+       private final CardLayout infoPanelCardLayout = new CardLayout();
 
-               /* FIXME - shut everything down properly. */
-               setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
-       }
+       /** The info panel. */
+       private final JPanel infoPanel = new JPanel(infoPanelCardLayout);
 
-       //
-       // ACTIONS
-       //
+       /** The mapping from filters to info panels. */
+       private final Map<Filter, FilterInfoPanel> filterInfoPanels = Maps.newHashMap();
 
        /**
-        * Adds the given controlled to this main window.
+        * Creates a new main window.
         *
-        * @param controlled
-        *              The controlled to add
+        * @param pipeline
+        *              The pipeline to display
         */
-       public void addControllers(Controlled controlled) {
-               List<Controller> controllers = controlled.controllers();
-               if (controllers.isEmpty()) {
-                       return;
+       public MainWindow(Pipeline pipeline) {
+               super(String.format("Sonitus %s", Version.version()));
+               this.pipeline = pipeline;
+               tabbedPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
+               final JPanel pipelineInfoPanel = new JPanel(new BorderLayout(12, 12));
+               PipelinePanel pipelinePanel = new PipelinePanel(pipeline);
+               pipelinePanel.addFilterSelectionListener(new FilterSelectionListener() {
+
+                       @Override
+                       public void filterSelected(Filter filter) {
+                               infoPanelCardLayout.show(infoPanel, filter.name());
+                       }
+               });
+               pipelineInfoPanel.add(pipelinePanel, BorderLayout.CENTER);
+               pipelineInfoPanel.add(infoPanel, BorderLayout.EAST);
+               tabbedPane.add("Pipeline", pipelineInfoPanel);
+               getContentPane().add(tabbedPane, BorderLayout.CENTER);
+               setSize(new Dimension(800, 450));
+
+               /* create info panels for all filters. */
+               for (Filter fliter : pipeline) {
+                       FilterInfoPanel filterInfoPanel = new FilterInfoPanel(fliter);
+                       infoPanel.add(filterInfoPanel, fliter.name());
+                       filterInfoPanels.put(fliter, filterInfoPanel);
                }
-               ControlledPane controlledPane = new ControlledPane(controlled);
-               tabbedPane.addTab(controlled.toString(), controlledPane);
+
+               Timer timer = new Timer(250, new ActionListener() {
+
+                       @Override
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               /* update all info panels. */
+                               for (Filter filter : MainWindow.this.pipeline) {
+                                       FilterInfoPanel filterInfoPanel = filterInfoPanels.get(filter);
+                                       filterInfoPanel.input(MainWindow.this.pipeline.trafficCounter(filter).input());
+                                       filterInfoPanel.output(MainWindow.this.pipeline.trafficCounter(filter).output());
+                                       filterInfoPanel.format(Optional.of(filter.metadata().format()));
+                               }
+                       }
+               });
+               timer.start();
+
+               /* FIXME - shut everything down properly. */
+               setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
 
 }