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