001package jmri.util;
002
003import java.awt.Component;
004import java.awt.Container;
005import java.awt.event.MouseMotionListener;
006import javax.swing.event.MouseInputAdapter;
007import jmri.util.swing.JmriMouseListener;
008
009/**
010 * A Visitor class for installing a MouseInputAdapter on a container and all of
011 * its subcomponents.
012 *
013 * This class is based on the KeyListenerInstaller class.
014 *
015 * @author Paul Bender Copyright 2005
016 */
017public class MouseInputAdapterInstaller {
018
019    /**
020     * Add a MouseInputAdapter to all components.
021     *
022     * @param m The MouseInputAdapter to add.
023     * @param c The container to which all components are given this listener
024     */
025    public static void installMouseInputAdapterOnAllComponents(MouseInputAdapter m, Container c) {
026        c.addMouseListener(m);
027        c.addMouseMotionListener(m);
028        Component[] components = c.getComponents();
029        for (int i = 0; i < components.length; i++) {
030            if (components[i] instanceof Container) {
031                MouseInputAdapterInstaller.installMouseInputAdapterOnAllComponents(m, (Container) components[i]);
032            } else {
033                c.addMouseListener(m);
034                c.addMouseMotionListener(m);
035            }
036        }
037    }
038
039    /**
040     * Add a MouseListener to all components.
041     *
042     * @param m The MouseListener to add.
043     * @param c The container to which all components are given this listener
044     */
045    public static void installMouseListenerOnAllComponents(JmriMouseListener m, Container c) {
046        c.addMouseListener(JmriMouseListener.adapt(m));
047        Component[] components = c.getComponents();
048        for (int i = 0; i < components.length; i++) {
049            if (components[i] instanceof Container) {
050                MouseInputAdapterInstaller.installMouseListenerOnAllComponents(m, (Container) components[i]);
051            } else {
052                c.addMouseListener(JmriMouseListener.adapt(m));
053            }
054        }
055    }
056
057    /**
058     * Add a MouseMotionListener to all components.
059     *
060     * @param m The MouseMotionListener to add.
061     * @param c The container to which all components are given this listener
062     */
063    public static void installMouseMotionListenerOnAllComponents(MouseMotionListener m, Container c) {
064        c.addMouseMotionListener(m);
065        Component[] components = c.getComponents();
066        for (int i = 0; i < components.length; i++) {
067            if (components[i] instanceof Container) {
068                MouseInputAdapterInstaller.installMouseMotionListenerOnAllComponents(m, (Container) components[i]);
069            } else {
070                c.addMouseMotionListener(m);
071            }
072        }
073    }
074
075}