Pull all interfaces into a single interface: Filter.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / gui / MainWindow.java
1 /*
2  * Sonitus - MainWindow.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sonitus.gui;
19
20 import java.awt.BorderLayout;
21 import java.awt.CardLayout;
22 import java.awt.Dimension;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.util.Map;
26 import javax.swing.BorderFactory;
27 import javax.swing.JFrame;
28 import javax.swing.JPanel;
29 import javax.swing.JTabbedPane;
30 import javax.swing.Timer;
31 import javax.swing.WindowConstants;
32
33 import net.pterodactylus.sonitus.data.Filter;
34 import net.pterodactylus.sonitus.data.Pipeline;
35 import net.pterodactylus.sonitus.gui.PipelinePanel.FilterSelectionListener;
36 import net.pterodactylus.sonitus.main.Version;
37
38 import com.google.common.base.Optional;
39 import com.google.common.collect.Maps;
40
41 /**
42  * Sonitus main window.
43  *
44  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45  */
46 public class MainWindow extends JFrame {
47
48         /** The pipeline to display. */
49         private final Pipeline pipeline;
50
51         /** The tabbed pane displaying all pipelines. */
52         private final JTabbedPane tabbedPane = new JTabbedPane();
53
54         /** The info panel card layout. */
55         private final CardLayout infoPanelCardLayout = new CardLayout();
56
57         /** The info panel. */
58         private final JPanel infoPanel = new JPanel(infoPanelCardLayout);
59
60         /** The mapping from filters to info panels. */
61         private final Map<Filter, FilterInfoPanel> filterInfoPanels = Maps.newHashMap();
62
63         /**
64          * Creates a new main window.
65          *
66          * @param pipeline
67          *              The pipeline to display
68          */
69         public MainWindow(Pipeline pipeline) {
70                 super(String.format("Sonitus %s", Version.version()));
71                 this.pipeline = pipeline;
72                 tabbedPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
73                 final JPanel pipelineInfoPanel = new JPanel(new BorderLayout(12, 12));
74                 PipelinePanel pipelinePanel = new PipelinePanel(pipeline);
75                 pipelinePanel.addFilterSelectionListener(new FilterSelectionListener() {
76
77                         @Override
78                         public void filterSelected(Filter filter) {
79                                 infoPanelCardLayout.show(infoPanel, filter.name());
80                         }
81                 });
82                 pipelineInfoPanel.add(pipelinePanel, BorderLayout.CENTER);
83                 pipelineInfoPanel.add(infoPanel, BorderLayout.EAST);
84                 tabbedPane.add("Pipeline", pipelineInfoPanel);
85                 getContentPane().add(tabbedPane, BorderLayout.CENTER);
86                 setSize(new Dimension(800, 450));
87
88                 /* create info panels for all filters. */
89                 for (Filter fliter : pipeline) {
90                         FilterInfoPanel filterInfoPanel = new FilterInfoPanel(fliter);
91                         infoPanel.add(filterInfoPanel, fliter.name());
92                         filterInfoPanels.put(fliter, filterInfoPanel);
93                 }
94
95                 Timer timer = new Timer(250, new ActionListener() {
96
97                         @Override
98                         public void actionPerformed(ActionEvent actionEvent) {
99                                 /* update all info panels. */
100                                 for (Filter filter : MainWindow.this.pipeline) {
101                                         FilterInfoPanel filterInfoPanel = filterInfoPanels.get(filter);
102                                         filterInfoPanel.input(MainWindow.this.pipeline.trafficCounter(filter).input());
103                                         filterInfoPanel.output(MainWindow.this.pipeline.trafficCounter(filter).output());
104                                         filterInfoPanel.format(Optional.of(filter.metadata().format()));
105                                 }
106                         }
107                 });
108                 timer.start();
109
110                 /* FIXME - shut everything down properly. */
111                 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
112         }
113
114 }