/* 
 * Distributed as part of AVS 3.0.0
 * 
 * Copyright (C) 2008 QArks.com
 *
 * Author: Pierre Meyer <support@qarks.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1, as 
 * published by the Free Software Foundation.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; see the file LICENSE.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */
package com.qarks.util.ui;

import java.awt.Point;
import java.awt.Window;
import java.awt.Dimension;
import java.awt.Component;
import java.awt.Toolkit;

public class DialogUtilities {

    private DialogUtilities() {
    }

    public static void centerWindow(Window windowToCenter,Component refComponent){
      Dimension dim1 = windowToCenter.getSize();
      Dimension dim2 = refComponent.getSize();
      Point pt1 = refComponent.getLocationOnScreen();
      if (((pt1.x+dim2.width)>0)&&((pt1.y+dim2.height)>0)){
        Point pt2 = new Point(pt1.x + (dim2.width - dim1.width) / 2,
                              pt1.y + (dim2.height - dim1.height) / 2);
        windowToCenter.setLocation(pt2);
        windowToCenter.setVisible(true);
      }
      else{
        centerOnScreen(windowToCenter);
      }
    }

    public static void centerOnScreen(Window window) {
        final Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
        window.setLocation( (int)(screenDim.getWidth() - window.getWidth()) / 2,
                     (int)(screenDim.getHeight() - window.getHeight()) / 2 );
        if ( !window.isShowing() ) {
            window.setVisible(true);
        }
    }

}
