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 */
016
017package org.opends.quicksetup.event;
018
019import java.awt.Component;
020import java.awt.Window;
021import java.awt.event.ComponentEvent;
022import java.awt.event.ComponentListener;
023
024/**
025 * This class is used to not allowing the user to reduce the size of a component
026 * below a certain size.  When we want to set a minimum size on an object we
027 * just create the object and then we add it as ComponentListener of the object.
028 *
029 * This is used basically by the QuickSetupDialog dialog.
030 *
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,
048      int minHeight)
049  {
050    this.comp = comp;
051    this.minWidth = minWidth + 2;
052    // It seems that we must add two points to the minWidth (the border of
053    // the frame)
054    if (comp instanceof Window)
055    {
056      this.minWidth += 2;
057    }
058
059    this.minHeight = minHeight;
060  }
061
062  /**
063   * ComponentListener implementation.
064   *
065   * When the method is called check the size and if it is below the minimum
066   * size specified in the constructor, resize it to the minimum size.
067   *
068   * @param ev the component event.
069   */
070  public void componentResized(ComponentEvent ev)
071  {
072    int width = comp.getWidth();
073    int height = comp.getHeight();
074    boolean resize = false;
075    if (width < minWidth)
076    {
077      resize = true;
078      width = minWidth;
079    }
080    if (height < minHeight)
081    {
082      resize = true;
083      height = minHeight;
084    }
085    if (resize)
086    {
087      comp.setSize(width, height);
088    }
089  }
090
091  /**
092   * ComponentListener implementation.
093   *
094   * Empty implementation.
095   * @param ev the component event.
096   */
097  public void componentMoved(ComponentEvent ev)
098  {
099  }
100
101  /**
102   * ComponentListener implementation.
103   *
104   * Empty implementation.
105   * @param ev the component event.
106   */
107  public void componentShown(ComponentEvent ev)
108  {
109  }
110
111  /**
112   * ComponentListener implementation.
113   *
114   * Empty implementation.
115   * @param ev the component event.
116   */
117  public void componentHidden(ComponentEvent ev)
118  {
119  }
120}