001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2001, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.gui.swing;
018:
019: // J2SE dependencies
020: import java.io.Serializable;
021: import javax.swing.JScrollBar;
022: import javax.swing.JScrollPane;
023: import javax.swing.BoundedRangeModel;
024: import javax.swing.event.ChangeEvent;
025: import javax.swing.event.ChangeListener;
026: import javax.swing.table.DefaultTableModel; // For JavaDoc only
027:
028: /**
029: * Scroll down a panel when new lines are added. This helper class require only the reference
030: * to the underlying {@link BoundedRangeModel}. If the model's value is equals to its maximal
031: * value and this maximal value increase, then this class increase the model's value as well.
032: * Example of use:
033: *
034: * <blockquote><pre>
035: * {@link DefaultTableModel} table = new DefaultTableModel();
036: * {@link JScrollPane} pane = new JScrollPane(new JTable(table));
037: * {@link AutoScroll} autos = new AutoScrool(pane.getVerticalScrollBar().getModel());
038: * // etc...
039: *
040: * // Now, add the new item to the table. The table
041: * // will be scrolled down automatically if needed.
042: * table.addRow(...);
043: * </pre></blockquote>
044: *
045: * @since 2.0
046: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/widgets-swing/src/main/java/org/geotools/gui/swing/AutoScroll.java $
047: * @version $Id: AutoScroll.java 20883 2006-08-07 13:48:09Z jgarnett $
048: * @author Martin Desruisseaux
049: */
050: final class AutoScroll implements ChangeListener, Serializable {
051: /**
052: * Serial number for interoperability with different versions.
053: */
054: private static final long serialVersionUID = 8932928616386789102L;
055:
056: /**
057: * The model for the vertical scrollbar.
058: */
059: private final BoundedRangeModel model;
060:
061: /**
062: * Properties of the {@link BoundedRangeModel} the last time {@link #sync} has been invoked.
063: */
064: private int value, extent, maximum;
065:
066: /**
067: * Constructs a new {@code AutoScroll} for the specified model.
068: */
069: public AutoScroll(final BoundedRangeModel model) {
070: this .model = model;
071: model.addChangeListener(this );
072: sync();
073: }
074:
075: /**
076: * Disposes any resources hold by this object.
077: * This method deregisters any listeners.
078: */
079: public void dispose() {
080: model.removeChangeListener(this );
081: }
082:
083: /**
084: * Copies current model's state into {@link #value},
085: * {@link #extent} and {@link #maximum} fields.
086: */
087: private void sync() {
088: value = model.getValue();
089: extent = model.getExtent();
090: maximum = model.getMaximum();
091: }
092:
093: /**
094: * Invoked automatically when the upper limit of {@link BoundedRangeModel} has increased.
095: * If the last row was visible prior the addition of new rows, then this method scrolls
096: * down the model in order to show the new rows.
097: */
098: public void stateChanged(final ChangeEvent event) {
099: final int oldValue = value;
100: final int oldExtent = extent;
101: final int oldMaximum = maximum;
102: sync();
103: if (oldValue + oldExtent >= oldMaximum) {
104: if (value == oldValue && extent >= oldExtent
105: && maximum > oldMaximum) {
106: model.setValue(oldValue + (maximum - oldMaximum));
107: }
108: }
109: }
110: }
|