Pull all interfaces into a single interface: Filter.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / gui / PipelinePanel.java
index 8c5fa15..93189ef 100644 (file)
 
 package net.pterodactylus.sonitus.gui;
 
+import static javax.swing.BorderFactory.createCompoundBorder;
+import static javax.swing.BorderFactory.createEmptyBorder;
 import static javax.swing.BorderFactory.createEtchedBorder;
 
+import java.awt.BorderLayout;
+import java.awt.Color;
+import java.awt.Component;
 import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 import java.awt.Insets;
@@ -26,31 +31,36 @@ import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.util.Collection;
 import java.util.EventListener;
-import java.util.List;
+import java.util.logging.Logger;
+import javax.swing.JComponent;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
+import javax.swing.UIManager;
 import javax.swing.event.EventListenerList;
 
-import net.pterodactylus.sonitus.data.ControlledComponent;
 import net.pterodactylus.sonitus.data.Filter;
+import net.pterodactylus.sonitus.data.Metadata;
+import net.pterodactylus.sonitus.data.MetadataListener;
 import net.pterodactylus.sonitus.data.Pipeline;
-import net.pterodactylus.sonitus.data.Sink;
-import net.pterodactylus.sonitus.data.Source;
-
-import com.google.common.collect.Lists;
 
 /**
- * {@link JPanel} that displays all components of a {@link Pipeline}.
+ * {@link JPanel} that displays all filters of a {@link Pipeline}.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class PipelinePanel extends JPanel {
 
+       /** The logger. */
+       private static final Logger logger = Logger.getLogger(PipelinePanel.class.getName());
+
        /** The pipeline being displayed. */
        private final Pipeline pipeline;
 
-       /** The component hover listeners. */
-       private final EventListenerList componentHoverListeners = new EventListenerList();
+       /** The filter selection listeners. */
+       private final EventListenerList filterSelectionListeners = new EventListenerList();
+
+       /** The currently selected filter. */
+       private JComponent selectedFilter;
 
        /**
         * Creates a new pipeline panel displaying the given pipeline.
@@ -69,13 +79,13 @@ public class PipelinePanel extends JPanel {
        //
 
        /**
-        * Adds the given component hover listener to this panel.
+        * Adds the given filter selection listener to this panel.
         *
-        * @param componentHoverListener
-        *              The component hover listener to add
+        * @param filterSelectionListener
+        *              The filter selection listener to add
         */
