/* Utilities.java
 *
 * created: Tue Sep 18 2001
 *
 * This file is part of Artemis
 * 
 * Copyright (C) 2001  Genome Research Limited
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header: /nfs/pathsoft/repository/powmap/diana/components/Utilities.java,v 1.3 2002/11/04 17:43:10 kmr Exp $
 */

package diana.components;

import java.awt.*;

/**
 *  Utilities methods used by the diana.components package.
 *
 *  @author Kim Rutherford <kmr@sanger.ac.uk>
 *  @version $Id: Utilities.java,v 1.3 2002/11/04 17:43:10 kmr Exp $
 **/

public class Utilities {
  /**
   *  Return the Frame that contains the given component.
   **/
  public static Frame getComponentFrame (final Component component) {
    Component search_component = component;

    while (search_component != null && !(search_component instanceof Frame)) {
      search_component = search_component.getParent ();
    }

    if (search_component == null) {
      return null;
    } else {
      return (Frame) search_component;
    }
  }

  /**
   *  Find the parent Frame of the given component and re-centre it on the
   *  screen.
   **/
  public static void centreOwningFrame (final Component component) {
    final Frame frame = getComponentFrame (component);

    if (frame == null) {
      // give up
      return;
    }

    final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

    final int x_position = (screen.width - frame.getSize ().width) / 2;
    int y_position = (screen.height - frame.getSize ().height) / 2;

    if (y_position < 10) {
      y_position = 10;
    }

    frame.setLocation (new Point (x_position, y_position));
  }
}
