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 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS.
026 */
027
028package org.opends.guitools.controlpanel.event;
029
030import java.awt.event.FocusEvent;
031import java.awt.event.FocusListener;
032
033import javax.swing.text.JTextComponent;
034
035/**
036 * A class used to be able to select the contents of the text field when
037 * it gets the focus.
038 *
039 */
040public class TextComponentFocusListener implements FocusListener
041{
042  private JTextComponent tf;
043
044  /**
045   * The constructor for this listener.
046   * @param tf the text field associated with this listener.
047   */
048  public TextComponentFocusListener(JTextComponent tf)
049  {
050    this.tf = tf;
051  }
052
053  /** {@inheritDoc} */
054  public void focusGained(FocusEvent e)
055  {
056    if (tf.getText() == null || "".equals(tf.getText()))
057    {
058      tf.setText(" ");
059      tf.selectAll();
060      tf.setText("");
061    }
062    else
063    {
064      tf.selectAll();
065    }
066  }
067
068  /** {@inheritDoc} */
069  public void focusLost(FocusEvent e)
070  {
071  }
072}