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