001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS.
026 */
027
028package org.opends.guitools.controlpanel.event;
029
030import java.awt.event.ComponentAdapter;
031import java.awt.event.ComponentEvent;
032
033import javax.swing.BorderFactory;
034import javax.swing.JScrollPane;
035import javax.swing.border.Border;
036import javax.swing.border.EmptyBorder;
037
038import org.opends.guitools.controlpanel.ui.ColorAndFontConstants;
039
040/**
041 * This is a listener that is basically used to update dynamically the border
042 * of a scroll bar.  This is used when we do not want to display the borders of
043 * the scrollpane if no scrollbars are visible.  So the code basically adds
044 * a component listener to the scroll pane and depending on whether the scroll
045 * bars are displayed or not some border to the scroll pane is added (or not).
046 *
047 */
048public class ScrollPaneBorderListener extends ComponentAdapter
049{
050  private JScrollPane scroll;
051  private Border emptyBorder = new EmptyBorder(0, 0, 0, 0);
052  private Border etchedBorder = BorderFactory.createMatteBorder(0, 0, 1, 0,
053      ColorAndFontConstants.defaultBorderColor);
054
055  /**
056   * Private constructor.
057   *
058   */
059  private ScrollPaneBorderListener()
060  {
061  }
062
063  /**
064   * Returns a scroll pane border listener that will apply a border only on the
065   * bottom of the scroll.
066   * @param scroll the scroll pane to update.
067   * @return a scroll pane border listener that will apply a border only on the
068   * bottom of the scroll.
069   */
070  public static ScrollPaneBorderListener createBottomBorderListener(
071      JScrollPane scroll)
072  {
073    ScrollPaneBorderListener listener = new ScrollPaneBorderListener();
074    listener.scroll = scroll;
075    scroll.getHorizontalScrollBar().addComponentListener(listener);
076    scroll.getVerticalScrollBar().addComponentListener(listener);
077    return listener;
078  }
079
080  /**
081   * Returns a scroll pane border listener that will apply a border on the
082   * bottom and on the top of the scroll.
083   * @param scroll the scroll pane to update.
084   * @return a scroll pane border listener that will apply a border on the
085   * bottom and on the top of the scroll.
086   */
087  public static ScrollPaneBorderListener createBottomAndTopBorderListener(
088      JScrollPane scroll)
089  {
090    ScrollPaneBorderListener listener = createBottomBorderListener(scroll);
091    listener.etchedBorder = BorderFactory.createMatteBorder(1, 0, 1, 0,
092        ColorAndFontConstants.defaultBorderColor);
093    return listener;
094  }
095
096  /**
097   * Returns a scroll pane border listener that will apply a full border to the
098   * scroll.
099   * @param scroll the scroll pane to update.
100   * @return a scroll pane border listener that will apply a full border to the
101   * scroll.
102   */
103  public static ScrollPaneBorderListener createFullBorderListener(
104      JScrollPane scroll)
105  {
106    ScrollPaneBorderListener listener = createBottomBorderListener(scroll);
107    listener.etchedBorder = BorderFactory.createMatteBorder(1, 1, 1, 1,
108        ColorAndFontConstants.defaultBorderColor);
109    return listener;
110  }
111
112  /** {@inheritDoc} */
113  public void componentShown(ComponentEvent ev)
114  {
115    updateBorder();
116  }
117
118  /** {@inheritDoc} */
119  public void componentHidden(ComponentEvent ev)
120  {
121    updateBorder();
122  }
123
124  /**
125   * Updates the border depending on whether the scroll bars are visible or not.
126   *
127   */
128  public void updateBorder()
129  {
130    boolean displayBorder = scroll.getVerticalScrollBar().isVisible() ||
131    scroll.getHorizontalScrollBar().isVisible();
132
133    if (displayBorder)
134    {
135      scroll.setBorder(etchedBorder);
136    }
137    else
138    {
139      scroll.setBorder(emptyBorder);
140    }
141  }
142}