001 /* 002 * LAPIS lightweight structured text processing system 003 * 004 * Copyright (C) 1998-2002 Carnegie Mellon University, 005 * Copyright (C) 2003 Massachusetts Institute of Technology. 006 * All rights reserved. 007 * 008 * This library is free software; you can redistribute it 009 * and/or modify it under the terms of the GNU General 010 * Public License as published by the Free Software 011 * Foundation, version 2. 012 * 013 * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/ 014 */ 015 016 017 package lapisx.swing; 018 019 import java.awt.*; 020 021 public abstract class Win { 022 public static void center (Window window, Component ref) { 023 position (window, ref, 0.5, 0.5); 024 } 025 026 public static void position (Window frame, Component ref, 027 double xfrac, double yfrac) { 028 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 029 Dimension size = frame.getSize(); 030 Dimension refSize = (ref != null) 031 ? ref.getSize() 032 : screenSize; 033 Point origin = (ref != null) 034 ? ref.getLocationOnScreen () 035 : new Point (0, 0); 036 037 int x = origin.x + relativePoint (xfrac, refSize.width, size.width); 038 int y = origin.y + relativePoint (yfrac, refSize.height, size.height); 039 040 // make sure frame is entirely on screen 041 x = Math.max (0, Math.min (screenSize.width - size.width, x)); 042 y = Math.max (0, Math.min (screenSize.height - size.height, y)); 043 044 frame.setLocation (x, y); 045 } 046 047 static int relativePoint (double frac, int parentLength, int childLength) { 048 if (frac < 0) 049 return (int) (frac * childLength); 050 else if (frac > 1) 051 return (int) (parentLength + (frac - 2) * childLength); 052 else 053 return (int) (frac * (parentLength - childLength)); 054 } 055 056 057 public static Frame findFrame (Component comp) { 058 for (; comp!=null; comp = comp.getParent ()) 059 if (comp instanceof Frame) { 060 return (Frame)comp; 061 } 062 return null; 063 } 064 065 public static Frame findFrameOrMakeFrame (Component parent) { 066 return (parent != null) ? findFrame (parent) : new Frame (); 067 } 068 069 070 }