01: /*
02: * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
03: * reserved.
04: *
05: * Redistribution and use in source and binary forms, with or without
06: * modification, are permitted provided that the following conditions
07: * are met:
08: *
09: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: *
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in
14: * the documentation and/or other materials provided with the
15: * distribution.
16: *
17: * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
18: * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
21: * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: *
29: */
30:
31: package rcm.util;
32:
33: import java.awt.*;
34:
35: public abstract class Win {
36: public static void center(Window window, Component ref) {
37: position(window, ref, 0.5, 0.5);
38: }
39:
40: public static void position(Window frame, Component ref,
41: double xfrac, double yfrac) {
42: Dimension screenSize = Toolkit.getDefaultToolkit()
43: .getScreenSize();
44: Dimension size = frame.getSize();
45: Dimension refSize = (ref != null) ? ref.getSize() : screenSize;
46: Point origin = (ref != null) ? ref.getLocationOnScreen()
47: : new Point(0, 0);
48:
49: int x = origin.x
50: + relativePoint(xfrac, refSize.width, size.width);
51: int y = origin.y
52: + relativePoint(yfrac, refSize.height, size.height);
53:
54: // make sure frame is entirely on screen
55: x = Math.max(0, Math.min(screenSize.width - size.width, x));
56: y = Math.max(0, Math.min(screenSize.height - size.height, y));
57:
58: frame.setLocation(x, y);
59: }
60:
61: static int relativePoint(double frac, int parentLength,
62: int childLength) {
63: if (frac < 0)
64: return (int) (frac * childLength);
65: else if (frac > 1)
66: return (int) (parentLength + (frac - 2) * childLength);
67: else
68: return (int) (frac * (parentLength - childLength));
69: }
70:
71: public static Frame findFrame(Component comp) {
72: for (; comp != null; comp = comp.getParent())
73: if (comp instanceof Frame) {
74: return (Frame) comp;
75: }
76: return null;
77: }
78:
79: public static Frame findFrameOrMakeFrame(Component parent) {
80: return (parent != null) ? findFrame(parent) : new Frame();
81: }
82:
83: }
|