2 * Sonitus - MainWindow.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sonitus.gui;
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;
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;
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;
38 import com.google.common.base.Optional;
39 import com.google.common.collect.Maps;
42 * Sonitus main window.
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public class MainWindow extends JFrame {
48 /** The pipeline to display. */
49 private final Pipeline pipeline;
51 /** The tabbed pane displaying all pipelines. */
52 private final JTabbedPane tabbedPane = new JTabbedPane();
54 /** The info panel card layout. */
55 private final CardLayout infoPanelCardLayout = new CardLayout();
57 /** The info panel. */
58 private final JPanel infoPanel = new JPanel(infoPanelCardLayout);
60 /** The mapping from filters to info panels. */
61 private final Map<Filter, FilterInfoPanel> filterInfoPanels = Maps.newHashMap();
64 * Creates a new main window.
67 * The pipeline to display
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() {
78 public void filterSelected(Filter filter) {
79 infoPanelCardLayout.show(infoPanel, filter.name());
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));
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);
95 Timer timer = new Timer(250, new ActionListener() {
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()));
110 /* FIXME - shut everything down properly. */
111 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);