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

 Copyright (C) 2000-2005  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.Color;
import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

/**
 * Scrollpane to work with TSTable components as viewports to
 * provide row header with time and index.
 * 
 * @author Markus Kraetzig
 */
public final class TSTableScrollPane extends JScrollPane {

    private TSTable tSTable = null;

    private JScrollBar vert = null;

    // private JScrollBar horiz = null;
    private TableModelListener tsTableModelListener = new TableModelListener() {
        public void tableChanged(TableModelEvent evt) {
            if (tSTable == null)
                return;
            Object obj = evt.getSource();
            if (obj instanceof TSTableModel) {
                setRowHeader(((TSTableModel) obj).getRowHeader(tSTable));
            }
        }
    };

    /**
     * Creates a TSTableScrollPane.
     */
    public TSTableScrollPane() {
        super();
    }

    /**
     * 
     * 
     * @param comp
     *            the viewport component
     */
    public void setViewportView(Component comp) {
        super.setViewportView(comp);

        if (comp instanceof TSTable) {
            tSTable = (TSTable) comp;

            JLabel label = new JLabel();
            label.setFont(getFont());
            label.setForeground(getForeground());
            label.setBackground(Color.lightGray);
            label.setHorizontalAlignment(SwingConstants.CENTER);
            label.setText("Time / Index");
            setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, label);

            tSTable.getModel().addTableModelListener(tsTableModelListener);
            vert = getVerticalScrollBar();
            vert.setUnitIncrement(tSTable.getRowHeight());
            // horiz = getHorizontalScrollBar();
            // Adjust scrolling such that only full rows are displayed.
            /*
             * AdjustmentListener adjVert = new AdjustmentListener() { public
             * void adjustmentValueChanged(AdjustmentEvent e) { int val =
             * vert.getValue(); int unit = vert.getUnitIncrement(); // Set the
             * value the closest multiply of unit. int newVal =
             * UMath.closestMultiple(val, unit); vert.setValue(newVal); } };
             * 
             * //vert.addAdjustmentListener(adjVert); // Adjust scrolling such
             * that only full cols are displayed. horiz =
             * getHorizontalScrollBar(); AdjustmentListener adjHoriz = new
             * AdjustmentListener() { public void
             * adjustmentValueChanged(AdjustmentEvent e) { if (tSTable == null)
             * return; int val = horiz.getValue(); if (tSTable.getColumnCount() <
             * 2) return; int unit =
             * tSTable.getColumnModel().getColumn(1).getWidth();
             * horiz.setUnitIncrement(unit); // Set the value the closest
             * multiply of unit. int newVal = UMath.closestMultiple(val, unit);
             * horiz.setValue(newVal); } };
             * 
             * //horiz.addAdjustmentListener(adjHoriz);
             */
        }

    }

}

 

Ads help maintain this website.