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-2016 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 */ 037public class ScrollPaneBorderListener extends ComponentAdapter 038{ 039 private JScrollPane scroll; 040 private Border emptyBorder = new EmptyBorder(0, 0, 0, 0); 041 private Border etchedBorder = BorderFactory.createMatteBorder(0, 0, 1, 0, 042 ColorAndFontConstants.defaultBorderColor); 043 044 /** Private constructor. */ 045 private ScrollPaneBorderListener() 046 { 047 } 048 049 /** 050 * Returns a scroll pane border listener that will apply a border only on the 051 * bottom of the scroll. 052 * @param scroll the scroll pane to update. 053 * @return a scroll pane border listener that will apply a border only on the 054 * bottom of the scroll. 055 */ 056 public static ScrollPaneBorderListener createBottomBorderListener( 057 JScrollPane scroll) 058 { 059 ScrollPaneBorderListener listener = new ScrollPaneBorderListener(); 060 listener.scroll = scroll; 061 scroll.getHorizontalScrollBar().addComponentListener(listener); 062 scroll.getVerticalScrollBar().addComponentListener(listener); 063 return listener; 064 } 065 066 /** 067 * Returns a scroll pane border listener that will apply a border on the 068 * bottom and on the top of the scroll. 069 * @param scroll the scroll pane to update. 070 * @return a scroll pane border listener that will apply a border on the 071 * bottom and on the top of the scroll. 072 */ 073 public static ScrollPaneBorderListener createBottomAndTopBorderListener( 074 JScrollPane scroll) 075 { 076 ScrollPaneBorderListener listener = createBottomBorderListener(scroll); 077 listener.etchedBorder = BorderFactory.createMatteBorder(1, 0, 1, 0, 078 ColorAndFontConstants.defaultBorderColor); 079 return listener; 080 } 081 082 /** 083 * Returns a scroll pane border listener that will apply a full border to the 084 * scroll. 085 * @param scroll the scroll pane to update. 086 * @return a scroll pane border listener that will apply a full border to the 087 * scroll. 088 */ 089 public static ScrollPaneBorderListener createFullBorderListener( 090 JScrollPane scroll) 091 { 092 ScrollPaneBorderListener listener = createBottomBorderListener(scroll); 093 listener.etchedBorder = BorderFactory.createMatteBorder(1, 1, 1, 1, 094 ColorAndFontConstants.defaultBorderColor); 095 return listener; 096 } 097 098 @Override 099 public void componentShown(ComponentEvent ev) 100 { 101 updateBorder(); 102 } 103 104 @Override 105 public void componentHidden(ComponentEvent ev) 106 { 107 updateBorder(); 108 } 109 110 /** Updates the border depending on whether the scroll bars are visible or not. */ 111 public void updateBorder() 112 { 113 boolean displayBorder = scroll.getVerticalScrollBar().isVisible() || 114 scroll.getHorizontalScrollBar().isVisible(); 115 116 if (displayBorder) 117 { 118 scroll.setBorder(etchedBorder); 119 } 120 else 121 { 122 scroll.setBorder(emptyBorder); 123 } 124 } 125}