/************************************************************************** /* This class provides some useful functions for manipulating windows. /* /* Copyright (c) 2003-2004 by Bernhard Bablok (mail@bablokb.de) /* /* This library is free software; you can redistribute it and/or modify /* it under the terms of the GNU Lesser General Public License as published /* by the Free Software Foundation; either version 2 of the License or /* (at your option) any later version. /* /* This library 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 Lesser General Public License for more details. /* /* You should have received a copy of the GNU Lesser General Public License /* along with this library; see the file COPYING.LESSER. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ package de.bablokb.luala.lib.gui; import java.awt.*; import java.util.*; /** This class provides some useful functions for manipulating windows.

This class was originally part of the bbutils-package. @version $Revision: 1.5 $ @author $Author: bablokb $ */ public class WindowHelper { ////////////////////////////////////////////////////////////////////////////// /** Center given window on its parent. If the parent is malpositioned, display the window at position (10,10). @param w The window */ public static void center(Container w) { Container parent = w.getParent(); Rectangle parentRect; if (parent == null) parentRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); else parentRect = parent.getBounds(); Rectangle windowRect = w.getBounds(); Point upperLeft = new Point(parentRect.x + (parentRect.width/2) - (windowRect.width/2), parentRect.y + (parentRect.height/2) - (windowRect.height/2)); if (upperLeft.x <= 0) upperLeft.x = parentRect.x/2; if (upperLeft.x <= 0) upperLeft.x = 10; if (upperLeft.y <= 0) upperLeft.y = parentRect.y/2; if (upperLeft.y <= 0) upperLeft.y = 10; w.setLocation(upperLeft); } ////////////////////////////////////////////////////////////////////////////// /** Set the location from attributes in the system properties. Needed properties are prefix.x, prefix.y, prefix.width, prefix.height @param w The window @param prefix Prefix string in properties file */ public static void setLocation(Window w, String prefix) { int x,y,wi,h; try { x = Integer.getInteger(prefix+".x").intValue(); y = Integer.getInteger(prefix+".y").intValue(); wi = Integer.getInteger(prefix+".width").intValue(); h = Integer.getInteger(prefix+".height").intValue(); w.setLocation(x,y); w.setSize(wi,h); } catch (Exception e) { w.pack(); center(w); } } ////////////////////////////////////////////////////////////////////////////// /** Save the location of the window position in a Properties object. @param w The window @param prefix Prefix string in properties file */ public static void saveLocation(Window w,String prefix) { Point loc = w.getLocation(); Dimension size = w.getSize(); Properties props = System.getProperties(); props.put(prefix+".x",String.valueOf(loc.x)); props.put(prefix+".y",String.valueOf(loc.y)); props.put(prefix+".width",String.valueOf(size.width)); props.put(prefix+".height",String.valueOf(size.height)); } }