/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Alexander Benkwitz, Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library 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
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.ts;

import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;

/**
 * 
 * Displays all available time series in a JList. It is
 * automatically updated if relevant changes in the underlying data occur. This
 * component has several default listeners that can itself be costumized or set
 * to alternative implementations. The listeners as well as the default renderer
 * are shared between all instances of TSList, but this can be
 * changed for one or all instances as well. The following default classes are
 * used:
 * 
 * 
 * @see com.jstatcom.ts.DefaultTSListPopup
 * @see com.jstatcom.ts.DefaultTSMouseListener
 * @see com.jstatcom.ts.DefaultTSKeyListener
 * @author A. Benkwitz, Markus Kraetzig 
 */

public final class TSList extends JList implements PropertyChangeListener {

    public static class DefaultTSListCellRenderer extends
            DefaultListCellRenderer {

        /**
         * Paints an element of the list by setting its text, its icon according
         * to the quality and the color according to whether it is selected or
         * not.
         */
        public java.awt.Component getListCellRendererComponent(JList list,
                Object value, int index, boolean isSelected,
                boolean cellHasFocus) {

            String name = value.toString();

            TS ts = TSHolder.getInstance().getTS(name);
            if (ts != null)
                setIcon(ts.type().getIcon());

            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }

            setText(name);

            setEnabled(list.isEnabled());
            setFont(list.getFont());

            return this;
        }

    }

    /**
     * Default listeners.
     */
    private JPopupMenu tSListPopup = DefaultTSListPopup.getSharedInstance();

    private KeyListener tSKeyListener = DefaultTSKeyListener
            .getSharedInstance();

    private MouseListener tSMouseListener = DefaultTSMouseListener
            .getSharedInstance();

    /**
     * The MouseAdapter for triggering the right click popup menu.
     */
    private MouseListener triggerPopUp = new MouseAdapter() {

        public void mouseReleased(MouseEvent e) {
            // only if nothing was selected before
            if (SwingUtilities.isRightMouseButton(e)){
                int index = locationToIndex(e.getPoint());
                if (!isSelectedIndex(index))
                    setSelectedIndex(index);
            }
                
            if (tSListPopup != null)
                tSListPopup.processMouseEvent(e, null, null);
        }
    };

    /**
     * ListCellRenderer default, but may be set to a different one.
     */
    private static final ListCellRenderer defaultTSListCellRenderer = new DefaultTSListCellRenderer();

    /**
     * Creates a TSList with the defaults.
     */
    public TSList() {
        super();

        setModel(TSHolder.getInstance().getAsListModel());
        setCellRenderer(defaultTSListCellRenderer);
        addMouseListener(triggerPopUp);
        addKeyListener(tSKeyListener);
        addMouseListener(tSMouseListener);
        TSHolder.getInstance().addPropertyChangeListener(this);
        setFixedCellHeight(16);
    }

    /**
     * This method returns the time series selected in the list as an
     * TS array.
     * 
     * @return the selected time series as an array
     */
    public TS[] getSelectedTS() {

        // look for list selection
        Object[] selection = getSelectedValues();

        // create and fill structure
        TS[] ts = new TS[selection.length];
        for (int i = 0; i < selection.length; i++)
            ts[i] = TSHolder.getInstance().getTS((String) selection[i]);

        return ts;
    }

    /**
     * Gets the currently used KeyListener.
     * 
     * @return the currently used key listener
     */
    public java.awt.event.KeyListener getTSKeyListener() {
        return tSKeyListener;
    }

    /**
     * Gets the currently used RIGHT mouse JPopupMenu.
     * 
     * @return the currently used RIGHT mouse popup
     */
    public JPopupMenu getTSListPopup() {
        return tSListPopup;
    }

    /**
     * Gets the currently used MouseListener.
     * 
     * @return the currently used mouse listener
     */
    public java.awt.event.MouseListener getTSMouseListener() {
        return tSMouseListener;
    }

    /**
     * This method is called whenever there is a property change in the
     * connected property fields. Action depends on the source determined.
     * 
     * @param evt
     *            property change event
     */
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getSource() instanceof TSHolder
                && evt.getPropertyName() == TSHolder.TS_ADDED) {
            TSHolder.getInstance().getTS((String) evt.getNewValue())
                    .addPropertyChangeListener(this);
            // Clear, otherwise selection might be not in sync.
            clearSelection();
        }
        if (evt.getSource() instanceof TSHolder
                && evt.getPropertyName() == TSHolder.TS_REMOVED) {
            // Clear, otherwise selection might be not in sync.
            clearSelection();
        }
        if (evt.getSource() instanceof TSHolder
                && evt.getPropertyName() == TSHolder.ALL_TS_REMOVED) {
            // Clear, otherwise selection might be not in sync.
            clearSelection();
        }
        if (evt.getSource() instanceof TS
                && evt.getPropertyName() == TS.TS_TYPE_CHANGED) {
            if (isVisible())
                repaint();
        }
        return;
    }

    /**
     * Sets new KeyListener, removes old one.
     * 
     * @param newTSKeyListener
     *            if null then it is just removed
     */
    public void setTSKeyListener(java.awt.event.KeyListener newTSKeyListener) {

        if (tSKeyListener != null)
            removeKeyListener(tSKeyListener);

        tSKeyListener = newTSKeyListener;

        if (tSKeyListener != null)
            addKeyListener(tSKeyListener);
    }

    /**
     * Sets a new popup whose processMouseEvent method is called,
     * when a mouseReleased event occured.
     * 
     * @param newTsListPopup
     *            if null then it is just removed
     */
    public void setTSListPopup(JPopupMenu newTsListPopup) {
        tSListPopup = newTsListPopup;

    }

    /**
     * Sets new MouseListener, removes old one.
     * 
     * @param newTSMouseListener
     *            if null then it is just removed
     */
    public void setTSMouseListener(
            java.awt.event.MouseListener newTSMouseListener) {

        if (tSMouseListener != null)
            removeMouseListener(tSMouseListener);

        tSMouseListener = newTSMouseListener;

        if (tSMouseListener != null)
            addMouseListener(tSMouseListener);

    }
}
 

Ads help maintain this website.