-       public void addComponentHoverListener(ComponentHoverListener componentHoverListener) {
-               componentHoverListeners.add(ComponentHoverListener.class, componentHoverListener);
+       public void addFilterSelectionListener(FilterSelectionListener filterSelectionListener) {
+               filterSelectionListeners.add(FilterSelectionListener.class, filterSelectionListener);
        }
 
        //
@@ -87,18 +97,13 @@ public class PipelinePanel extends JPanel {
                /* clear everything. */
                removeAll();
 
-               /* count all sinks. */
+               /* count all filters. */
                int sinkCount = 0;
-               List<Source> sources = Lists.newArrayList(pipeline.source());
-               while (!sources.isEmpty()) {
-                       Collection<Sink> sinks = pipeline.sinks(sources.remove(0));
-                       for (Sink sink : sinks) {
-                               /* only count real sinks, everything else is filter. */
-                               if (sink instanceof Filter) {
-                                       sources.add((Filter) sink);
-                               } else {
-                                       sinkCount++;
-                               }
+               for (Filter filter : pipeline.filters()) {
+                       Collection<Filter> sinks = pipeline.filters(filter);
+                       logger.finest(String.format("%s has %d filters: %s", filter.name(), sinks.size(), sinks));
+                       if (sinks.isEmpty()) {
+                               sinkCount++;
                        }
                }
 
@@ -108,71 +113,119 @@ public class PipelinePanel extends JPanel {
                        gridCellCount *= n;
                }
 
-               /* paint all components recursively. */
-               addControlled(pipeline.source(), 0, 0, gridCellCount);
+               /* paint all filters recursively. */
+               addFilter(pipeline.source(), 0, 0, gridCellCount, null);
        }
 
        /**
-        * Displays the given component.
+        * Displays the given filter.
         *
-        * @param controlledComponent
-        *              The component to add this panel.
+        * @param filter
+        *              The filter to add this panel.
         * @param level
-        *              The level at which to show the component (the source is level {@code 0})
+        *              The level at which to show the filter (the source is level {@code 0})
         * @param position
-        *              The position at which to display the component
+        *              The position at which to display the filter
         * @param width
-        *              The width of the component in grid cells
+        *              The width of the filter in grid cells
         */
-       private void addControlled(final ControlledComponent controlledComponent, int level, int position, int width) {
-               /* create a GUI component that displays the component. */
-               JLabel sourceLabel = new JLabel(controlledComponent.name());
-               sourceLabel.setBorder(createEtchedBorder());
-               sourceLabel.addMouseListener(new MouseAdapter() {
+       private void addFilter(final Filter filter, int level, int position, int width, Filter parentFilter) {
+               /* create a GUI component that displays the filter. */
+               final JPanel filterPanel = createFilterPanel(filter, parentFilter);
+               filterPanel.addMouseListener(new MouseAdapter() {
+
+                       @Override
+                       public void mouseClicked(MouseEvent e) {
+                               for (Component component : getComponents()) {
+                                       component.setBackground(UIManager.getColor("Panel.background"));
+                               }
+                               for (FilterSelectionListener filterSelectionListener : filterSelectionListeners.getListeners(FilterSelectionListener.class)) {
+                                       filterPanel.setBackground(Color.LIGHT_GRAY);
+                                       filterSelectionListener.filterSelected(filter);
+                               }
+                               selectedFilter = filterPanel;
+                       }
 
                        @Override
                        public void mouseEntered(MouseEvent mouseEvent) {
-                               for (ComponentHoverListener componentHoverListener : componentHoverListeners.getListeners(ComponentHoverListener.class)) {
-                                       componentHoverListener.componentEntered(controlledComponent);
+                               if (filterPanel != selectedFilter) {
+                                       filterPanel.setBackground(Color.white);
+                               }
+                       }
+
+                       @Override
+                       public void mouseExited(MouseEvent mouseEvent) {
+                               if (filterPanel != selectedFilter) {
+                                       filterPanel.setBackground(UIManager.getColor("Panel.background"));
                                }
                        }
                });
 
-               /* show component. */
-               add(sourceLabel, new GridBagConstraints(position, level, width, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
+               /* show filter. */
+               add(filterPanel, new GridBagConstraints(position, level, width, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
 
-               /* if the component does not have connected sinks, exit here. */
-               if (!(controlledComponent instanceof Source)) {
+               /* if the filter does not have connected filters, exit here. */
+               Collection<Filter> sinks = pipeline.filters(filter);
+               if (sinks.isEmpty()) {
+                       add(new JPanel(), new GridBagConstraints(position, 999, width, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
                        return;
                }
 
-               /* iterate over the component’s sinks. */
-               Collection<Sink> sinks = pipeline.sinks((Source) controlledComponent);
-               int sinkWidth = width / sinks.size();
-               int sinkIndex = 0;
-               for (Sink connectedSink : sinks) {
-                       /* distribute all sinks evenly below this source. */
-                       addControlled(connectedSink, level + 1, position + sinkIndex * sinkWidth, sinkWidth);
-                       sinkIndex++;
+               /* iterate over the filter’s connected filters. */
+               if (!sinks.isEmpty()) {
+                       int sinkWidth = width / sinks.size();
+                       int sinkIndex = 0;
+                       for (Filter connectedSink : sinks) {
+                               /* distribute all filters evenly below this source. */
+                               addFilter(connectedSink, level + 1, position + sinkIndex * sinkWidth, sinkWidth, filter);
+                               sinkIndex++;
+                       }
+               }
+       }
+
+       /**
+        * Creates a panel displaying a single filter.
+        *
+        * @param filter
+        *              The filter to display
+        * @return The created panel
+        */
+       private static JPanel createFilterPanel(final Filter filter, final Filter parentFilter) {
+               JPanel filterPanel = new JPanel(new BorderLayout(12, 12));
+               filterPanel.setBorder(createCompoundBorder(createEtchedBorder(), createEmptyBorder(0, 4, 0, 3)));
+               filterPanel.add(new JLabel(filter.name()), BorderLayout.WEST);
+               final JLabel titleLabel = new JLabel(filter.metadata().fullTitle());
+               titleLabel.setFont(titleLabel.getFont().deriveFont(titleLabel.getFont().getSize2D() * 0.8f));
+               filterPanel.add(titleLabel, BorderLayout.EAST);
+               if (parentFilter != null) {
+                       titleLabel.setVisible(!parentFilter.metadata().fullTitle().equals(filter.metadata().fullTitle()));
                }
+               filter.addMetadataListener(new MetadataListener() {
+
+                       @Override
+                       public void metadataUpdated(Filter filter, Metadata metadata) {
+                               titleLabel.setText(metadata.fullTitle());
+                               titleLabel.setVisible((parentFilter == null) || !parentFilter.metadata().fullTitle().equals(metadata.fullTitle()));
+                       }
+               });
+               return filterPanel;
        }
 
        /**
         * Interface for objects that want to be notified if the user moves the mouse
-        * cursor over a controlled component.
+        * cursor over a filter.
         *
         * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
         */
-       public static interface ComponentHoverListener extends EventListener {
+       public static interface FilterSelectionListener extends EventListener {
 
                /**
-                * Notifies the listener that the mouse is now over the given controlled
-                * component.
+                * Notifies the listener that the mouse is now over the given filter.
                 *
-                * @param controlledComponent
-                *              The controlled component now under the mouse
+                * @param filter
+                *              The filter now under the mouse
                 */
-               void componentEntered(ControlledComponent controlledComponent);
+               void filterSelected(Filter filter);
 
        }