001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.log4j.lf5.viewer;
018
019import java.awt.Adjustable;
020import java.awt.event.AdjustmentEvent;
021import java.awt.event.AdjustmentListener;
022
023/**
024 * An AdjustmentListener which ensures that an Adjustable (e.g. a Scrollbar)
025 * will "track" when the Adjustable expands.
026 * For example, when a vertical scroll bar is at its bottom anchor,
027 * the scrollbar will remain at the bottom.  When the vertical scroll bar
028 * is at any other location, then no tracking will happen.
029 * An instance of this class should only listen to one Adjustable as
030 * it retains state information about the Adjustable it listens to.
031 *
032 * @author Richard Wan
033 */
034
035// Contributed by ThoughtWorks Inc.
036
037public class TrackingAdjustmentListener implements AdjustmentListener {
038  //--------------------------------------------------------------------------
039  //   Constants:
040  //--------------------------------------------------------------------------
041
042  //--------------------------------------------------------------------------
043  //   Protected Variables:
044  //--------------------------------------------------------------------------
045
046  protected int _lastMaximum = -1;
047
048  //--------------------------------------------------------------------------
049  //   Private Variables:
050  //--------------------------------------------------------------------------
051
052  //--------------------------------------------------------------------------
053  //   Constructors:
054  //--------------------------------------------------------------------------
055
056  //--------------------------------------------------------------------------
057  //   Public Methods:
058  //--------------------------------------------------------------------------
059
060  public void adjustmentValueChanged(AdjustmentEvent e) {
061    Adjustable bar = e.getAdjustable();
062    int currentMaximum = bar.getMaximum();
063    if (bar.getMaximum() == _lastMaximum) {
064      return; // nothing to do, the adjustable has not expanded
065    }
066    int bottom = bar.getValue() + bar.getVisibleAmount();
067
068    if (bottom + bar.getUnitIncrement() >= _lastMaximum) {
069      bar.setValue(bar.getMaximum()); // use the most recent maximum
070    }
071    _lastMaximum = currentMaximum;
072  }
073
074  //--------------------------------------------------------------------------
075  //   Protected Methods:
076  //--------------------------------------------------------------------------
077
078  //--------------------------------------------------------------------------
079  //   Private Methods:
080  //--------------------------------------------------------------------------
081
082  //--------------------------------------------------------------------------
083  //   Nested Top-Level Classes or Interfaces
084  //--------------------------------------------------------------------------
085}
086