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 2006-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2016 ForgeRock AS.
016 */
017
018package org.opends.quicksetup.event;
019
020import java.awt.Component;
021import java.awt.Window;
022import java.awt.event.ComponentEvent;
023import java.awt.event.ComponentListener;
024
025/**
026 * This class is used to not allowing the user to reduce the size of a component
027 * below a certain size.  When we want to set a minimum size on an object we
028 * just create the object and then we add it as ComponentListener of the object.
029 *
030 * This is used basically by the QuickSetupDialog dialog.
031 */
032public class MinimumSizeComponentListener implements ComponentListener
033{
034  private Component comp;
035
036  private int minWidth;
037
038  private int minHeight;
039
040  /**
041   * Constructor for the MinimumSizeComponentListener.
042   *
043   * @param comp the component for which we want to set a minimum size
044   * @param minWidth the minimum width for the component
045   * @param minHeight the minimum height for the component
046   */
047  public MinimumSizeComponentListener(Component comp, int minWidth, int minHeight)
048  {
049    this.comp = comp;
050    this.minWidth = minWidth + 2;
051    // It seems that we must add two points to the minWidth (the border of the frame)
052    if (comp instanceof Window)
053    {
054      this.minWidth += 2;
055    }
056
057    this.minHeight = minHeight;
058  }
059
060  /**
061   * ComponentListener implementation.
062   * <p>
063   * When the method is called check the size and if it is below the minimum
064   * size specified in the constructor, resize it to the minimum size.
065   *
066   * @param ev the component event.
067   */
068  @Override
069  public void componentResized(ComponentEvent ev)
070  {
071    int width = comp.getWidth();
072    int height = comp.getHeight();
073    boolean resize = false;
074    if (width < minWidth)
075    {
076      resize = true;
077      width = minWidth;
078    }
079    if (height < minHeight)
080    {
081      resize = true;
082      height = minHeight;
083    }
084    if (resize)
085    {
086      comp.setSize(width, height);
087    }
088  }
089
090  @Override
091  public void componentMoved(ComponentEvent ev)
092  {
093    // no-op
094  }
095
096  @Override
097  public void componentShown(ComponentEvent ev)
098  {
099    // no-op
100  }
101
102  @Override
103  public void componentHidden(ComponentEvent ev)
104  {
105    // no-op
106  }
107}