Rename “Controlled” to “ControlledComponent.”
[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.ControlledComponent;
34 import net.pterodactylus.sonitus.data.Pipeline;
35 import net.pterodactylus.sonitus.gui.PipelinePanel.ComponentHoverListener;
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 controlled components. */
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 controlled components to info panels. */
61         private final Map<ControlledComponent, ComponentInfoPanel> controlledInfoPanels = 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.addComponentHoverListener(new ComponentHoverListener() {
76
77                         @Override
78                         public void componentEntered(ControlledComponent controlledComponent) {
79                                 infoPanelCardLayout.show(infoPanel, controlledComponent.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 components. */
89                 for (ControlledComponent controlledComponent : pipeline) {
90                         ComponentInfoPanel componentInfoPanel = new ComponentInfoPanel(controlledComponent);
91                         infoPanel.add(componentInfoPanel, controlledComponent.name());
92                         controlledInfoPanels.put(controlledComponent, componentInfoPanel);
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 (ControlledComponent controlled : MainWindow.this.pipeline) {
101                                         ComponentInfoPanel componentInfoPanel = controlledInfoPanels.get(controlled);
102                                         componentInfoPanel.input(MainWindow.this.pipeline.trafficCounter(controlled).input());
103                                         componentInfoPanel.output(MainWindow.this.pipeline.trafficCounter(controlled).output());
104                                         componentInfoPanel.format(Optional.of(controlled.metadata().format()));
105                                 }
106                         }
107                 });
108                 timer.start();
109
110                 /* FIXME - shut everything down properly. */
111                 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
112         }
113
114 }