View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.log4j.lf5.viewer;
    18
    19  import java.awt.Adjustable;
    20  import java.awt.event.AdjustmentEvent;
    21  import java.awt.event.AdjustmentListener;
    22
    23  /**
    24   * An AdjustmentListener which ensures that an Adjustable (e.g. a Scrollbar)
    25   * will "track" when the Adjustable expands.
    26   * For example, when a vertical scroll bar is at its bottom anchor,
    27   * the scrollbar will remain at the bottom.  When the vertical scroll bar
    28   * is at any other location, then no tracking will happen.
    29   * An instance of this class should only listen to one Adjustable as
    30   * it retains state information about the Adjustable it listens to.
    31   *
    32   * @author Richard Wan
    33   */
    34
    35  // Contributed by ThoughtWorks Inc.
    36
    37  public class TrackingAdjustmentListener implements AdjustmentListener {
    38    //--------------------------------------------------------------------------
    39    //   Constants:
    40    //--------------------------------------------------------------------------
    41
    42    //--------------------------------------------------------------------------
    43    //   Protected Variables:
    44    //--------------------------------------------------------------------------
    45
    46    protected int _lastMaximum = -1;
    47
    48    //--------------------------------------------------------------------------
    49    //   Private Variables:
    50    //--------------------------------------------------------------------------
    51
    52    //--------------------------------------------------------------------------
    53    //   Constructors:
    54    //--------------------------------------------------------------------------
    55
    56    //--------------------------------------------------------------------------
    57    //   Public Methods:
    58    //--------------------------------------------------------------------------
    59
    60    public void adjustmentValueChanged(AdjustmentEvent e) {
    61      Adjustable bar = e.getAdjustable();
    62      int currentMaximum = bar.getMaximum();
    63      if (bar.getMaximum() == _lastMaximum) {
    64        return; // nothing to do, the adjustable has not expanded
    65      }
    66      int bottom = bar.getValue() + bar.getVisibleAmount();
    67
    68      if (bottom + bar.getUnitIncrement() >= _lastMaximum) {
    69        bar.setValue(bar.getMaximum()); // use the most recent maximum
    70      }
    71      _lastMaximum = currentMaximum;
    72    }
    73
    74    //--------------------------------------------------------------------------
    75    //   Protected Methods:
    76    //--------------------------------------------------------------------------
    77
    78    //--------------------------------------------------------------------------
    79    //   Private Methods:
    80    //--------------------------------------------------------------------------
    81
    82    //--------------------------------------------------------------------------
    83    //   Nested Top-Level Classes or Interfaces
    84    //--------------------------------------------------------------------------
    85  }
    86