Show information and all controllers next to the pipeline.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 27 May 2013 20:29:43 +0000 (22:29 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 27 May 2013 20:56:28 +0000 (22:56 +0200)
src/main/java/net/pterodactylus/sonitus/gui/ControlledPane.java [deleted file]
src/main/java/net/pterodactylus/sonitus/gui/MainWindow.java

diff --git a/src/main/java/net/pterodactylus/sonitus/gui/ControlledPane.java b/src/main/java/net/pterodactylus/sonitus/gui/ControlledPane.java
deleted file mode 100644 (file)
index eb44bc7..0000000
+++ /dev/null
@@ -1,176 +0,0 @@
-/*
- * Sonitus - ControlledTab.java - Copyright © 2013 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sonitus.gui;
-
-import static javax.swing.BorderFactory.createEtchedBorder;
-import static javax.swing.BorderFactory.createTitledBorder;
-
-import java.awt.Font;
-import java.awt.GridBagConstraints;
-import java.awt.GridBagLayout;
-import java.awt.Insets;
-import javax.swing.BorderFactory;
-import javax.swing.Box;
-import javax.swing.JComponent;
-import javax.swing.JLabel;
-import javax.swing.JPanel;
-
-import net.pterodactylus.sonitus.data.Controlled;
-import net.pterodactylus.sonitus.data.Controller;
-import net.pterodactylus.sonitus.data.Metadata;
-import net.pterodactylus.sonitus.data.controller.Fader;
-import net.pterodactylus.sonitus.data.controller.Knob;
-import net.pterodactylus.sonitus.data.controller.Switch;
-import net.pterodactylus.sonitus.data.event.MetadataUpdated;
-
-import com.google.common.eventbus.EventBus;
-import com.google.common.eventbus.Subscribe;
-
-/**
- * Panel that displays all {@link Controller}s of a {@link Controlled}
- * component.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class ControlledPane extends JPanel {
-
-       /**
-        * Creates a new controlled pane.
-        *
-        * @param eventBus
-        *              The event bus
-        * @param controlled
-        *              The controlled whose controllers to display
-        */
-       public ControlledPane(EventBus eventBus, Controlled controlled) {
-               super(new GridBagLayout());
-               setBorder(BorderFactory.createEmptyBorder(6, 12, 12, 12));
-
-               MetadataPanel metadataPanel = new MetadataPanel(controlled);
-               eventBus.register(metadataPanel);
-               add(metadataPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
-               add(createControllerPanel(controlled), new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
-               add(Box.createVerticalGlue(), new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(6, 0, 0, 0), 0, 0));
-       }
-
-       //
-       // PRIVATE METHODS
-       //
-
-       /**
-        * Creates the controller panel for the given component.
-        *
-        * @param controlled
-        *              The component whose controllers to display
-        * @return The created controller panel
-        */
-       private JComponent createControllerPanel(Controlled controlled) {
-               JPanel controllerPanel = new JPanel(new GridBagLayout());
-               controllerPanel.setBorder(createTitledBorder(createEtchedBorder(), "Controller"));
-
-               int controllerIndex = 0;
-               for (Controller controller : controlled.controllers()) {
-                       controllerPanel.add(new JLabel(controller.name()), new GridBagConstraints(0, controllerIndex, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(6, 0, 0, 6), 0, 0));
-                       if (controller instanceof Fader) {
-                               controllerPanel.add(new FaderPanel((Fader) controller), new GridBagConstraints(1, controllerIndex, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
-                       } else if (controller instanceof Switch) {
-                               controllerPanel.add(new SwitchPanel((Switch) controller), new GridBagConstraints(1, controllerIndex, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
-                       } else if (controller instanceof Knob) {
-                               controllerPanel.add(new KnobPanel((Knob) controller), new GridBagConstraints(1, controllerIndex, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
-                       }
-                       ++controllerIndex;
-               }
-
-               return controllerPanel;
-       }
-
-       /**
-        * A {@link JPanel} that displays {@link Metadata} information about a {@link
-        * Controlled} component.
-        *
-        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
-        */
-       private static class MetadataPanel extends JPanel {
-
-               /** The controlled component. */
-               private final Controlled controlled;
-
-               /** The format label. */
-               private JLabel format;
-
-               /** The title label. */
-               private JLabel title;
-
-               /**
-                * Creates a new metadata panel.
-                *
-                * @param controlled
-                *              The controlled component
-                */
-               public MetadataPanel(Controlled controlled) {
-                       this.controlled = controlled;
-
-                       setBorder(createTitledBorder(createEtchedBorder(), "Metadata"));
-                       setLayout(new GridBagLayout());
-
-                       format = new JLabel();
-                       title = new JLabel();
-
-                       JLabel formatLabel = new JLabel("Format");
-                       formatLabel.setFont(formatLabel.getFont().deriveFont(Font.BOLD));
-                       JLabel titleLabel = new JLabel("Title");
-                       titleLabel.setFont(formatLabel.getFont().deriveFont(Font.BOLD));
-
-                       add(formatLabel, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
-                       add(format, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
-                       add(titleLabel, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
-                       add(title, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
-
-                       metadataUpdated(new MetadataUpdated(controlled, controlled.metadata()));
-               }
-
-               /**
-                * Called by the {@link EventBus} when a {@link Controlled} component updates
-                * its metadata.
-                *
-                * @param metadataUpdated
-                *              The metadata updated event
-                */
-               @Subscribe
-               public void metadataUpdated(MetadataUpdated metadataUpdated) {
-                       /* do we care about the event’s component? */
-                       if (!controlled.equals(metadataUpdated.controlled())) {
-                               return;
-                       }
-
-                       /* did we actually get any metadata? */
-                       Metadata metadata = metadataUpdated.metadata();
-                       if (metadata == null) {
-                               format.setText("");
-                               title.setText("");
-                               return;
-                       }
-
-                       /* set the texts. */
-                       format.setText(String.format("%s kHz, %d channel%s, %s", metadata.frequency() / 1000.0, metadata.channels(), metadata.channels() != 1 ? "s" : "", metadata.encoding()));
-                       title.setText(metadata.title());
-               }
-
-       }
-
-}
index 3077950..7d64497 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.Pipeline;
+import net.pterodactylus.sonitus.gui.PipelinePanel.ComponentHoverListener;
 import net.pterodactylus.sonitus.main.Version;
 
-import com.google.common.eventbus.EventBus;
+import com.google.common.base.Optional;
+import com.google.common.collect.Maps;
 
 /**
  * Sonitus main window.
@@ -39,50 +45,70 @@ import com.google.common.eventbus.EventBus;
  */
 public class MainWindow extends JFrame {
 
-       /** The event bus. */
-       private final EventBus eventBus;
-
        /** The pipeline to display. */
        private final Pipeline pipeline;
 
        /** The tabbed pane displaying all controlled components. */
        private final JTabbedPane tabbedPane = new JTabbedPane();
 
+       /** The info panel card layout. */
+       private final CardLayout infoPanelCardLayout = new CardLayout();
+
+       /** The info panel. */
+       private final JPanel infoPanel = new JPanel(infoPanelCardLayout);
+
+       /** The mapping from controlled components to info panels. */
+       private final Map<Controlled, ComponentInfoPanel> controlledInfoPanels = Maps.newHashMap();
+
        /**
         * Creates a new main window.
         *
-        * @param eventBus
-        *              The event bus
         * @param pipeline
         *              The pipeline to display
         */
-       public MainWindow(EventBus eventBus, Pipeline pipeline) {
+       public MainWindow(Pipeline pipeline) {
                super(String.format("Sonitus %s", Version.version()));
-               this.eventBus = eventBus;
                this.pipeline = pipeline;
                tabbedPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
-               tabbedPane.add("Pipeline", new PipelinePanel(pipeline));
+               final JPanel pipelineInfoPanel = new JPanel(new BorderLayout(12, 12));
+               PipelinePanel pipelinePanel = new PipelinePanel(pipeline);
+               pipelinePanel.addComponentHoverListener(new ComponentHoverListener() {
+
+                       @Override
+                       public void componentEntered(Controlled controlled) {
+                               infoPanelCardLayout.show(infoPanel, controlled.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));
 
-               /* FIXME - shut everything down properly. */
-               setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
-       }
+               /* create info panels for all components. */
+               for (Controlled controlled : pipeline) {
+                       ComponentInfoPanel componentInfoPanel = new ComponentInfoPanel(controlled);
+                       infoPanel.add(componentInfoPanel, controlled.name());
+                       controlledInfoPanels.put(controlled, componentInfoPanel);
+               }
 
-       //
-       // ACTIONS
-       //
+               Timer timer = new Timer(250, new ActionListener() {
 
-       /**
-        * Adds the given controlled to this main window.
-        *
-        * @param controlled
-        *              The controlled to add
-        */
-       public void addControllers(Controlled controlled) {
-               List<Controller<?>> controllers = controlled.controllers();
-               ControlledPane controlledPane = new ControlledPane(eventBus, controlled);
-               tabbedPane.addTab(controlled.name(), controlledPane);
+                       @Override
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               /* update all info panels. */
+                               for (Controlled controlled : MainWindow.this.pipeline) {
+                                       ComponentInfoPanel componentInfoPanel = controlledInfoPanels.get(controlled);
+                                       componentInfoPanel.input(MainWindow.this.pipeline.trafficCounter(controlled).input());
+                                       componentInfoPanel.output(MainWindow.this.pipeline.trafficCounter(controlled).output());
+                                       componentInfoPanel.format(Optional.of(controlled.metadata().format()));
+                               }
+                       }
+               });
+               timer.start();
+
+               /* FIXME - shut everything down properly. */
+               setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
 
 }